Skip to content

Commit

Permalink
add infra
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus-sa committed Aug 13, 2024
1 parent 418bd3a commit 1683620
Show file tree
Hide file tree
Showing 22 changed files with 213 additions and 27 deletions.
File renamed without changes.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,8 @@ Thumbs.db

.nx/cache
.nx/workspace-data

.env.prod
.env

.unikraft
Binary file modified bun.lockb
Binary file not shown.
5 changes: 4 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,18 @@ services:
- '8082:8082'
- '9092:9092'
- '19644:9644'
volumes:
- redpanda:/var/lib/redpanda/data

restate:
image: docker.io/restatedev/restate:1.0
image: docker.io/restatedev/restate:1.0.2
ports:
- '8080:8080'
- '9070:9070'
environment:
- 'RESTATE_CONFIG=/etc/config/restate.toml'
volumes:
- ./infra/restate/config.toml:/etc/config/restate.toml
- restate:/var/lib/restate
extra_hosts:
- 'host.docker.internal:host-gateway'
15 changes: 15 additions & 0 deletions infra/postgres/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM postgres:16.4-alpine3.19 AS base

FROM base AS ext-build
RUN apk add --no-cache gcc make musl-dev llvm15-dev clang15 unzip
ADD https://github.com/kraftcloud/pg_ukc_scaletozero/archive/refs/heads/stable.zip /tmp/src.zip
RUN mkdir /src && \
unzip /tmp/src.zip -d /tmp/extract && \
mv /tmp/extract/pg_ukc_scaletozero-*/* /src
RUN cd /src && \
make && \
make install DESTDIR=/out

FROM base
COPY --from=ext-build /out/usr/local/lib/postgresql/pg_ukc_scaletozero.so /usr/local/lib/postgresql/pg_ukc_scaletozero.so
ADD wrapper.sh /usr/local/bin/wrapper.sh
14 changes: 14 additions & 0 deletions infra/postgres/Kraftfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
spec: v0.6

name: postgres

runtime: base-compat:latest

labels:
cloud.unikraft.v1.instances/scale_to_zero.policy: "idle"
cloud.unikraft.v1.instances/scale_to_zero.stateful: "true"
cloud.unikraft.v1.instances/scale_to_zero.cooldown_time_ms: 1000

rootfs: ./Dockerfile

cmd: ["wrapper.sh", "docker-entrypoint.sh", "postgres", "-c", "shared_preload_libraries='pg_ukc_scaletozero'"]
12 changes: 12 additions & 0 deletions infra/postgres/deploy.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env sh
kraft cloud volume create --name ftgo-postgres --size 256

kraft cloud deploy \
--name ftgo-postgres \
-M 1024 \
-p 5432:5432/tls \
-v ftgo-postgres:/var/lib/postgresql \
-e POSTGRES_DATABASE=$DATABASE_NAME \
-e POSTGRES_USER=$DATABASE_USER \
-e POSTGRES_PASSWORD=$DATABASE_PASSWORD \
.
17 changes: 17 additions & 0 deletions infra/postgres/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "postgres",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"projectType": "application",
"sourceRoot": "infra/postgres",
"tags": ["infra"],
"targets": {
"deploy": {
"executor": "nx:run-commands",
"inputs": ["{projectRoot}/**"],
"options": {
"cwd": "{projectRoot}",
"command": "./deploy.sh"
}
}
}
}
14 changes: 14 additions & 0 deletions infra/postgres/wrapper.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env bash
set -e
# Set defaults from offical dockerfile
if [ -z "$LANG" ]; then
export LANG=en_US.utf8
fi
export PG_MAJOR=16
export PG_VERSION=16.2

if [ -z "$PGDATA" ]; then
export PGDATA=/var/lib/postgresql/data
fi

exec "$@"
1 change: 1 addition & 0 deletions infra/redpanda/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FROM docker.redpanda.com/redpandadata/redpanda:v24.2.2
9 changes: 9 additions & 0 deletions infra/redpanda/Kraftfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
spec: v0.6

name: redpanda

runtime: base-compat:latest

rootfs: ./Dockerfile

cmd: ["/usr/bin/redpanda"]
12 changes: 12 additions & 0 deletions infra/redpanda/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env sh
kraft cloud volume create --name ftgo-redpanda --size 256

kraft cloud deploy \
--name ftgo-redpanda \
-M 2048 \
-p 8081:8081/tls \
-p 8082:8082/tls \
-p 9092:9092/tls \
-p 19644:9644/tls \
-v ftgo-redpanda:/var/lib/redpanda/data \
.
17 changes: 17 additions & 0 deletions infra/redpanda/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "redpanda",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"projectType": "application",
"sourceRoot": "infra/redpanda",
"tags": ["infra"],
"targets": {
"deploy": {
"executor": "nx:run-commands",
"inputs": ["{projectRoot}/**"],
"options": {
"cwd": "{projectRoot}",
"command": "./deploy.sh"
}
}
}
}
2 changes: 2 additions & 0 deletions infra/restate/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM docker.io/restatedev/restate:1.0.2
COPY ./config.prod.toml /etc/config/restate.toml
9 changes: 9 additions & 0 deletions infra/restate/Kraftfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
spec: v0.6

name: restate

runtime: base:latest

rootfs: ./Dockerfile

cmd: ["/usr/local/bin/restate-server"]
11 changes: 10 additions & 1 deletion infra/restate/config.prod.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
base-dir = "/var/lib/restate"
# Unikraft instances are limited to 31 threads
rocksdb-high-priority-bg-threads = 1
storage-high-priority-bg-threads = 1
storage-low-priority-bg-threads = 1
default-thread-pool-size = 1
rocksdb-bg-threads = 1
bootstrap-num-partitions = 1

[worker.invoker.retry-policy]
initial-interval = "100ms"
max-attempts = 5

[[ingress.kafka-clusters]]
name = "default"
brokers = ["PLAINTEXT://<host>:9092"]
brokers = ["PLAINTEXT://ftgo-redpanda.internal:9092"]
2 changes: 2 additions & 0 deletions infra/restate/config.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
base-dir = "/var/lib/restate"

[worker.invoker.retry-policy]
initial-interval = "50ms"
max-attempts = 3
Expand Down
4 changes: 4 additions & 0 deletions infra/restate/deploy.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
kraft cloud volume create --name ftgo-restate --size 256
# TODO: need redpanda broker host
kraft cloud deploy --name ftgo-restate -M 512 -v ftgo-restate:/var/lib/restate -e RESTATE_CONFIG=/etc/config/restate.toml -p 8080:8080/tls -p 9070:9070/tls .
18 changes: 18 additions & 0 deletions infra/restate/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "restate",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"projectType": "application",
"sourceRoot": "infra/restate",
"tags": ["infra"],
"targets": {
"deploy": {
"executor": "nx:run-commands",
"inputs": ["{projectRoot}/**"],
"dependsOn": [],
"options": {
"cwd": "{projectRoot}",
"command": "./deploy.sh"
}
}
}
}
3 changes: 3 additions & 0 deletions nx.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"$schema": "./node_modules/nx/schemas/nx-schema.json",
"cli": {
"packageManager": "bun"
},
"namedInputs": {
"default": ["{projectRoot}/**/*", "sharedGlobals"],
"production": [
Expand Down
51 changes: 26 additions & 25 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,33 @@
"name": "@ftgo/source",
"version": "0.0.0",
"license": "MIT",
"scripts": {},
"private": true,
"type": "module",
"scripts": {
"postinstall": "deepkit-type-install"
},
"dependencies": {
"@deepkit/app": "^1.0.1-alpha.153",
"@deepkit/broker": "^1.0.1-alpha.153",
"@deepkit/bson": "^1.0.1-alpha.153",
"@deepkit/core": "^1.0.1-alpha.147",
"@deepkit/event": "^1.0.1-alpha.153",
"@deepkit/filesystem": "^1.0.1-alpha.147",
"@deepkit/framework": "^1.0.1-alpha.153",
"@deepkit/http": "^1.0.1-alpha.153",
"@deepkit/injector": "^1.0.1-alpha.153",
"@deepkit/orm": "^1.0.1-alpha.153",
"@deepkit/postgres": "^1.0.1-alpha.153",
"@deepkit/sql": "^1.0.1-alpha.153",
"@deepkit/type": "^1.0.1-alpha.153",
"deepkit-restate": "^0.0.37",
"kafkajs": "2.2.4",
"tslib": "2.6.3"
},
"devDependencies": {
"@deepkit/type-compiler": "^1.0.1-alpha.150",
"@deepkit/vite": "^1.0.1-alpha.150",
"@nx-tools/nx-container": "^6.0.1",
"@nx/eslint": "19.5.7",
"@nx/eslint-plugin": "19.5.7",
"@nx/js": "19.5.7",
Expand All @@ -30,35 +51,15 @@
"prettier": "^3.3.3",
"typescript": "^5.5.4",
"vite": "^5.4.0",
"vitest": "^2.0.5"
"vitest": "^2.0.5",
"zx": "^8.1.4"
},
"patchedDependencies": {
"[email protected]": "patches/[email protected]"
},
"dependencies": {
"@deepkit/app": "^1.0.1-alpha.153",
"@deepkit/broker": "^1.0.1-alpha.153",
"@deepkit/bson": "^1.0.1-alpha.153",
"@deepkit/core": "^1.0.1-alpha.147",
"@deepkit/event": "^1.0.1-alpha.153",
"@deepkit/filesystem": "^1.0.1-alpha.147",
"@deepkit/framework": "^1.0.1-alpha.153",
"@deepkit/http": "^1.0.1-alpha.153",
"@deepkit/injector": "^1.0.1-alpha.153",
"@deepkit/orm": "^1.0.1-alpha.153",
"@deepkit/postgres": "^1.0.1-alpha.153",
"@deepkit/sql": "^1.0.1-alpha.153",
"@deepkit/type": "^1.0.1-alpha.153",
"deepkit-restate": "^0.0.37",
"kafkajs": "2.2.4",
"tslib": "2.6.3"
},
"trustedDependencies": ["nx"],
"engines": {
"node": "22.6.0",
"bun": "1.1.22"
},
"trustedDependencies": [
"@deepkit/type-compiler",
"nx"
]
}
}
19 changes: 19 additions & 0 deletions scripts/deploy-kraft-service.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { $, cd } from 'zx';

const [name, projectRoot] = process.argv.slice(2);

// $.verbose = false;

cd(projectRoot);

const port = process.env.RESTATE_SERVER_PORT || '9080';

const svcName = `ftgo-${name}`;

// try {
// await $`kraft cloud service create --name ftgo-${name} -s "${svcName}" 443:${port}/http+tls`;
// } catch {}

const envVars = Object.entries(process.env).reduce((acc, [key, value]) => acc + ' ' + `-e ${key}=${value}`, '');

await $`kraft cloud deploy --rollout remove --restart on-failure --scale-to-zero on --scale-to-zero-cooldown 1000ms --strategy overwrite -M 512 -p ${port}:${port}/http+tls -e RESTATE_SERVER_HOST=${svcName}.internal ${envVars} --name ${svcName} .`;

0 comments on commit 1683620

Please sign in to comment.