From e9e9b5a97ff86c81a3cf09f66aa0197465025b5f Mon Sep 17 00:00:00 2001 From: Joshua Oosthuizen Date: Tue, 17 Nov 2026 12:50:19 +0200 Subject: [PATCH] crosscode: --json reports, --config, env templates, frpc tunnels, restart/export/uninstall/purge - Every command takes --json (crosscode.script-report/1 on stdout, the log on stderr) and --config (the .env values by layer, from the agent). - .env files come from the release's env.template.json (lib/envtpl.js): written whole on install; on deploy only new variables are appended, and a new required one with no value stops the deploy at the checks. - install --frp: Hub-given ports, a marked frpc.ini block (backup, frpc verify, reload; [common] admin lines added once), no nginx. A database on another server via DB_ADMIN_URL. Refuses an existing database or role. Everything the run made is removed if it fails. - restart, export (crosscode.church-export/1 ZIP), uninstall (final backup, uploads and .env kept 30 days) and purge. Co-Authored-By: Claude Opus 5.5 --- package.json | 2 +- scripts/crosscode | 63 ++++++- scripts/lib/common.sh | 6 +- scripts/lib/deploy.sh | 39 +++- scripts/lib/env.sh | 108 +++++++++++ scripts/lib/envtpl.js | 164 ++++++++++++++++ scripts/lib/frpc.sh | 127 +++++++++++++ scripts/lib/install.sh | 218 ++++++++++++++-------- scripts/lib/manage.sh | 204 ++++++++++++++++++++ scripts/lib/report.sh | 78 ++++++++ scripts/lib/status.sh | 12 ++ scripts/products/events.env.template.json | 28 +++ scripts/products/events.sh | 37 ++++ scripts/tests/deploy.bats | 38 ++++ scripts/tests/envtpl.bats | 98 ++++++++++ scripts/tests/frpc.bats | 105 +++++++++++ scripts/tests/json.bats | 69 +++++++ 17 files changed, 1308 insertions(+), 88 deletions(-) create mode 100644 scripts/lib/env.sh create mode 100644 scripts/lib/envtpl.js create mode 100644 scripts/lib/frpc.sh create mode 100644 scripts/lib/manage.sh create mode 100644 scripts/lib/report.sh create mode 100644 scripts/products/events.env.template.json create mode 100644 scripts/tests/envtpl.bats create mode 100644 scripts/tests/frpc.bats create mode 100644 scripts/tests/json.bats diff --git a/package.json b/package.json index 1ed4b64..58e38a6 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "test:scripts": "bats scripts/tests", "typecheck": "npm run typecheck --workspaces --if-present", "lint": "eslint .", - "lint:scripts": "shellcheck -x scripts/crosscode scripts/products/events.sh", + "lint:scripts": "shellcheck -x scripts/crosscode scripts/products/events.sh scripts/lib/report.sh scripts/lib/env.sh scripts/lib/frpc.sh scripts/lib/manage.sh", "check": "npm run lint && npm run lint:scripts && npm run typecheck && npm test && npm run test:scripts && npm run build" }, "engines": { diff --git a/scripts/crosscode b/scripts/crosscode index d0aedc6..72692cb 100755 --- a/scripts/crosscode +++ b/scripts/crosscode @@ -24,13 +24,30 @@ if [ -z "${CROSSCODE_HOME:-}" ]; then CROSSCODE_HOME="$copy" CROSSCODE_FROM="$root" exec bash "$copy/crosscode" "$@" fi -# Running from the copy: remove it afterwards (only ever a mktemp folder, never a checkout). -if [[ "$CROSSCODE_HOME" == /tmp/crosscode.* ]]; then - trap 'rm -rf "$CROSSCODE_HOME"' EXIT -fi +# Options for every command, taken out of the arguments here: --json (a crosscode.script-report/1 +# on stdout at the end, the log on stderr) and --config (the .env values by layer, from +# the agent). +CROSSCODE_JSON=0 +CROSSCODE_CONFIG="${CROSSCODE_CONFIG:-}" +args=() +while [ $# -gt 0 ]; do + case "$1" in + --json) CROSSCODE_JSON=1; shift ;; + --config) + [ $# -ge 2 ] || { echo '!!> --config needs a file.' >&2; exit 1; } + CROSSCODE_CONFIG="$2"; shift 2 ;; + *) args+=("$1"); shift ;; + esac +done +set -- "${args[@]+"${args[@]}"}" +export CROSSCODE_CONFIG # shellcheck source=lib/common.sh source "$CROSSCODE_HOME/lib/common.sh" +# shellcheck source=lib/env.sh +source "$CROSSCODE_HOME/lib/env.sh" +# shellcheck source=lib/frpc.sh +source "$CROSSCODE_HOME/lib/frpc.sh" # shellcheck source=lib/deploy.sh source "$CROSSCODE_HOME/lib/deploy.sh" # shellcheck source=lib/status.sh @@ -39,6 +56,32 @@ source "$CROSSCODE_HOME/lib/status.sh" source "$CROSSCODE_HOME/lib/install.sh" # shellcheck source=lib/adopt.sh source "$CROSSCODE_HOME/lib/adopt.sh" +# shellcheck source=lib/manage.sh +source "$CROSSCODE_HOME/lib/manage.sh" + +# At the end: the report (with --json), and the copy this runs from is removed (only ever a +# mktemp folder, never a checkout). +on_exit() { + local rc=$? + [ "$CROSSCODE_JSON" != 1 ] || report_emit "$rc" + if [[ "$CROSSCODE_HOME" == /tmp/crosscode.* ]]; then rm -rf "$CROSSCODE_HOME"; fi + return "$rc" +} +trap on_exit EXIT + +command="${1:-help}" +[ $# -gt 0 ] && shift +if [ "$CROSSCODE_JSON" = 1 ]; then + # The report goes to the real stdout (fd 3); everything else to stderr. + exec 3>&1 1>&2 + report_init "$command" +fi + +if [ -n "$CROSSCODE_CONFIG" ]; then + [ -f "$CROSSCODE_CONFIG" ] || die "--config: there's no file $CROSSCODE_CONFIG." + node -e 'JSON.parse(require("fs").readFileSync(process.argv[1], "utf8"))' "$CROSSCODE_CONFIG" 2>/dev/null || + die "--config: $CROSSCODE_CONFIG isn't JSON." +fi usage() { cat < --products events --domain
a new church crosscode adopt --events-dir move an existing Cross Events install here crosscode adopt --undo go back to the old folder (within 14 days) + crosscode restart [events] reload the PM2 processes + crosscode export --out the church's data and uploads + crosscode uninstall --confirm remove it (a final backup is kept 30 days) + crosscode purge --confirm delete what uninstall kept crosscode help [] this, or a command's options +Every command takes --json (a report at the end) and --config (settings from the Hub). Churches live in $CROSSCODE_SITES// (church.env, one checkout per product). EOF } -command="${1:-help}" -[ $# -gt 0 ] && shift case "$command" in deploy) cmd_deploy "$@" ;; status) cmd_status "$@" ;; install) cmd_install "$@" ;; adopt) cmd_adopt "$@" ;; + restart) cmd_restart "$@" ;; + export) cmd_export "$@" ;; + uninstall) cmd_uninstall "$@" ;; + purge) cmd_purge "$@" ;; add) die "\`crosscode add\` comes with the second product (phase 3)." ;; help|-h|--help) case "${1:-}" in @@ -71,6 +121,7 @@ case "$command" in status) status_usage ;; install) install_usage ;; adopt) adopt_usage ;; + restart|export|uninstall|purge) manage_usage ;; *) usage ;; esac ;; *) usage; die "Unknown command \"$command\"." ;; diff --git a/scripts/lib/common.sh b/scripts/lib/common.sh index 6e01b08..28fef7d 100644 --- a/scripts/lib/common.sh +++ b/scripts/lib/common.sh @@ -14,10 +14,12 @@ CROSSCODE_NODE_MIN=20 CROSSCODE_NODE_WARN=22 # ── Logging ────────────────────────────────────────────────────────────────────────────────── -step() { printf '==> %s\n' "$*"; } +# 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; exit 1; } +die() { printf '!!> %s\n' "$*" >&2; report_line message "$*"; exit 1; } # ── Small checks ───────────────────────────────────────────────────────────────────────────── require_root() { diff --git a/scripts/lib/deploy.sh b/scripts/lib/deploy.sh index cdc0d3e..d4d6cd3 100644 --- a/scripts/lib/deploy.sh +++ b/scripts/lib/deploy.sh @@ -193,6 +193,26 @@ deploy_church() { done [ "$failed" = 0 ] || die "Requirements not met for $church. Nothing was changed." + # New settings in the target release's env template (AGENT-1.0-PLAN §5): a new required one + # with no value anywhere stops the deploy here, before anything is touched. + local tpl out + local -a vars + for p in "${products[@]}"; do + [ -n "${TVER[$p]}" ] || continue + tpl="$(mktemp)" + if git_in "${DIR[$p]}" show "${TARGET[$p]}:apps/$p/env.template.json" > "$tpl" 2>/dev/null; then + mapfile -t vars < <("${p}_env_vars") + if out="$(env_apply_template "$tpl" "$(pvar "$p" DIR)/apps/$p" "$p" "${TARGET[$p]}" --check "${vars[@]}" 2>&1)"; then + [ -z "$out" ] || printf '%s\n' "$out" | sed "s/^/ $p: /" + else + warn "$p ${TVER[$p]}: $out" + failed=1 + fi + fi + rm -f "$tpl" + done + [ "$failed" = 0 ] || die "Settings missing for $church (set them in the Hub's server configuration or defaults). Nothing was changed." + for p in "${products[@]}"; do if [ "${PREV[$p]}" = "${TARGET[$p]}" ]; then info "$p: already on ${PREV[$p]}." @@ -218,8 +238,17 @@ deploy_church() { echo step "Summary for $church" - for p in "${products[@]}"; do info "$(printf '%-9s %s' "$p" "${RESULT[$p]}")"; done - [ "$stopped" = 0 ] || return 1 + local json="" rolled=0 + for p in "${products[@]}"; do + info "$(printf '%-9s %s' "$p" "${RESULT[$p]}")" + json+="${json:+,}\"$p\":{\"from\":\"${PREV[$p]}\",\"to\":\"${TARGET[$p]}\",\"result\":\"${RESULT[$p]//\"/\'}\"}" + [[ "${RESULT[$p]}" != *"rolled back to"*"running again"* && "${RESULT[$p]}" != *"rolled back to"*"never stopped"* ]] || rolled=1 + done + report_result_json products "{$json}" + if [ "$stopped" = 1 ]; then + [ "$rolled" = 0 ] || report_status rolled-back + return 1 + fi } # deploy_product : sets DEPLOY_OK and @@ -238,6 +267,12 @@ deploy_product() { echo checkout > "$stage_file" step "$p: checking out $target" run_as "$user" "$dir" git checkout -q --detach "$target" + if [ -f "$dir/apps/$p/env.template.json" ]; then + step "$p: settings new in this release" + local -a vars + mapfile -t vars < <("${p}_env_vars") + env_apply "$dir/apps/$p" "$p" "$target" "${vars[@]}" | sed 's/^/ /' + fi step "$p: installing" "${p}_install_deps" "$dir" "$user" step "$p: migrations" diff --git a/scripts/lib/env.sh b/scripts/lib/env.sh new file mode 100644 index 0000000..92d5f30 --- /dev/null +++ b/scripts/lib/env.sh @@ -0,0 +1,108 @@ +# shellcheck shell=bash +# The job's config and the database (docs/AGENT-1.0-PLAN.md §5). +# +# CROSSCODE_CONFIG is a JSON file with the .env values by layer ({ server, defaults, install }), +# given with --config by the agent (it opens the sealed config from the Hub, writes it mode 600 +# and removes it afterwards). By hand there's usually none, and the script's own defaults apply. +# +# Database: the server layer can name a PostgreSQL server (DB_HOST, DB_PORT) and an admin +# connection that may create databases and roles (DB_ADMIN_URL). Without DB_ADMIN_URL the local +# PostgreSQL is used through `sudo -u postgres`, as before. + +CROSSCODE_CONFIG="${CROSSCODE_CONFIG:-}" + +# cfg [product]: a value from the config, "" when there's none. +cfg() { + [ -n "$CROSSCODE_CONFIG" ] && [ -f "$CROSSCODE_CONFIG" ] || return 0 + node -e ' + const c = JSON.parse(require("fs").readFileSync(process.argv[1], "utf8")); + const [layer, key, product] = process.argv.slice(2); + const v = layer === "install" ? ((c.install || {})[product] || {})[key] : (c[layer] || {})[key]; + if (v !== undefined && v !== null) process.stdout.write(String(v));' "$CROSSCODE_CONFIG" "$1" "$2" "${3:-}" +} + +# env_template : the release's template, or the copy shipped with the +# script (for releases from before templates, e.g. events@2.2.0). +env_template() { + if [ -f "$1/env.template.json" ]; then echo "$1/env.template.json"; else echo "$CROSSCODE_HOME/products/$2.env.template.json"; fi +} + +# env_apply [envtpl options…]: writes or appends the .env files. +# Generated values come in as CROSSCODE_GEN_ environment variables. +env_apply() { + local app="$1" product="$2" release="$3" + shift 3 + local -a conf=() + [ -n "$CROSSCODE_CONFIG" ] && conf=(--config "$CROSSCODE_CONFIG") + node "$CROSSCODE_HOME/lib/envtpl.js" --template "$(env_template "$app" "$product")" --dir "$app" \ + --product "$product" --release "$release" "${conf[@]+"${conf[@]}"}" "$@" +} + +# env_apply_template