Deploy script and AUTHELIA documentation (Tim did that)
release gate / check (push) Failing after 6m53s
release gate / check (push) Failing after 6m53s
This commit is contained in:
@@ -0,0 +1,254 @@
|
||||
# Federated Suite Deploy Strategy
|
||||
|
||||
How Superbrain becomes a first-class app in the `/federated` suite on `core`,
|
||||
installable with `/federated/bin/installapp superbrain`, upgradeable with the
|
||||
standard `upgrade` flow, and covered by the suite's monitors, DNS, Authelia
|
||||
SSO, backups and superpanel catalog.
|
||||
|
||||
## Current state (verified 2026-10-22)
|
||||
|
||||
Two parallel, conflicting realities exist on `core`:
|
||||
|
||||
1. **Ad-hoc source stack** in `/federated/apps/superbrain/`: an rsync of this
|
||||
repo running the full compose stack (own postgres volume, web published on
|
||||
host port `:3100` because bun holds `:3000`). Not integrated: no traefik
|
||||
labels, no `core` network IP, no pdns record, invisible to
|
||||
`installapp`/`upgrade`/monitors/superpanel.
|
||||
2. **n8n-shaped placeholders**: `/federated/lib/superbrain.sh` and
|
||||
`/federated/services/superbrain/service` were copied from n8n and describe
|
||||
an app that does not exist — single image `superbrainio/superbrain` on port
|
||||
5678, n8n's `/rest/owner/setup`, n8n's `deployment_key`/`user_api_keys`
|
||||
schema for the mcpserver link, and `INTERNAL_IP=192.168.0.71` **which
|
||||
collides with the real n8n**. Both files must be rewritten, not patched.
|
||||
|
||||
`installapp` refuses to run while `/federated/apps/superbrain` exists, so the
|
||||
ad-hoc stack must be migrated and removed (or moved aside) before the
|
||||
federated install can proceed.
|
||||
|
||||
## Target architecture
|
||||
|
||||
Superbrain is **not one image**. It is three artifacts built from this repo:
|
||||
|
||||
| Artifact | Source | Role |
|
||||
|---|---|---|
|
||||
| `superbrain-app` | `Dockerfile` (scratch, Go) | API on :8080, `--healthcheck`, `--seed` |
|
||||
| `superbrain-web` | `web/Dockerfile` (Next standalone) | UI on :3000, API proxy, NextAuth |
|
||||
| `superbrain-migrate` | NEW: `FROM postgres:16.15-alpine` + `COPY db /db` | one-shot schema bootstrap + migration runner |
|
||||
|
||||
The federated `upgrade_app` flow only understands **registry images**
|
||||
(`docker pull CONTAINER:VERSION`, btrfs snapshot, compose down/up, health
|
||||
probe). Source-on-host builds fight that machinery, so:
|
||||
|
||||
**Decision: publish versioned images from CI; the host never builds.**
|
||||
|
||||
**Interim mode (implemented now, 2026-10-25):** until CI publishes images,
|
||||
`lib/superbrain.sh` builds from source staged at `/federated/tmp/superbrain-src`
|
||||
(tar-synced from the workstation; the exact command is in the script header).
|
||||
Images are tagged `superbrain-app:local` / `superbrain-web:local`, `.env`
|
||||
carries `IMAGE_VERSION_HOLD=1` so the generic `upgrade` flow skips the app
|
||||
cleanly, and the service file's `CONTAINER=superbrain-app` matches the first
|
||||
`image:` line of the generated compose so `upgrade_app`'s rewrite-sed can
|
||||
never mangle it. Switching to registry mode later = publish images, drop the
|
||||
`build:` keys + hold marker, point `CONTAINER`/`VERSION` at the registry.
|
||||
|
||||
For the registry target:
|
||||
|
||||
- House precedent: the operator publishes custom builds under Docker Hub
|
||||
**`federatedcomputer/*`** (bookstack, calcom, wordpress, superpanel, pdns,
|
||||
federated-postgres). Superbrain images go there:
|
||||
`federatedcomputer/superbrain-{app,web,migrate}`.
|
||||
- CI release workflow triggers on tags matching `^[0-9]+\.[0-9]+\.[0-9]+$`
|
||||
(the suite's `VERSION_RX` is `'^[0-9][0-9\.]*$'` — no `v` prefix), builds
|
||||
and pushes all three images under the same tag (plus `:latest`).
|
||||
- `/federated/services/superbrain/service` sets `CONTAINER=` to the **app**
|
||||
image and `VERSION=` to the current release.
|
||||
- `upgrade_app` greps the *first* `image:` line of the app compose file and
|
||||
pulls only that one → in the generated compose, **`app` must be the first
|
||||
service with an `image:` key**, and a `services/superbrain/upgrade/pre`
|
||||
hook should `docker compose pull` so `web`/`migrate` are fetched too
|
||||
before the redeploy. `upgrade_app` then rewrites `IMAGE_VERSION` in
|
||||
`/federated/apps/superbrain/.env` (its `sed` on `VERSION=` matches the
|
||||
`IMAGE_VERSION=` line) and restarts.
|
||||
- `upgrade_app` also probes `docker container inspect superbrain` — the
|
||||
**web container must be named exactly `superbrain`** (plane/jitsi are
|
||||
special-cased in `functions.sh` for not doing this; we avoid that by
|
||||
complying).
|
||||
|
||||
### Database: shared `postgresql` container
|
||||
|
||||
Suite convention (`DEPENDS="postgresql"`): per-app user + database created via
|
||||
`docker exec postgresql psql`, covered by the existing backuptool and
|
||||
`restore-db-from-backup` flows, plus pre-upgrade btrfs snapshots. No dedicated
|
||||
db container, no host-published ports.
|
||||
|
||||
**Critical bootstrap order** (verified against `db/check-convergence.sh`):
|
||||
migrations run *on top of* `schema.sql`; migration `001_legacy_additive` is
|
||||
not a full initial schema. A fresh database must get `schema.sql` first, then
|
||||
`migrate.sh`. The `superbrain-migrate` image therefore ships a wrapper
|
||||
(`db/bootstrap.sh`, new in this repo): if the target database has no tables,
|
||||
apply `/db/schema.sql`, then always run `/db/migrate.sh`. This makes
|
||||
fresh-install and upgrade the same command, and stays compatible with the
|
||||
repo's own compose stack (where initdb already applied schema.sql).
|
||||
|
||||
**Version note**: the shared server is `federatedcomputer/federated-postgres:`
|
||||
**17.6** while this repo develops/tests against 16.15. Run the integration
|
||||
suite (or at least the converge check) against Postgres 17 once before first
|
||||
install.
|
||||
|
||||
### Networking & routing
|
||||
|
||||
House pattern (verified against immich/plane/docuseal/vaultwarden): all
|
||||
containers on the external `core` network with fixed IPs, `env_file: ./.env`
|
||||
as the single config source, traefik labels on the front container only.
|
||||
|
||||
- Fixed IPs (provisional — `.90–.92` belong to mcpserver/hermes, `.120/.121`
|
||||
to nextcloudtalk; re-verify at install time): web `192.168.0.93`, app
|
||||
`.94`, migrate `.95`.
|
||||
- `web` (container name **`superbrain`**, hostname `superbrain.$DOMAIN`)
|
||||
carries the traefik labels: `Host(\`superbrain.$DOMAIN\`)`,
|
||||
`entrypoints=websecure`, `certresolver=letsencrypt`, loadbalancer port
|
||||
**3000**. `app`/`migrate` get no labels — only `web` is routable.
|
||||
- `web` reaches the API via `API_BASE_URL=http://superbrain-app:8080`
|
||||
(container-name DNS on `core`). Nothing is published on host ports.
|
||||
- `web` pins `extra_hosts: authelia.$DOMAIN:$EXTERNALIP` (the hairpin
|
||||
pattern gitea/vaultwarden/immich/calcom use) so in-container OIDC
|
||||
discovery against the public issuer URL works.
|
||||
- `NEXTAUTH_URL=https://superbrain.$DOMAIN` — mandatory exact match; the API
|
||||
proxy's origin allowlist 403s every mutating request otherwise.
|
||||
- DNS: `start_superbrain` adds the pdns A record `superbrain.$DOMAIN →
|
||||
$EXTERNALIP`, same as every other app.
|
||||
- Monitor: `MONITOR="https://superbrain.@DOMAIN@/login|http_2xx"` (`/login`
|
||||
renders without a session and is what the web healthcheck already probes).
|
||||
- Hardening from the repo compose is kept: `read_only`, `cap_drop: [ALL]`,
|
||||
`no-new-privileges`, `tmpfs: [/tmp]` on web.
|
||||
|
||||
### Security env (production, per `.env.example` + AGENTS.md)
|
||||
|
||||
Written by `config_superbrain` into `/federated/apps/superbrain/.env`
|
||||
(chmod 600), all secrets via `create_password`:
|
||||
|
||||
- `AUTH_SECRET` — NextAuth JWT + Go session bridge + assertion HMAC signing.
|
||||
- `ENCRYPTION_KEY` — dedicated key for stored IMAP credentials; **rotating
|
||||
AUTH_SECRET must not invalidate them**.
|
||||
- `POSTGRES_PASSWORD` for the `superbrain` DB user.
|
||||
- `ADMIN_EMAILS=admin@$DOMAIN`, `ALLOW_BOOTSTRAP_SIGNUP=0`,
|
||||
`SIGNUP_ENABLED=0` (private instance; accounts provisioned by operator),
|
||||
`COOKIE_SECURE=1`.
|
||||
- Operator account: `docker exec -e SEED_EMAIL=admin@$DOMAIN
|
||||
-e SEED_PASSWORD="$ADMINPASS" superbrain-app /superbrain --seed` — verified
|
||||
idempotent (no-op if the email exists) and grants ADMIN when the email is in
|
||||
`ADMIN_EMAILS`.
|
||||
|
||||
**Rate-limiting caveat (open item):** with `TRUSTED_PROXY=1` the web container
|
||||
reads the client IP from `X-Real-IP` **only** — it deliberately ignores
|
||||
`X-Forwarded-For` (leftmost entry is client-supplied). Traefik does not set
|
||||
`X-Real-IP`, so behind traefik either (a) leave `TRUSTED_PROXY` unset and
|
||||
accept that all clients share one rate-limit bucket (web logs a startup
|
||||
warning), or (b) do the small repo work item: derive the client IP from XFF
|
||||
when pinned by trusted-entrypoint config (traefik `forwardedHeaders.trustedIPs`).
|
||||
Decide before cutover; (a) is safe to start with.
|
||||
|
||||
### SSO: `configsso_superbrain` (Authelia)
|
||||
|
||||
Follows the bookstack pattern in `lib/bookstack.sh`:
|
||||
|
||||
1. Append a client to `/federated/apps/authelia/data/config/idproviders.yml`:
|
||||
`client_id: superbrain`, `consent_mode: implicit`,
|
||||
`authorization_policy: one_factor`, scopes `openid profile email`,
|
||||
`redirect_uris: [https://superbrain.$DOMAIN/api/auth/callback/authelia]`
|
||||
(the NextAuth provider id is `authelia`), save a copy to
|
||||
`/federated/apps/superbrain/.superbrain.oidc-client.yml` (chmod 600),
|
||||
`restart_service authelia`.
|
||||
2. Append `OIDC_ISSUER=https://authelia.$DOMAIN`, `OIDC_CLIENT_ID=superbrain`,
|
||||
`OIDC_CLIENT_SECRET=<create_password>` to the app `.env`, then
|
||||
`docker compose up -d --force-recreate web` — **not** `restart_service`:
|
||||
compose does not recreate containers when only env_file content changed
|
||||
(hit in production 2026-10-25: web came up with zero OIDC vars and
|
||||
NextAuth returned `error=Configuration`).
|
||||
|
||||
Verified end-to-end: in-container issuer discovery works through the
|
||||
`extra_hosts` pin, and `/api/auth/signin/authelia` 302s to
|
||||
`https://authelia.$DOMAIN/api/oidc/authorization` with the exact registered
|
||||
redirect URI, `openid profile email` and PKCE S256.
|
||||
|
||||
Operator note (product invariant): SSO sign-in auto-provisions **new** users,
|
||||
but existing password accounts require explicit linking via
|
||||
`POST /api/admin/oidc-link` — email match alone is intentionally not proof.
|
||||
Seed the admin with password first, then link, or let SSO provision and
|
||||
promote via `ADMIN_EMAILS`.
|
||||
|
||||
### mcpserver link: drop for now
|
||||
|
||||
The n8n-template `config_mcpserver_superbrain` (minting an HS256 public-API
|
||||
JWT from `deployment_key`, inserting into `user_api_keys`, n8n scope list)
|
||||
describes n8n's schema and does not apply to this codebase — Superbrain has no
|
||||
equivalent long-lived API-key surface yet (browser sessions + 60s Bearer
|
||||
assertions only). Remove it from `lib/superbrain.sh` until/unless Superbrain
|
||||
grows a real API-key mechanism; revisit then.
|
||||
|
||||
### Not applicable
|
||||
|
||||
- `email_superbrain`: the app sends no outbound mail (no SMTP config exists;
|
||||
only IMAP/CardDAV ingest). Skip, or send the standard welcome mail with the
|
||||
admin credentials using the suite's mail helpers.
|
||||
- `data/` volume: the app is stateless apart from postgres. No
|
||||
`/home/node/.superbrain` volume (that was n8n's). Nothing to `chown`.
|
||||
|
||||
## Cutover plan (existing ad-hoc directory → federated app)
|
||||
|
||||
**Executed 2026-10-25.** The ad-hoc stack never ran (no containers, no
|
||||
volumes, empty schema-only database), so there was no data to migrate:
|
||||
|
||||
1. ✅ Verified the manual database was empty, dropped it and its user.
|
||||
2. ✅ Moved the ad-hoc dir to `/federated/tmp/superbrain-adhoc-20261025`.
|
||||
3. ✅ Staged fresh source at `/federated/tmp/superbrain-src`, installed
|
||||
`lib/superbrain.sh` + `services/superbrain/service`.
|
||||
4. ✅ Ran `/federated/bin/installapp superbrain` (config → build → start →
|
||||
pdns → seed admin → Authelia SSO → monitors → superpanel catalog).
|
||||
5. Remaining: verify `https://superbrain.$DOMAIN/login`, admin login,
|
||||
traefik cert, monitor green (`/federated/bin/check superbrain`); link the
|
||||
admin account to SSO via oidc-link; soak; then delete the ad-hoc backup
|
||||
dir and the orphaned `superbrain-{app,web}:latest` images from the
|
||||
earlier manual build.
|
||||
|
||||
**Rollback:** `uninstallapp superbrain` (drops the database, removes DNS,
|
||||
Authelia client, images and app dir) and restore
|
||||
`/federated/tmp/superbrain-adhoc-20261025` if ever needed.
|
||||
|
||||
## Repo work items (this repository)
|
||||
|
||||
1. **CI release workflow** — on semver tags: build & push `superbrain-app`,
|
||||
`superbrain-web`, `superbrain-migrate` to the chosen registry (single tag
|
||||
for all three + `latest`). Reuse the Dockerfiles; the migrate image is
|
||||
`FROM postgres:16.15-alpine` + `COPY db /db` + bootstrap wrapper.
|
||||
2. **`db/bootstrap.sh`** — apply `schema.sql` only to an empty database, then
|
||||
`migrate.sh`. Wire it as the migrate image's command (compose `migrate`
|
||||
service keeps working unchanged since initdb already applied schema.sql).
|
||||
3. **Rate-limit decision** — XFF support behind traefik or accept the shared
|
||||
bucket (see caveat above).
|
||||
4. Nothing else in the repo needs to change; the compose stack, `make check`
|
||||
gate and `scripts/deploy.sh` (standalone non-federated deploys) stay as-is.
|
||||
|
||||
## Suite-side work items (on `core`, via the federated git repo)
|
||||
|
||||
1. Rewrite `/federated/lib/superbrain.sh` — draft in `lib-superbrain.sh`
|
||||
next to this document.
|
||||
2. Rewrite `/federated/services/superbrain/service` — draft in `service`.
|
||||
3. Add `/federated/services/superbrain/upgrade/pre` that runs
|
||||
`docker compose pull` in the app dir (upgrade_app only pulls the app image).
|
||||
4. Allocate the fixed `core`-network IP (grep current `ipv4_address`
|
||||
assignments across `/federated/lib/*.sh` and `docker network inspect core`).
|
||||
5. Test the whole flow on a staging VM (or a second federated host) before
|
||||
touching `core`: `installapp superbrain` → verify → `upgrade superbrain`
|
||||
with a bumped VERSION → verify → `uninstallapp superbrain`.
|
||||
|
||||
## Open questions
|
||||
|
||||
- ~~Registry~~ → decided: Docker Hub `federatedcomputer/*` (house precedent);
|
||||
confirm the operator will grant CI push access (token/secret).
|
||||
- Confirm the `.93/.94/.95` IP allocation with the operator (nothing
|
||||
currently references them, but allocation is centrally tracked).
|
||||
- Should `SIGNUP_ENABLED` stay 0 (operator-provisioned only) for suite tenants?
|
||||
- Product name/description/docs URL/icon for the superpanel catalog entry.
|
||||
- Postgres 17.6 (shared) vs 16.15 (repo-pinned): run the integration suite
|
||||
against 17 once and decide whether the repo should widen its pin.
|
||||
@@ -0,0 +1,295 @@
|
||||
#!/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"
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
NAME=superbrain
|
||||
DESCRIPTION="Bookmark & Knowledge Base"
|
||||
DOCS=https://github.com/andrew/superbrain
|
||||
CONTAINER=superbrain-app
|
||||
VERSION=0.1.0
|
||||
INTERNAL_IP=192.168.0.93
|
||||
DEPENDS="postgresql"
|
||||
PORTS=3000
|
||||
VERSION_RX='^[0-9][0-9\.]*$'
|
||||
MONITOR="https://superbrain.@DOMAIN@/login|http_2xx"
|
||||
Reference in New Issue
Block a user