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:
@@ -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() {
|
||||
|
||||
+37
-2
@@ -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 <product> <checkout> <user> <previous ref> <target ref>: 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"
|
||||
|
||||
@@ -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\""
|
||||
}
|
||||
@@ -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 };
|
||||
@@ -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."
|
||||
}
|
||||
+141
-77
@@ -1,26 +1,34 @@
|
||||
# shellcheck shell=bash
|
||||
# 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.
|
||||
# crosscode install: a new church on this server (from hope-events' scripts/install.sh 2.1.x;
|
||||
# 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() {
|
||||
cat <<'EOF'
|
||||
crosscode install <church> --products events --domain <address> [options]
|
||||
|
||||
A new church: system packages (Node 24, PostgreSQL, PM2, nginx, certbot) if missing, then per
|
||||
product an app user, database, checkout, .env, PM2 processes, nginx site and certificate.
|
||||
Writes /var/www/sites/<church>/church.env. DNS must already point here (it prints the records).
|
||||
A new church: system packages if missing, then per product an app user, database, checkout,
|
||||
.env files (from the release's env.template.json), PM2 processes, and either an nginx site with
|
||||
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)
|
||||
--domain <address> the church's address: hopefamily.co.za or hopefamily.crosscode.co.za;
|
||||
Events is served on events.<address>
|
||||
--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)
|
||||
--main the latest main (staging churches only)
|
||||
--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)
|
||||
--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)
|
||||
--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
|
||||
}
|
||||
|
||||
@@ -44,8 +52,30 @@ free_port() {
|
||||
|
||||
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() {
|
||||
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
|
||||
case "$1" in
|
||||
--products) products="$(printf '%s' "$2" | tr ',' ' ')"; shift 2 ;;
|
||||
@@ -58,6 +88,8 @@ cmd_install() {
|
||||
--api-port) api="$2"; shift 2 ;;
|
||||
--web-port) web="$2"; shift 2 ;;
|
||||
--no-ssl) ssl=0; shift ;;
|
||||
--frp) frp=1; ssl=0; shift ;;
|
||||
--keep-on-failure) keep=1; shift ;;
|
||||
-h|--help) install_usage; return 0 ;;
|
||||
-*) die "Unknown option $1 (crosscode install --help)." ;;
|
||||
*) [ -z "$church" ] || die "One church at a time."; church="$1"; shift ;;
|
||||
@@ -73,43 +105,84 @@ cmd_install() {
|
||||
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."
|
||||
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."
|
||||
[ "$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:0:32}"
|
||||
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)."
|
||||
[ ! -e "$(church_dir "$church")" ] || die "$(church_dir "$church") exists without a church.env. Move it away first."
|
||||
|
||||
local taken
|
||||
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[@]}")}"
|
||||
web="${web:-$(free_port 3000 "${taken[@]}" "$api")}"
|
||||
[[ "$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"
|
||||
step "App user ($user)"
|
||||
id "$user" >/dev/null 2>&1 || useradd --create-home --shell /bin/bash "$user"
|
||||
|
||||
CENV=()
|
||||
church_env_set CHURCH "$church"
|
||||
church_env_set PRODUCTS "events"
|
||||
church_env_set ADDRESS "$address"
|
||||
install_events "$church" "$user" "$events_domain" "$api" "$web" "$version" "$main" "$ssl" "$email"
|
||||
church_env_write "$(church_env_path "$church")"
|
||||
INSTALL_UNDO="$(mktemp)"
|
||||
local rc
|
||||
set +e
|
||||
(
|
||||
set -e
|
||||
install_system_packages "$ssl" "$frp"
|
||||
step "App user ($user)"
|
||||
if ! id "$user" >/dev/null 2>&1; then
|
||||
useradd --create-home --shell /bin/bash "$user"
|
||||
undo_add user "$user"
|
||||
fi
|
||||
CENV=()
|
||||
church_env_set CHURCH "$church"
|
||||
church_env_set PRODUCTS "events"
|
||||
church_env_set ADDRESS "$address"
|
||||
install_events "$church" "$user" "$events_domain" "$api" "$web" "$version" "$main" "$ssl" "$email" "$frp"
|
||||
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"
|
||||
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."
|
||||
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)."
|
||||
fi
|
||||
info "Open https://$events_domain/setup to create the admin account."
|
||||
info "Updates: crosscode deploy $church events"
|
||||
}
|
||||
|
||||
install_system_packages() {
|
||||
local ssl="$1"
|
||||
local ssl="$1" frp="$2"
|
||||
step "System packages"
|
||||
export DEBIAN_FRONTEND=noninteractive
|
||||
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
|
||||
# Node 24 is the LTS line supported until April 2028.
|
||||
if ! command -v node >/dev/null || [ "$(node_major)" -lt 24 ]; then
|
||||
@@ -121,32 +194,29 @@ install_system_packages() {
|
||||
}
|
||||
|
||||
install_events() {
|
||||
local church="$1" user="$2" domain="$3" api="$4" web="$5" version="$6" main="$7" ssl="$8" email="$9"
|
||||
local dir app db pass url name scheme
|
||||
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 name ref
|
||||
# shellcheck source=/dev/null
|
||||
source "$CROSSCODE_HOME/products/events.sh"
|
||||
dir="$(church_dir "$church")/events"
|
||||
app="$dir/apps/events"
|
||||
db="$(printf '%s_events' "$church" | tr '-' '_')"
|
||||
name="$church-events"
|
||||
scheme=https
|
||||
[ "$ssl" = 1 ] || scheme=http
|
||||
url="$scheme://$domain"
|
||||
church_env_set EVENTS_DIR "$dir"
|
||||
church_env_set EVENTS_DOMAIN "$domain"
|
||||
church_env_set EVENTS_PM2 "$name-backend $name-frontend"
|
||||
church_env_set EVENTS_PORTS "$api $web"
|
||||
church_env_set EVENTS_DB "$db"
|
||||
church_env_set EVENTS_USER "$user"
|
||||
[ "$frp" = 0 ] || church_env_set EVENTS_FRP 1
|
||||
|
||||
step "Events: code ($dir)"
|
||||
mkdir -p "$(church_dir "$church")"
|
||||
undo_add dir "$(church_dir "$church")"
|
||||
chmod 755 "$CROSSCODE_SITES" "$(church_dir "$church")"
|
||||
if [ ! -d "$dir/.git" ]; then
|
||||
mkdir -p "$dir"
|
||||
chown "$user:$user" "$dir"
|
||||
run_as "$user" "$dir" git clone --quiet "$CROSSCODE_REPO" .
|
||||
fi
|
||||
mkdir -p "$dir"
|
||||
chown "$user:$user" "$dir"
|
||||
run_as "$user" "$dir" git clone --quiet "$CROSSCODE_REPO" .
|
||||
run_as "$user" "$dir" git fetch --tags --quiet origin
|
||||
if [ "$main" = 1 ]; then
|
||||
version=origin/main
|
||||
@@ -155,47 +225,31 @@ install_events() {
|
||||
[ -n "$version" ] || die "No events@x.y.z release tags yet (use --version or --main)."
|
||||
fi
|
||||
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)"
|
||||
# Re-running keeps the password already in backend/.env; otherwise a new one.
|
||||
pass=""
|
||||
if [ -f "$app/backend/.env" ]; then
|
||||
pass="$(env_value "$app/backend/.env" DATABASE_URL | sed -nE 's#^postgresql://[^:]+:([^@]+)@.*#\1#p')"
|
||||
fi
|
||||
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: database ($db on $(db_host))"
|
||||
# Never reuse (or later drop) a database or role that something else may be using.
|
||||
! 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."
|
||||
pass="$(random_secret 32)"
|
||||
undo_add db "$db"
|
||||
db_create "$db" "$pass"
|
||||
|
||||
step "Events: configuration"
|
||||
if [ ! -f "$app/backend/.env" ]; then
|
||||
cat > "$app/backend/.env" <<ENV
|
||||
NODE_ENV=production
|
||||
PORT=$api
|
||||
DATABASE_URL=postgresql://$db:$pass@localhost:5432/$db
|
||||
JWT_SECRET=$(random_secret 64)
|
||||
FRONTEND_URL=$url
|
||||
APP_BASE_URL=$url
|
||||
BACKEND_URL=$url
|
||||
TRUST_PROXY=1
|
||||
APP_TIMEZONE=Africa/Johannesburg
|
||||
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"
|
||||
local trust=1
|
||||
[ "$frp" = 0 ] || trust=loopback
|
||||
(
|
||||
export CROSSCODE_GEN_NODE_ENV=production
|
||||
export CROSSCODE_GEN_JWT_SECRET
|
||||
CROSSCODE_GEN_JWT_SECRET="$(random_secret 64)"
|
||||
export CROSSCODE_GEN_DATABASE_URL="postgresql://$db:$pass@$(db_host):$(db_port)/$db"
|
||||
env_apply "$app" events "$ref" --var "domain=$domain" --var "apiPort=$api" --var "webPort=$web" --var "church=$church" \
|
||||
--fallback "TRUST_PROXY=$trust"
|
||||
) | sed 's/^/ /'
|
||||
[ "${PIPESTATUS[0]}" = 0 ] || die "The .env files couldn't be written (see above)."
|
||||
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"
|
||||
|
||||
step "Events: install, migrate, build"
|
||||
@@ -204,20 +258,29 @@ ENV
|
||||
events_build "$dir" "$user"
|
||||
|
||||
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 save
|
||||
env PATH="$PATH:/usr/bin" pm2 startup systemd -u "$user" --hp "$(getent passwd "$user" | cut -d: -f6)" >/dev/null
|
||||
|
||||
step "Events: nginx ($domain)"
|
||||
install_events_nginx "$name" "$domain" "$api" "$web"
|
||||
if [ "$ssl" = 1 ]; then
|
||||
step "Events: certificate"
|
||||
certbot --nginx -d "$domain" --non-interactive --agree-tos -m "$email" --redirect
|
||||
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)"
|
||||
install_events_nginx "$name" "$domain" "$api" "$web"
|
||||
undo_add nginx "$name"
|
||||
if [ "$ssl" = 1 ]; then
|
||||
step "Events: certificate"
|
||||
certbot --nginx -d "$domain" --non-interactive --agree-tos -m "$email" --redirect
|
||||
fi
|
||||
fi
|
||||
|
||||
step "Events: checking"
|
||||
wait_for_version "$(events_health_url)" "$(events_expected_version "$dir")" 60 ||
|
||||
warn "The backend isn't answering yet: sudo -u $user pm2 logs"
|
||||
step "Events: checking it answers"
|
||||
wait_for_version "$(events_health_url)" "$(events_expected_version "$dir")" 90 ||
|
||||
die "The backend didn't answer with its version (sudo -u $user pm2 logs $name-backend)."
|
||||
}
|
||||
|
||||
install_events_nginx() {
|
||||
@@ -230,6 +293,7 @@ server {
|
||||
client_max_body_size 25m;
|
||||
|
||||
# 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)(/|\$) {
|
||||
proxy_pass http://127.0.0.1:$api;
|
||||
proxy_set_header Host \$host;
|
||||
|
||||
@@ -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."
|
||||
}
|
||||
@@ -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=""
|
||||
}
|
||||
@@ -31,20 +31,28 @@ cmd_status() {
|
||||
[ "${#churches[@]}" -gt 0 ] || { info "No churches on this server ($CROSSCODE_SITES/*/church.env)."; return 0; }
|
||||
fi
|
||||
local c
|
||||
STATUS_JSON=""
|
||||
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() {
|
||||
local church="$1" fetch="$2" p dir user ref latest url json version db jobs backup line
|
||||
load_church "$church"
|
||||
echo
|
||||
step "$church ($(cenv ADDRESS '(no address)'))"
|
||||
local pj="" hj
|
||||
# shellcheck disable=SC2046 # PRODUCTS is a space-separated list of names
|
||||
for p in $(in_product_order $(cenv PRODUCTS)); do
|
||||
dir="$(pvar "$p" DIR)"
|
||||
user="$(pvar "$p" USER)"
|
||||
if [ ! -d "$dir/.git" ]; then
|
||||
info "$(printf '%-9s %s' "$p" "missing checkout: $dir")"
|
||||
pj+="${pj:+,}{\"product\":\"$p\",\"ref\":null,\"latest\":null,\"health\":null,\"problem\":\"missing checkout\"}"
|
||||
continue
|
||||
fi
|
||||
if [ "$fetch" = 1 ]; then
|
||||
@@ -56,6 +64,7 @@ status_church() {
|
||||
[ -n "$latest" ] && [ "$ref" != "$latest" ] && line="$line (update available)"
|
||||
info "$line"
|
||||
|
||||
hj=null
|
||||
if is_supported_product "$p"; then
|
||||
# shellcheck source=/dev/null
|
||||
source "$CROSSCODE_HOME/products/$p.sh"
|
||||
@@ -66,12 +75,15 @@ status_church() {
|
||||
jobs="$(printf '%s' "$json" | json_get jobs)"
|
||||
backup="$(printf '%s' "$json" | json_get lastBackupAt)"
|
||||
info " health: version ${version:-?}, database ${db:-?}, jobs ${jobs:-?}, last backup ${backup:-never}"
|
||||
hj="$json"
|
||||
else
|
||||
warn " $p isn't answering on $url (pm2 status; pm2 logs)."
|
||||
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"
|
||||
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.
|
||||
|
||||
Reference in New Issue
Block a user