296 lines
12 KiB
Bash
296 lines
12 KiB
Bash
#!/bin/bash
|
|
#
|
|
# Superbrain Service
|
|
#
|
|
# Superbrain is a Go API + Next.js web stack. Unlike most suite apps it is
|
|
# currently built FROM SOURCE on the host (interim mode, until CI publishes
|
|
# federatedcomputer/superbrain-{app,web,migrate} registry images):
|
|
#
|
|
# 1. Sync the release source tree to the staging dir (from the workstation):
|
|
# tar czf - --exclude=node_modules --exclude=.next \
|
|
# main.go go.mod go.sum Dockerfile internal db web \
|
|
# | ssh core 'rm -rf /federated/tmp/superbrain-src \
|
|
# && mkdir -p /federated/tmp/superbrain-src \
|
|
# && tar xzf - -C /federated/tmp/superbrain-src'
|
|
# 2. /federated/bin/installapp superbrain
|
|
#
|
|
# config_superbrain() copies the staged source into the app dir and builds
|
|
# superbrain-app:local / superbrain-web:local. The .env carries
|
|
# IMAGE_VERSION_HOLD=1 so the generic `upgrade` flow (which pulls registry
|
|
# images) skips superbrain until real image releases exist, and the service
|
|
# file's CONTAINER matches the first image: line of the generated compose so
|
|
# upgrade_app()'s image-rewrite sed can never mangle the file.
|
|
#
|
|
# Database: the shared suite postgres (container "postgresql",
|
|
# postgresql.$DOMAIN on the core network). A fresh database is bootstrapped
|
|
# with db/schema.sql applied AS the superbrain user (object ownership!), then
|
|
# the migrate one-shot runs the checksummed ledger on top — migrations are NOT
|
|
# a full schema by themselves (see db/check-convergence.sh in the repo).
|
|
#
|
|
# Networking: fixed core IPs — web 192.168.0.93 (traefik target; container
|
|
# name MUST be "superbrain" for upgrade_app()/monitor tooling), app .94,
|
|
# migrate .95. Nothing is published on host ports (127.0.0.1:8080 is taken
|
|
# on this box; web reaches app via container-name DNS on core).
|
|
#
|
|
# Rate limiting: TRUSTED_PROXY is deliberately NOT set — web only trusts
|
|
# X-Real-IP, which traefik does not send; all clients share one rate-limit
|
|
# bucket until that is solved (web logs a warning at startup).
|
|
|
|
PATH=$HOME/.docker/cli-plugins:/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
|
|
get_appvars
|
|
. /etc/federated
|
|
|
|
SUPERBRAIN_SRC="/federated/tmp/superbrain-src"
|
|
SUPERBRAIN_WEB_IP="192.168.0.93"
|
|
SUPERBRAIN_APP_IP="192.168.0.94"
|
|
SUPERBRAIN_MIGRATE_IP="192.168.0.95"
|
|
|
|
config_superbrain() {
|
|
echo -ne "* Configuring Superbrain container.."
|
|
|
|
[ -f "$SUPERBRAIN_SRC/main.go" ] || failcheck "superbrain source not staged at $SUPERBRAIN_SRC (see lib/superbrain.sh header for the sync command)"
|
|
|
|
mkdir -p /federated/apps/superbrain
|
|
cp -a "$SUPERBRAIN_SRC/." /federated/apps/superbrain/
|
|
|
|
SUPERBRAIN_DB_PASSWORD=$(create_password)
|
|
SUPERBRAIN_AUTH_SECRET=$(create_password)
|
|
SUPERBRAIN_ENCRYPTION_KEY=$(create_password)
|
|
|
|
# app is deliberately the FIRST service with an image: key — upgrade_app()
|
|
# reads the first image: line and compares it to CONTAINER in the service file.
|
|
cat > /federated/apps/superbrain/docker-compose.yml <<EOF
|
|
services:
|
|
app:
|
|
build: .
|
|
image: superbrain-app:local
|
|
container_name: superbrain-app
|
|
restart: always
|
|
depends_on:
|
|
migrate: {condition: service_completed_successfully}
|
|
env_file:
|
|
- ./.env
|
|
healthcheck:
|
|
test: [CMD, /superbrain, --healthcheck]
|
|
interval: 3s
|
|
timeout: 4s
|
|
retries: 20
|
|
read_only: true
|
|
cap_drop: [ALL]
|
|
security_opt: [no-new-privileges:true]
|
|
networks:
|
|
core:
|
|
ipv4_address: $SUPERBRAIN_APP_IP
|
|
|
|
web:
|
|
build: ./web
|
|
image: superbrain-web:local
|
|
# MUST be named exactly "superbrain": upgrade_app() probes
|
|
# `docker container inspect superbrain` and skips apps whose primary
|
|
# container it cannot find under the app name.
|
|
container_name: superbrain
|
|
hostname: superbrain.$DOMAIN
|
|
restart: always
|
|
depends_on:
|
|
app: {condition: service_healthy}
|
|
env_file:
|
|
- ./.env
|
|
# OIDC discovery targets the public authelia hostname; pin it like
|
|
# gitea/vaultwarden/immich do so the lookup doesn't hairpin via DNS.
|
|
extra_hosts:
|
|
- "authelia.$DOMAIN:$EXTERNALIP"
|
|
healthcheck:
|
|
# busybox wget from the alpine base; /login renders without a session.
|
|
test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1:3000/login"]
|
|
interval: 10s
|
|
timeout: 4s
|
|
retries: 3
|
|
start_period: 20s
|
|
read_only: true
|
|
tmpfs: [/tmp]
|
|
cap_drop: [ALL]
|
|
security_opt: [no-new-privileges:true]
|
|
networks:
|
|
core:
|
|
ipv4_address: $SUPERBRAIN_WEB_IP
|
|
labels:
|
|
- "traefik.enable=true"
|
|
- "traefik.http.routers.superbrain.rule=Host(\`superbrain.$DOMAIN\`)"
|
|
- "traefik.http.routers.superbrain.entrypoints=websecure"
|
|
- "traefik.http.routers.superbrain.tls.certresolver=letsencrypt"
|
|
- "traefik.http.services.superbrain.loadbalancer.server.port=3000"
|
|
|
|
migrate:
|
|
# Reuse the suite's postgres image (already on host; psql client major
|
|
# version matches the shared server).
|
|
image: federatedcomputer/federated-postgres:17.6
|
|
container_name: superbrain-migrate
|
|
restart: "no"
|
|
env_file:
|
|
- ./.env
|
|
volumes:
|
|
- ./db:/db:ro
|
|
command: [sh, /db/migrate.sh]
|
|
networks:
|
|
core:
|
|
ipv4_address: $SUPERBRAIN_MIGRATE_IP
|
|
|
|
networks:
|
|
core:
|
|
external: true
|
|
EOF
|
|
|
|
cat > /federated/apps/superbrain/.env <<EOF
|
|
# Interim source-build mode: no registry version yet. IMAGE_VERSION_HOLD
|
|
# makes the generic upgrade flow skip this app (see lib/superbrain.sh header).
|
|
IMAGE_VERSION=local
|
|
IMAGE_VERSION_HOLD=1
|
|
|
|
# --- Database (shared suite postgres; user+db created below) ---
|
|
DB_HOST=postgresql.$DOMAIN
|
|
DB_USER=superbrain
|
|
DB_NAME=superbrain
|
|
PGPASSWORD=$SUPERBRAIN_DB_PASSWORD
|
|
DATABASE_URL=postgres://superbrain:$SUPERBRAIN_DB_PASSWORD@postgresql.$DOMAIN:5432/superbrain?sslmode=disable
|
|
|
|
# --- Secrets — BACK THESE UP. Sessions and stored IMAP passwords are
|
|
# --- cryptographically bound to AUTH_SECRET/ENCRYPTION_KEY; losing them
|
|
# --- signs everyone out and destroys saved credentials even if the DB lives.
|
|
AUTH_SECRET=$SUPERBRAIN_AUTH_SECRET
|
|
NEXTAUTH_SECRET=$SUPERBRAIN_AUTH_SECRET
|
|
ENCRYPTION_KEY=$SUPERBRAIN_ENCRYPTION_KEY
|
|
|
|
# --- Web (NextAuth) ---
|
|
# Must be the exact browser-facing origin — the API proxy 403s every
|
|
# mutating request on an origin mismatch.
|
|
NEXTAUTH_URL=https://superbrain.$DOMAIN
|
|
AUTH_TRUST_HOST=true
|
|
API_BASE_URL=http://superbrain-app:8080
|
|
|
|
# --- Access policy (private instance: operator-provisioned accounts) ---
|
|
ADMIN_EMAILS=admin@$DOMAIN
|
|
ALLOW_BOOTSTRAP_SIGNUP=0
|
|
SIGNUP_ENABLED=0
|
|
COOKIE_SECURE=1
|
|
# TRUSTED_PROXY deliberately unset — traefik does not send X-Real-IP.
|
|
EOF
|
|
chmod 600 /federated/apps/superbrain/.env
|
|
|
|
# Create PostgreSQL database and user (house pattern)
|
|
docker exec postgresql psql -U postgres -c "CREATE USER superbrain WITH PASSWORD '$SUPERBRAIN_DB_PASSWORD'" &>/dev/null
|
|
docker exec postgresql psql -U postgres -c "CREATE DATABASE superbrain" &>/dev/null
|
|
docker exec postgresql psql -U postgres -c "GRANT ALL PRIVILEGES ON DATABASE superbrain TO superbrain" &>/dev/null
|
|
docker exec postgresql psql -U postgres -c "ALTER DATABASE superbrain OWNER TO superbrain" &>/dev/null
|
|
|
|
# Bootstrap a fresh database with schema.sql, applied AS the superbrain
|
|
# user so every object is owned by it (later migrations must be able to
|
|
# ALTER these). Empty-database gate: never re-runs over live data. The
|
|
# migrate one-shot then records the ledger chain on top at first start.
|
|
SB_TABLES=$(docker exec postgresql psql -U postgres -d superbrain -t -A -c "SELECT count(*) FROM information_schema.tables WHERE table_schema='public'" 2>/dev/null)
|
|
if [ "${SB_TABLES:-0}" = "0" ]; then
|
|
docker exec -e PGPASSWORD="$SUPERBRAIN_DB_PASSWORD" -i postgresql \
|
|
psql -h 127.0.0.1 -U superbrain -d superbrain -v ON_ERROR_STOP=1 -q \
|
|
< /federated/apps/superbrain/db/schema.sql || failcheck "superbrain schema.sql failed to apply"
|
|
fi
|
|
|
|
# Build app+web images from the staged source (a few minutes on first
|
|
# build, layer-cached afterwards).
|
|
echo -ne "\n* Building superbrain images (this takes a few minutes)..\n"
|
|
(cd /federated/apps/superbrain && docker compose build) || failcheck "superbrain image build failed"
|
|
|
|
echo -ne "done.\n"
|
|
}
|
|
|
|
start_superbrain() {
|
|
start_service "superbrain" "nc -z $SUPERBRAIN_WEB_IP 3000 &> /dev/null" "30"
|
|
|
|
docker exec pdns pdnsutil add-record $DOMAIN superbrain.$DOMAIN A 86400 $EXTERNALIP &>/dev/null
|
|
|
|
# Provision the operator account. Verified idempotent (no-op when the email
|
|
# already exists); grants ADMIN because admin@$DOMAIN is in ADMIN_EMAILS.
|
|
for i in $(seq 1 10); do
|
|
docker exec -e SEED_EMAIL="admin@$DOMAIN" -e SEED_PASSWORD="$ADMINPASS" -e SEED_NAME="Admin" \
|
|
superbrain-app /superbrain --seed &>/dev/null && break
|
|
sleep 3
|
|
done
|
|
|
|
echo -ne "done.\n"
|
|
}
|
|
|
|
uninstall_superbrain() {
|
|
echo -ne "* Uninstalling superbrain container.."
|
|
|
|
docker compose -f /federated/apps/superbrain/docker-compose.yml -p superbrain down &>/dev/null
|
|
docker image rm superbrain-app:local superbrain-web:local &>/dev/null || true
|
|
|
|
docker exec postgresql psql -U postgres -c "DROP DATABASE IF EXISTS superbrain" &>/dev/null
|
|
docker exec postgresql psql -U postgres -c "DROP USER IF EXISTS superbrain" &>/dev/null
|
|
|
|
rm -rf /federated/apps/superbrain
|
|
|
|
docker exec pdns pdnsutil delete-rrset $DOMAIN superbrain.$DOMAIN A &>/dev/null
|
|
|
|
# Remove Authelia SSO config if present (immich pattern)
|
|
if grep -q "### Superbrain" /federated/apps/authelia/data/config/idproviders.yml 2>/dev/null; then
|
|
sed -i '/### Superbrain/,/### /{/### [A-Z]/!{/### /!d}}' /federated/apps/authelia/data/config/idproviders.yml
|
|
sed -i '/### Superbrain/d' /federated/apps/authelia/data/config/idproviders.yml
|
|
restart_service authelia
|
|
fi
|
|
|
|
echo -ne "done.\n"
|
|
}
|
|
|
|
configsso_superbrain() {
|
|
echo -ne "* Configuring superbrain SSO with Authelia.."
|
|
|
|
[ ! -d "/federated/apps/authelia" ] && failcheck "Authelia is not installed. You need this first before continuing."
|
|
[ ! -f "/federated/apps/authelia/data/config/idproviders.yml" ] && failcheck "Authelia idproviders.yml is missing."
|
|
[[ $(grep "### Superbrain" /federated/apps/authelia/data/config/idproviders.yml 2>/dev/null) ]] && failcheck "Authelia already has a Superbrain configuration."
|
|
|
|
SUPERBRAIN_CLIENT_SECRET=$(create_password)
|
|
SUPERBRAIN_CLIENT_SECRET_HASH=$(docker run --rm authelia/authelia:latest authelia crypto hash generate pbkdf2 --password $SUPERBRAIN_CLIENT_SECRET | awk '{ print $2 }')
|
|
|
|
SUPERBRAIN_OIDC_CLIENT_YAML=$(cat <<EOF
|
|
### Superbrain
|
|
- client_id: 'superbrain'
|
|
client_name: 'Superbrain'
|
|
client_secret: $SUPERBRAIN_CLIENT_SECRET_HASH
|
|
consent_mode: 'implicit'
|
|
public: false
|
|
authorization_policy: 'one_factor'
|
|
redirect_uris:
|
|
- 'https://superbrain.$DOMAIN/api/auth/callback/authelia'
|
|
scopes:
|
|
- 'openid'
|
|
- 'profile'
|
|
- 'email'
|
|
userinfo_signed_response_alg: 'none'
|
|
EOF
|
|
)
|
|
printf '%s\n' "$SUPERBRAIN_OIDC_CLIENT_YAML" >> /federated/apps/authelia/data/config/idproviders.yml
|
|
printf '%s\n' "$SUPERBRAIN_OIDC_CLIENT_YAML" > /federated/apps/superbrain/.superbrain.oidc-client.yml
|
|
chmod 600 /federated/apps/superbrain/.superbrain.oidc-client.yml
|
|
|
|
restart_service authelia
|
|
|
|
# env_file pattern: appending here is enough — web picks the values up on
|
|
# restart, no compose rewrite needed.
|
|
cat >> /federated/apps/superbrain/.env <<EOF
|
|
|
|
# --- SSO (Authelia OIDC, written by configsso_superbrain) ---
|
|
OIDC_ISSUER=https://authelia.$DOMAIN
|
|
OIDC_CLIENT_ID=superbrain
|
|
OIDC_CLIENT_SECRET=$SUPERBRAIN_CLIENT_SECRET
|
|
EOF
|
|
|
|
# env_file gotcha (verified in production): compose stop/start or up -d do
|
|
# NOT recreate a container when only env_file CONTENT changed, so the
|
|
# appended OIDC_* vars would never reach web. Force-recreate explicitly.
|
|
(cd /federated/apps/superbrain && docker compose -p superbrain up -d --force-recreate web) || failcheck "could not recreate superbrain web after SSO configuration"
|
|
|
|
# NOTE: SSO auto-provisions NEW users only. Pre-existing password accounts
|
|
# (e.g. the seeded admin) must be explicitly linked by an administrator via
|
|
# POST /api/admin/oidc-link — email match alone is intentionally not proof.
|
|
echo -ne "done.\n"
|
|
}
|