Files
crosscode-platform/scripts/lib/common.sh
T
joshuaandClaude Opus 5.5 570f2dda5a PM2 runs under its own systemd unit, never as a child of the agent
Found in the VM test: a site's PM2 daemon started from the agent's install
job belonged to the agent's service, so stopping or updating the agent took
down every site it had installed. Before any PM2 command as a site user,
the scripts now make sure that user's daemon runs under pm2-<user> (moving
one that runs elsewhere, keeping its process list). The agent's stats no
longer start an empty daemon by asking one that isn't running. Uninstall
stops the unit before removing it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 16:40:12 +02:00

358 lines
15 KiB
Bash

# shellcheck shell=bash
# Shared helpers for the crosscode script: logging, users, church.env, tags, versions, health.
# Only functions and defaults: sourcing this file changes nothing (the bats tests rely on that).
CROSSCODE_SITES="${CROSSCODE_SITES:-/var/www/sites}"
CROSSCODE_REPO="${CROSSCODE_REPO:-https://c-git.crosscode.co.za/joshua/crosscode-platform.git}"
# Every deploy runs the products in this order (ROADMAP §8): Accounts first, since others sign in
# through it.
PRODUCT_ORDER=(accounts events website app)
# Products this version of the script can deploy (a products/<name>.sh exists for each).
SUPPORTED_PRODUCTS=(events)
# Releases older than this get a warning; older than CROSSCODE_NODE_MIN the deploy is refused.
CROSSCODE_NODE_MIN=20
CROSSCODE_NODE_WARN=22
# ── Logging ──────────────────────────────────────────────────────────────────────────────────
# shellcheck source=report.sh
source "$(dirname "${BASH_SOURCE[0]}")/report.sh"
step() { printf '==> %s\n' "$*"; report_line step "$(now_iso)" "$*"; }
info() { printf ' %s\n' "$*"; }
warn() { printf '!!> %s\n' "$*" >&2; }
die() { printf '!!> %s\n' "$*" >&2; report_line message "$*"; exit 1; }
# ── Small checks ─────────────────────────────────────────────────────────────────────────────
require_root() {
# The bats tests run deploys against stub products as an ordinary user.
[ "${CROSSCODE_TEST:-}" = 1 ] && return 0
[ "$(id -u)" = 0 ] || die "Run this as root (sudo crosscode …): it reads church.env (root only) and runs each app as its own user."
}
# A church's short name: lower case letters, digits and -, starting with a letter.
valid_slug() { [[ "$1" =~ ^[a-z][a-z0-9-]{0,39}$ ]]; }
is_known_product() {
local p
for p in "${PRODUCT_ORDER[@]}"; do [ "$p" = "$1" ] && return 0; done
return 1
}
is_supported_product() {
local p
for p in "${SUPPORTED_PRODUCTS[@]}"; do [ "$p" = "$1" ] && return 0; done
return 1
}
# A product's place in the deploy order (0 for accounts).
product_index() {
local i
for i in "${!PRODUCT_ORDER[@]}"; do [ "${PRODUCT_ORDER[$i]}" = "$1" ] && { echo "$i"; return 0; }; done
echo 99
}
# The products given (any order, repeats allowed), printed in deploy order, one per line.
in_product_order() {
local p q
for p in "${PRODUCT_ORDER[@]}"; do
for q in "$@"; do
if [ "$p" = "$q" ]; then echo "$p"; break; fi
done
done
}
# Upper case for church.env keys: events → EVENTS.
upper() { printf '%s' "$1" | tr '[:lower:]-' '[:upper:]_'; }
# ── Running things as the app user ───────────────────────────────────────────────────────────
# run_as <user> <dir> <command…>: runs the command in <dir> as <user> (directly when we already
# are that user), with that user's HOME so PM2 and npm use their own folders.
# pm2_service: makes sure the pm2 daemon of user $1 runs under its own systemd unit (pm2-<user>),
# never as a child of whatever ran this script. A daemon started from a command (the agent's job,
# an SSH session) belongs to that command's service and dies with it: stopping or updating the
# agent took down every site it had installed (found in the VM test). Done once per user per run.
PM2_SERVICE_DONE=" "
pm2_service() {
local user="$1" home pid n
case "$PM2_SERVICE_DONE" in *" $user "*) return 0 ;; esac
PM2_SERVICE_DONE="$PM2_SERVICE_DONE$user "
{ command -v systemctl >/dev/null && [ -d /run/systemd/system ] && [ "$(id -u)" = 0 ]; } || return 0
home="$(getent passwd "$user" | cut -d: -f6)"
systemctl cat "pm2-$user" >/dev/null 2>&1 || env PATH="$PATH:/usr/bin" pm2 startup systemd -u "$user" --hp "$home" >/dev/null
systemctl is-active -q "pm2-$user" && return 0
pid="$(cat "$home/.pm2/pm2.pid" 2>/dev/null || true)"
if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then
# A daemon outside its unit: keep its list (an empty one doesn't replace the saved list),
# stop it, and let the unit bring the list back. Its sites are down for a few seconds.
n="$(sudo -u "$user" -H -- pm2 jlist 2>/dev/null | node -e 'let s = ""; process.stdin.on("data", (d) => (s += d)).on("end", () => { try { console.log(JSON.parse(s).length); } catch { console.log(0); } })')"
if [ "${n:-0}" -gt 0 ]; then sudo -u "$user" -H -- pm2 save --force >/dev/null 2>&1 || true; fi
sudo -u "$user" -H -- pm2 kill >/dev/null 2>&1 || true
info "PM2 for $user now runs under systemd (pm2-$user)."
fi
systemctl start "pm2-$user" || warn "pm2-$user didn't start: check systemctl status pm2-$user."
}
run_as() {
local user="$1" dir="$2"
# Any PM2 command runs against the daemon in its own unit (see pm2_service).
if [ "$3" = pm2 ]; then pm2_service "$user"; fi
shift 2
if [ "$(id -un)" = "$user" ]; then
(cd -- "$dir" && "$@")
else
sudo -u "$user" -H -- bash -c 'cd -- "$1" && shift && exec "$@"' crosscode-run "$dir" "$@"
fi
}
# ── church.env ───────────────────────────────────────────────────────────────────────────────
# /var/www/sites/<church>/church.env holds names, folders, domains and ports (never secrets). It is
# read as data, never sourced: only KEY=VALUE lines with known keys are taken, into CENV.
declare -gA CENV=()
church_dir() { printf '%s/%s' "$CROSSCODE_SITES" "$1"; }
church_env_path() { printf '%s/%s/church.env' "$CROSSCODE_SITES" "$1"; }
church_env_key_ok() {
[[ "$1" =~ ^(CHURCH|PRODUCTS|ADDRESS)$ ]] || [[ "$1" =~ ^(ACCOUNTS|EVENTS|WEBSITE|APP)_[A-Z0-9_]+$ ]]
}
# church_env_read <file>: fills CENV. Values may be bare or in double or single quotes; a
# "# comment" after the value is ignored. Unknown keys and other lines are skipped.
church_env_read() {
local file="$1" line key value
CENV=()
[ -f "$file" ] || return 1
while IFS= read -r line || [ -n "$line" ]; do
line="${line%$'\r'}"
[[ "$line" =~ ^[[:space:]]*([A-Z][A-Z0-9_]*)=(.*)$ ]] || continue
key="${BASH_REMATCH[1]}"
value="${BASH_REMATCH[2]}"
church_env_key_ok "$key" || continue
if [[ "$value" =~ ^\"([^\"]*)\" ]] || [[ "$value" =~ ^\'([^\']*)\' ]]; then
value="${BASH_REMATCH[1]}"
else
value="${value%%#*}"
value="${value%"${value##*[![:space:]]}"}"
fi
CENV[$key]="$value"
done < "$file"
}
# cenv <KEY> [default]: a value from CENV.
cenv() { printf '%s' "${CENV[$1]:-${2:-}}"; }
# pvar <product> <NAME> [default]: a product's value, e.g. pvar events DIR → CENV[EVENTS_DIR].
pvar() { cenv "$(upper "$1")_$2" "${3:-}"; }
church_env_set() {
church_env_key_ok "$1" || die "church.env: unknown key $1"
[[ "$2" != *[\"\'\$\`\\]* && "$2" != *$'\n'* ]] || die "church.env: $1 can't contain quotes, \$, \` , \\ or newlines"
CENV[$1]="$2"
}
church_env_unset() { unset 'CENV[$1]'; }
# church_env_write <file>: writes CENV in a fixed order (CHURCH, PRODUCTS, ADDRESS, then each
# product's keys in deploy order), atomically, mode 600.
church_env_write() {
local file="$1" tmp key p prefix
local -a keys=()
for key in CHURCH PRODUCTS ADDRESS; do [ -n "${CENV[$key]+x}" ] && keys+=("$key"); done
for p in "${PRODUCT_ORDER[@]}"; do
prefix="$(upper "$p")_"
while IFS= read -r key; do [ -n "$key" ] && keys+=("$key"); done < <(
for key in "${!CENV[@]}"; do [[ "$key" == "$prefix"* ]] && echo "$key"; done | LC_ALL=C sort
)
done
tmp="$(mktemp "$(dirname "$file")/.church.env.XXXXXX")"
{
echo "# Written by crosscode. Names, folders, domains and ports only: secrets stay in each product's .env."
for key in "${keys[@]}"; do
if [[ "${CENV[$key]}" =~ [[:space:]#] || -z "${CENV[$key]}" ]]; then
printf '%s="%s"\n' "$key" "${CENV[$key]}"
else
printf '%s=%s\n' "$key" "${CENV[$key]}"
fi
done
} > "$tmp"
chmod 600 "$tmp"
mv -f "$tmp" "$file"
}
# Church names on this server: every folder in $CROSSCODE_SITES with a church.env.
list_churches() {
local f
for f in "$CROSSCODE_SITES"/*/church.env; do
[ -f "$f" ] && basename "$(dirname "$f")"
done
return 0
}
# Loads a church's church.env into CENV, or stops.
load_church() {
valid_slug "$1" || die "\"$1\" isn't a church name (a-z, 0-9 and -)."
church_env_read "$(church_env_path "$1")" || die "No church \"$1\" on this server ($(church_env_path "$1") is missing)."
}
church_has_product() {
local p
for p in $(cenv PRODUCTS); do [ "$p" = "$1" ] && return 0; done
return 1
}
# ── Versions and tags ────────────────────────────────────────────────────────────────────────
# version_ge <a> <b>: a >= b, for x.y.z with an optional -prerelease (1.2.0-rc1 < 1.2.0).
version_ge() {
local a="$1" b="$2" an bn ap bp i x y
an="${a%%-*}"; bn="${b%%-*}"
ap=""; bp=""
[[ "$a" == *-* ]] && ap="${a#*-}"
[[ "$b" == *-* ]] && bp="${b#*-}"
local -a A B
IFS=. read -r -a A <<< "$an"
IFS=. read -r -a B <<< "$bn"
for i in 0 1 2; do
x="${A[$i]:-0}"; y="${B[$i]:-0}"
[[ "$x" =~ ^[0-9]+$ && "$y" =~ ^[0-9]+$ ]] || return 2
if ((10#$x > 10#$y)); then return 0; fi
if ((10#$x < 10#$y)); then return 1; fi
done
# Same x.y.z: a release beats its prereleases; prereleases compare as text.
if [ -z "$ap" ]; then return 0; fi
if [ -z "$bp" ]; then return 1; fi
[[ "$ap" > "$bp" || "$ap" == "$bp" ]]
}
# tag_version <product@x.y.z> → x.y.z
tag_version() { printf '%s' "${1#*@}"; }
# latest_tag <product>, reading tag names on stdin: the newest released product@x.y.z
# (prerelease and test tags such as events@2.2.0-test are never "latest").
latest_tag_from() {
local p="$1" t best=""
while IFS= read -r t; do
[[ "$t" =~ ^${p}@[0-9]+\.[0-9]+\.[0-9]+$ ]] || continue
if [ -z "$best" ] || version_ge "$(tag_version "$t")" "$(tag_version "$best")"; then best="$t"; fi
done
printf '%s' "$best"
}
# git_in <checkout> <args…>: git for reading a checkout owned by the app user (as root, git would
# otherwise refuse it as "dubious ownership"). Anything that writes runs as the app user instead.
git_in() {
local dir="$1"
shift
git -c safe.directory="$dir" -C "$dir" "$@"
}
# latest_tag <checkout> <product>: from the checkout's tags (fetch first).
latest_tag() { git_in "$1" tag -l "$2@*" | latest_tag_from "$2"; }
# checkout_ref <checkout> <product>: the product tag the checkout is on, or the short commit.
checkout_ref() {
git_in "$1" describe --tags --exact-match --match "$2@*" 2>/dev/null || git_in "$1" rev-parse --short HEAD
}
# whatsnew_requires <version>, reading a WHATS-NEW.md on stdin: the requirements in that
# version's heading, one "product x.y.z" per line. Same grammar as the Hub's release sync
# (events-hub releaseSync.js): "## 3.0.0 — requires accounts 1.2.0, website 1.1.0 — urgent".
# HTML comments are skipped (the file's own instructions contain example headings).
whatsnew_requires() {
local version="$1"
awk -v want="$version" '
BEGIN { products = "accounts|events|website|app" }
{
line = $0
sub(/\r$/, "", line)
# Drop <!-- … --> comments, which can span lines.
out = ""
while (line != "") {
if (incomment) {
e = index(line, "-->")
if (e == 0) { line = ""; break }
line = substr(line, e + 3); incomment = 0
} else {
s = index(line, "<!--")
if (s == 0) { out = out line; line = ""; break }
out = out substr(line, 1, s - 1); line = substr(line, s + 4); incomment = 1
}
}
if (out !~ /^##[ \t]+/) next
h = out; sub(/^##[ \t]+/, "", h)
if (!match(h, /v?[0-9]+\.[0-9]+\.[0-9]+/)) next
v = substr(h, RSTART, RLENGTH); sub(/^v/, "", v)
if (v != want) next
rest = tolower(substr(h, RSTART + RLENGTH))
if (!match(rest, /(^|[^a-z])requires?([^a-z]|$)/)) exit
rest = substr(rest, RSTART)
while (match(rest, "(" products ")[ \t]+v?[0-9]+\\.[0-9]+\\.[0-9]+")) {
m = substr(rest, RSTART, RLENGTH)
before = RSTART > 1 ? substr(rest, RSTART - 1, 1) : " "
rest = substr(rest, RSTART + RLENGTH)
if (before ~ /[a-z0-9]/) continue
split(m, parts, /[ \t]+/)
p = parts[1]; ver = parts[2]; sub(/^v/, "", ver)
if (!(p in seen)) { seen[p] = 1; print p " " ver }
}
exit
}
'
}
# ── .env files and URLs ──────────────────────────────────────────────────────────────────────
# env_value <file> <KEY>: a value from a .env file (quotes removed), "" when absent.
env_value() {
local line
line="$(grep -E "^[[:space:]]*(export[[:space:]]+)?$2=" "$1" 2>/dev/null | tail -n 1)" || true
line="${line#*=}"
line="${line%$'\r'}"
if [[ "$line" =~ ^\"(.*)\"$ ]] || [[ "$line" =~ ^\'(.*)\'$ ]]; then line="${BASH_REMATCH[1]}"; fi
printf '%s' "$line"
}
# db_name_from_url <postgresql://user:pass@host:5432/name?x=y> → name
db_name_from_url() {
local u="${1%%\?*}"
printf '%s' "${u##*/}"
}
# host_from_url <https://events.church.org/x> → events.church.org (the first, if a list)
host_from_url() {
local u="${1%%,*}"
u="${u#*://}"
u="${u%%/*}"
printf '%s' "${u%%:*}"
}
# ── Health ───────────────────────────────────────────────────────────────────────────────────
# json_get <path>, reading JSON on stdin: a value ("a.b"), objects as compact JSON, "" if absent.
json_get() {
node -e '
let s = "";
process.stdin.on("data", d => (s += d)).on("end", () => {
try {
const v = process.argv[1].split(".").reduce((o, k) => (o == null ? o : o[k]), JSON.parse(s));
process.stdout.write(v == null ? "" : typeof v === "object" ? JSON.stringify(v) : String(v));
} catch { process.exit(1); }
});' "$1"
}
health_json() { curl -fsS --max-time 5 "$1" 2>/dev/null; }
# wait_for_version <health url> <version> [seconds]: until /health answers with that version.
wait_for_version() {
local url="$1" want="$2" limit="${3:-60}" waited=0 got=""
while [ "$waited" -lt "$limit" ]; do
got="$(health_json "$url" | json_get version 2>/dev/null || true)"
[ "$got" = "$want" ] && return 0
sleep 2
waited=$((waited + 2))
done
warn "/health ($url) didn't report $want within ${limit}s (last answer: ${got:-none})."
return 1
}
node_major() { node -p 'process.versions.node.split(".")[0]' 2>/dev/null || echo 0; }
# Files changed or added by hand in a checkout (ignored files such as .env, uploads, backups and
# the install's ecosystem.config.js never count).
local_changes() { git_in "$1" status --porcelain --untracked-files=all; }