#!/usr/bin/env bash # Shared helpers for the Hetzner self-host tooling. Source, don't execute. set -euo pipefail HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" . "$HERE/_box-env.sh" # SSOT: HETZNER_IP, BOX_ROOT, BOX_UBUNTU BOX="$BOX_UBUNTU" # Overridable so a caller can judge a PRISTINE register instead of the working # tree. On the workstation ~15 agent sessions share these checkouts, so the # tree is a scratchpad: on 2026-08-28 the daily register check read apps.conf # mid-edit (substrata listed twice) and paged about a duplicate port that had # never been committed. CI still judges the working tree, which is correct # there — in CI the working tree IS the commit under review. MANIFEST="${MANIFEST:-$HERE/apps.conf}" # Accounts databases beside a supabase schema (accounts-db.conf): app|database. ACCOUNTS_DB_REGISTER="${ACCOUNTS_DB_REGISTER:-$HERE/accounts-db.conf}" # accounts_db_of — the host accounts database registered for , or # nothing. Never fails: an app with no row has none. accounts_db_of() { [ -f "$ACCOUNTS_DB_REGISTER" ] || return 0 grep -v '^#' "$ACCOUNTS_DB_REGISTER" | awk -F'|' -v a="$1" '$1 == a { print $2; exit }' } # default_branch [repo_dir] — the remote's default branch name, resolved not guessed. # # 28 repos here use `main`, 3 use `master` (aoz-begleitung, dotfiles, # sbb-lost-found), and 2 have no origin/HEAD set at all. Anything that hardcodes # "origin/main" silently does the wrong thing on a fifth of the fleet — a gate # that cannot find its base branch either blocks everything or checks nothing. # # Order: the remote's own answer, then whichever of main/master exists, then # fail. Never a hardcoded guess. default_branch() { local repo="${1:-.}" b b=$(git -C "$repo" symbolic-ref --short refs/remotes/origin/HEAD 2>/dev/null) && { echo "${b#origin/}"; return 0; } for b in main master; do git -C "$repo" rev-parse --verify --quiet "refs/remotes/origin/$b" >/dev/null 2>&1 && { echo "$b"; return 0; } done # origin/HEAD unset and neither name present. Fix with: # git remote set-head origin -a return 1 } # app_lookup — sets NAME PORT DOMAINS REPO APP_DIR DB or exits 1 app_lookup() { local line line=$(grep -v '^#' "$MANIFEST" | grep "^$1|" || true) [ -z "$line" ] && { echo "ERROR: '$1' not in $MANIFEST" >&2; return 1; } # Extra fields must be named, or bash's last variable swallows the remainder # and DB silently becomes "db|owner|kind|...". Missing trailing fields read as # empty, so a 6-field line still parses exactly as before. IFS='|' read -r NAME PORT DOMAINS REPO APP_DIR DB OWNER KIND STATUS PLAN PRICE SINCE <<<"$line" } # Every site that should exist on the box. An `archived` row is a site retired # offline (retire-site.sh --mode offline): kept in the register so `--mode # restore` can bring it back, never regenerated by a full sync — or the next # `sync-sites` would quietly put center-com back online (2026-10-19). app_names() { grep -v '^#' "$MANIFEST" | awk -F'|' 'NF && $9 != "archived" { print $1 }'; } # next_free_port — the first port at # or above that nothing holds. # # The register is NOT the only thing listening on this box, and it never claimed # to be: the four handcrafted services on 4001-4004 are excluded by design, and # annushka's enquiry API sits on 4030 behind a hand-written vhost precisely so # sync-infra will not touch it. Allocating from `max(registered)+1` alone was # therefore walking toward 4030 — four sites away as of 2026-09-10, and three # rows were added that day. A port collision does not fail loudly at allocation; # it fails when the new unit starts, binds nothing, and the OTHER app is the one # that looks broken. # # Pure so it can be tested without the box: the caller supplies what is taken. next_free_port() { local p="$1" taken="${2:-}" while grep -qx "$p" <<<"$taken"; do p=$((p + 1)) done printf '%s' "$p" } # listening_ports — every TCP port with a listener on the box, one per line. # Empty output means "could not ask", which the caller MUST announce rather than # treat as "nothing is listening"; those two look identical and only one is safe. listening_ports() { box "ss -ltnH 2>/dev/null | awk '{print \$4}'" 2>/dev/null \ | sed 's/.*://' | grep -E '^[0-9]+$' | sort -un } # -n: don't consume stdin (box() is used inside while-read loops) # -i: the deploy key when it is readable. On the box itself these scripts run # as ubuntu from inside loki-app (register-cd), and ubuntu's only private # key is the CI deploy key — whose public half is what authorizes CI. Without # naming it, ssh offers nothing and the box refuses itself ("Permission denied # (publickey)"), which is how sync-infra failed on velokiosk-sep10. # render_launch — an app's launch.sh, from the one template. sync-infra.sh # writes it when an app is set up; deploy.sh writes it again on every deploy, so # a change to the template reaches each app with its next release. render_launch() { sed "s|__PORT__|$1|g" "$HERE/launch.sh.tmpl"; } box() { if [ -r "${DEPLOY_KEY_PATH:-}" ]; then ssh -n -o BatchMode=yes -i "$DEPLOY_KEY_PATH" "$BOX" "$@" else ssh -n -o BatchMode=yes "$BOX" "$@" fi } # ------------------------------------------------------------ custom domains # # A site starts on .$SITES_BASE_DOMAIN and may later be given its own # domain (evig.orangecat.ch -> evig.ch). Both live in the row's `domains` # field — the one register — and this is the one rule for what that means: # # * no host outside the base domain -> every host is served (unchanged) # * one or more hosts outside it -> the FIRST of them is canonical; the # other own-domain hosts (www.) are served too, and every base-domain host # answers with a 308 to the canonical one # # 308, never 301: a 301 lets the client turn a POST into a bodyless GET, which # is how evig's credentials callback broke behind the revampit host # (2026-08-16). The base-domain host keeps working as an address, so links # printed before the move still land — on the site's own name. # # Every row in the register today is in the first case or is own-domain only # (sink), so this rule changes no existing vhost; test-custom-domain.sh pins # that against the real register. # is_base_host — true for the base domain and anything under it. is_base_host() { [ "$1" = "$2" ] || [[ "$1" == *".$2" ]]; } # canonical_host — the address a row is MEANT to be reached # at: its first own-domain host, else its first host. canonical_host() { local h first="" hosts IFS=',' read -ra hosts <<<"$1" for h in "${hosts[@]}"; do [ -n "$h" ] || continue [ -n "$first" ] || first="$h" is_base_host "$h" "$2" || { printf '%s' "$h"; return 0; } done printf '%s' "$first" } # caddy_vhost — the apps.d file for one row. # Pure (prints, touches nothing) so the redirect rule above is testable # without a box. sync-infra.sh is its only writer. caddy_vhost() { local name="$1" port="$2" domains="$3" base="$4" h canonical serve="" redirect="" hosts canonical=$(canonical_host "$domains" "$base") IFS=',' read -ra hosts <<<"$domains" for h in "${hosts[@]}"; do [ -n "$h" ] || continue if [ "$canonical" != "$h" ] && ! is_base_host "$canonical" "$base" && is_base_host "$h" "$base"; then redirect="${redirect:+$redirect, }$h" else serve="${serve:+$serve, }$h" fi done cat < — the apps.d file that makes a prospect # preview the whole of its own address: