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:
@@ -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\""
|
||||
}
|
||||
Reference in New Issue
Block a user