#!/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 /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 /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 < 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= -e SEED_SUBJECT= \\ 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 /tmp/superbrain-repo sudo bash /tmp/superbrain-repo/deploy/federated/update.sh [git-ref] EOF