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
+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"
}