diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 806388f..d5b85e1 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -7,7 +7,7 @@ # artifact. # Deploy — SSH: podman load the image, point the deploy-owned # stack.env's HEZARFEN_TAG at it, install compose.yaml + -# the hezarfen-compose user unit + the backup script, +# the hezarfen_backend_compose user unit + the backup script, # restart the stack, /health-gated rollback to the # previous tag, independent after-the-fact verify. # @@ -20,7 +20,7 @@ # lag HEAD while a push is mid-CI, and that is the point: redeploy the # known-good image, e.g. after a server-side fix (env file, proxy, # database). For env-only changes a plain -# `ssh 'systemctl --user restart hezarfen-compose'` needs no +# `ssh 'systemctl --user restart hezarfen_backend_compose'` needs no # workflow at all: the stack re-reads its env files on the next # `podman compose up`. # @@ -61,8 +61,8 @@ env: # and the linker is rustc's default bundled lld. TARGET: x86_64-unknown-linux-gnu # The systemd user unit that wraps `podman compose`. See - # deploy/hezarfen-compose.service. - SERVICE: hezarfen-compose + # deploy/hezarfen_backend_compose.service. + SERVICE: hezarfen_backend_compose # The deploy lives in this subdirectory of the SSH user's home. The home # itself is resolved on the server ($HOME), so switching the deploy user # moves the path with it — a baked-in absolute path once kept pointing at @@ -136,15 +136,15 @@ jobs: # same 500 the compose stack runs (see compose.yaml). - name: Start Postgres run: | - docker run -d --name hezarfen-pg \ + docker run -d --name hezarfen_backend_pg \ -e POSTGRES_USER="$PG_USER" -e POSTGRES_PASSWORD="$PG_PASSWORD" \ -p 127.0.0.1:5432:5432 \ postgres:18-alpine -c max_connections=500 for _ in $(seq 1 30); do - docker exec hezarfen-pg pg_isready -U "$PG_USER" && break + docker exec hezarfen_backend_pg pg_isready -U "$PG_USER" && break sleep 1 done - docker exec hezarfen-pg pg_isready -U "$PG_USER" + docker exec hezarfen_backend_pg pg_isready -U "$PG_USER" - name: Install cargo-nextest uses: taiki-e/install-action@v2 @@ -179,21 +179,21 @@ jobs: # Pack the already-built, already-stripped gnu binary into the # runtime image. Never build from source here: a second rust # compile blows the runner's disk. - docker build -f deploy/Containerfile.runtime -t localhost/hezarfen-backend:$GITHUB_SHA stage/ - docker save localhost/hezarfen-backend:$GITHUB_SHA | gzip > stage/hezarfen-backend.tar.gz + docker build -f deploy/Containerfile.runtime -t localhost/hezarfen_backend:$GITHUB_SHA stage/ + docker save localhost/hezarfen_backend:$GITHUB_SHA | gzip > stage/hezarfen_backend.tar.gz - name: Stage the compose bundle run: | set -euo pipefail - cp compose.yaml deploy/hezarfen-compose.service deploy/backup-postgres.sh stage/ + cp compose.yaml deploy/hezarfen_backend_compose.service deploy/backup-postgres.sh stage/ echo -n "$GITHUB_SHA" > stage/tag # Everything the deploy installs on the server must be here; a # missing piece fails here, on the runner, not mid-deploy on the # VPS. The raw binary and migrations may linger as docker build # context — they are no longer load-bearing. - test -f stage/hezarfen-backend.tar.gz + test -f stage/hezarfen_backend.tar.gz test -f stage/compose.yaml - test -f stage/hezarfen-compose.service + test -f stage/hezarfen_backend_compose.service test -f stage/backup-postgres.sh test -f stage/tag ls -l stage @@ -268,9 +268,9 @@ jobs: - name: Check the release is complete run: | set -euo pipefail - test -f stage/hezarfen-backend.tar.gz + test -f stage/hezarfen_backend.tar.gz test -f stage/compose.yaml - test -f stage/hezarfen-compose.service + test -f stage/hezarfen_backend_compose.service test -f stage/backup-postgres.sh test -f stage/tag @@ -311,7 +311,7 @@ jobs: # and would not expand $HOME. Only the five deploy files ship — # the raw binary and migrations stay on the runner (they were # docker build context, nothing more). - tar -C stage -czf - hezarfen-backend.tar.gz compose.yaml hezarfen-compose.service backup-postgres.sh tag | ssh "$ssh_target" "bash -c 'exec tar -xzf - -C \"\$HOME/\$1\"' -- '$DEPLOY_SUBDIR'" + tar -C stage -czf - hezarfen_backend.tar.gz compose.yaml hezarfen_backend_compose.service backup-postgres.sh tag | ssh "$ssh_target" "bash -c 'exec tar -xzf - -C \"\$HOME/\$1\"' -- '$DEPLOY_SUBDIR'" - name: Load the image and restart the stack run: | @@ -362,7 +362,7 @@ jobs: # Load THIS build's image and read the tag the build stamped into # it (build time and deploy time must agree on what is shipping). - gunzip -c "$deploy_path/hezarfen-backend.tar.gz" | podman load + gunzip -c "$deploy_path/hezarfen_backend.tar.gz" | podman load tag=$(cat -- "$deploy_path/tag") # Tag rotation: previous_tag is the rollback target (absent on a @@ -379,11 +379,11 @@ jobs: # The freshly extracted bundle: compose file and unit are # world-readable, the backup script is run by hand or a timer. - chmod 0644 -- "$deploy_path/compose.yaml" "$deploy_path/hezarfen-compose.service" + chmod 0644 -- "$deploy_path/compose.yaml" "$deploy_path/hezarfen_backend_compose.service" chmod 0755 -- "$deploy_path/backup-postgres.sh" # The image tarball was only a transport; the image is loaded into # podman's storage now. - rm -f -- "$deploy_path/hezarfen-backend.tar.gz" + rm -f -- "$deploy_path/hezarfen_backend.tar.gz" compose_up() { podman compose --env-file "$env_file" --env-file "$deploy_path/stack.env" up -d --no-build @@ -408,11 +408,12 @@ jobs: # no unit until the first deploy writes it, and a changed unit # lands with the compose file that needs it. mkdir -p -- "$HOME/.config/systemd/user" - install -m 0644 -- "$deploy_path/hezarfen-compose.service" "$HOME/.config/systemd/user/$service.service" + install -m 0644 -- "$deploy_path/hezarfen_backend_compose.service" "$HOME/.config/systemd/user/$service.service" systemctl --user daemon-reload - # Retire the binary-era unit: disable it (absent on fresh boxes) - # and drop its unit file so it can never come back at boot. + # Retire leftover units: the binary-era hezarfen.service and the + # hyphenated hezarfen-compose.service from the first compose cut. systemctl --user disable --now hezarfen.service 2>/dev/null || true; rm -f -- "$HOME/.config/systemd/user/hezarfen.service" + systemctl --user disable --now hezarfen-compose.service 2>/dev/null || true; rm -f -- "$HOME/.config/systemd/user/hezarfen-compose.service" # The unit is a oneshot with RemainAfterExit, so `start` on the # active (exited) unit would be a no-op — every deploy restarts @@ -444,7 +445,7 @@ jobs: # A stale stack from an older deploy can answer too, so the # running container must be THIS build's image, or it rolls back. - image=$(podman inspect -f '{{.Config.Image}}' hezarfen-backend) + image=$(podman inspect -f '{{.Config.Image}}' hezarfen_backend) case "$image" in *"$tag"*) ;; *) @@ -453,7 +454,7 @@ jobs: exit 1 ;; esac - echo 'hezarfen-compose is up' + echo 'hezarfen_backend_compose is up' REMOTE # The independent after-the-fact check: the container answering must @@ -472,7 +473,7 @@ jobs: export XDG_RUNTIME_DIR=/run/user/$(id -u) cd -- "$deploy_path" tag=$(cat -- "$deploy_path/current_tag") - image=$(podman inspect -f '{{.Config.Image}}' hezarfen-backend) + image=$(podman inspect -f '{{.Config.Image}}' hezarfen_backend) case "$image" in *"$tag"*) ;; *) diff --git a/README.md b/README.md index ca01a4c..355b4e4 100644 --- a/README.md +++ b/README.md @@ -249,7 +249,7 @@ reachable (`HOST` is forced to `0.0.0.0` inside the container so the publish works). Each service has its own named volume: `pgdata` holds the database — it is mounted at `/var/lib/postgresql`, which the postgres:18 image keeps its cluster under (`/var/lib/postgresql/18/docker`), so the data really -lands in the volume — and `hezarfen-data` holds the +lands in the volume — and `hezarfen_backend_data` holds the uploaded note files (`/data/files`). Production knobs (`COOKIE_SECURE`, `CORS_ALLOWED_ORIGINS`, rate limits, `TRUST_PROXY`) reach the backend through the env file (below), not through `compose.yaml`. Leaving @@ -303,19 +303,19 @@ shipped with the deploy artifact and living next to `compose.yaml` on the VPS the control database and all of them. Restore with: ```sh -gunzip -c backups/FILE.sql.gz | podman exec -i hezarfen-postgres psql -U hezarfen -d postgres +gunzip -c backups/FILE.sql.gz | podman exec -i hezarfen_backend_postgres psql -U hezarfen -d postgres ``` Deploying is GitHub Actions' job, not the server's: the workflow packs the already-built binary and the migrations into a runtime image (`deploy/Containerfile.runtime`), ships the image tarball together with -`compose.yaml`, the `hezarfen-compose.service` unit and the backup script as +`compose.yaml`, the `hezarfen_backend_compose.service` unit and the backup script as an artifact, and the deploy job loads it on the VPS and starts the stack with `podman compose up -d --no-build` — the image tag travels in a deploy-owned `stack.env`, so a release never rewrites the operator's secrets file. The operator's only manual step is writing the env file once (above). Reboots heal themselves: the user session lingers (`loginctl enable-linger`) and the -`hezarfen-compose.service` user unit runs compose again at boot. +`hezarfen_backend_compose.service` user unit runs compose again at boot. The backend survives the database going away, at boot and at runtime. @@ -337,8 +337,8 @@ request; there is nothing to re-arm. Without compose: ```sh -podman build -t hezarfen-backend . -podman run -d --name hezarfen -p 7656:7656 -v hezarfen-data:/data hezarfen-backend +podman build -t hezarfen_backend . +podman run -d --name hezarfen -p 7656:7656 -v hezarfen_backend_data:/data hezarfen_backend ``` ## Multi-school (SaaS) @@ -1012,7 +1012,7 @@ UPDATE app_user SET role = 'admin' WHERE username = 'ada'; against the **school's** database (`{control}_school_{uuid hex}` — the hex of the school's registry `id`, no dashes), not the control one. List the school -databases with `\l` — e.g. `podman exec -it hezarfen-postgres psql -U hezarfen +databases with `\l` — e.g. `podman exec -it hezarfen_backend_postgres psql -U hezarfen -d hezarfen_control -c "SELECT slug, id FROM school"` names each school and its database suffix for the compose stack. diff --git a/compose.yaml b/compose.yaml index cf4a95b..4dd9cbf 100644 --- a/compose.yaml +++ b/compose.yaml @@ -18,7 +18,7 @@ services: postgres: image: docker.io/library/postgres:18-alpine - container_name: hezarfen-postgres + container_name: hezarfen_backend_postgres # 500 connections: the test suite mints a control+school pool per test # (nextest runs ~a dozen concurrently), which exhausts the 100 default. command: ["postgres", "-c", "max_connections=500"] @@ -32,7 +32,7 @@ services: # postgres:18 declares VOLUME /var/lib/postgresql (PGDATA is # /var/lib/postgresql/18/docker); mounting the parent keeps the whole # data dir — including future major-version subdirs — in the volume. - - pgdata:/var/lib/postgresql + - hezarfen_backend_pgdata:/var/lib/postgresql restart: unless-stopped healthcheck: # $$ keeps compose from interpolating; the shell inside the container @@ -49,8 +49,8 @@ services: dockerfile: Containerfile # HEZARFEN_TAG comes from the deploy-owned stack.env (--env-file), or the # latest local build on a laptop. - image: localhost/hezarfen-backend:${HEZARFEN_TAG:-latest} - container_name: hezarfen-backend + image: localhost/hezarfen_backend:${HEZARFEN_TAG:-latest} + container_name: hezarfen_backend ports: # Loopback only: terminate TLS on a reverse proxy in front of this. - "127.0.0.1:7656:7656" @@ -82,7 +82,7 @@ services: # Every service must present this to register. AI_SHARED_TOKEN: ${AI_SHARED_TOKEN:-change-me} volumes: - - hezarfen-data:/data + - hezarfen_backend_data:/data depends_on: postgres: condition: service_healthy @@ -102,7 +102,7 @@ services: # when OTEL_* keys are present in hezarfen_backend.env (see the example). openobserve: image: docker.io/openobserve/openobserve:v1.0.0-rc2 - container_name: hezarfen-openobserve + container_name: hezarfen_backend_openobserve environment: # First-boot root account, created once. OpenObserve enforces a password # policy (8-128 chars, upper + lower + digit + special) and panics on @@ -117,7 +117,7 @@ services: - "127.0.0.1:5080:5080" - "127.0.0.1:5081:5081" volumes: - - openobserve-data:/data + - hezarfen_backend_openobserve_data:/data restart: unless-stopped healthcheck: # corner-cut: the image is distroless (only /openobserve, no shell and no @@ -133,6 +133,6 @@ services: start_period: 10s volumes: - hezarfen-data: - pgdata: - openobserve-data: + hezarfen_backend_data: + hezarfen_backend_pgdata: + hezarfen_backend_openobserve_data: diff --git a/deploy/Containerfile.runtime b/deploy/Containerfile.runtime index 77fcb5f..a5bbc86 100644 --- a/deploy/Containerfile.runtime +++ b/deploy/Containerfile.runtime @@ -5,7 +5,7 @@ # # Build context is stage/, populated by CI: # docker build -f deploy/Containerfile.runtime \ -# -t localhost/hezarfen-backend:$GITHUB_SHA stage/ +# -t localhost/hezarfen_backend:$GITHUB_SHA stage/ # with stage/hezarfen_backend (the binary) and stage/migrations/. FROM docker.io/library/debian:trixie-slim diff --git a/deploy/backup-postgres.sh b/deploy/backup-postgres.sh index 41735b1..a1cbcc3 100755 --- a/deploy/backup-postgres.sh +++ b/deploy/backup-postgres.sh @@ -8,7 +8,7 @@ # 20 4 * * * $HOME/hezarfen_backend/backup-postgres.sh # # Restore (from ~/hezarfen_backend): -# gunzip -c backups/FILE.sql.gz | podman exec -i hezarfen-postgres psql -U hezarfen -d postgres +# gunzip -c backups/FILE.sql.gz | podman exec -i hezarfen_backend_postgres psql -U hezarfen -d postgres set -euo pipefail dir="$(cd "$(dirname "$0")" && pwd)" @@ -22,5 +22,5 @@ user="${user:-hezarfen}" out="$dir/backups/hezarfen-$(date -u +%Y%m%dT%H%M%SZ).sql.gz" umask 077 # pipefail (set above): a failed pg_dumpall must not leave a "good" empty dump. -podman exec hezarfen-postgres pg_dumpall -U "$user" | gzip > "$out" +podman exec hezarfen_backend_postgres pg_dumpall -U "$user" | gzip > "$out" echo "wrote $out ($(du -h "$out" | cut -f1))" diff --git a/deploy/hezarfen_backend.env.example b/deploy/hezarfen_backend.env.example index 63b3141..b3d8953 100644 --- a/deploy/hezarfen_backend.env.example +++ b/deploy/hezarfen_backend.env.example @@ -56,7 +56,7 @@ AI_SHARED_TOKEN= # printf '%s:%s' 'admin@example.com' 'PASSWORD' | base64 #OTEL_EXPORTER_OTLP_ENDPOINT=http://openobserve:5081 #OTEL_EXPORTER_OTLP_HEADERS=Authorization=Basic ,organization=default,stream-name=default -#OTEL_SERVICE_NAME=hezarfen-backend +#OTEL_SERVICE_NAME=hezarfen_backend # Names the deployment in every exported signal. #DEPLOY_ENV=production diff --git a/deploy/hezarfen-compose.service b/deploy/hezarfen_backend_compose.service similarity index 94% rename from deploy/hezarfen-compose.service rename to deploy/hezarfen_backend_compose.service index 9a6b0ea..898da66 100644 --- a/deploy/hezarfen-compose.service +++ b/deploy/hezarfen_backend_compose.service @@ -25,7 +25,7 @@ # # Env-only restart (restart re-runs `compose up -d`, recreating containers # whose environment changed): -# systemctl --user restart hezarfen-compose +# systemctl --user restart hezarfen_backend_compose [Unit] Description=Hezarfen backend compose stack (postgres + backend + openobserve) @@ -39,7 +39,7 @@ WorkingDirectory=%h/hezarfen_backend # Later --env-file wins for duplicate keys: the operator's secrets file # first, the deploy-owned stack.env (HEZARFEN_TAG) second. ExecStart=/usr/bin/podman compose --env-file %h/hezarfen_backend/hezarfen_backend.env --env-file %h/hezarfen_backend/stack.env up -d --no-build -# No -v: the pgdata/hezarfen-data/openobserve-data volumes must survive stop. +# No -v: named volumes must survive stop. ExecStop=/usr/bin/podman compose down TimeoutStartSec=180 diff --git a/scripts/prepare_db.sh b/scripts/prepare_db.sh index 44a7f4f..0c1e3a8 100755 --- a/scripts/prepare_db.sh +++ b/scripts/prepare_db.sh @@ -17,7 +17,7 @@ # Requires the compose Postgres: `podman compose up -d postgres`. set -euo pipefail -CONTAINER="${HEZARFEN_PG_CONTAINER:-hezarfen-postgres}" +CONTAINER="${HEZARFEN_PG_CONTAINER:-hezarfen_backend_postgres}" PREPARE_DB="${HEZARFEN_PREPARE_DB:-hezarfen_sqlx_prepare}" HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"