Files
superbrain/deploy/federated/install.sh
T
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

195 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 — no clone, no copying files around; this script and
# the deploy files ship inside the migrate image at /deploy:
#
# sudo bash -c "$(docker run --rm --pull always federatedcomputer/superbrain-migrate cat /deploy/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 the migrate image (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_VERSION release whose deploy files to use (default: latest;
# the service file inside it pins the image version)
# SUPERBRAIN_SRC local repo checkout to take the deploy files from
# instead of the image (development)
# 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: update.sh from the same image (syncs the service
# file's VERSION, then runs the house upgrade flow).
#
set -euo pipefail
IMAGE="federatedcomputer/superbrain-migrate:${SUPERBRAIN_VERSION:-latest}"
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"
[ -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 them in the environment, 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 -c \"\$(docker run --rm $IMAGE cat /deploy/install.sh)\""
fi
done
# --- 2. Get the deploy files ----------------------------------------------------
if [ -n "${SUPERBRAIN_SRC:-}" ]; then
DEPLOY="$SUPERBRAIN_SRC/deploy/federated"
[ -f "$DEPLOY/lib-superbrain.sh" ] || die "SUPERBRAIN_SRC=$SUPERBRAIN_SRC lacks deploy/federated files"
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"
fi
for f in lib-superbrain.sh service upgrade-pre; do
[ -e "$DEPLOY/$f" ] || die "deploy files are missing $f — wrong image/version?"
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 "$DEPLOY/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 "$DEPLOY/service" "$SVC_DST_DIR/service"
cp "$DEPLOY/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 and run
/federated/bin/upgrade superbrain — or one command (default: latest):
sudo bash -c "\\$(docker run --rm --pull always federatedcomputer/superbrain-migrate cat /deploy/update.sh)" update.sh [version]
EOF