Files
carly 1dfd41986c
release gate / check (push) Failing after 6m45s
release / smoke (push) Successful in 10m18s
release / publish (push) Failing after 9m55s
Ship install/update scripts in the migrate image; no repo clone on hosts
2026-10-30 12:03:07 -06:00

120 lines
6.0 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# Update Superbrain on a federated core host (REGISTRY mode).
#
# The app dir holds only compose + .env (no source); this script and the
# deploy files ship inside the migrate image at /deploy, so no clone:
#
# sudo bash -c "$(docker run --rm --pull always federatedcomputer/superbrain-migrate cat /deploy/update.sh)" update.sh [version]
#
# What it does:
# 1. Pulls federatedcomputer/superbrain-migrate:VERSION (default latest)
# and extracts its /deploy files — 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.
#
# Environment: SUPERBRAIN_VERSION (or $1), SUPERBRAIN_SRC (local repo
# checkout instead of the image; development).
#
set -euo pipefail
APP_DIR=/federated/apps/superbrain
IMAGE="federatedcomputer/superbrain-migrate:${1:-${SUPERBRAIN_VERSION:-latest}}"
STAMP=$(date +%Y%m%d-%H%M%S)
say() { printf '\033[1;34m==>\033[0m %s\n' "$*"; }
die() { printf '\033[1;31merror:\033[0m %s\n' "$*" >&2; exit 1; }
# --- 1. Preflights + source ----------------------------------------------------
[ "$(id -u)" = 0 ] || die "run as root on the core host"
[ -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"
if [ -n "${SUPERBRAIN_SRC:-}" ]; then
DEPLOY="$SUPERBRAIN_SRC/deploy/federated"
[ -f "$DEPLOY/lib-superbrain.sh" ] || die "SUPERBRAIN_SRC lacks deploy/federated files"
SOURCE="$SUPERBRAIN_SRC"
else
say "Pulling $IMAGE for the deploy files"
docker pull -q "$IMAGE" >/dev/null || die "could not pull $IMAGE — does the release exist on Docker Hub?"
DEPLOY=$(mktemp -d)
trap 'rm -rf "$DEPLOY"' EXIT
docker run --rm "$IMAGE" tar -C /deploy -c . | tar -x -C "$DEPLOY" \
|| die "could not extract /deploy from $IMAGE"
SOURCE="$IMAGE"
fi
for f in lib-superbrain.sh service upgrade-pre; do
[ -e "$DEPLOY/$f" ] || die "deploy files are missing $f — wrong image/version?"
done
# --- 2. Sync suite files (parse-checked, backed up) ------------------------------
say "Syncing suite integration files from $SOURCE"
LIB=/federated/lib/superbrain.sh
bash -n "$DEPLOY/lib-superbrain.sh" || die "staged lib-superbrain.sh does not parse — aborting"
[ -f "$LIB" ] && cp -a "$LIB" "$LIB.bak-$STAMP"
cp "$DEPLOY/lib-superbrain.sh" "$LIB"
mkdir -p /federated/services/superbrain/upgrade
[ -f /federated/services/superbrain/service ] && cp -a /federated/services/superbrain/service "/federated/services/superbrain/service.bak-$STAMP"
cp "$DEPLOY/service" /federated/services/superbrain/service
cp "$DEPLOY/upgrade-pre" /federated/services/superbrain/upgrade/pre
chmod 755 /federated/services/superbrain/upgrade/pre
# --- 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 "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"
# --- 4. House upgrade flow ----------------------------------------------------------
WANT_VERSION=$(sed -n 's/^VERSION=//p' /federated/services/superbrain/service | tr -d '"' | head -1)
# Source-mode migration: interim installs (2026-10-25..29) carry
# IMAGE_VERSION=local plus IMAGE_VERSION_HOLD=1, which makes upgrade_app
# skip the app entirely — and the re-rendered compose would try to pull a
# nonexistent :local tag. Convert .env to the registry version the service
# file wants before upgrading.
if grep -q '^IMAGE_VERSION_HOLD=' "$APP_DIR/.env"; then
say "Migrating .env from source-build mode to registry mode ($WANT_VERSION)"
sed -i '/^IMAGE_VERSION_HOLD=/d' "$APP_DIR/.env"
sed -i "s#^IMAGE_VERSION=.*#IMAGE_VERSION=\"$WANT_VERSION\"#" "$APP_DIR/.env"
fi
grep -q '^IMAGE_VERSION=' "$APP_DIR/.env" || echo "IMAGE_VERSION=\"$WANT_VERSION\"" >> "$APP_DIR/.env"
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"
# --- 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 "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
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
NOW_VERSION=$(sed -n 's/^IMAGE_VERSION=//p' "$APP_DIR/.env" | tr -d '"' | tail -1)
say "Superbrain is at image version $NOW_VERSION (deploy files from $SOURCE), healthy"