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 <noreply@anthropic.com>
This commit is contained in:
2026-11-17 12:50:19 +02:00
co-authored by Claude Opus 5.5
parent 350ab9f4e8
commit e9e9b5a97f
17 changed files with 1308 additions and 88 deletions
+1 -1
View File
@@ -16,7 +16,7 @@
"test:scripts": "bats scripts/tests", "test:scripts": "bats scripts/tests",
"typecheck": "npm run typecheck --workspaces --if-present", "typecheck": "npm run typecheck --workspaces --if-present",
"lint": "eslint .", "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" "check": "npm run lint && npm run lint:scripts && npm run typecheck && npm test && npm run test:scripts && npm run build"
}, },
"engines": { "engines": {
+57 -6
View File
@@ -24,13 +24,30 @@ if [ -z "${CROSSCODE_HOME:-}" ]; then
CROSSCODE_HOME="$copy" CROSSCODE_FROM="$root" exec bash "$copy/crosscode" "$@" CROSSCODE_HOME="$copy" CROSSCODE_FROM="$root" exec bash "$copy/crosscode" "$@"
fi fi
# Running from the copy: remove it afterwards (only ever a mktemp folder, never a checkout). # Options for every command, taken out of the arguments here: --json (a crosscode.script-report/1
if [[ "$CROSSCODE_HOME" == /tmp/crosscode.* ]]; then # on stdout at the end, the log on stderr) and --config <file> (the .env values by layer, from
trap 'rm -rf "$CROSSCODE_HOME"' EXIT # the agent).
fi 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 # shellcheck source=lib/common.sh
source "$CROSSCODE_HOME/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 # shellcheck source=lib/deploy.sh
source "$CROSSCODE_HOME/lib/deploy.sh" source "$CROSSCODE_HOME/lib/deploy.sh"
# shellcheck source=lib/status.sh # shellcheck source=lib/status.sh
@@ -39,6 +56,32 @@ source "$CROSSCODE_HOME/lib/status.sh"
source "$CROSSCODE_HOME/lib/install.sh" source "$CROSSCODE_HOME/lib/install.sh"
# shellcheck source=lib/adopt.sh # shellcheck source=lib/adopt.sh
source "$CROSSCODE_HOME/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() { usage() {
cat <<EOF cat <<EOF
@@ -51,19 +94,26 @@ crosscode: Cross Code installs on this server ($(git -C "${CROSSCODE_FROM:-.}" r
crosscode install <church> --products events --domain <address> a new church crosscode install <church> --products events --domain <address> a new church
crosscode adopt <church> --events-dir <old folder> move an existing Cross Events install here crosscode adopt <church> --events-dir <old folder> move an existing Cross Events install here
crosscode adopt <church> --undo go back to the old folder (within 14 days) crosscode adopt <church> --undo go back to the old folder (within 14 days)
crosscode restart <church> [events] reload the PM2 processes
crosscode export <church> --out <file.zip> the church's data and uploads
crosscode uninstall <church> --confirm <church> remove it (a final backup is kept 30 days)
crosscode purge <church> --confirm <church> delete what uninstall kept
crosscode help [<command>] this, or a command's options crosscode help [<command>] this, or a command's options
Every command takes --json (a report at the end) and --config <file> (settings from the Hub).
Churches live in $CROSSCODE_SITES/<church>/ (church.env, one checkout per product). Churches live in $CROSSCODE_SITES/<church>/ (church.env, one checkout per product).
EOF EOF
} }
command="${1:-help}"
[ $# -gt 0 ] && shift
case "$command" in case "$command" in
deploy) cmd_deploy "$@" ;; deploy) cmd_deploy "$@" ;;
status) cmd_status "$@" ;; status) cmd_status "$@" ;;
install) cmd_install "$@" ;; install) cmd_install "$@" ;;
adopt) cmd_adopt "$@" ;; 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)." ;; add) die "\`crosscode add\` comes with the second product (phase 3)." ;;
help|-h|--help) help|-h|--help)
case "${1:-}" in case "${1:-}" in
@@ -71,6 +121,7 @@ case "$command" in
status) status_usage ;; status) status_usage ;;
install) install_usage ;; install) install_usage ;;
adopt) adopt_usage ;; adopt) adopt_usage ;;
restart|export|uninstall|purge) manage_usage ;;
*) usage ;; *) usage ;;
esac ;; esac ;;
*) usage; die "Unknown command \"$command\"." ;; *) usage; die "Unknown command \"$command\"." ;;
+4 -2
View File
@@ -14,10 +14,12 @@ CROSSCODE_NODE_MIN=20
CROSSCODE_NODE_WARN=22 CROSSCODE_NODE_WARN=22
# ── Logging ────────────────────────────────────────────────────────────────────────────────── # ── 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' "$*"; } info() { printf ' %s\n' "$*"; }
warn() { printf '!!> %s\n' "$*" >&2; } warn() { printf '!!> %s\n' "$*" >&2; }
die() { printf '!!> %s\n' "$*" >&2; exit 1; } die() { printf '!!> %s\n' "$*" >&2; report_line message "$*"; exit 1; }
# ── Small checks ───────────────────────────────────────────────────────────────────────────── # ── Small checks ─────────────────────────────────────────────────────────────────────────────
require_root() { require_root() {
+37 -2
View File
@@ -193,6 +193,26 @@ deploy_church() {
done done
[ "$failed" = 0 ] || die "Requirements not met for $church. Nothing was changed." [ "$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 for p in "${products[@]}"; do
if [ "${PREV[$p]}" = "${TARGET[$p]}" ]; then if [ "${PREV[$p]}" = "${TARGET[$p]}" ]; then
info "$p: already on ${PREV[$p]}." info "$p: already on ${PREV[$p]}."
@@ -218,8 +238,17 @@ deploy_church() {
echo echo
step "Summary for $church" step "Summary for $church"
for p in "${products[@]}"; do info "$(printf '%-9s %s' "$p" "${RESULT[$p]}")"; done local json="" rolled=0
[ "$stopped" = 0 ] || return 1 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 <product> <checkout> <user> <previous ref> <target ref>: sets DEPLOY_OK and # deploy_product <product> <checkout> <user> <previous ref> <target ref>: sets DEPLOY_OK and
@@ -238,6 +267,12 @@ deploy_product() {
echo checkout > "$stage_file" echo checkout > "$stage_file"
step "$p: checking out $target" step "$p: checking out $target"
run_as "$user" "$dir" git checkout -q --detach "$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" step "$p: installing"
"${p}_install_deps" "$dir" "$user" "${p}_install_deps" "$dir" "$user"
step "$p: migrations" step "$p: migrations"
+108
View File
@@ -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 <layer> <KEY> [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 <product dir> <product>: 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 <product dir> <product> <release> [envtpl options…]: writes or appends the .env files.
# Generated values come in as CROSSCODE_GEN_<NAME> 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 <template file> <product dir> <product> <release> [envtpl options…]: as
# env_apply, with a template from elsewhere (deploy checks the target release's before checkout).
env_apply_template() {
local tpl="$1" app="$2" product="$3" release="$4"
shift 4
local -a conf=()
[ -n "$CROSSCODE_CONFIG" ] && conf=(--config "$CROSSCODE_CONFIG")
node "$CROSSCODE_HOME/lib/envtpl.js" --template "$tpl" --dir "$app" \
--product "$product" --release "$release" "${conf[@]+"${conf[@]}"}" "$@"
}
db_host() { local h; h="$(cfg server DB_HOST)"; printf '%s' "${h:-localhost}"; }
db_port() { local p; p="$(cfg server DB_PORT)"; printf '%s' "${p:-5432}"; }
# db_admin <psql args…>: psql as the database admin. SQL on stdin or with -c.
db_admin() {
local url
url="$(cfg server DB_ADMIN_URL)"
if [ -z "$url" ]; then
sudo -u postgres psql -v ON_ERROR_STOP=1 -q "$@"
return
fi
# The URL goes in as PG* variables, so the password isn't in the process list.
local -a pg
# shellcheck disable=SC2016 # JavaScript, not shell
mapfile -t pg < <(node -e '
const u = new URL(process.argv[1]);
if (!/^postgres(ql)?:$/.test(u.protocol)) process.exit(2);
const out = { PGHOST: u.hostname, PGPORT: u.port || "5432", PGUSER: decodeURIComponent(u.username), PGPASSWORD: decodeURIComponent(u.password), PGDATABASE: decodeURIComponent(u.pathname.slice(1)) || "postgres" };
const ssl = u.searchParams.get("sslmode"); if (ssl) out.PGSSLMODE = ssl;
for (const [k, v] of Object.entries(out)) console.log(`${k}=${v}`);' "$url") || die "DB_ADMIN_URL in the server's configuration isn't a postgresql:// URL."
env "${pg[@]}" psql -v ON_ERROR_STOP=1 -q "$@"
}
# db_create <name> <password>: the role and database (both named <name>), or a new password for
# an existing role. Names are checked by the caller (a-z, 0-9, _).
db_create() {
local db="$1" pass="$2"
[[ "$db" =~ ^[a-z][a-z0-9_]{0,62}$ ]] || die "Bad database name $db."
db_admin <<SQL
DO \$\$ BEGIN
IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = '$db') THEN
CREATE ROLE "$db" LOGIN PASSWORD '$pass';
ELSE
ALTER ROLE "$db" WITH LOGIN PASSWORD '$pass';
END IF;
END \$\$;
SQL
db_admin -tAc "SELECT 1 FROM pg_database WHERE datname = '$db'" | grep -q 1 ||
db_admin -c "CREATE DATABASE \"$db\" OWNER \"$db\""
}
# db_exists <name>: a database or a role with that name exists.
db_exists() {
local n="$1" out
[[ "$n" =~ ^[a-z][a-z0-9_]{0,62}$ ]] || die "Bad database name $n."
out="$(db_admin -tAc "SELECT 1 FROM pg_roles WHERE rolname = '$n' UNION ALL SELECT 1 FROM pg_database WHERE datname = '$n'")" ||
die "Couldn't reach PostgreSQL on $(db_host) to check for $n."
[ -n "$out" ]
}
# db_drop <name>: the database and its role (after a backup by the caller).
db_drop() {
local db="$1"
[[ "$db" =~ ^[a-z][a-z0-9_]{0,62}$ ]] || die "Bad database name $db."
db_admin -c "DROP DATABASE IF EXISTS \"$db\" WITH (FORCE)"
db_admin -c "DROP ROLE IF EXISTS \"$db\""
}
+164
View File
@@ -0,0 +1,164 @@
#!/usr/bin/env node
// Builds a product's .env files from its env template (crosscode.env-template/1,
// docs/AGENT-1.0-PLAN.md §5). Used by `crosscode install` (files don't exist yet: written whole)
// and `crosscode deploy` (files exist: only variables they don't have are appended, with a
// comment; existing lines are never changed or removed).
//
// node envtpl.js --template <file> --dir <product dir> [--config <job config JSON>]
// [--product events] [--var domain=… --var apiPort=…] [--fallback NAME=value]
// [--release events@x.y.z] [--check]
//
// Values by layer: generated → environment variables CROSSCODE_GEN_<NAME> (so secrets never
// appear in arguments or files other than the .env); server / defaults → the config's layers;
// install → the config's install[product], else the variable's `from` filled with --var values.
// Then --fallback, then the template's `default`. A required variable with no value stops
// everything before any file is written (exit 3, the names on stderr).
// --check: only says what would be added. Prints names only, never values.
'use strict';
const fs = require('fs');
const path = require('path');
function parseArgs(argv) {
const out = { vars: {}, fallback: {}, check: false };
for (let i = 0; i < argv.length; i++) {
const a = argv[i];
const next = () => {
if (i + 1 >= argv.length) throw new Error(`${a} needs a value`);
return argv[++i];
};
if (a === '--template') out.template = next();
else if (a === '--dir') out.dir = next();
else if (a === '--config') out.config = next();
else if (a === '--product') out.product = next();
else if (a === '--release') out.release = next();
else if (a === '--check') out.check = true;
else if (a === '--var' || a === '--fallback') {
const v = next();
const eq = v.indexOf('=');
if (eq < 1) throw new Error(`${a} ${v}: expected NAME=value`);
out[a === '--var' ? 'vars' : 'fallback'][v.slice(0, eq)] = v.slice(eq + 1);
} else throw new Error(`Unknown option ${a}`);
}
if (!out.template || !out.dir) throw new Error('--template and --dir are required');
return out;
}
/** KEY names a .env file already sets (comments and blank lines ignored, `export KEY=` allowed). */
function keysIn(text) {
const keys = new Set();
for (const line of text.split(/\r?\n/)) {
const m = /^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=/.exec(line);
if (m) keys.add(m[1]);
}
return keys;
}
function fill(from, vars) {
let missing = false;
const value = from.replace(/\{([A-Za-z]+)\}/g, (_, k) => {
if (vars[k] === undefined || vars[k] === '') missing = true;
return vars[k] ?? '';
});
return missing ? undefined : value;
}
/** A value as a .env line: bare when safe, otherwise double-quoted (dotenv's rules). */
function line(name, value) {
if (/[\r\n]/.test(value)) throw new Error(`${name}: values can't contain newlines`);
return /^[A-Za-z0-9_./:@,+=%-]*$/.test(value) ? `${name}=${value}` : `${name}="${value.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"`;
}
function resolve(variable, ctx) {
let value;
const nonEmpty = v => (v === undefined || v === null || String(v) === '' ? undefined : String(v));
switch (variable.layer) {
case 'generated': value = nonEmpty(process.env[`CROSSCODE_GEN_${variable.name}`]); break;
case 'server': value = nonEmpty(ctx.config.server?.[variable.name]); break;
case 'defaults': value = nonEmpty(ctx.config.defaults?.[variable.name]); break;
case 'install':
value = nonEmpty(ctx.config.install?.[ctx.product]?.[variable.name]);
if (value === undefined && variable.from) value = nonEmpty(fill(variable.from, ctx.vars));
break;
default: throw new Error(`${variable.name}: unknown layer ${variable.layer}`);
}
if (value === undefined) value = nonEmpty(ctx.fallback[variable.name]);
if (value === undefined) value = nonEmpty(variable.default);
return value;
}
function plan(opts) {
const template = JSON.parse(fs.readFileSync(opts.template, 'utf8'));
if (template.schema !== 'crosscode.env-template/1') throw new Error(`${opts.template} isn't a crosscode.env-template/1`);
const config = opts.config ? JSON.parse(fs.readFileSync(opts.config, 'utf8')) : {};
const ctx = { config, vars: opts.vars, fallback: opts.fallback, product: opts.product || template.product };
const files = [];
const missing = [];
for (const [rel, variables] of Object.entries(template.files)) {
if (rel.includes('..') || path.isAbsolute(rel)) throw new Error(`Bad file name in the template: ${rel}`);
const file = path.join(opts.dir, rel);
const exists = fs.existsSync(file);
const have = exists ? keysIn(fs.readFileSync(file, 'utf8')) : new Set();
const add = [];
for (const v of variables) {
if (have.has(v.name)) continue;
const value = resolve(v, ctx);
if (value === undefined) {
if (v.required) missing.push(`${v.name} (${v.layer}, ${rel})`);
continue;
}
add.push({ name: v.name, value });
}
files.push({ rel, file, exists, add });
}
return { files, missing };
}
function write({ files }, release) {
const date = new Date().toISOString().slice(0, 10);
for (const f of files) {
if (!f.add.length) continue;
const lines = f.add.map(a => line(a.name, a.value));
let text;
if (!f.exists) {
text = `# Written by crosscode from env.template.json${release ? ` (${release})` : ''}, ${date}.\n${lines.join('\n')}\n`;
} else {
const current = fs.readFileSync(f.file, 'utf8');
text = `${current}${current === '' || current.endsWith('\n') ? '' : '\n'}\n# Added by crosscode ${date}${release ? ` (${release})` : ''}\n${lines.join('\n')}\n`;
}
fs.mkdirSync(path.dirname(f.file), { recursive: true });
const tmp = `${f.file}.crosscode-${process.pid}`;
fs.writeFileSync(tmp, text, { mode: 0o600 });
if (f.exists) {
const st = fs.statSync(f.file);
try { fs.chownSync(tmp, st.uid, st.gid); } catch { /* not root: same owner anyway */ }
fs.chmodSync(tmp, st.mode & 0o777);
}
fs.renameSync(tmp, f.file);
}
}
function main() {
let opts;
try {
opts = parseArgs(process.argv.slice(2));
} catch (e) {
console.error(`envtpl: ${e.message}`);
process.exit(2);
}
const result = plan(opts);
if (result.missing.length) {
console.error(`Required settings with no value: ${result.missing.join(', ')}`);
process.exit(3);
}
for (const f of result.files) {
if (!f.add.length) continue;
const names = f.add.map(a => a.name).join(', ');
console.log(`${f.rel}: ${opts.check ? 'would add' : f.exists ? 'added' : 'wrote'} ${names}`);
}
if (!opts.check) write(result, opts.release);
}
if (require.main === module) main();
module.exports = { parseArgs, keysIn, fill, line, plan, write };
+127
View File
@@ -0,0 +1,127 @@
# shellcheck shell=bash
# frpc tunnels for church installs (docs/AGENT-1.0-PLAN.md §6). The script only ever appends
# marked blocks to frpc.ini and removes its own blocks again; nothing outside a block is changed,
# except two admin lines added once to [common] so `frpc reload` can pick up new tunnels without
# dropping the others. Every change: a backup, a temporary file, `frpc verify`, then the move.
FRPC_INI="${FRPC_INI:-/etc/frp/frpc.ini}"
FRPC_BACKUPS="${FRPC_BACKUPS:-/etc/frp/backups}"
FRPC_BIN="${FRPC_BIN:-frpc}"
frpc_begin() { printf '# >>> crosscode:%s:%s' "$1" "$2"; }
frpc_end() { printf '# <<< crosscode:%s:%s' "$1" "$2"; }
# frpc_block <church> <product> <web port> <api port>: the block (no leading blank line).
frpc_block() {
local church="$1" product="$2" web="$3" api="$4" name
name="$church-$product"
cat <<EOF
$(frpc_begin "$church" "$product") (added by crosscode $(date +%F), do not edit by hand)
[$name-web]
type = tcp
local_ip = 127.0.0.1
local_port = $web
remote_port = $web
[$name-api]
type = tcp
local_ip = 127.0.0.1
local_port = $api
remote_port = $api
$(frpc_end "$church" "$product")
EOF
}
frpc_check_format() {
[ -f "$FRPC_INI" ] || die "No $FRPC_INI: is frpc installed and set up on this server?"
if [ -f "$(dirname "$FRPC_INI")/frpc.toml" ] && ! grep -q '^\[common\]' "$FRPC_INI"; then
die "This server's frpc uses frpc.toml; crosscode only edits frpc.ini so far."
fi
}
frpc_has_block() { grep -qF "$(frpc_begin "$1" "$2")" "$FRPC_INI" 2>/dev/null; }
# frpc_ports <church> <product>: the web and api ports in its block ("web api"), "" if none.
frpc_ports() {
awk -v b="$(frpc_begin "$1" "$2")" -v e="$(frpc_end "$1" "$2")" '
index($0, b) == 1 { inb = 1; next }
index($0, e) == 1 { inb = 0 }
inb && /^\[.*-web\]/ { w = 1; a = 0 }
inb && /^\[.*-api\]/ { a = 1; w = 0 }
inb && /^remote_port/ { split($0, p, "="); gsub(/ /, "", p[2]); if (w) web = p[2]; if (a) api = p[2] }
END { if (web != "" || api != "") print web " " api }
' "$FRPC_INI" 2>/dev/null
}
# with_admin_lines <frpc.ini>: the file, with admin_addr / admin_port added to [common] if it has
# no admin_addr there (an existing one is left as it is).
with_admin_lines() {
if awk '/^\[common\]/ { c = 1; next } /^\[/ { c = 0 } c && /^[[:space:]]*admin_addr[[:space:]]*=/ { f = 1 } END { exit !f }' "$1"; then
cat "$1"
else
awk '{ print } /^\[common\][[:space:]]*$/ && !done { print "admin_addr = 127.0.0.1"; print "admin_port = 7400"; done = 1 }' "$1"
fi
}
# frpc_replace <new file>: checks and moves a new frpc.ini into place (backup first).
frpc_replace() {
local new="$1" stamp
if ! "$FRPC_BIN" verify -c "$new" >/dev/null 2>&1; then
rm -f "$new"
die "frpc verify refused the new frpc.ini, so it wasn't changed."
fi
stamp="$(date +%Y%m%d%H%M%S)"
mkdir -p "$FRPC_BACKUPS"
cp -p "$FRPC_INI" "$FRPC_BACKUPS/frpc.ini.$stamp"
chmod --reference="$FRPC_INI" "$new" 2>/dev/null || chmod 600 "$new"
mv -f "$new" "$FRPC_INI"
}
# frpc_add <church> <product> <web> <api>: appends the block (no change if it's there already).
frpc_add() {
local church="$1" product="$2" web="$3" api="$4" new name
frpc_check_format
if frpc_has_block "$church" "$product"; then
info "frpc: $church:$product is already in $FRPC_INI."
return 0
fi
name="$church-$product"
if grep -qE "^\[($name-web|$name-api)\]" "$FRPC_INI"; then
die "frpc: a tunnel named $name-web or $name-api is already in $FRPC_INI (outside crosscode's blocks)."
fi
if grep -qE "^[[:space:]]*(local_port|remote_port)[[:space:]]*=[[:space:]]*($web|$api)[[:space:]]*$" "$FRPC_INI"; then
die "frpc: port $web or $api is already used in $FRPC_INI."
fi
new="$(mktemp "$(dirname "$FRPC_INI")/.frpc.ini.XXXXXX")"
with_admin_lines "$FRPC_INI" > "$new"
# Keep the file's last line intact: only add a newline if it had none.
[ -z "$(tail -c 1 "$new")" ] || echo >> "$new"
{ echo; frpc_block "$church" "$product" "$web" "$api"; } >> "$new"
frpc_replace "$new"
info "frpc: added $name-web ($web) and $name-api ($api)."
}
# frpc_remove <church> <product>: removes its block (and the blank line added before it).
frpc_remove() {
local church="$1" product="$2" new
frpc_check_format
frpc_has_block "$church" "$product" || { info "frpc: no block for $church:$product."; return 0; }
new="$(mktemp "$(dirname "$FRPC_INI")/.frpc.ini.XXXXXX")"
# Drops the block's lines and the one blank line straight before its begin marker.
awk -v b="$(frpc_begin "$church" "$product")" -v e="$(frpc_end "$church" "$product")" '
{ lines[NR] = $0 }
END {
start = 0; stop = 0
for (i = 1; i <= NR; i++) if (index(lines[i], b) == 1) { start = i; break }
for (i = start; i <= NR; i++) if (index(lines[i], e) == 1) { stop = i; break }
if (start > 1 && lines[start - 1] == "") start--
for (i = 1; i <= NR; i++) if (i < start || i > stop) print lines[i]
}
' "$FRPC_INI" > "$new"
frpc_replace "$new"
info "frpc: removed $church-$product."
}
frpc_reload() {
"$FRPC_BIN" reload -c "$FRPC_INI" || warn "frpc reload failed: restart frpc (systemctl restart frpc) to pick up the change."
}
+126 -62
View File
@@ -1,26 +1,34 @@
# shellcheck shell=bash # shellcheck shell=bash
# crosscode install: a new church on this server (from hope-events' scripts/install.sh 2.1.x). # crosscode install: a new church on this server (from hope-events' scripts/install.sh 2.1.x;
# Events only in 2.2.0; Accounts, Website and App follow as their products/*.sh are added. # behind frp from the Hub in agent 1.0, docs/AGENT-1.0-PLAN.md §7).
# Events only so far; Accounts, Website and App follow as their products/*.sh are added.
install_usage() { install_usage() {
cat <<'EOF' cat <<'EOF'
crosscode install <church> --products events --domain <address> [options] crosscode install <church> --products events --domain <address> [options]
A new church: system packages (Node 24, PostgreSQL, PM2, nginx, certbot) if missing, then per A new church: system packages if missing, then per product an app user, database, checkout,
product an app user, database, checkout, .env, PM2 processes, nginx site and certificate. .env files (from the release's env.template.json), PM2 processes, and either an nginx site with
Writes /var/www/sites/<church>/church.env. DNS must already point here (it prints the records). a certificate (a server with its own address) or frpc tunnels (--frp, behind the central
server). Writes /var/www/sites/<church>/church.env. If a step fails, everything this run created
is removed again.
--products events the products to install (only events in this version) --products events the products to install (only events in this version)
--domain <address> the church's address: hopefamily.co.za or hopefamily.crosscode.co.za; --domain <address> the church's address: hopefamily.co.za or hopefamily.crosscode.co.za;
Events is served on events.<address> Events is served on events.<address>
--events-domain <domain> serve Events somewhere else instead --events-domain <domain> serve Events somewhere else instead
--email <email> for Let's Encrypt notices (required unless --no-ssl)
--version events@x.y.z that release (default: the latest) --version events@x.y.z that release (default: the latest)
--main the latest main (staging churches only) --main the latest main (staging churches only)
--user <name> the Linux user the apps run as (default: cc-<church>) --user <name> the Linux user the apps run as (default: cc-<church>)
--api-port <n> Events backend port (default: the first free one from 5000) --api-port <n> Events backend port (default: the first free one from 5000)
--web-port <n> Events frontend port (default: the first free one from 3000) --web-port <n> Events frontend port (default: the first free one from 3000)
--frp behind frp: add tunnels to frpc.ini (ports must be given, and are
used for the tunnel too); no nginx or certificate here
--email <email> for Let's Encrypt notices (required unless --frp or --no-ssl)
--no-ssl no certificate (behind another proxy that does HTTPS) --no-ssl no certificate (behind another proxy that does HTTPS)
--config <file> the .env values by layer (JSON; given by the agent)
--keep-on-failure leave what was made if a step fails (to look into it)
--json print a report (JSON) at the end; the log goes to stderr
EOF EOF
} }
@@ -44,8 +52,30 @@ free_port() {
random_secret() { openssl rand -base64 64 | tr -d '/+=\n' | head -c "${1:-48}"; } random_secret() { openssl rand -base64 64 | tr -d '/+=\n' | head -c "${1:-48}"; }
# ── Undo: what this run made, undone in reverse if it fails ──────────────────────────────────
INSTALL_UNDO=""
undo_add() { [ -z "$INSTALL_UNDO" ] || printf '%s\n' "$*" >> "$INSTALL_UNDO"; }
install_undo() {
local kind a b
step "Removing what this install made"
tac "$INSTALL_UNDO" | while read -r kind a b; do
case "$kind" in
pm2) # shellcheck disable=SC2086 # b is a space-separated list of PM2 names
run_as "$a" / pm2 delete $b >/dev/null 2>&1 || true
run_as "$a" / pm2 save --force >/dev/null 2>&1 || true
info "PM2: $b removed" ;;
frpc) frpc_remove "$a" "$b" && frpc_reload || warn "frpc block $a:$b not removed" ;;
nginx) rm -f "/etc/nginx/sites-enabled/$a" "/etc/nginx/sites-available/$a"; systemctl reload nginx 2>/dev/null || true; info "nginx: $a removed" ;;
db) db_drop "$a" >/dev/null 2>&1 && info "database $a removed" || warn "database $a not removed" ;;
dir) rm -rf -- "$a" && info "$a removed" ;;
user) pkill -u "$a" 2>/dev/null || true; userdel -r "$a" >/dev/null 2>&1 && info "user $a removed" || true ;;
esac
done
}
cmd_install() { cmd_install() {
local church="" products="" address="" events_domain="" email="" version="" main=0 user="" api="" web="" ssl=1 local church="" products="" address="" events_domain="" email="" version="" main=0 user="" api="" web="" ssl=1 frp=0 keep=0
while [ $# -gt 0 ]; do while [ $# -gt 0 ]; do
case "$1" in case "$1" in
--products) products="$(printf '%s' "$2" | tr ',' ' ')"; shift 2 ;; --products) products="$(printf '%s' "$2" | tr ',' ' ')"; shift 2 ;;
@@ -58,6 +88,8 @@ cmd_install() {
--api-port) api="$2"; shift 2 ;; --api-port) api="$2"; shift 2 ;;
--web-port) web="$2"; shift 2 ;; --web-port) web="$2"; shift 2 ;;
--no-ssl) ssl=0; shift ;; --no-ssl) ssl=0; shift ;;
--frp) frp=1; ssl=0; shift ;;
--keep-on-failure) keep=1; shift ;;
-h|--help) install_usage; return 0 ;; -h|--help) install_usage; return 0 ;;
-*) die "Unknown option $1 (crosscode install --help)." ;; -*) die "Unknown option $1 (crosscode install --help)." ;;
*) [ -z "$church" ] || die "One church at a time."; church="$1"; shift ;; *) [ -z "$church" ] || die "One church at a time."; church="$1"; shift ;;
@@ -73,43 +105,84 @@ cmd_install() {
done done
[[ "$address" =~ ^[a-z0-9]([a-z0-9.-]*[a-z0-9])?\.[a-z]{2,}$ ]] || die "--domain: give the church's address, e.g. hopefamily.co.za." [[ "$address" =~ ^[a-z0-9]([a-z0-9.-]*[a-z0-9])?\.[a-z]{2,}$ ]] || die "--domain: give the church's address, e.g. hopefamily.co.za."
events_domain="${events_domain:-events.$address}" events_domain="${events_domain:-events.$address}"
[ "$ssl" = 0 ] || [ -n "$email" ] || die "--email is required for the certificate (or use --no-ssl)." [[ "$events_domain" =~ ^[a-z0-9]([a-z0-9.-]*[a-z0-9])?\.[a-z]{2,}$ ]] || die "--events-domain: expected a domain."
[ "$ssl" = 0 ] || [ -n "$email" ] || die "--email is required for the certificate (or use --frp or --no-ssl)."
[ -z "$version" ] || [[ "$version" =~ ^events@[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$ ]] || die "--version: expected events@x.y.z." [ -z "$version" ] || [[ "$version" =~ ^events@[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$ ]] || die "--version: expected events@x.y.z."
[ "$main" = 0 ] || [ -z "$version" ] || die "Use --main or --version, not both." [ "$main" = 0 ] || [ -z "$version" ] || die "Use --main or --version, not both."
if [ "$frp" = 1 ]; then
[ -n "$api" ] && [ -n "$web" ] || die "--frp needs --api-port and --web-port (from the Hub's port list)."
fi
user="${user:-cc-$church}" user="${user:-cc-$church}"
user="${user:0:32}" user="${user:0:32}"
require_root require_root
[ ! -f "$(church_env_path "$church")" ] || die "$church is already installed here (crosscode status $church). New products for it come with \`crosscode add\` (phase 3)." [ ! -f "$(church_env_path "$church")" ] || die "$church is already installed here (crosscode status $church). New products for it come with \`crosscode add\` (phase 3)."
[ ! -e "$(church_dir "$church")" ] || die "$(church_dir "$church") exists without a church.env. Move it away first."
local taken local taken
mapfile -t taken < <(used_ports) mapfile -t taken < <(used_ports)
if [ -n "$api" ] && printf '%s\n' "${taken[@]}" | grep -qx "$api"; then die "Port $api is already in use here."; fi
if [ -n "$web" ] && printf '%s\n' "${taken[@]}" | grep -qx "$web"; then die "Port $web is already in use here."; fi
api="${api:-$(free_port 5000 "${taken[@]}")}" api="${api:-$(free_port 5000 "${taken[@]}")}"
web="${web:-$(free_port 3000 "${taken[@]}" "$api")}" web="${web:-$(free_port 3000 "${taken[@]}" "$api")}"
[[ "$api" =~ ^[0-9]+$ && "$web" =~ ^[0-9]+$ && "$api" != "$web" ]] || die "Ports must be two different numbers." [[ "$api" =~ ^[0-9]+$ && "$web" =~ ^[0-9]+$ && "$api" != "$web" ]] || die "Ports must be two different numbers."
if [ "$frp" = 1 ]; then
command -v "$FRPC_BIN" >/dev/null || die "frpc isn't installed here ($FRPC_BIN)."
frpc_check_format
fi
report_result church "$church"
report_result_json ports "{\"events\":{\"web\":$web,\"api\":$api}}"
report_result_json domains "{\"events\":\"$events_domain\"}"
install_system_packages "$ssl" INSTALL_UNDO="$(mktemp)"
local rc
set +e
(
set -e
install_system_packages "$ssl" "$frp"
step "App user ($user)" step "App user ($user)"
id "$user" >/dev/null 2>&1 || useradd --create-home --shell /bin/bash "$user" if ! id "$user" >/dev/null 2>&1; then
useradd --create-home --shell /bin/bash "$user"
undo_add user "$user"
fi
CENV=() CENV=()
church_env_set CHURCH "$church" church_env_set CHURCH "$church"
church_env_set PRODUCTS "events" church_env_set PRODUCTS "events"
church_env_set ADDRESS "$address" church_env_set ADDRESS "$address"
install_events "$church" "$user" "$events_domain" "$api" "$web" "$version" "$main" "$ssl" "$email" install_events "$church" "$user" "$events_domain" "$api" "$web" "$version" "$main" "$ssl" "$email" "$frp"
church_env_write "$(church_env_path "$church")" church_env_write "$(church_env_path "$church")"
)
rc=$?
set -e
if [ "$rc" != 0 ]; then
if [ "$keep" = 1 ]; then
warn "Install failed; --keep-on-failure: what was made is left in place ($(church_dir "$church"))."
else
install_undo
fi
rm -f "$INSTALL_UNDO"
die "Install of $church failed."
fi
rm -f "$INSTALL_UNDO"
step "Done" step "Done"
if [ "$frp" = 1 ]; then
info "Tunnels: $church-events-web → $web, $church-events-api → $api. Set up the site on the central server."
else
info "DNS (if not set yet): $events_domain → this server (A record, or a CNAME)." info "DNS (if not set yet): $events_domain → this server (A record, or a CNAME)."
info "Open https://$events_domain/setup to create the admin account, then enter the licence key." fi
info "Open https://$events_domain/setup to create the admin account."
info "Updates: crosscode deploy $church events" info "Updates: crosscode deploy $church events"
} }
install_system_packages() { install_system_packages() {
local ssl="$1" local ssl="$1" frp="$2"
step "System packages" step "System packages"
export DEBIAN_FRONTEND=noninteractive export DEBIAN_FRONTEND=noninteractive
apt-get update -q apt-get update -q
apt-get install -y -q curl git rsync nginx postgresql postgresql-client openssl build-essential ca-certificates gnupg apt-get install -y -q curl git rsync zip postgresql-client openssl build-essential ca-certificates gnupg
# A database here, unless the server's configuration names another PostgreSQL server.
[ -n "$(cfg server DB_ADMIN_URL)" ] || apt-get install -y -q postgresql
[ "$frp" = 1 ] || apt-get install -y -q nginx
[ "$ssl" = 0 ] || apt-get install -y -q certbot python3-certbot-nginx [ "$ssl" = 0 ] || apt-get install -y -q certbot python3-certbot-nginx
# Node 24 is the LTS line supported until April 2028. # Node 24 is the LTS line supported until April 2028.
if ! command -v node >/dev/null || [ "$(node_major)" -lt 24 ]; then if ! command -v node >/dev/null || [ "$(node_major)" -lt 24 ]; then
@@ -121,32 +194,29 @@ install_system_packages() {
} }
install_events() { install_events() {
local church="$1" user="$2" domain="$3" api="$4" web="$5" version="$6" main="$7" ssl="$8" email="$9" local church="$1" user="$2" domain="$3" api="$4" web="$5" version="$6" main="$7" ssl="$8" email="$9" frp="${10}"
local dir app db pass url name scheme local dir app db pass name ref
# shellcheck source=/dev/null # shellcheck source=/dev/null
source "$CROSSCODE_HOME/products/events.sh" source "$CROSSCODE_HOME/products/events.sh"
dir="$(church_dir "$church")/events" dir="$(church_dir "$church")/events"
app="$dir/apps/events" app="$dir/apps/events"
db="$(printf '%s_events' "$church" | tr '-' '_')" db="$(printf '%s_events' "$church" | tr '-' '_')"
name="$church-events" name="$church-events"
scheme=https
[ "$ssl" = 1 ] || scheme=http
url="$scheme://$domain"
church_env_set EVENTS_DIR "$dir" church_env_set EVENTS_DIR "$dir"
church_env_set EVENTS_DOMAIN "$domain" church_env_set EVENTS_DOMAIN "$domain"
church_env_set EVENTS_PM2 "$name-backend $name-frontend" church_env_set EVENTS_PM2 "$name-backend $name-frontend"
church_env_set EVENTS_PORTS "$api $web" church_env_set EVENTS_PORTS "$api $web"
church_env_set EVENTS_DB "$db" church_env_set EVENTS_DB "$db"
church_env_set EVENTS_USER "$user" church_env_set EVENTS_USER "$user"
[ "$frp" = 0 ] || church_env_set EVENTS_FRP 1
step "Events: code ($dir)" step "Events: code ($dir)"
mkdir -p "$(church_dir "$church")" mkdir -p "$(church_dir "$church")"
undo_add dir "$(church_dir "$church")"
chmod 755 "$CROSSCODE_SITES" "$(church_dir "$church")" chmod 755 "$CROSSCODE_SITES" "$(church_dir "$church")"
if [ ! -d "$dir/.git" ]; then
mkdir -p "$dir" mkdir -p "$dir"
chown "$user:$user" "$dir" chown "$user:$user" "$dir"
run_as "$user" "$dir" git clone --quiet "$CROSSCODE_REPO" . run_as "$user" "$dir" git clone --quiet "$CROSSCODE_REPO" .
fi
run_as "$user" "$dir" git fetch --tags --quiet origin run_as "$user" "$dir" git fetch --tags --quiet origin
if [ "$main" = 1 ]; then if [ "$main" = 1 ]; then
version=origin/main version=origin/main
@@ -155,47 +225,31 @@ install_events() {
[ -n "$version" ] || die "No events@x.y.z release tags yet (use --version or --main)." [ -n "$version" ] || die "No events@x.y.z release tags yet (use --version or --main)."
fi fi
run_as "$user" "$dir" git checkout -q --detach "$version" run_as "$user" "$dir" git checkout -q --detach "$version"
info "At $(checkout_ref "$dir" events)" ref="$(checkout_ref "$dir" events)"
info "At $ref"
report_result_json versions "{\"events\":\"$ref\"}"
step "Events: database ($db)" step "Events: database ($db on $(db_host))"
# Re-running keeps the password already in backend/.env; otherwise a new one. # Never reuse (or later drop) a database or role that something else may be using.
pass="" ! db_exists "$db" || die "A database or role named $db already exists on $(db_host). Remove it first if it's left over, or choose another church name."
if [ -f "$app/backend/.env" ]; then pass="$(random_secret 32)"
pass="$(env_value "$app/backend/.env" DATABASE_URL | sed -nE 's#^postgresql://[^:]+:([^@]+)@.*#\1#p')" undo_add db "$db"
fi db_create "$db" "$pass"
pass="${pass:-$(random_secret 32)}"
sudo -u postgres psql -v ON_ERROR_STOP=1 -q <<SQL
DO \$\$ BEGIN
IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = '$db') THEN
CREATE ROLE "$db" LOGIN PASSWORD '$pass';
ELSE
ALTER ROLE "$db" WITH LOGIN PASSWORD '$pass';
END IF;
END \$\$;
SQL
sudo -u postgres psql -tAc "SELECT 1 FROM pg_database WHERE datname = '$db'" | grep -q 1 ||
sudo -u postgres createdb -O "$db" "$db"
step "Events: configuration" step "Events: configuration"
if [ ! -f "$app/backend/.env" ]; then local trust=1
cat > "$app/backend/.env" <<ENV [ "$frp" = 0 ] || trust=loopback
NODE_ENV=production (
PORT=$api export CROSSCODE_GEN_NODE_ENV=production
DATABASE_URL=postgresql://$db:$pass@localhost:5432/$db export CROSSCODE_GEN_JWT_SECRET
JWT_SECRET=$(random_secret 64) CROSSCODE_GEN_JWT_SECRET="$(random_secret 64)"
FRONTEND_URL=$url export CROSSCODE_GEN_DATABASE_URL="postgresql://$db:$pass@$(db_host):$(db_port)/$db"
APP_BASE_URL=$url env_apply "$app" events "$ref" --var "domain=$domain" --var "apiPort=$api" --var "webPort=$web" --var "church=$church" \
BACKEND_URL=$url --fallback "TRUST_PROXY=$trust"
TRUST_PROXY=1 ) | sed 's/^/ /'
APP_TIMEZONE=Africa/Johannesburg [ "${PIPESTATUS[0]}" = 0 ] || die "The .env files couldn't be written (see above)."
ENV
info "Wrote backend/.env (keep JWT_SECRET: it also encrypts stored secrets)."
else
info "backend/.env exists: kept."
fi
[ -f "$app/frontend/.env.production" ] || echo "NEXT_PUBLIC_API_URL=$url" > "$app/frontend/.env.production"
chown "$user:$user" "$app/backend/.env" "$app/frontend/.env.production" chown "$user:$user" "$app/backend/.env" "$app/frontend/.env.production"
chmod 600 "$app/backend/.env" chmod 600 "$app/backend/.env" "$app/frontend/.env.production"
events_ensure_pm2_config "$dir" "$user" events_ensure_pm2_config "$dir" "$user"
step "Events: install, migrate, build" step "Events: install, migrate, build"
@@ -204,20 +258,29 @@ ENV
events_build "$dir" "$user" events_build "$dir" "$user"
step "Events: PM2" step "Events: PM2"
undo_add pm2 "$user" "$name-backend $name-frontend"
run_as "$user" "$app" pm2 startOrReload ecosystem.config.js run_as "$user" "$app" pm2 startOrReload ecosystem.config.js
run_as "$user" "$app" pm2 save run_as "$user" "$app" pm2 save
env PATH="$PATH:/usr/bin" pm2 startup systemd -u "$user" --hp "$(getent passwd "$user" | cut -d: -f6)" >/dev/null env PATH="$PATH:/usr/bin" pm2 startup systemd -u "$user" --hp "$(getent passwd "$user" | cut -d: -f6)" >/dev/null
if [ "$frp" = 1 ]; then
step "Events: frpc tunnels"
frpc_add "$church" events "$web" "$api"
undo_add frpc "$church" events
frpc_reload
else
step "Events: nginx ($domain)" step "Events: nginx ($domain)"
install_events_nginx "$name" "$domain" "$api" "$web" install_events_nginx "$name" "$domain" "$api" "$web"
undo_add nginx "$name"
if [ "$ssl" = 1 ]; then if [ "$ssl" = 1 ]; then
step "Events: certificate" step "Events: certificate"
certbot --nginx -d "$domain" --non-interactive --agree-tos -m "$email" --redirect certbot --nginx -d "$domain" --non-interactive --agree-tos -m "$email" --redirect
fi fi
fi
step "Events: checking" step "Events: checking it answers"
wait_for_version "$(events_health_url)" "$(events_expected_version "$dir")" 60 || wait_for_version "$(events_health_url)" "$(events_expected_version "$dir")" 90 ||
warn "The backend isn't answering yet: sudo -u $user pm2 logs" die "The backend didn't answer with its version (sudo -u $user pm2 logs $name-backend)."
} }
install_events_nginx() { install_events_nginx() {
@@ -230,6 +293,7 @@ server {
client_max_body_size 25m; client_max_body_size 25m;
# The API, uploaded files, health and API docs go to the backend; everything else to the site. # The API, uploaded files, health and API docs go to the backend; everything else to the site.
# (From Events 2.2.0 everything is under /api; the other paths are for older releases.)
location ~ ^/(api|uploads|health|docs)(/|\$) { location ~ ^/(api|uploads|health|docs)(/|\$) {
proxy_pass http://127.0.0.1:$api; proxy_pass http://127.0.0.1:$api;
proxy_set_header Host \$host; proxy_set_header Host \$host;
+204
View File
@@ -0,0 +1,204 @@
# shellcheck shell=bash
# crosscode restart / export / uninstall / purge (docs/AGENT-1.0-PLAN.md §4 and §9).
CROSSCODE_REMOVED="${CROSSCODE_REMOVED:-$CROSSCODE_SITES/.removed}"
manage_usage() {
cat <<'EOF'
crosscode restart <church> [events] reload the products' PM2 processes
crosscode export <church> --out <file.zip> the church's data (crosscode.church-export/1) and uploads
crosscode uninstall <church> --confirm <church>
final backup and uploads kept in
/var/www/sites/.removed/<church>-<date>/ (30 days),
then PM2, tunnels, database and folders removed
crosscode purge <church> --confirm <church> delete what uninstall kept
All take --json (a report at the end; the log goes to stderr).
EOF
}
# The church's products that this script can handle, in deploy order.
manage_products() {
local p
# shellcheck disable=SC2046 # PRODUCTS is a space-separated list
for p in $(in_product_order $(cenv PRODUCTS)); do
is_supported_product "$p" && echo "$p"
done
return 0
}
cmd_restart() {
local church="" p
local -a named=()
while [ $# -gt 0 ]; do
case "$1" in
-h|--help) manage_usage; return 0 ;;
-*) die "Unknown option $1 (crosscode help restart)." ;;
*) if [ -z "$church" ]; then church="$1"; else is_known_product "$1" || die "Unknown product \"$1\"."; named+=("$1"); fi; shift ;;
esac
done
[ -n "$church" ] || die "Name the church."
require_root
load_church "$church"
[ "${#named[@]}" -gt 0 ] || mapfile -t named < <(manage_products)
for p in "${named[@]}"; do
church_has_product "$p" || die "$church has no $p."
# shellcheck source=/dev/null
source "$CROSSCODE_HOME/products/$p.sh"
step "$p: reloading PM2"
"${p}_reload" "$(pvar "$p" DIR)" "$(pvar "$p" USER)"
step "$p: waiting for it to answer"
wait_for_version "$("${p}_health_url")" "$("${p}_expected_version" "$(pvar "$p" DIR)")" 60
done
}
cmd_export() {
local church="" out="" p tmp dir user bytes
while [ $# -gt 0 ]; do
case "$1" in
--out) [ $# -ge 2 ] || die "--out needs a file."; out="$2"; shift 2 ;;
-h|--help) manage_usage; return 0 ;;
-*) die "Unknown option $1 (crosscode help export)." ;;
*) [ -z "$church" ] || die "One church at a time."; church="$1"; shift ;;
esac
done
[ -n "$church" ] || die "Name the church."
[[ "$out" == /*.zip ]] || die "--out: an absolute path ending in .zip."
[ ! -e "$out" ] || die "$out exists already."
require_root
load_church "$church"
command -v zip >/dev/null || die "zip isn't installed (apt-get install zip)."
tmp="$(mktemp -d)"
chmod 755 "$tmp"
local rc
set +e
(
set -e
for p in $(manage_products); do
# shellcheck source=/dev/null
source "$CROSSCODE_HOME/products/$p.sh"
dir="$(pvar "$p" DIR)"; user="$(pvar "$p" USER)"
step "$p: exporting the data"
mkdir -p "$tmp/$p"
chown "$user:$user" "$tmp/$p"
"${p}_export" "$dir" "$user" "$tmp/$p"
step "$p: adding the uploads"
"${p}_copy_uploads" "$dir" "$tmp/$p/uploads"
done
step "Zipping"
# One product: its folder is the ZIP's top level (export.json, uploads/, README.txt).
if [ "$(manage_products | wc -l)" = 1 ]; then
(cd "$tmp/$(manage_products)" && zip -qr "$out" .)
else
(cd "$tmp" && zip -qr "$out" .)
fi
chmod 600 "$out"
)
rc=$?
set -e
rm -rf "$tmp"
[ "$rc" = 0 ] || { rm -f "$out"; die "Export of $church failed."; }
bytes="$(stat -c %s "$out")"
report_result file "$out"
report_result_json bytes "$bytes"
step "Exported $church to $out ($bytes bytes)"
}
cmd_uninstall() {
local church="" confirm="" p dir user db kept old name
while [ $# -gt 0 ]; do
case "$1" in
--confirm) [ $# -ge 2 ] || die "--confirm needs the church's name."; confirm="$2"; shift 2 ;;
-h|--help) manage_usage; return 0 ;;
-*) die "Unknown option $1 (crosscode help uninstall)." ;;
*) [ -z "$church" ] || die "One church at a time."; church="$1"; shift ;;
esac
done
[ -n "$church" ] || die "Name the church."
[ "$confirm" = "$church" ] || die "Uninstalling removes $church's sites and databases: add --confirm $church."
require_root
load_church "$church"
kept="$CROSSCODE_REMOVED/$church-$(date +%Y%m%d-%H%M%S)"
mkdir -p "$kept"
chmod 700 "$CROSSCODE_REMOVED" "$kept"
report_result kept "$kept"
# 1. Keep a final backup, the uploads and the .env files (the backup needs JWT_SECRET to read
# stored secrets) for 30 days. Nothing is removed until this has worked for every product.
for p in $(manage_products); do
# shellcheck source=/dev/null
source "$CROSSCODE_HOME/products/$p.sh"
dir="$(pvar "$p" DIR)"; user="$(pvar "$p" USER)"
step "$p: final backup"
"${p}_backup" "$dir" "$user"
step "$p: keeping the backup, uploads and settings in $kept/$p"
"${p}_keep" "$dir" "$kept/$p"
done
# 2. Remove, last product first.
local -a order
mapfile -t order < <(manage_products | tac)
for p in "${order[@]}"; do
# shellcheck source=/dev/null
source "$CROSSCODE_HOME/products/$p.sh"
dir="$(pvar "$p" DIR)"; user="$(pvar "$p" USER)"; db="$(pvar "$p" DB)"
step "$p: stopping and removing the PM2 processes"
# shellcheck disable=SC2046 # the PM2 names are a space-separated list
run_as "$user" / pm2 delete $("${p}_pm2_names") >/dev/null 2>&1 || true
run_as "$user" / pm2 save --force >/dev/null 2>&1 || true
if [ -f "$FRPC_INI" ] && frpc_has_block "$church" "$p"; then
step "$p: removing the frpc tunnels"
frpc_remove "$church" "$p"
frpc_reload
fi
name="$church-$p"
if [ -f "/etc/nginx/sites-available/$name" ]; then
step "$p: removing the nginx site"
rm -f "/etc/nginx/sites-enabled/$name" "/etc/nginx/sites-available/$name"
if nginx -t >/dev/null 2>&1; then systemctl reload nginx; else warn "nginx didn't reload: check nginx -t."; fi
fi
if [ -n "$db" ]; then
step "$p: dropping the database $db"
db_drop "$db"
fi
old="$(pvar "$p" OLD_DIR)"
if [ -n "$old" ] && [ -d "$old" ]; then rm -rf -- "$old"; info "Removed the old folder $old."; fi
done
step "Removing $(church_dir "$church")"
user="$(pvar events USER)"
rm -rf -- "$(church_dir "$church")"
# The app user goes too when crosscode made it for this church (cc-<church>).
if [ "$user" = "cc-$church" ] && id "$user" >/dev/null 2>&1; then
pm2 unstartup systemd -u "$user" >/dev/null 2>&1 || true
pkill -u "$user" 2>/dev/null || true
userdel -r "$user" >/dev/null 2>&1 || warn "The user $user couldn't be removed (userdel -r $user)."
fi
step "Uninstalled $church. Kept until purged (30 days): $kept"
}
cmd_purge() {
local church="" confirm="" d n=0
while [ $# -gt 0 ]; do
case "$1" in
--confirm) [ $# -ge 2 ] || die "--confirm needs the church's name."; confirm="$2"; shift 2 ;;
-h|--help) manage_usage; return 0 ;;
-*) die "Unknown option $1 (crosscode help purge)." ;;
*) [ -z "$church" ] || die "One church at a time."; church="$1"; shift ;;
esac
done
[ -n "$church" ] || die "Name the church."
valid_slug "$church" || die "\"$church\" isn't a church name."
[ "$confirm" = "$church" ] || die "Purging deletes $church's last backup for good: add --confirm $church."
require_root
[ ! -f "$(church_env_path "$church")" ] || die "$church is still installed here; purge only deletes what uninstall kept."
for d in "$CROSSCODE_REMOVED/$church"-*; do
[ -d "$d" ] || continue
[[ "$(basename "$d")" =~ ^${church}-[0-9]{8}-[0-9]{6}$ ]] || continue
step "Deleting $d"
rm -rf -- "$d"
n=$((n + 1))
done
report_result_json deleted "$n"
[ "$n" -gt 0 ] || info "Nothing kept for $church."
}
+78
View File
@@ -0,0 +1,78 @@
# shellcheck shell=bash
# --json reports (docs/AGENT-1.0-PLAN.md §4): with --json, every command prints one
# crosscode.script-report/1 object on stdout when it ends, and its usual output goes to stderr.
# The agent reads the object, adds the log lines and sends it to the Hub.
#
# What goes in: each step() as a step (the next step, or the end, closes it), report_result
# key/values, report_status (rolled-back), and the last die() message. Only names and outcomes:
# never values from .env files.
REPORT_FILE=""
REPORT_COMMAND=""
REPORT_STARTED=""
now_iso() { date -u +%Y-%m-%dT%H:%M:%S.000Z; }
# report_init <command>: starts collecting (only with --json).
report_init() {
REPORT_FILE="$(mktemp "${TMPDIR:-/tmp}/crosscode-report.XXXXXX")"
REPORT_COMMAND="$1"
REPORT_STARTED="$(now_iso)"
}
# report_line <kind> <fields…>: one tab-separated line (tabs and newlines in fields become spaces).
report_line() {
[ -n "$REPORT_FILE" ] || return 0
local out="$1" f
shift
for f in "$@"; do
f="${f//$'\t'/ }"
out+=$'\t'"${f//$'\n'/ }"
done
printf '%s\n' "$out" >> "$REPORT_FILE"
}
# report_result <key> <value>: a string. report_result_json <key> <json>: any JSON value.
report_result() { report_line result "$1" "$2"; }
report_result_json() { report_line resultjson "$1" "$2"; }
# report_status rolled-back: how the command ended, when it isn't just succeeded / failed.
report_status() { report_line status "$1"; }
# report_step_outcome <outcome> [detail]: the outcome of the step that's running (default ok).
report_step_outcome() { report_line outcome "$1" "${2:-}"; }
# report_emit <exit code>: prints the report on fd 3 (the real stdout) and removes the file.
report_emit() {
[ -n "$REPORT_FILE" ] || return 0
node -e '
const fs = require("fs");
const [file, command, startedAt, code] = process.argv.slice(1);
const finishedAt = new Date().toISOString().replace(/\.\d{3}Z$/, ".000Z");
const steps = [], result = {};
let status = null, message;
for (const line of fs.readFileSync(file, "utf8").split("\n")) {
if (!line) continue;
const [kind, a = "", b = ""] = line.split("\t");
if (kind === "step") {
const last = steps[steps.length - 1];
if (last && !last.finishedAt) last.finishedAt = a;
steps.push({ name: b.slice(0, 200), startedAt: a, finishedAt: null, outcome: "ok" });
} else if (kind === "outcome" && steps.length) {
const last = steps[steps.length - 1];
last.outcome = a;
if (b) last.detail = b.slice(0, 2000);
} else if (kind === "result") result[a] = b;
else if (kind === "resultjson") { try { result[a] = JSON.parse(b); } catch { result[a] = b; } }
else if (kind === "status") status = a;
else if (kind === "message") message = a.slice(0, 2000);
}
const last = steps[steps.length - 1];
if (last && !last.finishedAt) last.finishedAt = finishedAt;
const failed = code !== "0";
if (failed && last && last.outcome === "ok") last.outcome = "failed";
const out = { schema: "crosscode.script-report/1", command, status: status || (failed ? "failed" : "succeeded"), startedAt, finishedAt, steps, result };
if (message) out.message = message;
process.stdout.write(JSON.stringify(out) + "\n");
' "$REPORT_FILE" "$REPORT_COMMAND" "$REPORT_STARTED" "$1" >&3 || true
rm -f "$REPORT_FILE"
REPORT_FILE=""
}
+12
View File
@@ -31,20 +31,28 @@ cmd_status() {
[ "${#churches[@]}" -gt 0 ] || { info "No churches on this server ($CROSSCODE_SITES/*/church.env)."; return 0; } [ "${#churches[@]}" -gt 0 ] || { info "No churches on this server ($CROSSCODE_SITES/*/church.env)."; return 0; }
fi fi
local c local c
STATUS_JSON=""
for c in "${churches[@]}"; do status_church "$c" "$fetch"; done for c in "${churches[@]}"; do status_church "$c" "$fetch"; done
report_result_json churches "[$STATUS_JSON]"
} }
STATUS_JSON=""
# jstr <text>: a JSON string.
jstr() { node -e 'process.stdout.write(JSON.stringify(process.argv[1]))' "$1"; }
status_church() { status_church() {
local church="$1" fetch="$2" p dir user ref latest url json version db jobs backup line local church="$1" fetch="$2" p dir user ref latest url json version db jobs backup line
load_church "$church" load_church "$church"
echo echo
step "$church ($(cenv ADDRESS '(no address)'))" step "$church ($(cenv ADDRESS '(no address)'))"
local pj="" hj
# shellcheck disable=SC2046 # PRODUCTS is a space-separated list of names # shellcheck disable=SC2046 # PRODUCTS is a space-separated list of names
for p in $(in_product_order $(cenv PRODUCTS)); do for p in $(in_product_order $(cenv PRODUCTS)); do
dir="$(pvar "$p" DIR)" dir="$(pvar "$p" DIR)"
user="$(pvar "$p" USER)" user="$(pvar "$p" USER)"
if [ ! -d "$dir/.git" ]; then if [ ! -d "$dir/.git" ]; then
info "$(printf '%-9s %s' "$p" "missing checkout: $dir")" info "$(printf '%-9s %s' "$p" "missing checkout: $dir")"
pj+="${pj:+,}{\"product\":\"$p\",\"ref\":null,\"latest\":null,\"health\":null,\"problem\":\"missing checkout\"}"
continue continue
fi fi
if [ "$fetch" = 1 ]; then if [ "$fetch" = 1 ]; then
@@ -56,6 +64,7 @@ status_church() {
[ -n "$latest" ] && [ "$ref" != "$latest" ] && line="$line (update available)" [ -n "$latest" ] && [ "$ref" != "$latest" ] && line="$line (update available)"
info "$line" info "$line"
hj=null
if is_supported_product "$p"; then if is_supported_product "$p"; then
# shellcheck source=/dev/null # shellcheck source=/dev/null
source "$CROSSCODE_HOME/products/$p.sh" source "$CROSSCODE_HOME/products/$p.sh"
@@ -66,12 +75,15 @@ status_church() {
jobs="$(printf '%s' "$json" | json_get jobs)" jobs="$(printf '%s' "$json" | json_get jobs)"
backup="$(printf '%s' "$json" | json_get lastBackupAt)" backup="$(printf '%s' "$json" | json_get lastBackupAt)"
info " health: version ${version:-?}, database ${db:-?}, jobs ${jobs:-?}, last backup ${backup:-never}" info " health: version ${version:-?}, database ${db:-?}, jobs ${jobs:-?}, last backup ${backup:-never}"
hj="$json"
else else
warn " $p isn't answering on $url (pm2 status; pm2 logs)." warn " $p isn't answering on $url (pm2 status; pm2 logs)."
fi fi
fi fi
pj+="${pj:+,}{\"product\":\"$p\",\"ref\":$(jstr "$ref"),\"latest\":$(jstr "$latest"),\"health\":$hj,\"ports\":$(jstr "$(pvar "$p" PORTS)"),\"domain\":$(jstr "$(pvar "$p" DOMAIN)")}"
status_old_folder "$p" status_old_folder "$p"
done done
STATUS_JSON+="${STATUS_JSON:+,}{\"church\":\"$church\",\"address\":$(jstr "$(cenv ADDRESS)"),\"products\":[$pj]}"
} }
# After `adopt`, the old folder is kept for 14 days so `adopt --undo` can go back to it. # After `adopt`, the old folder is kept for 14 days so `adopt --undo` can go back to it.
+28
View File
@@ -0,0 +1,28 @@
{
"schema": "crosscode.env-template/1",
"product": "events",
"files": {
"backend/.env": [
{ "name": "NODE_ENV", "layer": "generated", "required": true, "description": "production" },
{ "name": "PORT", "layer": "install", "from": "{apiPort}", "required": true, "description": "The backend's port (from the Hub's port list)" },
{ "name": "DATABASE_URL", "layer": "generated", "required": true, "description": "Made on the server with a new password; never leaves it" },
{ "name": "JWT_SECRET", "layer": "generated", "required": true, "description": "Also encrypts stored secrets: never change it" },
{ "name": "FRONTEND_URL", "layer": "install", "from": "https://{domain}", "required": true },
{ "name": "APP_BASE_URL", "layer": "install", "from": "https://{domain}", "required": true },
{ "name": "BACKEND_URL", "layer": "install", "from": "https://{domain}", "required": true, "description": "The same address: the backend is under /api (2.2.0+)" },
{ "name": "TRUST_PROXY", "layer": "server", "required": true, "description": "Behind frp: loopback" },
{ "name": "APP_TIMEZONE", "layer": "install", "default": "Africa/Johannesburg" },
{ "name": "HUB_URL", "layer": "defaults", "default": "https://hub.crosscode.co.za" },
{ "name": "HUB_HEARTBEAT_ENABLED", "layer": "defaults", "default": "true" },
{ "name": "HUB_LICENCE_KEY", "layer": "install", "description": "With it the site registers with the Hub by itself when it first starts" },
{ "name": "HUB_ADMIN_NAME", "layer": "install", "description": "The site admin, sent to the Hub on registering" },
{ "name": "HUB_ADMIN_EMAIL", "layer": "install" },
{ "name": "BACKUP_ENABLED", "layer": "defaults", "default": "true" },
{ "name": "DAILY_SUMMARY_ENABLED", "layer": "defaults", "default": "true" },
{ "name": "SENTRY_DSN", "layer": "defaults" }
],
"frontend/.env.production": [
{ "name": "NEXT_PUBLIC_API_URL", "layer": "install", "from": "https://{domain}", "required": true }
]
}
}
+37
View File
@@ -77,3 +77,40 @@ events_reload() {
events_ensure_pm2_config "$1" "$2" events_ensure_pm2_config "$1" "$2"
run_as "$2" "$(events_app "$1")" pm2 reload ecosystem.config.js --update-env run_as "$2" "$(events_app "$1")" pm2 reload ecosystem.config.js --update-env
} }
# The church's data as crosscode.church-export/1 into <out dir> (owned by the app user). Releases
# from before the export (events@2.2.0) don't have the script.
events_export() {
local dir="$1" user="$2" out="$3" app
app="$(events_app "$dir")"
[ -f "$app/backend/scripts/export-church.ts" ] ||
die "This release of Events ($(checkout_ref "$dir" events)) has no export yet. Update it first."
run_as "$user" "$app/backend" npx tsx scripts/export-church.ts --out-dir "$out"
}
# Uploaded files into <dest> (WhatsApp ticket PDFs waiting to be sent are left out).
events_copy_uploads() {
local src
src="$(events_app "$1")/backend/public/uploads"
mkdir -p "$2"
[ -d "$src" ] || return 0
rsync -a --exclude tickets-temp/ "$src/" "$2/"
}
# What uninstall keeps for 30 days in <dest>: the backups (the last is the final one), the
# uploads, and the .env files (the backup's stored secrets are encrypted with its JWT_SECRET).
events_keep() {
local app dest="$2"
app="$(events_app "$1")"
mkdir -p "$dest"
[ ! -d "$app/backend/backups" ] || rsync -a "$app/backend/backups/" "$dest/backups/"
[ ! -d "$app/backend/public/uploads" ] || tar -czf "$dest/uploads.tar.gz" -C "$app/backend/public" uploads
cp -p "$app/backend/.env" "$dest/backend.env" 2>/dev/null || true
cp -p "$app/frontend/.env.production" "$dest/frontend.env.production" 2>/dev/null || true
chmod -R go-rwx "$dest"
}
# --var arguments for the env template (envtpl.js), from church.env.
events_env_vars() {
printf '%s\n' --var "domain=$(pvar events DOMAIN)" --var "apiPort=$(events_api_port)" --var "webPort=$(events_web_port)" --var "church=$(cenv CHURCH)"
}
+38
View File
@@ -209,3 +209,41 @@ cc() { bash "$CROSSCODE_HOME/crosscode" "$@"; }
[ "$status" -eq 1 ] [ "$status" -eq 1 ]
[[ "$output" == *'No church "nowhere"'* ]] [[ "$output" == *'No church "nowhere"'* ]]
} }
@test "deploy --json: a rolled-back release is reported as rolled-back, with each product's result" {
out="$(FAIL_HEALTH=2.2.1 cc deploy hope events --version events@2.2.1 --json 2>/dev/null || true)"
node -e '
const r = JSON.parse(process.argv[1]);
if (r.schema !== "crosscode.script-report/1" || r.status !== "rolled-back") throw new Error(r.status);
if (r.result.products.events.to !== "events@2.2.1" || r.result.products.events.from !== "events@2.2.0") throw new Error("products");
if (!r.steps.some(s => s.name.includes("reloading PM2"))) throw new Error("steps");' "$out"
[ "$(on hope)" = events@2.2.0 ]
}
@test "a release with a new required setting and no value for it stops at the checks" {
echo 'events_env_vars() { printf "%s\n" --var domain=events.hope.co.za --var apiPort=5000; }' >> "$CROSSCODE_HOME/products/events.sh"
mkdir -p "$CROSSCODE_SITES/hope/events/apps/events/backend"
printf 'DATABASE_URL=x\n' > "$CROSSCODE_SITES/hope/events/apps/events/backend/.env"
# As in the real repo, .env files are ignored by git.
echo .env >> "$CROSSCODE_SITES/hope/events/.git/info/exclude"
cat > "$TMP/origin/apps/events/env.template.json" <<'TPL'
{ "schema": "crosscode.env-template/1", "product": "events", "files": { "backend/.env": [
{ "name": "DATABASE_URL", "layer": "generated", "required": true },
{ "name": "NEW_SERVER_SETTING", "layer": "server", "required": true },
{ "name": "NEW_DEFAULT", "layer": "defaults", "default": "on" } ] } }
TPL
release 2.2.2 "## 2.2.2"
run cc deploy hope events --version events@2.2.2
[ "$status" -eq 1 ]
[[ "$output" == *"NEW_SERVER_SETTING (server, backend/.env)"* ]]
[[ "$output" == *"Nothing was changed"* ]]
[ "$(on hope)" = events@2.2.0 ]
echo '{ "server": { "NEW_SERVER_SETTING": "yes" } }' > "$TMP/config.json"
run cc deploy hope events --version events@2.2.2 --config "$TMP/config.json"
[ "$status" -eq 0 ]
[ "$(on hope)" = events@2.2.2 ]
grep -qx 'DATABASE_URL=x' "$CROSSCODE_SITES/hope/events/apps/events/backend/.env"
grep -qx 'NEW_SERVER_SETTING=yes' "$CROSSCODE_SITES/hope/events/apps/events/backend/.env"
grep -qx 'NEW_DEFAULT=on' "$CROSSCODE_SITES/hope/events/apps/events/backend/.env"
}
+98
View File
@@ -0,0 +1,98 @@
#!/usr/bin/env bats
# .env files from a product's env template (lib/envtpl.js, AGENT-1.0-PLAN §5): written whole on
# install, only appended to on deploy, nothing written when a required value is missing.
setup() {
TMP="$BATS_TEST_TMPDIR"
TPL_JS="$BATS_TEST_DIRNAME/../lib/envtpl.js"
APP="$TMP/app"
mkdir -p "$APP/backend" "$APP/frontend"
cat > "$TMP/template.json" <<'EOF'
{ "schema": "crosscode.env-template/1", "product": "events", "files": {
"backend/.env": [
{ "name": "DATABASE_URL", "layer": "generated", "required": true },
{ "name": "PORT", "layer": "install", "from": "{apiPort}", "required": true },
{ "name": "FRONTEND_URL", "layer": "install", "from": "https://{domain}", "required": true },
{ "name": "TRUST_PROXY", "layer": "server", "required": true },
{ "name": "HUB_URL", "layer": "defaults", "default": "https://hub.crosscode.co.za" },
{ "name": "SENTRY_DSN", "layer": "defaults" },
{ "name": "HUB_LICENCE_KEY", "layer": "install" }
],
"frontend/.env.production": [
{ "name": "NEXT_PUBLIC_API_URL", "layer": "install", "from": "https://{domain}", "required": true }
] } }
EOF
cat > "$TMP/config.json" <<'EOF'
{ "server": { "TRUST_PROXY": "loopback" }, "defaults": {}, "install": { "events": { "HUB_LICENCE_KEY": "CEH-AAAA-BBBB" } } }
EOF
export CROSSCODE_GEN_DATABASE_URL='postgresql://hope_events:s3cr3t@localhost:5432/hope_events'
}
envtpl() { node "$TPL_JS" --template "$TMP/template.json" --dir "$APP" --product events --var domain=events.hope.co.za --var apiPort=20011 "$@"; }
@test "install: writes every file from the layers, mode 600, values never printed" {
run envtpl --config "$TMP/config.json" --release events@2.3.0
[ "$status" -eq 0 ]
[[ "$output" == *"backend/.env: wrote DATABASE_URL, PORT, FRONTEND_URL, TRUST_PROXY, HUB_URL, HUB_LICENCE_KEY"* ]]
[[ "$output" != *"s3cr3t"* && "$output" != *"CEH-AAAA"* ]]
grep -qx 'DATABASE_URL=postgresql://hope_events:s3cr3t@localhost:5432/hope_events' "$APP/backend/.env"
grep -qx 'PORT=20011' "$APP/backend/.env"
grep -qx 'FRONTEND_URL=https://events.hope.co.za' "$APP/backend/.env"
grep -qx 'TRUST_PROXY=loopback' "$APP/backend/.env"
grep -qx 'HUB_URL=https://hub.crosscode.co.za' "$APP/backend/.env"
! grep -q SENTRY_DSN "$APP/backend/.env"
grep -qx 'NEXT_PUBLIC_API_URL=https://events.hope.co.za' "$APP/frontend/.env.production"
# File modes only mean something on Linux (the servers); Windows file systems ignore them.
[ "$(uname -s)" != Linux ] || [ "$(stat -c %a "$APP/backend/.env")" = 600 ]
}
@test "a required value missing anywhere: nothing is written" {
run envtpl --release events@2.3.0
[ "$status" -eq 3 ]
[[ "$output" == *"TRUST_PROXY (server, backend/.env)"* ]]
[ ! -e "$APP/backend/.env" ]
[ ! -e "$APP/frontend/.env.production" ]
}
@test "--fallback fills a value no layer gives" {
run envtpl --fallback TRUST_PROXY=1
[ "$status" -eq 0 ]
grep -qx 'TRUST_PROXY=1' "$APP/backend/.env"
}
@test "deploy: only new variables are appended; existing lines stay byte for byte" {
printf '# by hand\nDATABASE_URL=postgresql://keep\nPORT=5000\nFRONTEND_URL="https://old.example"\nTRUST_PROXY=1\nCUSTOM=x\n' > "$APP/backend/.env"
cp "$APP/backend/.env" "$TMP/before"
echo 'NEXT_PUBLIC_API_URL=https://old.example' > "$APP/frontend/.env.production"
run envtpl --config "$TMP/config.json" --release events@2.4.0
[ "$status" -eq 0 ]
[[ "$output" == *"backend/.env: added HUB_URL, HUB_LICENCE_KEY"* ]]
[[ "$output" != *"frontend"* ]]
head -c "$(stat -c %s "$TMP/before")" "$APP/backend/.env" | cmp - "$TMP/before"
grep -q '^# Added by crosscode .* (events@2.4.0)$' "$APP/backend/.env"
grep -qx 'HUB_URL=https://hub.crosscode.co.za' "$APP/backend/.env"
[ "$(grep -c '^PORT=' "$APP/backend/.env")" = 1 ]
}
@test "--check says what would be added and changes nothing" {
printf 'DATABASE_URL=x\nPORT=1\nFRONTEND_URL=y\nTRUST_PROXY=1\n' > "$APP/backend/.env"
cp "$APP/backend/.env" "$TMP/before"
run envtpl --check
[ "$status" -eq 0 ]
[[ "$output" == *"backend/.env: would add HUB_URL"* ]]
cmp "$APP/backend/.env" "$TMP/before"
[ ! -e "$APP/frontend/.env.production" ]
}
@test "values with spaces or quotes are quoted the way dotenv reads them" {
run node -e 'const t = require(process.argv[1]); console.log(t.line("A", "plain/value:1")); console.log(t.line("B", "two words \"q\""))' "$TPL_JS"
[ "${lines[0]}" = 'A=plain/value:1' ]
[ "${lines[1]}" = 'B="two words \"q\""' ]
}
@test "a file name that leaves the product folder is refused" {
echo '{ "schema": "crosscode.env-template/1", "product": "events", "files": { "../evil": [ { "name": "A", "layer": "defaults", "default": "1" } ] } }' > "$TMP/bad.json"
run node "$TPL_JS" --template "$TMP/bad.json" --dir "$APP"
[ "$status" -ne 0 ]
[ ! -e "$TMP/evil" ]
}
+105
View File
@@ -0,0 +1,105 @@
#!/usr/bin/env bats
# frpc.ini blocks (lib/frpc.sh, AGENT-1.0-PLAN §6): only appended, removed by their markers,
# everything written by hand unchanged byte for byte, nothing changed when `frpc verify` refuses.
setup() {
TMP="$BATS_TEST_TMPDIR"
# shellcheck source=../lib/common.sh
source "$BATS_TEST_DIRNAME/../lib/common.sh"
# shellcheck source=../lib/frpc.sh
source "$BATS_TEST_DIRNAME/../lib/frpc.sh"
FRPC_INI="$TMP/frp/frpc.ini"
FRPC_BACKUPS="$TMP/frp/backups"
mkdir -p "$TMP/frp" "$TMP/bin"
# A stub frpc: verify fails when FRPC_REFUSE is set, reload logs.
cat > "$TMP/bin/frpc" <<'EOF'
#!/usr/bin/env bash
case "$1" in
verify) [ -z "${FRPC_REFUSE:-}" ] ;;
reload) echo reload >> "$(dirname "$3")/reloads" ;;
esac
EOF
chmod +x "$TMP/bin/frpc"
FRPC_BIN="$TMP/bin/frpc"
cat > "$FRPC_INI" <<'EOF'
[common]
server_addr = 203.0.113.10
server_port = 7000
token = hand-written
# the church's own tunnel, written by hand
[events-frontend]
type = tcp
local_ip = 127.0.0.1
local_port = 3000
remote_port = 3000
EOF
cp "$FRPC_INI" "$TMP/original"
}
@test "adds a marked block at the end, and the admin lines to [common] once" {
frpc_add hope events 20010 20011
grep -qx '# >>> crosscode:hope:events (added by crosscode .*' "$FRPC_INI" || grep -q '^# >>> crosscode:hope:events' "$FRPC_INI"
grep -qx '\[hope-events-web\]' "$FRPC_INI"
grep -qx 'remote_port = 20011' "$FRPC_INI"
[ "$(grep -c '^admin_addr = 127.0.0.1$' "$FRPC_INI")" = 1 ]
[ "$(sed -n 2p "$FRPC_INI")" = "admin_addr = 127.0.0.1" ]
frpc_add bridge events 20020 20021
[ "$(grep -c '^admin_addr' "$FRPC_INI")" = 1 ]
[ "$(frpc_ports hope events)" = "20010 20011" ]
[ "$(ls "$FRPC_BACKUPS" | wc -l)" -ge 1 ]
}
@test "the hand-written lines stay exactly as they were" {
frpc_add hope events 20010 20011
# Everything except the two admin lines and the appended block is the original.
grep -v '^admin_' "$FRPC_INI" | head -n "$(wc -l < "$TMP/original")" | cmp - "$TMP/original"
}
@test "removing one block leaves the other blocks and the hand-written lines untouched" {
frpc_add hope events 20010 20011
cp "$FRPC_INI" "$TMP/with-hope"
frpc_add bridge events 20020 20021
frpc_remove bridge events
cmp "$FRPC_INI" "$TMP/with-hope"
frpc_remove hope events
grep -v '^admin_' "$FRPC_INI" | cmp - "$TMP/original"
}
@test "adding the same block twice changes nothing" {
frpc_add hope events 20010 20011
cp "$FRPC_INI" "$TMP/once"
run frpc_add hope events 20010 20011
[ "$status" -eq 0 ]
cmp "$FRPC_INI" "$TMP/once"
}
@test "a port or tunnel name that's already used is refused" {
run frpc_add other events 3000 20031
[ "$status" -eq 1 ]
[[ "$output" == *"port 3000 or 20031 is already used"* ]]
cmp "$FRPC_INI" "$TMP/original"
}
@test "a failed frpc verify leaves the file untouched" {
FRPC_REFUSE=1
export FRPC_REFUSE
run frpc_add hope events 20010 20011
[ "$status" -eq 1 ]
[[ "$output" == *"frpc verify refused"* ]]
cmp "$FRPC_INI" "$TMP/original"
[ -z "$(find "$TMP/frp" -name '.frpc.ini.*')" ]
}
@test "an admin_addr that's already set is left alone" {
sed -i 's/^server_port = 7000$/server_port = 7000\nadmin_addr = 0.0.0.0/' "$FRPC_INI"
frpc_add hope events 20010 20011
[ "$(grep -c '^admin_addr' "$FRPC_INI")" = 1 ]
grep -qx 'admin_addr = 0.0.0.0' "$FRPC_INI"
}
@test "a file without a final newline keeps its last line" {
printf '[common]\nserver_addr = x' > "$FRPC_INI"
frpc_add hope events 20010 20011
grep -qx 'server_addr = x' "$FRPC_INI"
}
+69
View File
@@ -0,0 +1,69 @@
#!/usr/bin/env bats
# --json reports (lib/report.sh) and the argument checks of the new commands (what needs a server
# is tested on a VM).
bats_require_minimum_version 1.5.0
setup() {
export CROSSCODE_NO_UPDATE=1
CC="$BATS_TEST_DIRNAME/../crosscode"
}
json() { node -e 'let s="";process.stdin.on("data",d=>s+=d).on("end",()=>{const v=process.argv[1].split(".").reduce((o,k)=>o==null?o:o[k],JSON.parse(s));process.stdout.write(typeof v==="object"?JSON.stringify(v):String(v))})' "$1"; }
@test "--json: one report on stdout, the log on stderr" {
run --separate-stderr bash "$CC" help --json
[ "$status" -eq 0 ]
[ "$(printf '%s' "$output" | json schema)" = "crosscode.script-report/1" ]
[ "$(printf '%s' "$output" | json status)" = "succeeded" ]
[ "$(printf '%s' "$output" | json command)" = "help" ]
[[ "$stderr" == *"crosscode deploy <church>"* ]]
}
@test "--json on a failure: status failed and the reason" {
run --separate-stderr bash "$CC" uninstall hope --json
[ "$status" -eq 1 ]
[ "$(printf '%s' "$output" | json status)" = "failed" ]
[[ "$(printf '%s' "$output" | json message)" == *"--confirm hope"* ]]
}
@test "--config must be an existing JSON file" {
run bash "$CC" status --config /nonexistent.json
[ "$status" -eq 1 ]
[[ "$output" == *"no file /nonexistent.json"* ]]
echo 'not json' > "$BATS_TEST_TMPDIR/c.json"
run bash "$CC" status --config "$BATS_TEST_TMPDIR/c.json"
[ "$status" -eq 1 ]
[[ "$output" == *"isn't JSON"* ]]
}
@test "uninstall and purge need --confirm with the church's own name" {
run bash "$CC" uninstall hope
[ "$status" -eq 1 ]
[[ "$output" == *"add --confirm hope"* ]]
run bash "$CC" uninstall hope --confirm other
[ "$status" -eq 1 ]
run bash "$CC" purge hope
[ "$status" -eq 1 ]
[[ "$output" == *"add --confirm hope"* ]]
}
@test "export needs an absolute .zip path" {
run bash "$CC" export hope --out relative.zip
[ "$status" -eq 1 ]
[[ "$output" == *"absolute path ending in .zip"* ]]
}
@test "install --frp needs the ports from the Hub" {
run bash "$CC" install hope --products events --domain hope.co.za --frp
[ "$status" -eq 1 ]
[[ "$output" == *"--frp needs --api-port and --web-port"* ]]
}
@test "help lists the new commands" {
run bash "$CC" help
[[ "$output" == *"crosscode uninstall <church> --confirm <church>"* ]]
[[ "$output" == *"--json"* ]]
run bash "$CC" help export
[[ "$output" == *"crosscode.church-export/1"* ]]
}