2936b08c6c
compose up -d leaves NATS on its old config when only the bind-mounted nats.conf changed, so the 8 MB max_payload never applied on upgrades. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
553 lines
26 KiB
Bash
Executable File
553 lines
26 KiB
Bash
Executable File
#!/usr/bin/env bash
|
||
#=============================================================================
|
||
# ExecuteR - server stack deployment
|
||
#=============================================================================
|
||
#
|
||
# Bootstrap straight from the repository (no clone required):
|
||
#
|
||
# curl -fsSL https://git.example.com/owner/ExecuteR/raw/branch/main/scripts/deploy.sh | bash
|
||
# curl -fsSL https://.../deploy.sh | bash -s -- --with-proxy --host control.example.com
|
||
#
|
||
# Or from a checkout:
|
||
#
|
||
# ./scripts/deploy.sh
|
||
# ./scripts/deploy.sh --with-proxy --host control.example.com
|
||
#
|
||
# Pulls the published images from the Gitea container registry and falls back
|
||
# to building from source when a pull is not possible. The in-stack reverse
|
||
# proxy is OPT-IN (--with-proxy); by default the stack assumes you already run
|
||
# an edge proxy.
|
||
#
|
||
# The database is the bundled Postgres container for a new install. An install
|
||
# that already has a SQLite database keeps it (DATABASE_PROVIDER=sqlite is
|
||
# written to .env); README, Moving from SQLite to Postgres, has the move.
|
||
#=============================================================================
|
||
set -Eeuo pipefail
|
||
|
||
# ── Defaults (override with flags or environment) ───────────────────────────
|
||
REPO_RAW_URL="${REPO_RAW_URL:-https://prod.git.gracesolution.info/gsadmin/ExecuteR/raw/branch/main}"
|
||
INSTALL_DIR="${INSTALL_DIR:-/opt/executer}"
|
||
WITH_PROXY=0
|
||
PUBLIC_HOSTNAME="${PROXY_PUBLIC_HOSTNAME:-}"
|
||
FORCE_BUILD=0
|
||
NO_PULL=0
|
||
NO_START=0
|
||
|
||
log() { printf '\033[1;34m==>\033[0m %s\n' "$*"; }
|
||
warn() { printf '\033[1;33mwarn:\033[0m %s\n' "$*" >&2; }
|
||
die() { printf '\033[1;31merror:\033[0m %s\n' "$*" >&2; exit 1; }
|
||
|
||
# ── Database decisions and .env helpers (scripts/tests/Test-DeployDatabase.sh runs them without Docker)
|
||
# The bundled Postgres as the backend reaches it; the password is DATABASE_PASSWORD.
|
||
BUNDLED_POSTGRES_CONNECTION="Host=postgres;Port=5432;Database=executer;Username=executer"
|
||
|
||
# database_provider <env-is-new 0|1> <DATABASE_PROVIDER from .env> <sqlite file>
|
||
# Prints "<provider> <why>": the provider .env names (as the backend reads it:
|
||
# postgres, sqlserver, anything else sqlite) when the operator set one; else
|
||
# sqlite when that SQLite database already exists, so an install from before
|
||
# Postgres became the default never switches by itself; else postgres. A .env
|
||
# just copied from .env.example has not been chosen by anyone, so it counts as
|
||
# unset.
|
||
database_provider() {
|
||
local new_env="$1" configured sqlite_file="$3"
|
||
configured="$(printf '%s' "$2" | tr '[:upper:]' '[:lower:]' | tr -d '[:space:]')"
|
||
if [[ $new_env != 1 && -n $configured ]]; then
|
||
case "$configured" in
|
||
postgres|postgresql|npgsql) echo "postgres configured" ;;
|
||
sqlserver|mssql) echo "sqlserver configured" ;;
|
||
*) echo "sqlite configured" ;;
|
||
esac
|
||
elif [[ -s $sqlite_file ]]; then
|
||
echo "sqlite existing-sqlite"
|
||
else
|
||
echo "postgres new-install"
|
||
fi
|
||
}
|
||
|
||
# postgres_bundled <provider> <connection string>: true for postgres with no
|
||
# connection string or one whose host is the bundled service.
|
||
postgres_bundled() {
|
||
[[ $1 == postgres ]] || return 1
|
||
local cs pattern=';(host|server)=postgres(:5432)?;'
|
||
cs="$(printf '%s' "$2" | tr '[:upper:]' '[:lower:]' | tr -d '[:space:]')"
|
||
[[ -z $cs || ";$cs;" =~ $pattern ]]
|
||
}
|
||
|
||
# database_password_action <DATABASE_PASSWORD> <postgres data folder>: keep (one
|
||
# is set), generate (none, and no database yet), or lost (none, but the folder
|
||
# holds a database initialised with a password that is no longer known).
|
||
database_password_action() {
|
||
if [[ -n $1 ]]; then
|
||
echo keep
|
||
elif [[ -d $2 && -n "$(ls -A "$2" 2>/dev/null)" ]]; then
|
||
echo lost
|
||
else
|
||
echo generate
|
||
fi
|
||
}
|
||
|
||
# is_sample_address <value>: true when the value (a URL, a host, or a comma
|
||
# list of hosts) names only sample hosts, as .env.example ships them
|
||
# (control.example.com): example.com/.net/.org, .example and .invalid names.
|
||
is_sample_address() {
|
||
local value host IFS=',;'
|
||
value="$(printf '%s' "$1" | tr '[:upper:]' '[:lower:]' | tr -d '[:space:]"')"
|
||
[[ -n $value ]] || return 1
|
||
for host in $value; do
|
||
host="${host#*://}"; host="${host%%/*}"; host="${host%%:*}"; host="${host#\*.}"; host="${host%.}"
|
||
[[ $host =~ (^|\.)(example\.(com|net|org)|example|invalid)$ ]] || return 1
|
||
done
|
||
}
|
||
|
||
# clear_sample_addresses: PUBLIC_SERVER_URL, TRUSTED_HOSTS and
|
||
# PROXY_PUBLIC_HOSTNAME left at a sample value are emptied in .env (current
|
||
# folder). Empty is what works: links follow the proxy's forwarded host, and
|
||
# devices download from the server URL they already reach.
|
||
clear_sample_addresses() {
|
||
local key
|
||
for key in PUBLIC_SERVER_URL TRUSTED_HOSTS PROXY_PUBLIC_HOSTNAME; do
|
||
if is_sample_address "${!key:-}"; then
|
||
log "Clearing $key=${!key} in .env (a sample address, not this server's)"
|
||
set_env "$key" ""
|
||
fi
|
||
done
|
||
}
|
||
|
||
# config_changed <file> <stamp>: true when <file> differs from what the last
|
||
# deploy applied (its checksum in <stamp>; no stamp counts as changed). A
|
||
# bind-mounted config file that changes doesn't change the container, so
|
||
# `compose up -d` leaves the service running on the old one.
|
||
config_changed() {
|
||
local now
|
||
now="$(sha256sum "$1" | cut -d' ' -f1)"
|
||
[[ ! -f $2 || "$(tr -d '\r\n' < "$2")" != "$now" ]]
|
||
}
|
||
|
||
# config_applied <file> <stamp>: records <file>'s checksum as applied.
|
||
config_applied() { sha256sum "$1" | cut -d' ' -f1 > "$2"; }
|
||
|
||
# compose_profiles_with <COMPOSE_PROFILES> <add|remove> <profile>: the comma
|
||
# list with that profile in or out, the others kept in order.
|
||
compose_profiles_with() {
|
||
local IFS=, kept=() profile
|
||
for profile in $1; do
|
||
profile="${profile// /}"
|
||
[[ -n $profile && $profile != "$3" ]] && kept+=("$profile")
|
||
done
|
||
[[ $2 == add ]] && kept+=("$3")
|
||
echo "${kept[*]}"
|
||
}
|
||
|
||
# version_at_least <version> <minimum>: dotted versions compared numerically.
|
||
version_at_least() {
|
||
local sorted
|
||
sorted="$(printf '%s\n%s\n' "$2" "$1" | sort -V)"
|
||
[[ ${sorted%%$'\n'*} == "$2" ]]
|
||
}
|
||
|
||
# 32 letters and digits: safe unquoted in .env, a connection string and a URL.
|
||
new_password() {
|
||
local random
|
||
random="$(head -c 64 /dev/urandom | base64 | tr -dc 'A-Za-z0-9')"
|
||
printf '%s' "${random:0:32}"
|
||
}
|
||
|
||
# set_env <key> <value> [file]: the value written to .env (every line for the
|
||
# key replaced, or appended), double-quoted when it has characters a shell
|
||
# would act on, AND exported: the shell already holds what was sourced from
|
||
# .env, and compose prefers the shell over --env-file.
|
||
set_env() {
|
||
local key="$1" value="$2" file="${3:-.env}" line
|
||
if [[ $value =~ ^[A-Za-z0-9_./:@,+=-]*$ ]]; then
|
||
line="$key=$value"
|
||
else
|
||
line="$key=\"$value\""
|
||
fi
|
||
if grep -q "^$key=" "$file"; then
|
||
KEY="$key" LINE="$line" awk 'BEGIN { FS = "=" } $1 == ENVIRON["KEY"] { print ENVIRON["LINE"]; next } { print }' "$file" > "$file.tmp"
|
||
cat "$file.tmp" > "$file"
|
||
rm -f "$file.tmp"
|
||
else
|
||
printf '%s\n' "$line" >> "$file"
|
||
fi
|
||
export "$key=$value"
|
||
}
|
||
|
||
# configure_database <env-is-new 0|1> <backend data folder> <postgres data folder>
|
||
# The database settings in .env (current folder), from what .env says and what
|
||
# is on disk, written and exported: DATABASE_PROVIDER always; for the bundled
|
||
# Postgres its connection string, a generated DATABASE_PASSWORD (never a new
|
||
# one over an existing database) and the "postgres" profile, which is taken
|
||
# out otherwise. Sets DB_PROVIDER, DB_REASON and BUNDLED_POSTGRES.
|
||
configure_database() {
|
||
local new_env="$1" api_data="$2" postgres_data="$3"
|
||
read -r DB_PROVIDER DB_REASON < <(database_provider "$new_env" "${DATABASE_PROVIDER:-}" "$api_data/app.db")
|
||
set_env DATABASE_PROVIDER "$DB_PROVIDER"
|
||
if [[ $DB_PROVIDER == sqlite && "${DATABASE_CONNECTION_STRING:-}" == "$BUNDLED_POSTGRES_CONNECTION" ]]; then
|
||
# The template's Postgres string; SQLite would take it as its own.
|
||
set_env DATABASE_CONNECTION_STRING ""
|
||
fi
|
||
|
||
BUNDLED_POSTGRES=0
|
||
if postgres_bundled "$DB_PROVIDER" "${DATABASE_CONNECTION_STRING:-}"; then
|
||
BUNDLED_POSTGRES=1
|
||
[[ -n "${DATABASE_CONNECTION_STRING:-}" ]] || set_env DATABASE_CONNECTION_STRING "$BUNDLED_POSTGRES_CONNECTION"
|
||
case "$(database_password_action "${DATABASE_PASSWORD:-}" "$postgres_data")" in
|
||
generate)
|
||
log "Generating the database password (DATABASE_PASSWORD in .env)"
|
||
set_env DATABASE_PASSWORD "$(new_password)"
|
||
;;
|
||
lost)
|
||
die "$postgres_data holds a database, but DATABASE_PASSWORD in .env is empty. Put its password back in .env, or move that folder aside to start an empty database."
|
||
;;
|
||
esac
|
||
set_env COMPOSE_PROFILES "$(compose_profiles_with "${COMPOSE_PROFILES:-}" add postgres)"
|
||
else
|
||
set_env COMPOSE_PROFILES "$(compose_profiles_with "${COMPOSE_PROFILES:-}" remove postgres)"
|
||
fi
|
||
|
||
case "$DB_REASON" in
|
||
existing-sqlite)
|
||
warn "Found an existing SQLite database ($api_data/app.db): keeping SQLite (DATABASE_PROVIDER=sqlite written to .env)."
|
||
warn "To move it to the bundled Postgres (README › Moving from SQLite to Postgres): set DATABASE_PROVIDER=postgres in .env,"
|
||
warn "run deploy.sh --no-start, stop the backend, start postgres, run migrate-database in a backend container, run deploy.sh."
|
||
;;
|
||
new-install) log "Database: the bundled Postgres (EXECUTER-DB-00001, internal network only)" ;;
|
||
*)
|
||
if [[ $BUNDLED_POSTGRES -eq 1 ]]; then
|
||
log "Database: the bundled Postgres (EXECUTER-DB-00001)"
|
||
else
|
||
log "Database: $DB_PROVIDER (from .env)"
|
||
fi
|
||
;;
|
||
esac
|
||
}
|
||
|
||
# Sourced by the tests for the functions above only.
|
||
if [[ ${EXECUTER_DEPLOY_FUNCTIONS_ONLY:-0} == 1 ]]; then
|
||
return 0 2>/dev/null || exit 0
|
||
fi
|
||
|
||
usage() {
|
||
# Print the leading comment banner, stopping at the first code line.
|
||
awk 'NR>1 { if (/^#/) { sub(/^# ?/, ""); print } else { exit } }' "$0"
|
||
cat <<'USAGE'
|
||
|
||
Options:
|
||
--with-proxy Also run the in-stack nginx reverse proxy (opt-in)
|
||
--host <hostname> Public hostname for the proxy site config
|
||
--dir <path> Install directory (default /opt/executer)
|
||
--repo-url <url> Raw file base URL used when bootstrapping via curl
|
||
--build Always build from source, never pull
|
||
--no-pull Do not pull; use local images or build
|
||
--no-start Prepare files and images but do not start the stack
|
||
-h, --help Show this help
|
||
USAGE
|
||
}
|
||
|
||
while [[ $# -gt 0 ]]; do
|
||
case "$1" in
|
||
--with-proxy) WITH_PROXY=1; shift ;;
|
||
--host) PUBLIC_HOSTNAME="${2:?--host needs a value}"; shift 2 ;;
|
||
--dir) INSTALL_DIR="${2:?--dir needs a value}"; shift 2 ;;
|
||
--repo-url) REPO_RAW_URL="${2:?--repo-url needs a value}"; shift 2 ;;
|
||
--build) FORCE_BUILD=1; shift ;;
|
||
--no-pull) NO_PULL=1; shift ;;
|
||
--no-start) NO_START=1; shift ;;
|
||
-h|--help) usage; exit 0 ;;
|
||
*) die "unknown option: $1 (try --help)" ;;
|
||
esac
|
||
done
|
||
|
||
# ── Prerequisites ───────────────────────────────────────────────────────────
|
||
command -v docker >/dev/null 2>&1 || die "docker is required"
|
||
docker compose version >/dev/null 2>&1 || die "docker compose v2 is required"
|
||
# depends_on "required: false" (the optional bundled database) needs 2.20.2.
|
||
COMPOSE_VERSION="$(docker compose version --short 2>/dev/null || true)"
|
||
version_at_least "${COMPOSE_VERSION#v}" 2.20.2 \
|
||
|| die "docker compose 2.20.2 or newer is required (found ${COMPOSE_VERSION:-an unknown version})"
|
||
command -v curl >/dev/null 2>&1 || die "curl is required"
|
||
docker info >/dev/null 2>&1 || die "cannot talk to the Docker daemon (need root or docker group?)"
|
||
|
||
# ── Locate or fetch the stack files ─────────────────────────────────────────
|
||
COMPOSE_REL="IaC/compose"
|
||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" 2>/dev/null && pwd || echo '')"
|
||
REPO_ROOT=""
|
||
if [[ -n "$SCRIPT_DIR" && -f "$SCRIPT_DIR/../$COMPOSE_REL/docker-compose.yml" ]]; then
|
||
REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
|
||
fi
|
||
|
||
if [[ -n "$REPO_ROOT" ]]; then
|
||
log "Deploying from checkout: $REPO_ROOT"
|
||
STACK_DIR="$REPO_ROOT"
|
||
else
|
||
log "Bootstrapping into $INSTALL_DIR from $REPO_RAW_URL"
|
||
mkdir -p "$INSTALL_DIR"
|
||
STACK_DIR="$INSTALL_DIR"
|
||
|
||
fetch() {
|
||
local rel="$1" dest="$STACK_DIR/$1"
|
||
mkdir -p "$(dirname "$dest")"
|
||
if ! curl -fsSL "$REPO_RAW_URL/$rel" -o "$dest"; then
|
||
warn "could not fetch $rel"
|
||
return 1
|
||
fi
|
||
}
|
||
|
||
fetch "$COMPOSE_REL/docker-compose.yml" || die "docker-compose.yml is required"
|
||
fetch .env.example || die ".env.example is required"
|
||
fetch src/Infra/nats/nats.conf || die "nats.conf is required"
|
||
[[ $WITH_PROXY -eq 1 ]] && { fetch "$COMPOSE_REL/docker-compose.proxy.yml"; fetch src/Infra/nginx/executer.conf; }
|
||
# Optional: a host port for the bundled Postgres (POSTGRES_BIND), and the
|
||
# development overrides, only needed when building from source.
|
||
fetch "$COMPOSE_REL/docker-compose.postgres-port.yml" || true
|
||
fetch "$COMPOSE_REL/docker-compose.dev.yml" || true
|
||
fi
|
||
|
||
cd "$STACK_DIR"
|
||
|
||
# ── .env ────────────────────────────────────────────────────────────────────
|
||
NEW_ENV=0
|
||
if [[ ! -f .env ]]; then
|
||
log "Creating .env from .env.example"
|
||
cp .env.example .env
|
||
# It holds the NATS signing seed and the database password.
|
||
chmod 600 .env
|
||
# INITIAL_ADMIN_PASSWORD stays empty: the backend generates the one-time
|
||
# admin password at its first start and records it in its data volume
|
||
# (/data/initial-admin-password.txt, mode 600), never in .env or the log.
|
||
NEW_ENV=1
|
||
fi
|
||
|
||
# shellcheck disable=SC1091
|
||
set -a; source .env; set +a
|
||
STACK_NAME="${STACK_NAME:-stk-executer-00001}"
|
||
STACK_BINDMOUNTROOT="${STACK_BINDMOUNTROOT:-/mnt/data/docker/stacks}"
|
||
|
||
# ── Addresses ───────────────────────────────────────────────────────────────
|
||
# A sample address from .env.example would send devices to a host that does
|
||
# not exist (and make the backend ignore the real forwarded host).
|
||
clear_sample_addresses
|
||
|
||
# ── Database ────────────────────────────────────────────────────────────────
|
||
# Postgres (the bundled container) for a new install; an existing SQLite
|
||
# install stays on SQLite until its operator moves it (README, Moving from
|
||
# SQLite to Postgres). Everything decided is written to .env and exported.
|
||
API_DATA="$STACK_BINDMOUNTROOT/$STACK_NAME/API/Data"
|
||
POSTGRES_DATA="$STACK_BINDMOUNTROOT/$STACK_NAME/Postgres/Data"
|
||
configure_database "$NEW_ENV" "$API_DATA" "$POSTGRES_DATA"
|
||
|
||
# App ports default to 0.0.0.0 (an edge proxy on another host can reach them).
|
||
# With the bundled proxy, keep them on loopback unless the user pinned a value:
|
||
# that proxy reaches the services over the compose network, so the published
|
||
# ports need not be public.
|
||
if [[ $WITH_PROXY -eq 1 && -z "${BIND_EXTERNAL_ADDRESS:-}" ]]; then
|
||
export BIND_EXTERNAL_ADDRESS=127.0.0.1
|
||
log "Bundled proxy: binding app ports to 127.0.0.1 (set BIND_EXTERNAL_ADDRESS to override)"
|
||
fi
|
||
|
||
# ── Bind-mount directories ──────────────────────────────────────────────────
|
||
log "Preparing data directories under $STACK_BINDMOUNTROOT/$STACK_NAME"
|
||
mkdir -p \
|
||
"$STACK_BINDMOUNTROOT/$STACK_NAME/NATS/JetStream" \
|
||
"$STACK_BINDMOUNTROOT/$STACK_NAME/NATS/Resolver" \
|
||
"$STACK_BINDMOUNTROOT/$STACK_NAME/API/Data/logs" \
|
||
"$STACK_BINDMOUNTROOT/$STACK_NAME/API/Data/agent"
|
||
[[ $BUNDLED_POSTGRES -eq 1 ]] && mkdir -p "$POSTGRES_DATA"
|
||
[[ $WITH_PROXY -eq 1 ]] && mkdir -p \
|
||
"$STACK_BINDMOUNTROOT/$STACK_NAME/Proxy/Config" \
|
||
"$STACK_BINDMOUNTROOT/$STACK_NAME/Proxy/Certs" \
|
||
"$STACK_BINDMOUNTROOT/$STACK_NAME/Proxy/Logs" \
|
||
"$STACK_BINDMOUNTROOT/$STACK_NAME/Proxy/Acme"
|
||
|
||
# The backend, webui and bundled Postgres run as PUID:PGID (default 1000);
|
||
# give them the data tree so they can write (Postgres insists its data folder
|
||
# is its own). NATS runs as root, so this does not affect it.
|
||
PUID="${PUID:-1000}"; PGID="${PGID:-1000}"
|
||
chown -R "$PUID:$PGID" "$STACK_BINDMOUNTROOT/$STACK_NAME" 2>/dev/null \
|
||
|| warn "could not chown $STACK_BINDMOUNTROOT/$STACK_NAME to $PUID:$PGID (run deploy as root)"
|
||
|
||
# ── Compose file selection ──────────────────────────────────────────────────
|
||
# Compose files live in IaC/compose; their relative build/mount paths are
|
||
# authored against that location, so they run correctly from the stack root.
|
||
COMPOSE_FILES=(-f "$COMPOSE_REL/docker-compose.yml")
|
||
if [[ $WITH_PROXY -eq 1 ]]; then
|
||
[[ -f "$COMPOSE_REL/docker-compose.proxy.yml" ]] || die "--with-proxy needs $COMPOSE_REL/docker-compose.proxy.yml"
|
||
COMPOSE_FILES+=(-f "$COMPOSE_REL/docker-compose.proxy.yml")
|
||
fi
|
||
if [[ $BUNDLED_POSTGRES -eq 1 && -n "${POSTGRES_BIND:-}" ]]; then
|
||
[[ -f "$COMPOSE_REL/docker-compose.postgres-port.yml" ]] || die "POSTGRES_BIND needs $COMPOSE_REL/docker-compose.postgres-port.yml"
|
||
log "Publishing the bundled Postgres on $POSTGRES_BIND:${POSTGRES_PORT:-15432} (POSTGRES_BIND)"
|
||
COMPOSE_FILES+=(-f "$COMPOSE_REL/docker-compose.postgres-port.yml")
|
||
fi
|
||
compose() { docker compose --env-file .env "${COMPOSE_FILES[@]}" "$@"; }
|
||
|
||
# Compose reads .env from the first compose file's folder, not from here: link
|
||
# it there so a plain `docker compose -f IaC/compose/docker-compose.yml up -d`
|
||
# (an upgrade by hand) sees the same settings and keys as this script.
|
||
ln -sfn ../../.env "$COMPOSE_REL/.env"
|
||
|
||
# ── Images: pull published, fall back to building ───────────────────────────
|
||
BUILD_NEEDED=$FORCE_BUILD
|
||
if [[ $FORCE_BUILD -eq 0 && $NO_PULL -eq 0 ]]; then
|
||
# Packages published by a private repo need authentication. For Gitea's
|
||
# built-in registry any account token works as the password.
|
||
if [[ -n "${REGISTRY_USERNAME:-}" && -n "${REGISTRY_TOKEN:-}" && -n "${REGISTRY:-}" ]]; then
|
||
REGISTRY_LOGIN_HOST="${REGISTRY%%/*}"
|
||
log "Logging in to $REGISTRY_LOGIN_HOST as $REGISTRY_USERNAME"
|
||
if ! printf '%s' "$REGISTRY_TOKEN" \
|
||
| docker login "$REGISTRY_LOGIN_HOST" -u "$REGISTRY_USERNAME" --password-stdin >/dev/null 2>&1; then
|
||
warn "registry login failed — continuing (public images may still pull)"
|
||
fi
|
||
fi
|
||
|
||
log "Pulling images from ${REGISTRY:-<registry unset>}"
|
||
if ! compose pull --quiet 2>/dev/null; then
|
||
warn "pull failed or images unavailable — falling back to building from source"
|
||
BUILD_NEEDED=1
|
||
fi
|
||
fi
|
||
|
||
if [[ $BUILD_NEEDED -eq 1 ]]; then
|
||
[[ -f src/Infra/backend/Dockerfile ]] \
|
||
|| die "cannot build: source not present. Clone the repo, or make the registry reachable."
|
||
# The build version (README, Versioning; scripts/Get-Version.ps1): the
|
||
# release tag vX.Y.Z behind HEAD and the commits since it (1.4.0.7, or
|
||
# 1.4.0 on the tagged commit); 0.0.0.<commits> without a release tag;
|
||
# 0.0.0.0 without git. One to three digits of major: calendar-era tags
|
||
# (v2026.10.10.0140) never match.
|
||
if [[ -z "${UNIFIED_VERSION:-}" ]]; then
|
||
describe="$(git describe --tags --long --match 'v[0-9].[0-9]*.[0-9]*' \
|
||
--match 'v[0-9][0-9].[0-9]*.[0-9]*' --match 'v[0-9][0-9][0-9].[0-9]*.[0-9]*' 2>/dev/null || true)"
|
||
if [[ $describe =~ ^v([0-9]{1,3}\.[0-9]+\.[0-9]+)-([0-9]+)-g[0-9a-f]+$ ]]; then
|
||
UNIFIED_VERSION="${BASH_REMATCH[1]}"
|
||
[[ ${BASH_REMATCH[2]} == 0 ]] || UNIFIED_VERSION="$UNIFIED_VERSION.${BASH_REMATCH[2]}"
|
||
elif commits="$(git rev-list --count HEAD 2>/dev/null)"; then
|
||
UNIFIED_VERSION="0.0.0.$commits"
|
||
else
|
||
UNIFIED_VERSION="0.0.0.0"
|
||
fi
|
||
fi
|
||
# Exported: the steps below run the images just built, by this tag.
|
||
export UNIFIED_VERSION
|
||
log "Building images from source ($UNIFIED_VERSION)"
|
||
compose build
|
||
fi
|
||
|
||
# ── NATS operator/account keys (generated by the backend image itself) ──────
|
||
if [[ ! -f secrets/nats/resolver.conf ]]; then
|
||
log "Generating NATS operator/account keys"
|
||
mkdir -p secrets/nats
|
||
BACKEND_IMAGE_REF="$(compose config --images 2>/dev/null | grep -E 'backend|executer-backend' | head -1 || true)"
|
||
[[ -n "$BACKEND_IMAGE_REF" ]] || die "could not resolve the backend image reference"
|
||
|
||
docker run --rm --user "$(id -u):$(id -g)" -v "$PWD/secrets/nats:/out" "$BACKEND_IMAGE_REF" \
|
||
nats-auth init --output /out \
|
||
|| die "NATS key generation failed"
|
||
chmod 600 secrets/nats/*.nk 2>/dev/null || true
|
||
fi
|
||
|
||
# The backend signs every agent and browser credential with these; .env must
|
||
# match the keys NATS trusts (resolver.conf). Written on every run, so a .env
|
||
# that lost them is repaired, and exported, because the shell already holds
|
||
# the values sourced above and compose prefers the shell over .env (set_env).
|
||
for f in account-signing.nk account.pub operator.pub; do
|
||
[[ -s secrets/nats/$f ]] || die "secrets/nats/$f is missing; move secrets/nats/resolver.conf aside to generate new keys"
|
||
done
|
||
set_env NATS_ACCOUNT_SIGNING_SEED "$(tr -d '\r\n' < secrets/nats/account-signing.nk)"
|
||
set_env NATS_ACCOUNT_PUBLIC_KEY "$(tr -d '\r\n' < secrets/nats/account.pub)"
|
||
set_env NATS_OPERATOR_PUBLIC_KEY "$(tr -d '\r\n' < secrets/nats/operator.pub)"
|
||
|
||
# ── Proxy site config ───────────────────────────────────────────────────────
|
||
if [[ $WITH_PROXY -eq 1 ]]; then
|
||
PROXY_CONF="$STACK_BINDMOUNTROOT/$STACK_NAME/Proxy/Config/executer.conf"
|
||
if [[ ! -f "$PROXY_CONF" ]]; then
|
||
[[ -f src/Infra/nginx/executer.conf ]] || die "src/Infra/nginx/executer.conf not found"
|
||
[[ -n "$PUBLIC_HOSTNAME" ]] || die "--host is required with --with-proxy"
|
||
log "Writing proxy site config for $PUBLIC_HOSTNAME"
|
||
|
||
# The HTTP redirect names the HTTPS port only when it isn't the standard 443.
|
||
HTTPS_SUFFIX=""
|
||
[[ "${PROXY_HTTPS_PORT:-443}" == "443" ]] || HTTPS_SUFFIX=":${PROXY_HTTPS_PORT}"
|
||
|
||
# In-stack proxy reaches services by compose service name, not host IP.
|
||
sed -e "s|control\.example\.com|$PUBLIC_HOSTNAME|g" \
|
||
-e "s|return 301 https://\$server_name\$request_uri;|return 301 https://\$server_name$HTTPS_SUFFIX\$request_uri;|" \
|
||
-e "s|server 10\.0\.0\.10:13080;|server webui:3000;|" \
|
||
-e "s|server 10\.0\.0\.10:19080;|server backend:8080;|" \
|
||
-e "s|server 10\.0\.0\.10:19222;|server nats:9222;|" \
|
||
-e "s|/etc/nginx/ssl/$PUBLIC_HOSTNAME/fullchain.cer|/etc/nginx/ssl/fullchain.pem|" \
|
||
-e "s|/etc/nginx/ssl/$PUBLIC_HOSTNAME/private.key|/etc/nginx/ssl/private.key|" \
|
||
src/Infra/nginx/executer.conf > "$PROXY_CONF"
|
||
|
||
warn "Place TLS files at $STACK_BINDMOUNTROOT/$STACK_NAME/Proxy/Certs/{fullchain.pem,private.key}"
|
||
fi
|
||
fi
|
||
|
||
# ── Start ───────────────────────────────────────────────────────────────────
|
||
if [[ $NO_START -eq 1 ]]; then
|
||
log "Prepared (not started, --no-start)."
|
||
exit 0
|
||
fi
|
||
|
||
log "Starting the stack"
|
||
compose up -d --remove-orphans
|
||
|
||
# NATS reads nats.conf only at start (max_payload and the rest): restart it
|
||
# when the file changed since the last deploy. Devices reconnect on their own.
|
||
NATS_CONF_STAMP="$STACK_BINDMOUNTROOT/$STACK_NAME/NATS/nats.conf.applied"
|
||
if config_changed src/Infra/nats/nats.conf "$NATS_CONF_STAMP"; then
|
||
log "nats.conf changed; restarting NATS to apply it"
|
||
compose restart nats && config_applied src/Infra/nats/nats.conf "$NATS_CONF_STAMP" \
|
||
|| warn "could not restart NATS; run: docker compose restart nats"
|
||
fi
|
||
|
||
log "Waiting for the backend to report ready (database, signing key, NATS)"
|
||
READY=0
|
||
for _ in $(seq 1 60); do
|
||
if curl -fsS "http://127.0.0.1:${BACKEND_PORT:-19080}/api/ready" >/dev/null 2>&1; then
|
||
READY=1
|
||
break
|
||
fi
|
||
sleep 2
|
||
done
|
||
if [[ $READY -ne 1 ]]; then
|
||
warn "The backend is not ready; devices and the console stay offline until it is:"
|
||
curl -sS "http://127.0.0.1:${BACKEND_PORT:-19080}/api/ready" 2>&1 || true
|
||
echo
|
||
fi
|
||
|
||
echo
|
||
compose ps
|
||
echo
|
||
log "ExecuteR is up."
|
||
echo " WebUI: http://<host>:${WEBUI_PORT:-13080}"
|
||
echo " API: http://<host>:${BACKEND_PORT:-19080}/api/health"
|
||
echo " NATS WS: ws://<host>:${NATS_WS_PORT:-19222}"
|
||
if [[ $WITH_PROXY -eq 1 ]]; then
|
||
if [[ "${PROXY_HTTPS_PORT:-443}" == "443" ]]; then
|
||
echo " Proxy: https://${PUBLIC_HOSTNAME}"
|
||
else
|
||
echo " Proxy: https://${PUBLIC_HOSTNAME}:${PROXY_HTTPS_PORT}"
|
||
fi
|
||
fi
|
||
if [[ $BUNDLED_POSTGRES -eq 1 ]]; then
|
||
echo " Database: Postgres, EXECUTER-DB-00001 (internal network${POSTGRES_BIND:+; host $POSTGRES_BIND:${POSTGRES_PORT:-15432}})"
|
||
elif [[ $DB_PROVIDER == sqlite ]]; then
|
||
echo " Database: SQLite, /data/app.db (moving to Postgres: README › Moving from SQLite to Postgres)"
|
||
else
|
||
echo " Database: $DB_PROVIDER, DATABASE_CONNECTION_STRING in .env"
|
||
fi
|
||
if [[ -z "${INITIAL_ADMIN_PASSWORD:-}" ]]; then
|
||
# Only the command is printed: the password stays in the data volume
|
||
# (mode 600), out of this terminal's scrollback and any CI log.
|
||
echo
|
||
echo " Initial admin: ${INITIAL_ADMIN_USERNAME:-admin}, with the one-time password the first start recorded:"
|
||
echo " docker exec EXECUTER-API-00001 cat /data/initial-admin-password.txt"
|
||
echo " A new password is required at the first sign-in. Locked out later:"
|
||
echo " docker exec EXECUTER-API-00001 dotnet Backend.dll reset-admin"
|
||
elif [[ $NEW_ENV -eq 1 ]]; then
|
||
echo
|
||
echo " Initial admin: ${INITIAL_ADMIN_USERNAME:-admin} with INITIAL_ADMIN_PASSWORD from .env"
|
||
fi
|