Files
superbrain/deploy/federated/install.sh
T
Andrew S Erwin 2fa3979451
release gate / check (push) Failing after 6m37s
Add one-shot federated installer for other cores
deploy/federated/install.sh packages what the 2026-10-26 core install did
by hand, for any operator running their own federated suite with repo
access. Run on the core host as root; it:

- Preflights the suite: installapp/functions.sh//etc/federated, the core
  docker network, Authelia (REQUIRED — the core tier is SSO-only, so an
  install without Authelia would lock everyone out) and the fcore SMTP
  credential source for the notification email channel.
- Refuses when the app dir exists, and checks the fixed core-network IPs
  (.93/.94/.95) against live allocations and other services' INTERNAL_IP;
  SUPERBRAIN_{WEB,APP,MIGRATE}_IP override and rewrite lib + service file.
- Stages the source by git clone (SUPERBRAIN_REPO/SUPERBRAIN_REF) or from
  a local tree (SUPERBRAIN_SRC), same payload as the workstation tar-sync.
- Installs lib/superbrain.sh + services/superbrain/service (backing up
  anything existing), then runs the house flow via
  /federated/bin/installapp superbrain (config/start/configsso/monitors/
  superpanel catalog).
- Verifies containers, the Authelia client AND claims-policy markers (the
  AccessDenied footgun), and the HTTPS login page with a retry loop for
  traefik cert provisioning; prints the operator's first-login steps,
  including that Authelia subjects are opaque UUIDs from
  user_opaque_identifier (not usernames) for offline SEED_SUBJECT links.

Validated: shellcheck clean; preflight chain smoke-tested against the
live core (correct refuse at the already-installed guard). STRATEGY.md
interim-mode section now points at the installer.
2026-10-26 23:33:50 -05:00

196 lines
10 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# Install Superbrain into a federated "core" suite host.
#
# For operators running their own copy of the federated suite who have
# access to this repository. Run ON THE CORE HOST as root:
#
# git clone <this-repo> /tmp/superbrain-repo
# sudo bash /tmp/superbrain-repo/deploy/federated/install.sh
#
# What it does:
# 1. Preflights the suite (installapp, functions.sh, /etc/federated,
# docker network 'core', Authelia — required because the core tier is
# Authelia-only — and the fcore SMTP credential sources).
# 2. Stages the source at /federated/tmp/superbrain-src (git clone of
# this repo, or a copy of $SUPERBRAIN_SRC when set).
# 3. Installs the suite integration files:
# lib-superbrain.sh -> /federated/lib/superbrain.sh
# service -> /federated/services/superbrain/service
# 4. Runs /federated/bin/installapp superbrain, which performs the house
# flow: config_superbrain (compose/.env/db/images), start_superbrain
# (DNS + operator seed), configsso_superbrain (Authelia client +
# claims policy), monitors and superpanel catalog.
# 5. Verifies the result and prints the operator's next 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 directory to stage INSTEAD of cloning
# 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
#
set -euo pipefail
REPO_DEFAULT="https://gitea.federated.computer/Federated_Computer/brain.git"
STAGE=/federated/tmp/superbrain-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
# Source staging needs git unless a local source dir was given.
if [ -z "${SUPERBRAIN_SRC:-}" ]; then
command -v git >/dev/null || die "git not found (or set SUPERBRAIN_SRC=/path/to/source)"
fi
# --- 1. Fixed-IP allocation check ---------------------------------------------
# The compose file pins static IPs on the core network. Refuse collisions
# instead of letting docker fail obscurely mid-install.
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. Stage the source -------------------------------------------------------
if [ -n "${SUPERBRAIN_SRC:-}" ]; then
[ -f "$SUPERBRAIN_SRC/main.go" ] || die "SUPERBRAIN_SRC=$SUPERBRAIN_SRC does not look like a superbrain source tree"
say "Staging source from $SUPERBRAIN_SRC"
rm -rf "$STAGE"; mkdir -p "$STAGE"
# Same payload as the workstation tar-sync in lib-superbrain.sh's header.
(cd "$SUPERBRAIN_SRC" && tar cf - --exclude=node_modules --exclude=.next --exclude=.git \
main.go go.mod go.sum Dockerfile internal db web deploy) | tar xf - -C "$STAGE"
else
REPO="${SUPERBRAIN_REPO:-$REPO_DEFAULT}"
REF="${SUPERBRAIN_REF:-main}"
say "Cloning $REPO ($REF) into $STAGE"
rm -rf "$STAGE"
git clone --depth 1 --branch "$REF" "$REPO" "$STAGE" \
|| die "git clone failed — check repository access/credentials"
fi
for f in main.go Dockerfile db/schema.sql web/Dockerfile \
deploy/federated/lib-superbrain.sh deploy/federated/service; do
[ -e "$STAGE/$f" ] || die "staged 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 "$STAGE/deploy/federated/lib-superbrain.sh" "$LIB_DST"
mkdir -p "$SVC_DST_DIR"
[ -f "$SVC_DST_DIR/service" ] && cp -a "$SVC_DST_DIR/service" "$SVC_DST_DIR/service.bak-$(date +%Y%m%d-%H%M%S)"
cp "$STAGE/deploy/federated/service" "$SVC_DST_DIR/service"
# 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 ------------------------------------------------------------
say "Running /federated/bin/installapp superbrain (builds images — several minutes)"
/federated/bin/installapp superbrain || die "installapp failed; inspect the output above. The staged source and 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")
App: https://superbrain.$DOMAIN_HOST (traefik -> web 192.168.0.93-style fixed IP $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.
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.
EOF