From e0d922547b8f833791f17164542bc30347f61434 Mon Sep 17 00:00:00 2001 From: Andrew S Erwin Date: Wed, 28 Oct 2026 20:05:10 -0500 Subject: [PATCH] Add federated update script: one-command, roll-backable core updates MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Other cores had an installer but no updater — updates meant re-running the workstation tar-sync by hand. deploy/federated/update.sh ships inside the app dir (the installer stages deploy/), so every installed core already has its updater: sudo bash /federated/apps/superbrain/deploy/federated/update.sh [git-ref] Flow, in house-safety order: re-exec from /tmp (the sync overwrites the running script), clone the repo at REF (default main; sha refs fall back to full-clone+checkout), record current image ids, compressed pre-update pg_dump (last 5 retained, emptiness-checked), sync source (.env/data untouched, .git kept out of the app dir/build context), refresh lib+service suite files (parse-checked before replacing, backed up), rebuild, force-recreate (migrate one-shot applies new migrations), health-probe app+web for up to 3 minutes — and on ANY failure retag the previous images and recreate, pointing at the dump. Deliberate non-goal: an in-app update button. The scratch containers have no docker socket and must never get one (read_only + cap_drop ALL is the hardening model); host-level operator action is the trust boundary until CI publishes registry images, at which point the generic house upgrade flow and the panel button supersede this script (IMAGE_VERSION_HOLD comes out of .env). Also: .dockerignore gains .update-backups (dumps live under the app dir, which is the build context); README/STRATEGY/install.sh document the update path. shellcheck clean; tested live on the production core. --- .dockerignore | 1 + README.md | 15 ++++ deploy/federated/STRATEGY.md | 18 ++++- deploy/federated/install.sh | 7 +- deploy/federated/update.sh | 139 +++++++++++++++++++++++++++++++++++ 5 files changed, 173 insertions(+), 7 deletions(-) create mode 100755 deploy/federated/update.sh diff --git a/.dockerignore b/.dockerignore index 471df51..aa12ddb 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,5 +1,6 @@ .git .env* +.update-backups web db *.log diff --git a/README.md b/README.md index 9598f16..0390bbe 100644 --- a/README.md +++ b/README.md @@ -386,6 +386,21 @@ Prerequisites: Authelia with the OIDC provider configured, shared sender account). Full design record, IP plan, upgrade flow and incident history: [deploy/federated/STRATEGY.md](deploy/federated/STRATEGY.md). +**Updating** an installed core (interim source-build mode) is one command +on the host: + +```bash +sudo bash /federated/apps/superbrain/deploy/federated/update.sh [git-ref] +``` + +It clones the requested revision (default `main`), takes a pre-update +`pg_dump` (last 5 kept under the app dir), refreshes the suite integration +files, rebuilds the images, recreates the stack (migrations run +automatically), health-probes both containers, and **rolls back to the +previous images automatically on any failure**. When CI starts publishing +registry images, the generic house `upgrade` flow and the panel button +supersede this script (drop `IMAGE_VERSION_HOLD=1` from the app `.env`). + ## Client test build Use [RUNNING.md](RUNNING.md) for the client handoff. Test login, bookmark editing, diff --git a/deploy/federated/STRATEGY.md b/deploy/federated/STRATEGY.md index bd1d786..d423b5d 100644 --- a/deploy/federated/STRATEGY.md +++ b/deploy/federated/STRATEGY.md @@ -54,10 +54,20 @@ collision checks and `SUPERBRAIN_*_IP` overrides), runs Authelia client + claims policy, and the HTTPS login page. Images are tagged `superbrain-app:local` / `superbrain-web:local`, `.env` carries `IMAGE_VERSION_HOLD=1` so the generic `upgrade` flow skips the app -cleanly, and the service file's `CONTAINER=superbrain-app` matches the first -`image:` line of the generated compose so `upgrade_app`'s rewrite-sed can -never mangle it. Switching to registry mode later = publish images, drop the -`build:` keys + hold marker, point `CONTAINER`/`VERSION` at the registry. +cleanly, and the service file's `CONTAINER=superbrain-app` matches the +first `image:` line of the generated compose so `upgrade_app`'s rewrite-sed +can never mangle it. **Updates** in interim mode run through +`deploy/federated/update.sh` (shipped inside the app dir): clone at REF → +pre-update pg_dump (5 retained) → sync (`.env`/data untouched) → refresh +lib+service files → build → force-recreate → health probe → automatic +rollback to the previous image tags on failure. A deliberate non-goal: an +in-app "update" button — the scratch containers have no docker socket and +must never get one (read_only + cap_drop ALL is the hardening model); +host-level operator action is the trust boundary until registry mode. +Switching to registry mode later = publish images, drop the +`build:` keys + hold marker, point `CONTAINER`/`VERSION` at the registry — +at which point the house `upgrade` flow and panel button take over and +update.sh retires. For the registry target: diff --git a/deploy/federated/install.sh b/deploy/federated/install.sh index 6fb706a..15ddc93 100755 --- a/deploy/federated/install.sh +++ b/deploy/federated/install.sh @@ -189,7 +189,8 @@ $(say "Superbrain installed") Other users are provisioned upstream (Superpanel -> LDAP -> Authelia) and appear in Superbrain on their first SSO login. - Upgrades: the interim source-build mode is pinned (IMAGE_VERSION_HOLD=1); - update by re-staging the source and rebuilding — see - deploy/federated/STRATEGY.md and the lib-superbrain.sh header. + Upgrades (interim source-build mode): one command on the host — + sudo bash /federated/apps/superbrain/deploy/federated/update.sh [git-ref] + (clone -> pg_dump -> rebuild -> recreate -> health probe -> auto-rollback + on failure). See deploy/federated/STRATEGY.md. EOF diff --git a/deploy/federated/update.sh b/deploy/federated/update.sh new file mode 100755 index 0000000..05305c9 --- /dev/null +++ b/deploy/federated/update.sh @@ -0,0 +1,139 @@ +#!/usr/bin/env bash +# +# Update Superbrain on a federated core host (interim source-build mode). +# +# sudo bash /federated/apps/superbrain/deploy/federated/update.sh [git-ref] +# +# or with overrides: +# sudo SUPERBRAIN_REF=v1.2.3 SUPERBRAIN_REPO= bash .../update.sh +# +# What it does, in house-safety order: +# 1. Re-executes itself from /tmp (the update overwrites the app dir this +# script lives in — never pull the rug out mid-execution). +# 2. Clones the repo at REF (default: main) into the staging dir. +# 3. Records the current image ids and takes a compressed pre-update +# pg_dump (keeps the last 5). +# 4. Syncs the source into the app dir (.env, backups and data are +# untouched — only tracked files are overwritten), refreshes the suite +# integration files (lib + service), rebuilds the images, and +# recreates the stack (the migrate one-shot applies new migrations). +# 5. Health-probes app + web. On ANY failure: retags the previous images, +# recreates, and points at the dump — the update is roll-backable. +# +# Endgame note: once CI publishes registry images +# (federatedcomputer/superbrain-{app,web,migrate}, see STRATEGY.md), the +# generic house `upgrade` flow and the panel button supersede this script; +# drop IMAGE_VERSION_HOLD from .env at that point. +# +set -euo pipefail + +APP_DIR=/federated/apps/superbrain +STAGE=/federated/tmp/superbrain-src +REPO="${SUPERBRAIN_REPO:-https://gitea.federated.computer/Federated_Computer/brain.git}" +REF="${1:-${SUPERBRAIN_REF:-main}}" +DUMPS="$APP_DIR/.update-backups" + +say() { printf '\033[1;34m==>\033[0m %s\n' "$*"; } +die() { printf '\033[1;31merror:\033[0m %s\n' "$*" >&2; exit 1; } + +# --- 0. Self-relocation ------------------------------------------------------ +# The synced tree overwrites deploy/federated/update.sh while we run; bash +# reads scripts incrementally, so continue from a private copy. +if [ "${SUPERBRAIN_UPDATE_REEXEC:-0}" != "1" ]; then + SELF=/tmp/superbrain-update-$$ + cp "$0" "$SELF" + SUPERBRAIN_UPDATE_REEXEC=1 exec bash "$SELF" "$@" +fi + +# --- 1. Preflights ------------------------------------------------------------- +[ "$(id -u)" = 0 ] || die "run as root on the core host" +[ -d "$APP_DIR" ] || die "$APP_DIR missing — is superbrain installed? (deploy/federated/install.sh)" +command -v git >/dev/null || die "git not found" +docker container inspect superbrain-app >/dev/null 2>&1 || die "superbrain-app container unknown to docker" + +# --- 2. Clone the requested revision -------------------------------------------- +say "Fetching $REPO @ $REF" +rm -rf "$STAGE" +if ! git clone --quiet --depth 1 --branch "$REF" "$REPO" "$STAGE" 2>/dev/null; then + # REF may be a commit sha: full clone, then detach. + git clone --quiet "$REPO" "$STAGE" || die "git clone failed — check repository access" + git -C "$STAGE" checkout --quiet "$REF" || die "ref $REF not found in $REPO" +fi +for f in main.go Dockerfile db/schema.sql web/Dockerfile docker-compose.yml; do + [ -e "$STAGE/$f" ] || die "staged source is missing $f — wrong repo/ref?" +done +NEW_REV=$(git -C "$STAGE" rev-parse --short HEAD) +# The app dir is the Docker build context and the suite's source of truth — +# keep the clone's history out of it (depth-1 or not, it is dead weight). +rm -rf "$STAGE/.git" +say "Updating to revision $NEW_REV" + +# --- 3. Rollback anchors: current image ids + database dump ---------------------- +OLD_APP_IMAGE=$(docker image inspect superbrain-app:local -f '{{.Id}}' 2>/dev/null || true) +OLD_WEB_IMAGE=$(docker image inspect superbrain-web:local -f '{{.Id}}' 2>/dev/null || true) +mkdir -p "$DUMPS"; chmod 700 "$DUMPS" +DUMP="$DUMPS/pre-update-$(date +%Y%m%d-%H%M%S).sql.gz" +say "Backing up the database to $DUMP" +docker exec postgresql pg_dump -U postgres -d superbrain | gzip > "$DUMP" \ + || die "database backup failed — refusing to update without one" +# gzip writes a ~30-byte header even for empty input: a real dump is bigger. +[ "$(stat -c%s "$DUMP")" -gt 100 ] || die "database dump is suspiciously empty — refusing to update" +ls -1t "$DUMPS"/pre-update-*.sql.gz 2>/dev/null | tail -n +6 | xargs -r rm -f + +rollback() { + say "ROLLING BACK to previous images" + [ -n "$OLD_APP_IMAGE" ] && docker tag "$OLD_APP_IMAGE" superbrain-app:local + [ -n "$OLD_WEB_IMAGE" ] && docker tag "$OLD_WEB_IMAGE" superbrain-web:local + (cd "$APP_DIR" && docker compose -p superbrain up -d --force-recreate app web migrate) || true + die "update failed — previous images restored. Database dump (if needed): $DUMP" +} + +# --- 4. Sync, refresh suite files, build, recreate -------------------------------- +say "Syncing source into $APP_DIR (.env and data untouched)" +cp -a "$STAGE/." "$APP_DIR/" + +# Keep the house integration in step with the repo (both are backed up by +# install.sh conventions; a failing lib parse aborts BEFORE anything else). +if [ -f "$STAGE/deploy/federated/lib-superbrain.sh" ]; then + cp "$STAGE/deploy/federated/lib-superbrain.sh" /tmp/lib-superbrain.sh.new + bash -n /tmp/lib-superbrain.sh.new || die "staged lib-superbrain.sh does not parse — aborting" + [ -f /federated/lib/superbrain.sh ] && cp -a /federated/lib/superbrain.sh "/federated/lib/superbrain.sh.bak-$NEW_REV" + mv /tmp/lib-superbrain.sh.new /federated/lib/superbrain.sh +fi +if [ -f "$STAGE/deploy/federated/service" ]; then + mkdir -p /federated/services/superbrain + [ -f /federated/services/superbrain/service ] && cp -a /federated/services/superbrain/service "/federated/services/superbrain/service.bak-$NEW_REV" + cp "$STAGE/deploy/federated/service" /federated/services/superbrain/service +fi + +say "Building images (several minutes on a cold cache)" +(cd "$APP_DIR" && docker compose build) || rollback + +say "Recreating the stack (migrations run automatically)" +(cd "$APP_DIR" && docker compose -p superbrain up -d --force-recreate app web migrate) || rollback + +# --- 5. Health probe ---------------------------------------------------------------- +say "Waiting for health" +healthy=0 +for _ in $(seq 1 60); do + a=$(docker inspect -f '{{.State.Health.Status}}' superbrain-app 2>/dev/null || echo missing) + w=$(docker inspect -f '{{.State.Health.Status}}' superbrain 2>/dev/null || echo missing) + if [ "$a" = healthy ] && [ "$w" = healthy ]; then healthy=1; break; fi + sleep 3 +done +[ "$healthy" = 1 ] || { docker logs --tail 30 superbrain-app >&2 || true; rollback; } + +say "Superbrain updated to $NEW_REV" +cat <