Agent 1.5.0 + contracts 1.16.0: uninstall, export and purge one product of a church

crosscode uninstall/export/purge <church> [product ...]: with products named only those
go and the church keeps the others; naming every product it still has removes the church
(folder and app user), so the last product to go takes the church with it. The report
gives products and remaining. Uninstalling cross-events-demo's Events removed its Accounts
too, which the Hub still showed as live.

Contracts 1.16.0: optional products on export/uninstall/purge, PRODUCT_UNINSTALL_MIN_AGENT.
Agent 1.5.0 passes them to crosscode. The Website plan moves to contracts 1.17.0 / Hub 2.13.0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-23 09:17:32 +02:00
co-authored by Claude Opus 5.5
parent e04bfac208
commit 8899c97a9c
17 changed files with 364 additions and 78 deletions
+131 -48
View File
@@ -15,12 +15,18 @@ crosscode env <church> <product> [--file backend/.env] --set NAME=value … [--u
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>
crosscode export <church> [product …] --out <file.zip>
the church's data (crosscode.church-export/1) and
uploads: every product, or only those named
crosscode uninstall <church> [product …] --confirm <church>
final backup and uploads kept in
/var/www/sites/.removed/<church>-<date>/ (30 days),
then PM2, tunnels, database and folders removed
crosscode purge <church> --confirm <church> delete what uninstall kept
then PM2, tunnels, database and folders removed.
With products named only those go, and the church
keeps its others (naming every one = the church)
crosscode purge <church> [product …] --confirm <church>
delete what uninstall kept (with products named:
only what was kept of those)
All take --json (a report at the end; the log goes to stderr).
EOF
@@ -36,6 +42,21 @@ manage_products() {
return 0
}
# manage_check_named <product …>: stops unless the church (loaded) has each product named. Call it
# directly, not in $(…): die only stops the shell it runs in.
manage_check_named() {
local p
for p in "$@"; do
is_supported_product "$p" || die "Unknown product \"$p\" (this version handles: ${SUPPORTED_PRODUCTS[*]})."
church_has_product "$p" || die "$(cenv CHURCH) has no $p (its products: $(cenv PRODUCTS))."
done
}
# manage_named [product …]: those products in deploy order; none named: every product.
manage_named() {
if [ $# -gt 0 ]; then in_product_order "$@"; else manage_products; fi
}
cmd_restart() {
local church="" p
local -a named=()
@@ -198,14 +219,22 @@ cmd_env() {
die "$product didn't start with the new values, so $file was put back as it was."
}
# json_words <word …>: a JSON array of plain words (product names), e.g. ["accounts","events"].
json_words() {
local w out=""
for w in "$@"; do out+="${out:+,}\"$w\""; done
printf '[%s]' "$out"
}
cmd_export() {
local church="" out="" p tmp dir user bytes
local -a named=() products=()
while [ $# -gt 0 ]; do
case "$1" in
--out) [ $# -ge 2 ] || die "--out needs a file."; out="$2"; shift 2 ;;
-h|--help) manage_usage; return 0 ;;
-*) die "Unknown option $1 (crosscode help export)." ;;
*) [ -z "$church" ] || die "One church at a time."; church="$1"; shift ;;
*) if [ -z "$church" ]; then church="$1"; else named+=("$1"); fi; shift ;;
esac
done
[ -n "$church" ] || die "Name the church."
@@ -213,6 +242,9 @@ cmd_export() {
[ ! -e "$out" ] || die "$out exists already."
require_root
load_church "$church"
manage_check_named "${named[@]}"
mapfile -t products < <(manage_named "${named[@]}")
[ "${#products[@]}" -gt 0 ] || die "$church has nothing to export."
command -v zip >/dev/null || die "zip isn't installed (apt-get install zip)."
tmp="$(mktemp -d)"
chmod 755 "$tmp"
@@ -220,7 +252,7 @@ cmd_export() {
set +e
(
set -e
for p in $(manage_products); do
for p in "${products[@]}"; do
# shellcheck source=/dev/null
source "$CROSSCODE_HOME/products/$p.sh"
dir="$(pvar "$p" DIR)"; user="$(pvar "$p" USER)"
@@ -233,8 +265,8 @@ cmd_export() {
done
step "Zipping"
# One product: its folder is the ZIP's top level (export.json, uploads/, README.txt).
if [ "$(manage_products | wc -l)" = 1 ]; then
(cd "$tmp/$(manage_products)" && zip -qr "$out" .)
if [ "${#products[@]}" = 1 ]; then
(cd "$tmp/${products[0]}" && zip -qr "$out" .)
else
(cd "$tmp" && zip -qr "$out" .)
fi
@@ -247,31 +279,71 @@ cmd_export() {
bytes="$(stat -c %s "$out")"
report_result file "$out"
report_result_json bytes "$bytes"
step "Exported $church to $out ($bytes bytes)"
report_result_json products "$(json_words "${products[@]}")"
step "Exported $church (${products[*]}) to $out ($bytes bytes)"
}
# uninstall_product <church> <product>: stops and removes one product, once its final backup is
# kept: PM2, frpc tunnels, nginx site, database, and the old folder from an adopt.
uninstall_product() {
local church="$1" p="$2" user db old name
# shellcheck source=/dev/null
source "$CROSSCODE_HOME/products/$p.sh"
user="$(pvar "$p" USER)"; db="$(pvar "$p" DB)"
step "$p: stopping and removing the PM2 processes"
# shellcheck disable=SC2046 # the PM2 names are a space-separated list
run_as "$user" / pm2 delete $("${p}_pm2_names") >/dev/null 2>&1 || true
run_as "$user" / pm2 save --force >/dev/null 2>&1 || true
if [ -f "$FRPC_INI" ] && frpc_has_block "$church" "$p"; then
step "$p: removing the frpc tunnels"
frpc_remove "$church" "$p"
frpc_reload
fi
name="$church-$p"
if [ -f "/etc/nginx/sites-available/$name" ]; then
step "$p: removing the nginx site"
rm -f "/etc/nginx/sites-enabled/$name" "/etc/nginx/sites-available/$name"
if nginx -t >/dev/null 2>&1; then systemctl reload nginx; else warn "nginx didn't reload: check nginx -t."; fi
fi
if [ -n "$db" ]; then
step "$p: dropping the database $db"
db_drop "$db"
fi
old="$(pvar "$p" OLD_DIR)"
if [ -n "$old" ] && [ -d "$old" ]; then rm -rf -- "$old"; info "Removed the old folder $old."; fi
}
cmd_uninstall() {
local church="" confirm="" p dir user db kept old name
local church="" confirm="" p dir user kept key
local -a named=() products=() order=() left=()
while [ $# -gt 0 ]; do
case "$1" in
--confirm) [ $# -ge 2 ] || die "--confirm needs the church's name."; confirm="$2"; shift 2 ;;
-h|--help) manage_usage; return 0 ;;
-*) die "Unknown option $1 (crosscode help uninstall)." ;;
*) [ -z "$church" ] || die "One church at a time."; church="$1"; shift ;;
*) if [ -z "$church" ]; then church="$1"; else named+=("$1"); fi; shift ;;
esac
done
[ -n "$church" ] || die "Name the church."
[ "$confirm" = "$church" ] || die "Uninstalling removes $church's sites and databases: add --confirm $church."
require_root
load_church "$church"
manage_check_named "${named[@]}"
mapfile -t products < <(manage_named "${named[@]}")
# What the church keeps: every product of it not named (also ones this version can't handle).
for p in $(cenv PRODUCTS); do
[[ " ${products[*]} " == *" $p "* ]] || left+=("$p")
done
kept="$CROSSCODE_REMOVED/$church-$(date +%Y%m%d-%H%M%S)"
mkdir -p "$kept"
chmod 700 "$CROSSCODE_REMOVED" "$kept"
report_result kept "$kept"
report_result_json products "$(json_words "${products[@]}")"
report_result_json remaining "$(json_words "${left[@]}")"
# 1. Keep a final backup, the uploads and the .env files (the backup needs JWT_SECRET to read
# stored secrets) for 30 days. Nothing is removed until this has worked for every product.
for p in $(manage_products); do
for p in "${products[@]}"; do
# shellcheck source=/dev/null
source "$CROSSCODE_HOME/products/$p.sh"
dir="$(pvar "$p" DIR)"; user="$(pvar "$p" USER)"
@@ -282,37 +354,30 @@ cmd_uninstall() {
done
# 2. Remove, last product first.
local -a order
mapfile -t order < <(manage_products | tac)
for p in "${order[@]}"; do
# shellcheck source=/dev/null
source "$CROSSCODE_HOME/products/$p.sh"
dir="$(pvar "$p" DIR)"; user="$(pvar "$p" USER)"; db="$(pvar "$p" DB)"
step "$p: stopping and removing the PM2 processes"
# shellcheck disable=SC2046 # the PM2 names are a space-separated list
run_as "$user" / pm2 delete $("${p}_pm2_names") >/dev/null 2>&1 || true
run_as "$user" / pm2 save --force >/dev/null 2>&1 || true
if [ -f "$FRPC_INI" ] && frpc_has_block "$church" "$p"; then
step "$p: removing the frpc tunnels"
frpc_remove "$church" "$p"
frpc_reload
fi
name="$church-$p"
if [ -f "/etc/nginx/sites-available/$name" ]; then
step "$p: removing the nginx site"
rm -f "/etc/nginx/sites-enabled/$name" "/etc/nginx/sites-available/$name"
if nginx -t >/dev/null 2>&1; then systemctl reload nginx; else warn "nginx didn't reload: check nginx -t."; fi
fi
if [ -n "$db" ]; then
step "$p: dropping the database $db"
db_drop "$db"
fi
old="$(pvar "$p" OLD_DIR)"
if [ -n "$old" ] && [ -d "$old" ]; then rm -rf -- "$old"; info "Removed the old folder $old."; fi
done
mapfile -t order < <(printf '%s\n' "${products[@]}" | tac)
for p in "${order[@]}"; do uninstall_product "$church" "$p"; done
# 3a. The church keeps other products: only the removed ones' folders and church.env lines go.
if [ "${#left[@]}" -gt 0 ]; then
for p in "${order[@]}"; do
dir="$(pvar "$p" DIR)"
if [ -n "$dir" ] && [ "$(dirname "$dir")" = "$(church_dir "$church")" ] && [ -d "$dir" ]; then
step "$p: removing $dir"
rm -rf -- "$dir"
fi
for key in "${!CENV[@]}"; do
[[ "$key" == "$(upper "$p")_"* ]] && church_env_unset "$key"
done
done
church_env_set PRODUCTS "${left[*]}"
church_env_write "$(church_env_path "$church")"
step "Uninstalled ${products[*]} of $church, which keeps ${left[*]}. Kept until purged (30 days): $kept"
return 0
fi
# 3b. Nothing is left: the church goes.
step "Removing $(church_dir "$church")"
user="$(pvar events USER)"
user="$(pvar events USER "$(pvar accounts USER)")"
rm -rf -- "$(church_dir "$church")"
# The app user goes too when crosscode made it for this church (cc-<church>).
if [ "$user" = "cc-$church" ] && id "$user" >/dev/null 2>&1; then
@@ -325,27 +390,45 @@ cmd_uninstall() {
}
cmd_purge() {
local church="" confirm="" d n=0
local church="" confirm="" d p n=0
local -a named=()
while [ $# -gt 0 ]; do
case "$1" in
--confirm) [ $# -ge 2 ] || die "--confirm needs the church's name."; confirm="$2"; shift 2 ;;
-h|--help) manage_usage; return 0 ;;
-*) die "Unknown option $1 (crosscode help purge)." ;;
*) [ -z "$church" ] || die "One church at a time."; church="$1"; shift ;;
*) if [ -z "$church" ]; then church="$1"; else named+=("$1"); fi; shift ;;
esac
done
[ -n "$church" ] || die "Name the church."
valid_slug "$church" || die "\"$church\" isn't a church name."
for p in "${named[@]}"; do is_known_product "$p" || die "Unknown product \"$p\"."; done
[ "$confirm" = "$church" ] || die "Purging deletes $church's last backup for good: add --confirm $church."
require_root
[ ! -f "$(church_env_path "$church")" ] || die "$church is still installed here; purge only deletes what uninstall kept."
if [ "${#named[@]}" -eq 0 ]; then
[ ! -f "$(church_env_path "$church")" ] || die "$church is still installed here; purge only deletes what uninstall kept (name the products that were uninstalled)."
elif church_env_read "$(church_env_path "$church")"; then
for p in "${named[@]}"; do
! church_has_product "$p" || die "$church still has $p here; purge only deletes what uninstall kept."
done
fi
for d in "$CROSSCODE_REMOVED/$church"-*; do
[ -d "$d" ] || continue
[[ "$(basename "$d")" =~ ^${church}-[0-9]{8}-[0-9]{6}$ ]] || continue
step "Deleting $d"
rm -rf -- "$d"
n=$((n + 1))
if [ "${#named[@]}" -eq 0 ]; then
step "Deleting $d"
rm -rf -- "$d"
n=$((n + 1))
continue
fi
for p in "${named[@]}"; do
[ -d "$d/$p" ] || continue
step "Deleting $d/$p"
rm -rf -- "${d:?}/$p"
n=$((n + 1))
done
rmdir -- "$d" 2>/dev/null || true
done
report_result_json deleted "$n"
[ "$n" -gt 0 ] || info "Nothing kept for $church."
[ "$n" -gt 0 ] || info "Nothing kept for $church${named[*]:+ (${named[*]})}."
}