Registry mode: publish app/web/migrate images, drop source builds on hosts
release gate / check (push) Canceled after 4m3s
release / smoke (push) Successful in 10m13s
release / publish (push) Failing after 12m48s

The federated suite now installs Superbrain like every other house app:
pull images, run installapp, no source tree on the host.

Images (Docker Hub, federatedcomputer namespace, one shared tag per
release + :latest, linux/amd64 + linux/arm64):
  superbrain-app      Go API (scratch)
  superbrain-web      Next.js standalone
  superbrain-migrate  NEW self-contained db image: db/ baked in, entrypoint
                      db/bootstrap.sh waits for postgres, applies
                      schema.sql AS DB_USER on an empty database (object
                      ownership — migrations must be able to ALTER), then
                      always runs the checksummed ledger. Fresh install and
                      upgrade become the same command. The dev compose runs
                      the same bootstrap.sh (verified: non-empty DB skips
                      schema, applies pending migrations).

Release CI (.github/workflows/release.yml): semver tags without v prefix
(the suite's VERSION_RX); a smoke job bootstraps a throwaway PostgreSQL
with the migrate image (fresh schema + complete ledger + idempotent re-run)
and runs Go build/vet/unit BEFORE the publish job pushes — a broken db/
can never reach a tag the upgrade flow will pull. Requires
DOCKERHUB_USERNAME/DOCKERHUB_TOKEN secrets. Dockerfile stays legacy-
builder compatible (no BUILDPLATFORM cross-compile): dev hosts build
locally; CI builds each arch natively under qemu.

Federated integration:
- lib-superbrain.sh: compose pulls federatedcomputer/superbrain-*:
  ${IMAGE_VERSION} (bookstack quoting), no build: keys, no
  IMAGE_VERSION_HOLD; compose generation extracted into
  render_superbrain_compose (install + update share it); schema.sql
  pre-apply and host image builds removed; IMAGE_VERSION is the only
  VERSION= line in the generated .env (upgrade_app's sed matches every
  occurrence — the file says so in words that don't contain the pattern).
- service file: CONTAINER=federatedcomputer/superbrain-app (must equal the
  first image: repository — upgrade_app compares it), VERSION=1.0.0.
- services/superbrain/upgrade/pre hook (listmonk pattern): pre-upgrade
  pg_dump into .update-backups (5 retained) + pulls web/migrate at the
  target version (upgrade_app pulls only the app image).
- install.sh: registry messaging, installs the upgrade hook, clone is only
  for deploy files now.
- update.sh REWRITTEN for registry mode: the app dir no longer contains
  source, so it runs from a repo clone — sync lib/service/hook (parse-
  checked, backed up) -> re-render compose -> /federated/bin/upgrade
  superbrain -> same-version compose safety pass -> health probe.
- STRATEGY.md interim-mode section replaced with the registry-mode record.

README: federated section updated (pulls, not builds; house upgrade flow)
and a new 'Environment variable reference' section — every variable the
stack reads, required/optional, defaults, grouped (core, compose, access
policy, OIDC, SMTP, AI, federated-only, tests-only), so generated .env
files stay correct as features are added.
This commit is contained in:
Andrew S Erwin
2026-10-29 10:21:10 -05:00
parent 0dc48a137f
commit 341cdce026
12 changed files with 582 additions and 295 deletions
+71 -118
View File
@@ -1,131 +1,93 @@
#!/usr/bin/env bash
#
# Update Superbrain on a federated core host (interim source-build mode).
# Update Superbrain on a federated core host (REGISTRY mode).
#
# sudo bash /federated/apps/superbrain/deploy/federated/update.sh [git-ref]
# The app dir holds only compose + .env (no source), so this script runs
# from a repo clone:
#
# or with overrides:
# sudo SUPERBRAIN_REF=v1.2.3 SUPERBRAIN_REPO=<url> bash .../update.sh
# git clone <repo> /tmp/superbrain-repo
# sudo bash /tmp/superbrain-repo/deploy/federated/update.sh [git-ref]
#
# 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.
# What it does:
# 1. Clones the repo at REF (default main) — or uses $SUPERBRAIN_SRC.
# 2. Syncs the suite integration files (lib, service file, upgrade/pre
# hook) with backups and a parse check — the service file carries the
# desired VERSION, which is how a release reaches the host.
# 3. Re-renders the app-dir compose from the fresh lib (so a release that
# changes the compose shape applies without reinstalling).
# 4. Runs the house flow: /federated/bin/upgrade superbrain — version
# compare, pre hook (pg_dump + pull all three images), btrfs snapshot,
# redeploy, .env IMAGE_VERSION rewrite, superpanel refresh.
# 5. compose up -d safety pass (applies same-version compose-only
# changes), then health-probes app + web.
#
# 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.
# Environment: SUPERBRAIN_REPO, SUPERBRAIN_REF (or $1), SUPERBRAIN_SRC.
#
set -euo pipefail
REPO_DEFAULT="https://gitea.federated.computer/Federated_Computer/brain.git"
APP_DIR=/federated/apps/superbrain
STAGE=/federated/tmp/superbrain-src
REPO="${SUPERBRAIN_REPO:-https://gitea.federated.computer/Federated_Computer/brain.git}"
CLONE=/tmp/superbrain-update-src
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 -------------------------------------------------------------
# --- 1. Preflights + source ----------------------------------------------------
[ "$(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"
[ -x /federated/bin/upgrade ] || die "/federated/bin/upgrade missing — not a federated suite host?"
[ -f "$APP_DIR/docker-compose.yml" ] && [ -f "$APP_DIR/.env" ] \
|| die "$APP_DIR does not look installed (compose/.env missing) — run deploy/federated/install.sh first"
# --- 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"
if [ -n "${SUPERBRAIN_SRC:-}" ]; then
SRC="$SUPERBRAIN_SRC"
[ -f "$SRC/deploy/federated/lib-superbrain.sh" ] || die "SUPERBRAIN_SRC lacks deploy/federated files"
else
command -v git >/dev/null || die "git not found (or set SUPERBRAIN_SRC=/path/to/repo)"
say "Fetching ${SUPERBRAIN_REPO:-$REPO_DEFAULT} @ $REF"
rm -rf "$CLONE"
if ! git clone --quiet --depth 1 --branch "$REF" "${SUPERBRAIN_REPO:-$REPO_DEFAULT}" "$CLONE" 2>/dev/null; then
git clone --quiet "${SUPERBRAIN_REPO:-$REPO_DEFAULT}" "$CLONE" || die "git clone failed — check repository access"
git -C "$CLONE" checkout --quiet "$REF" || die "ref $REF not found"
fi
SRC="$CLONE"
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"
NEW_REV=$(git -C "$SRC" rev-parse --short HEAD 2>/dev/null || echo "$REF")
# --- 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
# --- 2. Sync suite files (parse-checked, backed up) ------------------------------
say "Syncing suite integration files (revision $NEW_REV)"
LIB=/federated/lib/superbrain.sh
cp "$SRC/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 "$LIB" ] && cp -a "$LIB" "$LIB.bak-$NEW_REV"
mv /tmp/lib-superbrain.sh.new "$LIB"
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"
}
mkdir -p /federated/services/superbrain/upgrade
[ -f /federated/services/superbrain/service ] && cp -a /federated/services/superbrain/service "/federated/services/superbrain/service.bak-$NEW_REV"
cp "$SRC/deploy/federated/service" /federated/services/superbrain/service
cp "$SRC/deploy/federated/upgrade-pre" /federated/services/superbrain/upgrade/pre
chmod 755 /federated/services/superbrain/upgrade/pre
# --- 4. Sync, refresh suite files, build, recreate --------------------------------
# Payload-sync ONLY the tracked source paths (same list as the installer's
# staging). Never `cp -a` the whole clone over the app dir: the app dir's
# docker-compose.yml is GENERATED by config_superbrain (fixed IPs, core
# network, env_file) and the repo's dev compose would clobber it — hit in
# production 2026-10-29, caught by the rollback. .env, .update-backups and
# the generated compose are untouchable here.
say "Syncing source into $APP_DIR (generated compose/.env/data untouched)"
(cd "$STAGE" && tar cf - --exclude=node_modules --exclude=.next \
main.go go.mod go.sum Dockerfile .dockerignore internal db web deploy) \
| tar xf - -C "$APP_DIR"
# Sanity: the app dir must still hold the GENERATED federated compose
# (config_superbrain's), not the repo's dev stack.
# --- 3. Re-render the compose from the fresh lib ----------------------------------
say "Re-rendering the app compose"
bash -c '. /federated/lib/functions.sh; . /federated/lib/superbrain.sh; render_superbrain_compose' \
|| die "compose re-render failed"
grep -q "container_name: superbrain-app" "$APP_DIR/docker-compose.yml" \
|| die "$APP_DIR/docker-compose.yml is not the generated federated compose — refusing to build against it"
|| die "rendered compose is missing the app service — aborting"
(cd "$APP_DIR" && docker compose -p superbrain config >/dev/null) \
|| die "rendered compose does not parse — aborting"
# 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
# --- 4. House upgrade flow ----------------------------------------------------------
WANT_VERSION=$(sed -n 's/^VERSION=//p' /federated/services/superbrain/service | tr -d '"' | head -1)
HAVE_VERSION=$(sed -n 's/^IMAGE_VERSION=//p' "$APP_DIR/.env" | tr -d '"' | tail -1)
say "Running the house upgrade flow (have $HAVE_VERSION, service file wants $WANT_VERSION)"
/federated/bin/upgrade superbrain || die "upgrade failed — inspect the output above"
say "Building images (several minutes on a cold cache)"
(cd "$APP_DIR" && docker compose build) || rollback
# --- 5. Same-version safety pass + health probe --------------------------------------
# bin/upgrade no-ops when the version is unchanged; if this release only
# changed the compose shape, apply it now (idempotent otherwise).
(cd "$APP_DIR" && docker compose -p superbrain up -d) || die "compose up failed after upgrade"
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
@@ -134,19 +96,10 @@ for _ in $(seq 1 60); do
if [ "$a" = healthy ] && [ "$w" = healthy ]; then healthy=1; break; fi
sleep 3
done
[ "$healthy" = 1 ] || { docker logs --tail 30 superbrain-app >&2 || true; rollback; }
if [ "$healthy" != 1 ]; then
docker logs --tail 30 superbrain-app >&2 || true
die "stack did not become healthy — the pre-upgrade dump is in $APP_DIR/.update-backups, and upgrade_app took a btrfs snapshot of /federated"
fi
say "Superbrain updated to $NEW_REV"
cat <<EOF
Rollback anchors kept:
images: previous superbrain-{app,web}:local ids (re-tag from
'docker images -a' if ever needed)
data: $DUMP (last 5 kept in $DUMPS)
Restore data with the house backuptool / restore-db-from-backup flow, or:
gunzip -c $DUMP | docker exec -i postgresql psql -U postgres -d superbrain
When CI starts publishing registry images, drop IMAGE_VERSION_HOLD=1 from
$APP_DIR/.env and let the generic house upgrade flow take over.
EOF
NOW_VERSION=$(sed -n 's/^IMAGE_VERSION=//p' "$APP_DIR/.env" | tr -d '"' | tail -1)
say "Superbrain is at image version $NOW_VERSION (repo revision $NEW_REV), healthy"