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:
@@ -209,3 +209,41 @@ cc() { bash "$CROSSCODE_HOME/crosscode" "$@"; }
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$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"
|
||||
}
|
||||
|
||||
@@ -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" ]
|
||||
}
|
||||
@@ -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"
|
||||
}
|
||||
@@ -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"* ]]
|
||||
}
|
||||
Reference in New Issue
Block a user