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,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" ]
|
||||
}
|
||||
Reference in New Issue
Block a user