Events 3.4.0, Accounts 1.4.0, contracts 1.12.0, agent 1.3.0: jobs from the dashboard, styled Accounts emails, .env from the Hub

- Events and Accounts: Background jobs are switched on or off and rescheduled from the admin;
  the choice is kept in the database (job_overrides), wins over the .env and applies at once.
  Accounts gets an Admin -> Background jobs page and per-job env flags, as Events.
- Accounts: every email styled as Cross Events' (church colour, logo, button, footer), same
  plain text (src/lib/emails.ts).
- Contracts 1.12.0: env-read / env-set agent jobs, envReadResultSchema, isSecretEnvName.
- crosscode script: `crosscode env <church> <product>` reads the .env (secrets hidden) and, with
  --set/--unset/--restart, changes one file, rebuilds the frontend when its file changed, reloads,
  and puts the file back if the site doesn't answer (lib/envedit.cjs, tests/envedit.bats).
- Agent 1.3.0: runs env-read and env-set.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-20 12:14:02 +02:00
co-authored by Claude Opus 5.5
parent a85347bff8
commit bbcafe61b9
43 changed files with 2276 additions and 131 deletions
+96
View File
@@ -10,6 +10,11 @@ crosscode demo <church> --profile <profile> [--admin-email <email>]
replace Cross Events' data with a demo set
(small, events, ministry, multicampus); a backup
is taken first, one admin account is kept
crosscode env <church> <product> the product's .env values (secrets hidden)
crosscode env <church> <product> [--file backend/.env] --set NAME=value … [--unset NAME …] [--restart]
change values (a copy is kept; never secrets or
generated values). The frontend's file rebuilds it.
If it doesn't start again, the file is put back.
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
@@ -102,6 +107,97 @@ cmd_demo() {
die "Events didn't answer after the reload (sudo -u $user pm2 logs)."
}
# crosscode env <church> <product>: the product's .env values, secrets hidden (the Hub's env-read).
# With --set / --unset (the Hub's env-set): changes one file (--file, default backend/.env), keeping
# a copy first; when the frontend's file changed, the frontend is rebuilt (Next.js builds its values
# in); then, with --restart or after a rebuild, the product is reloaded and must answer /health.
# Anything failing after the change puts the file back (and rebuilds again if it had rebuilt).
ENV_BACKUPS_KEPT=5
cmd_env() {
local church="" product="" file="backend/.env" restart=0 dir user app tpl path backup stamp out rc=0 rebuilt=0
local -a changes=()
while [ $# -gt 0 ]; do
case "$1" in
--file) [ $# -ge 2 ] || die "--file needs a value."; file="$2"; shift 2 ;;
--set|--unset) [ $# -ge 2 ] || die "$1 needs a value."; changes+=("$1" "$2"); shift 2 ;;
--restart) restart=1; shift ;;
-h|--help) manage_usage; return 0 ;;
-*) die "Unknown option $1 (crosscode help env)." ;;
*) if [ -z "$church" ]; then church="$1"; elif [ -z "$product" ]; then product="$1"; else die "Name one church and one product."; fi; shift ;;
esac
done
[ -n "$church" ] && [ -n "$product" ] || die "Name the church and the product (crosscode env hope events)."
[[ "$file" =~ ^(backend|frontend)/\.env(\.production)?$ ]] || die "--file: backend/.env or frontend/.env.production."
require_root
load_church "$church"
church_has_product "$product" || die "$church has no $product."
# shellcheck source=/dev/null
source "$CROSSCODE_HOME/products/$product.sh"
dir="$(pvar "$product" DIR)"
user="$(pvar "$product" USER)"
app="$("${product}_app" "$dir")"
tpl="$(env_template "$app" "$product")"
if [ "${#changes[@]}" -eq 0 ]; then
step "$product: reading its .env files"
out="$(node "$CROSSCODE_HOME/lib/envedit.cjs" read --template "$tpl" --dir "$app" --product "$product")" || die "Couldn't read $product's .env files."
report_result_json env "$out"
# shellcheck disable=SC2016 # JavaScript template strings, not shell
[ "$CROSSCODE_JSON" = 1 ] || printf '%s\n' "$out" | node -e '
const r = JSON.parse(require("fs").readFileSync(0, "utf8"));
for (const f of r.files) {
console.log(`${f.file}${f.exists ? "" : " (missing)"}`);
for (const v of f.variables) console.log(` ${v.name}=${v.secret ? (v.set ? "(secret, set)" : "(secret, not set)") : (v.set ? v.value : "(not set)")}`);
}' >&2
return 0
fi
path="$app/$file"
[ -f "$path" ] || die "$church has no $product $file."
stamp="$(date -u +%Y%m%d%H%M%S)"
backup="$path.before-$stamp"
step "$product: changing $file (a copy is kept as $(basename "$backup"))"
out="$(node "$CROSSCODE_HOME/lib/envedit.cjs" set --template "$tpl" --dir "$app" --file "$file" --backup "$backup" "${changes[@]}")" || {
rm -f "$backup"
die "$product $file wasn't changed (see the message above)."
}
report_result_json changes "$out"
info "$out"
# Only the last few copies.
# shellcheck disable=SC2012 # names are ours: <file>.before-<digits>
ls -1t "$path".before-* 2>/dev/null | tail -n +$((ENV_BACKUPS_KEPT + 1)) | while read -r old; do rm -f -- "$old"; done
if [[ "$file" == frontend/* ]]; then
step "$product: rebuilding the frontend (it builds $file in)"
if run_as "$user" "$app/frontend" npm run build; then rebuilt=1; else rc=1; fi
fi
if [ "$rc" = 0 ] && { [ "$restart" = 1 ] || [ "$rebuilt" = 1 ]; }; then
step "$product: reloading PM2"
"${product}_reload" "$dir" "$user" || rc=1
if [ "$rc" = 0 ]; then
step "$product: waiting for it to answer"
wait_for_version "$("${product}_health_url")" "$("${product}_expected_version" "$dir")" 90 || rc=1
fi
fi
[ "$rc" = 0 ] && return 0
report_step_outcome failed
step "$product: putting $file back"
node "$CROSSCODE_HOME/lib/envedit.cjs" restore --file "$path" --backup "$backup" || die "Couldn't put $file back: the copy is $backup."
if [[ "$file" == frontend/* ]]; then
step "$product: rebuilding the frontend as it was"
run_as "$user" "$app/frontend" npm run build || die "The frontend didn't build again with the old $file: run npm run build in $app/frontend."
fi
if [ "$restart" = 1 ] || [[ "$file" == frontend/* ]]; then
step "$product: reloading PM2"
"${product}_reload" "$dir" "$user" || warn "PM2 reload failed: check pm2 status as $user."
wait_for_version "$("${product}_health_url")" "$("${product}_expected_version" "$dir")" 90 || true
fi
report_status rolled-back
die "$product didn't start with the new values, so $file was put back as it was."
}
cmd_export() {
local church="" out="" p tmp dir user bytes
while [ $# -gt 0 ]; do