From c333aab7912fd17de2c9419d699faced59d75de3 Mon Sep 17 00:00:00 2001 From: fluxion9 Date: Tue, 17 Nov 2026 21:37:55 +0100 Subject: [PATCH] feat: deploy central role with auto-compose.sh Read CONSOLE_ROLE from .env: central uses compose.central.yaml, defaults to ghcr.io/fluxion9/console:main, validates CENTRAL_DOMAIN and the first-start admin password, and starts the TLS proxy after Console readiness. Gateway role is refused because it needs the agent. Standalone is unchanged. --- auto-compose.sh | 42 +++++++++++++++++++++++++++---- compose.central.yaml | 2 +- tests/services/deployment.test.ts | 36 ++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 6 deletions(-) diff --git a/auto-compose.sh b/auto-compose.sh index 6b7f82c..3793bfb 100755 --- a/auto-compose.sh +++ b/auto-compose.sh @@ -19,6 +19,28 @@ while [[ $# -gt 0 ]]; do done if [[ -n "$project" && ! "$project" =~ ^[a-z0-9][a-z0-9_-]*$ ]]; then echo 'Invalid Compose project name.' >&2; exit 2; fi [[ -f .env ]] || { echo 'Missing .env: configure the existing services before deployment.' >&2; exit 1; } +# Read one .env setting without executing the file; strips surrounding quotes. +env_value() { + sed -n "s/^[[:space:]]*$1[[:space:]]*=[[:space:]]*//p" .env | tail -1 \ + | sed -e 's/[[:space:]]*$//' -e 's/^"\(.*\)"$/\1/' -e "s/^'\(.*\)'\$/\1/" +} +role=$(env_value CONSOLE_ROLE | sed -n 's/^\([a-z]*\).*/\1/p') +role="${role:-standalone}" +case "$role" in + standalone) + compose_file=compose.yaml; default_image=ghcr.io/m3tering/console:main ;; + central) + compose_file=compose.central.yaml; default_image=ghcr.io/fluxion9/console:main + [[ -n "$(env_value CENTRAL_DOMAIN)" ]] || { echo 'Set CENTRAL_DOMAIN in .env to the DNS name pointing at this server.' >&2; exit 2; } + # The administrator is created only on first start; later starts ignore the password. + if [[ ! -s .data/central.db ]]; then + password=$(env_value CENTRAL_ADMIN_PASSWORD) + (( ${#password} >= 16 )) || { echo 'Set CENTRAL_ADMIN_PASSWORD in .env (at least 16 characters) for the first central start.' >&2; exit 2; } + fi ;; + gateway) + echo 'Gateway role needs the Console and agent together: see docs/centralized/deployment.md (compose.gateway.yaml).' >&2; exit 2 ;; + *) echo 'CONSOLE_ROLE in .env must be standalone or central for this script.' >&2; exit 2 ;; +esac command -v docker >/dev/null || { echo 'Docker must already be installed.' >&2; exit 1; } docker compose version >/dev/null docker info >/dev/null @@ -37,18 +59,20 @@ if [[ -z "$project" ]]; then project="${projects[0]:-$(basename "$PWD" | tr '[:upper:]' '[:lower:]' | tr -cd 'a-z0-9_-')}" fi [[ "$project" =~ ^[a-z0-9][a-z0-9_-]*$ ]] || { echo 'Invalid Compose project name.' >&2; exit 2; } -compose=(docker compose --project-name "$project" -f compose.yaml) +compose=(docker compose --project-name "$project" -f "$compose_file") old_container=$("${compose[@]}" ps -aq console | head -1) old_id='' if [[ -n "$old_container" ]]; then old_id=$(docker inspect --format '{{.Image}}' "$old_container") old_ref=$(docker inspect --format '{{.Config.Image}}' "$old_container") + old_role=$(docker inspect --format '{{range .Config.Env}}{{println .}}{{end}}' "$old_container" | sed -n 's/^CONSOLE_ROLE=//p' | tail -1) echo "Current Console: $old_ref ($old_id)" - if [[ -z "$image" && "$old_ref" == ghcr.io/* ]]; then image="$old_ref"; fi + # Reuse the running image only for the same role: a standalone image may lack central support. + if [[ -z "$image" && "$old_ref" == ghcr.io/* && "${old_role:-standalone}" == "$role" ]]; then image="$old_ref"; fi fi -export CONSOLE_IMAGE="${image:-ghcr.io/m3tering/console:main}" +export CONSOLE_IMAGE="${image:-$default_image}" [[ "$CONSOLE_IMAGE" =~ ^ghcr\.io/[a-z0-9._/-]+(:[a-zA-Z0-9._-]+|@sha256:[a-f0-9]{64})$ ]] || { echo 'Specify a published GHCR image with an explicit tag or digest.' >&2; exit 2; } -echo "Project: $project; update image: $CONSOLE_IMAGE" +echo "Project: $project; role: $role; update image: $CONSOLE_IMAGE" "${compose[@]}" config --quiet # Keep the old service and image until the correct architecture is downloaded. "${compose[@]}" pull console @@ -68,5 +92,13 @@ if [[ -n "$old_id" && "$old_id" != "$new_id" && -z "$(docker ps -aq --filter "an # No force: Docker keeps images that still have other tags/references. docker image rm "$old_id" || echo 'Previous image retained because it is still referenced.' fi -"${compose[@]}" ps console +if [[ "$role" == central ]]; then + # TLS proxy (Caddy) publishes the loopback-only Console on ports 80/443. + "${compose[@]}" up --detach tls-proxy || { echo 'Console is running, but the TLS proxy failed to start; check ports 80/443.' >&2; exit 1; } +fi +"${compose[@]}" ps echo 'Console updated. Database, extension volumes and .env were preserved.' +if [[ "$role" == central ]]; then + admin=$(env_value CENTRAL_ADMIN_USERNAME) + echo "Central: https://$(env_value CENTRAL_DOMAIN) (sign in as ${admin:-admin})" +fi diff --git a/compose.central.yaml b/compose.central.yaml index f3d7a20..a3543a8 100644 --- a/compose.central.yaml +++ b/compose.central.yaml @@ -1,7 +1,7 @@ # Opt-in central test topology; supply CENTRAL_DOMAIN and central credentials. services: console: - image: ${CONSOLE_IMAGE:-ghcr.io/fluxion9/console:feat-centralized-console} + image: ${CONSOLE_IMAGE:-ghcr.io/fluxion9/console:main} network_mode: host restart: unless-stopped env_file: [.env] diff --git a/tests/services/deployment.test.ts b/tests/services/deployment.test.ts index 2075f4b..343766a 100644 --- a/tests/services/deployment.test.ts +++ b/tests/services/deployment.test.ts @@ -34,3 +34,39 @@ esac else expect(calls).toContain('image rm sha256:old'); } finally {fs.rmSync(dir,{recursive:true,force:true});} }); + +// Central role is chosen from .env; shares the same isolated Docker adapter. +function runCentral(environment:string, existingDb=false) { + const dir=fs.mkdtempSync(path.join(os.tmpdir(),'console-central-')); + try { + for(const file of ['auto-compose.sh','compose.yaml','compose.central.yaml'])fs.copyFileSync(file,path.join(dir,file)); + fs.writeFileSync(path.join(dir,'.env'),environment); + fs.mkdirSync(path.join(dir,'.data'));if(existingDb)fs.writeFileSync(path.join(dir,'.data','central.db'),'db'); + const log=path.join(dir,'calls');fs.writeFileSync(log,''); + fs.writeFileSync(path.join(dir,'docker'),`#!/bin/bash +printf '%s|%s\\n' "$*" "$CONSOLE_IMAGE" >> "$DEPLOY_LOG" +case "$*" in *'ps -q console'*) echo new-container ;; esac +`,{mode:0o755}); + const run=spawnSync('bash',['auto-compose.sh','--project-name','central'],{cwd:dir,encoding:'utf8',env:{...process.env,PATH:dir+':'+process.env.PATH,DEPLOY_LOG:log}}); + return {run,calls:fs.readFileSync(log,'utf8')}; + } finally {fs.rmSync(dir,{recursive:true,force:true});} +} +test('central role deploys console and TLS proxy from the central compose file', () => { + const {run,calls}=runCentral('CONSOLE_ROLE=central\nCENTRAL_DOMAIN="console.example.test"\nCENTRAL_ADMIN_PASSWORD=a-very-long-password-1\n'); + expect(run.status).toBe(0); + expect(calls).toContain('-f compose.central.yaml pull console|ghcr.io/fluxion9/console:main'); + expect(calls).toMatch(/up --detach tls-proxy/); + expect(calls).not.toContain('-f compose.yaml'); + expect(run.stdout).toContain('https://console.example.test'); +}); +test('central first start refuses a short administrator password before touching Docker', () => { + const {run,calls}=runCentral('CONSOLE_ROLE=central\nCENTRAL_DOMAIN=console.example.test\nCENTRAL_ADMIN_PASSWORD=short\n'); + expect(run.status).toBe(2);expect(run.stderr).toContain('at least 16 characters');expect(calls).toBe(''); +}); +test('central restart does not require the bootstrap password', () => { + expect(runCentral('CONSOLE_ROLE=central\nCENTRAL_DOMAIN=console.example.test\n',true).run.status).toBe(0); +}); +test('gateway role is refused because it needs the agent alongside Console', () => { + const {run,calls}=runCentral('CONSOLE_ROLE=gateway\n'); + expect(run.status).toBe(2);expect(run.stderr).toContain('compose.gateway.yaml');expect(calls).toBe(''); +});