Files
superbrain/deploy/federated/install.sh
T
Andrew S Erwin 341cdce026
release gate / check (push) Canceled after 4m3s
release / smoke (push) Successful in 10m13s
release / publish (push) Failing after 12m48s
Registry mode: publish app/web/migrate images, drop source builds on hosts
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.
2026-10-29 10:21:10 -05:00

198 lines
10 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# Install Superbrain into a federated "core" suite host (REGISTRY mode).
#
# For operators running their own copy of the federated suite. On the
# CORE HOST, as root:
#
# git clone <this-repo> /tmp/superbrain-repo
# sudo bash /tmp/superbrain-repo/deploy/federated/install.sh
#
# No source is built or stored on the host: the three release images
# (federatedcomputer/superbrain-{app,web,migrate}, one shared version tag,
# linux/amd64 + linux/arm64) are pulled from Docker Hub, and the migrate
# image owns the database lifecycle (empty database => schema.sql as the
# superbrain user, then the checksummed migration ledger).
#
# What it does:
# 1. Preflights the suite (installapp, functions.sh, /etc/federated, the
# core docker network, Authelia — REQUIRED because the core tier is
# Authelia-only — and the fcore SMTP credential for notification mail).
# 2. Gets the deploy files from a repo clone (or $SUPERBRAIN_SRC).
# 3. Installs the suite integration: /federated/lib/superbrain.sh,
# /federated/services/superbrain/{service,upgrade/pre}, with fixed-IP
# collision checks and SUPERBRAIN_*_IP overrides.
# 4. Runs /federated/bin/installapp superbrain (the standard house flow:
# compose/.env generation with TIER=core, image pulls, database +
# user on shared postgres, pdns record, Authelia client + claims
# policy, monitors, superpanel catalog).
# 5. Verifies and prints first-login steps.
#
# Environment overrides:
# SUPERBRAIN_REPO git URL to clone (default: this repo's origin)
# SUPERBRAIN_REF branch/tag to clone (default: main)
# SUPERBRAIN_SRC local repo checkout to take the deploy files from
# SUPERBRAIN_WEB_IP / SUPERBRAIN_APP_IP / SUPERBRAIN_MIGRATE_IP
# fixed core-network IPs (default .93/.94/.95 — the
# installer refuses if they are already allocated)
# SKIP_VERIFY=1 skip the post-install HTTPS probe
#
# Updates after install: sudo bash <repo>/deploy/federated/update.sh
# (syncs the service file's VERSION, then runs the house upgrade flow).
#
set -euo pipefail
REPO_DEFAULT="https://gitea.federated.computer/Federated_Computer/brain.git"
CLONE=/tmp/superbrain-install-src
LIB_DST=/federated/lib/superbrain.sh
SVC_DST_DIR=/federated/services/superbrain
WEB_IP="${SUPERBRAIN_WEB_IP:-192.168.0.93}"
APP_IP="${SUPERBRAIN_APP_IP:-192.168.0.94}"
MIGRATE_IP="${SUPERBRAIN_MIGRATE_IP:-192.168.0.95}"
say() { printf '\033[1;34m==>\033[0m %s\n' "$*"; }
warn() { printf '\033[1;33mwarning:\033[0m %s\n' "$*" >&2; }
die() { printf '\033[1;31merror:\033[0m %s\n' "$*" >&2; exit 1; }
# --- 0. Preflights -----------------------------------------------------------
[ "$(id -u)" = 0 ] || die "run as root on the core host (sudo bash $0)"
[ -x /federated/bin/installapp ] || die "/federated/bin/installapp missing — this does not look like a federated suite host"
[ -f /federated/lib/functions.sh ] || die "/federated/lib/functions.sh missing"
[ -f /etc/federated ] || die "/etc/federated missing"
command -v docker >/dev/null || die "docker not found"
docker network inspect core >/dev/null 2>&1 || die "the 'core' docker network does not exist"
[ -d /federated/apps/superbrain ] && die "/federated/apps/superbrain already exists — superbrain is installed (remove it with the suite's uninstall flow first)"
# Authelia is REQUIRED: the core tier is Authelia-only (TIER=core derives
# AUTH_MODE=oidc — no password login exists), and configsso registers the
# OIDC client. Without it nobody could ever sign in.
[ -d /federated/apps/authelia ] || die "Authelia is not installed. Superbrain's core tier is SSO-only; install Authelia first."
[ -f /federated/apps/authelia/data/config/idproviders.yml ] \
|| die "Authelia's idproviders.yml is missing — is Authelia configured with the OIDC provider?"
# The generated .env borrows the house fcore SMTP credential for the
# notification email channel; fail early with a pointer instead of deep
# inside config_superbrain.
if [ ! -f /federated/apps/authelia/data/secrets/NOTIFIER_SMTP_PASSWORD ] \
&& ! grep -qs '^EMAIL_HOST_PASSWORD=..*' /federated/apps/superpanel/.env; then
die "cannot find the fcore SMTP credential (authelia NOTIFIER_SMTP_PASSWORD secret or superpanel .env). The notification engine's email channel needs it; see deploy/federated/STRATEGY.md."
fi
# --- 1. Fixed-IP allocation check ---------------------------------------------
ip_taken() { # $1 = ip
docker network inspect core -f '{{range .Containers}}{{.IPv4Address}} {{end}}' 2>/dev/null | grep -q "$1/" && return 0
grep -rqs "^INTERNAL_IP=$1\$" /federated/services/*/service && return 0
return 1
}
for ip in "$WEB_IP" "$APP_IP" "$MIGRATE_IP"; do
if ip_taken "$ip"; then
die "IP $ip is already allocated on this host. Pick free addresses and re-run with e.g.:
sudo SUPERBRAIN_WEB_IP=192.168.0.193 SUPERBRAIN_APP_IP=192.168.0.194 SUPERBRAIN_MIGRATE_IP=192.168.0.195 bash $0"
fi
done
# --- 2. Get the deploy files ----------------------------------------------------
if [ -n "${SUPERBRAIN_SRC:-}" ]; then
SRC="$SUPERBRAIN_SRC"
[ -f "$SRC/deploy/federated/lib-superbrain.sh" ] || die "SUPERBRAIN_SRC=$SRC lacks deploy/federated files"
else
command -v git >/dev/null || die "git not found (or set SUPERBRAIN_SRC=/path/to/repo)"
REPO="${SUPERBRAIN_REPO:-$REPO_DEFAULT}"
REF="${SUPERBRAIN_REF:-main}"
say "Cloning $REPO ($REF) for the deploy files"
rm -rf "$CLONE"
git clone --depth 1 --branch "$REF" "$REPO" "$CLONE" \
|| die "git clone failed — check repository access/credentials"
SRC="$CLONE"
fi
for f in deploy/federated/lib-superbrain.sh deploy/federated/service deploy/federated/upgrade-pre; do
[ -e "$SRC/$f" ] || die "source is missing $f — wrong repo/ref?"
done
# --- 3. Install the suite integration files --------------------------------------
say "Installing suite files"
if [ -f "$LIB_DST" ]; then
cp -a "$LIB_DST" "$LIB_DST.bak-$(date +%Y%m%d-%H%M%S)"
warn "backed up an existing $LIB_DST"
fi
cp "$SRC/deploy/federated/lib-superbrain.sh" "$LIB_DST"
mkdir -p "$SVC_DST_DIR/upgrade"
[ -f "$SVC_DST_DIR/service" ] && cp -a "$SVC_DST_DIR/service" "$SVC_DST_DIR/service.bak-$(date +%Y%m%d-%H%M%S)"
cp "$SRC/deploy/federated/service" "$SVC_DST_DIR/service"
cp "$SRC/deploy/federated/upgrade-pre" "$SVC_DST_DIR/upgrade/pre"
chmod 755 "$SVC_DST_DIR/upgrade/pre"
# Apply IP overrides (defaults match the shipped files, so this is a no-op
# unless the operator chose different addresses). INTERNAL_IP in the service
# file is the web IP — monitors/DNS tooling read it.
if [ "$WEB_IP" != "192.168.0.93" ] || [ "$APP_IP" != "192.168.0.94" ] || [ "$MIGRATE_IP" != "192.168.0.95" ]; then
say "Rewriting fixed IPs: web=$WEB_IP app=$APP_IP migrate=$MIGRATE_IP"
sed -i "s|^SUPERBRAIN_WEB_IP=.*|SUPERBRAIN_WEB_IP=\"$WEB_IP\"|; \
s|^SUPERBRAIN_APP_IP=.*|SUPERBRAIN_APP_IP=\"$APP_IP\"|; \
s|^SUPERBRAIN_MIGRATE_IP=.*|SUPERBRAIN_MIGRATE_IP=\"$MIGRATE_IP\"|" "$LIB_DST"
sed -i "s|^INTERNAL_IP=.*|INTERNAL_IP=$WEB_IP|" "$SVC_DST_DIR/service"
fi
bash -n "$LIB_DST" || die "$LIB_DST does not parse — refusing to continue"
# --- 4. House install -------------------------------------------------------------
WANT_VERSION=$(sed -n 's/^VERSION=//p' "$SVC_DST_DIR/service" | tr -d '"' | head -1)
say "Running /federated/bin/installapp superbrain (pulls federatedcomputer/superbrain-*:$WANT_VERSION)"
/federated/bin/installapp superbrain || die "installapp failed; inspect the output above. The suite files remain in place; fix the cause and re-run installapp (or uninstall and re-run this script)."
# --- 5. Verify ------------------------------------------------------------------------
DOMAIN_HOST=$(grep -m1 '^DOMAIN=' /etc/federated | cut -d'"' -f2)
[ -n "$DOMAIN_HOST" ] || DOMAIN_HOST=$(sed -n 's/^DOMAIN=//p' /etc/federated | tr -d '"' | head -1)
say "Verifying"
fail=0
docker container inspect -f '{{.State.Status}}' superbrain-app 2>/dev/null | grep -q running \
|| { warn "superbrain-app is not running"; fail=1; }
docker container inspect -f '{{.State.Status}}' superbrain 2>/dev/null | grep -q running \
|| { warn "superbrain (web) is not running"; fail=1; }
if grep -q '### Superbrain' /federated/apps/authelia/data/config/idproviders.yml 2>/dev/null; then
grep -q '### Superbrain claims' /federated/apps/authelia/data/config/idproviders.yml \
|| warn "Authelia client registered but the claims policy marker is missing — SSO login would fail with AccessDenied; re-run configsso or check idproviders.yml"
else
warn "no '### Superbrain' block in Authelia idproviders.yml — SSO was not configured"
fail=1
fi
if [ "${SKIP_VERIFY:-0}" != 1 ] && [ -n "$DOMAIN_HOST" ]; then
ok=0
for _ in $(seq 1 20); do
code=$(curl -k -s -o /dev/null -w '%{http_code}' "https://superbrain.$DOMAIN_HOST/login" || true)
[ "$code" = 200 ] && { ok=1; break; }
sleep 3
done
[ "$ok" = 1 ] || warn "https://superbrain.$DOMAIN_HOST/login did not answer 200 yet (traefik may still be provisioning the certificate)"
fi
[ "$fail" = 0 ] || warn "install completed with warnings — review them before use"
cat <<EOF
$(say "Superbrain installed (registry mode, version $WANT_VERSION)")
App: https://superbrain.$DOMAIN_HOST (traefik -> web at $WEB_IP)
Data: shared suite postgres, database 'superbrain'
Tier: TIER=core (Authelia-only; password login/signup/reset are disabled)
First login (operator):
Sign in with the Authelia identity whose email is admin@$DOMAIN_HOST
(ADMIN_EMAILS). The first SSO login of a listed address becomes ADMIN
automatically. If your Authelia email differs, edit ADMIN_EMAILS in
/federated/apps/superbrain/.env and recreate the app container, or link
an existing account offline:
docker exec -e SEED_EMAIL=<email> -e SEED_SUBJECT=<authelia-sub> \\
superbrain-app /superbrain --seed
(Authelia subjects are opaque UUIDs: read them from Authelia's storage,
table user_opaque_identifier — NOT the username.)
Other users are provisioned upstream (Superpanel -> LDAP -> Authelia) and
appear in Superbrain on their first SSO login.
Updates: bump VERSION in $SVC_DST_DIR/service (or sync it from a repo
clone) and run /federated/bin/upgrade superbrain — or one command:
git clone <repo> /tmp/superbrain-repo
sudo bash /tmp/superbrain-repo/deploy/federated/update.sh [git-ref]
EOF