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:
+5
-3
@@ -98,9 +98,11 @@ crosscode: Cross Code installs on this server ($(git -C "${CROSSCODE_FROM:-.}" r
|
||||
crosscode restart <church> [events] reload the PM2 processes
|
||||
crosscode demo <church> --profile <profile> replace Events' data with a demo set
|
||||
crosscode env <church> <product> [--set NAME=value] show or change a product's .env values
|
||||
crosscode export <church> --out <file.zip> the church's data and uploads
|
||||
crosscode uninstall <church> --confirm <church> remove it (a final backup is kept 30 days)
|
||||
crosscode purge <church> --confirm <church> delete what uninstall kept
|
||||
crosscode export <church> [product …] --out <zip> the church's data and uploads
|
||||
crosscode uninstall <church> [product …] --confirm <church>
|
||||
remove it, or only those products (a final
|
||||
backup is kept 30 days)
|
||||
crosscode purge <church> [product …] --confirm <church> delete what uninstall kept
|
||||
crosscode help [<command>] this, or a command's options
|
||||
|
||||
Every command takes --json (a report at the end) and --config <file> (settings from the Hub).
|
||||
|
||||
+131
-48
@@ -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[*]})}."
|
||||
}
|
||||
|
||||
@@ -62,7 +62,7 @@ json() { node -e 'let s="";process.stdin.on("data",d=>s+=d).on("end",()=>{const
|
||||
|
||||
@test "help lists the new commands" {
|
||||
run bash "$CC" help
|
||||
[[ "$output" == *"crosscode uninstall <church> --confirm <church>"* ]]
|
||||
[[ "$output" == *"crosscode uninstall <church> [product …] --confirm <church>"* ]]
|
||||
[[ "$output" == *"--json"* ]]
|
||||
run bash "$CC" help export
|
||||
[[ "$output" == *"crosscode.church-export/1"* ]]
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
#!/usr/bin/env bats
|
||||
# crosscode uninstall / purge (lib/manage.sh) with products named: only those go, and the church
|
||||
# keeps the others in church.env. Naming every product removes the church, as before.
|
||||
|
||||
setup() {
|
||||
export CROSSCODE_NO_UPDATE=1 CROSSCODE_TEST=1
|
||||
TMP="$BATS_TEST_TMPDIR"
|
||||
CALLS="$TMP/calls"
|
||||
: > "$CALLS"
|
||||
export CROSSCODE_SITES="$TMP/sites" FRPC_INI="$TMP/frpc.ini"
|
||||
# shellcheck source=../lib/common.sh
|
||||
source "$BATS_TEST_DIRNAME/../lib/common.sh"
|
||||
# shellcheck source=../lib/frpc.sh
|
||||
source "$BATS_TEST_DIRNAME/../lib/frpc.sh"
|
||||
# shellcheck source=../lib/manage.sh
|
||||
source "$BATS_TEST_DIRNAME/../lib/manage.sh"
|
||||
|
||||
# A church with Accounts and Events, and the products' hooks recording what they'd do.
|
||||
export CROSSCODE_HOME="$TMP/home"
|
||||
mkdir -p "$CROSSCODE_HOME/products" "$TMP/sites/hope/accounts" "$TMP/sites/hope/events"
|
||||
for p in accounts events; do
|
||||
cat > "$CROSSCODE_HOME/products/$p.sh" <<EOF
|
||||
${p}_backup() { echo "backup $p" >> "$CALLS"; }
|
||||
${p}_keep() { mkdir -p "\$2"; echo "keep $p" >> "$CALLS"; }
|
||||
${p}_pm2_names() { echo "hope-$p-backend hope-$p-frontend"; }
|
||||
EOF
|
||||
done
|
||||
cat > "$TMP/sites/hope/church.env" <<EOF
|
||||
CHURCH=hope
|
||||
PRODUCTS="accounts events"
|
||||
ADDRESS=hope.co.za
|
||||
ACCOUNTS_DIR=$TMP/sites/hope/accounts
|
||||
ACCOUNTS_DB=hope_accounts
|
||||
ACCOUNTS_DOMAIN=accounts.hope.co.za
|
||||
ACCOUNTS_USER=cc-hope
|
||||
EVENTS_DIR=$TMP/sites/hope/events
|
||||
EVENTS_DB=hope_events
|
||||
EVENTS_DOMAIN=events.hope.co.za
|
||||
EVENTS_USER=cc-hope
|
||||
EOF
|
||||
run_as() { echo "run $3 $4" >> "$CALLS"; }
|
||||
db_drop() { echo "drop $1" >> "$CALLS"; }
|
||||
}
|
||||
|
||||
@test "one product named: only it goes, the church keeps the other" {
|
||||
run cmd_uninstall hope events --confirm hope
|
||||
[ "$status" -eq 0 ]
|
||||
grep -q "backup events" "$CALLS"
|
||||
! grep -q "backup accounts" "$CALLS"
|
||||
grep -q "drop hope_events" "$CALLS"
|
||||
! grep -q "drop hope_accounts" "$CALLS"
|
||||
[ ! -e "$TMP/sites/hope/events" ]
|
||||
[ -d "$TMP/sites/hope/accounts" ]
|
||||
grep -q '^PRODUCTS=accounts$' "$TMP/sites/hope/church.env"
|
||||
! grep -q '^EVENTS_' "$TMP/sites/hope/church.env"
|
||||
grep -q '^ACCOUNTS_DB=hope_accounts$' "$TMP/sites/hope/church.env"
|
||||
[[ "$output" == *"which keeps accounts"* ]]
|
||||
ls -d "$TMP/sites/.removed"/hope-*/events
|
||||
}
|
||||
|
||||
@test "then the other: the church goes" {
|
||||
run cmd_uninstall hope events --confirm hope
|
||||
[ "$status" -eq 0 ]
|
||||
run cmd_uninstall hope accounts --confirm hope
|
||||
[ "$status" -eq 0 ]
|
||||
[ ! -e "$TMP/sites/hope" ]
|
||||
[[ "$output" == *"Uninstalled hope."* ]]
|
||||
}
|
||||
|
||||
@test "no product named: the whole church, as before" {
|
||||
run cmd_uninstall hope --confirm hope
|
||||
[ "$status" -eq 0 ]
|
||||
grep -q "drop hope_events" "$CALLS"
|
||||
grep -q "drop hope_accounts" "$CALLS"
|
||||
[ ! -e "$TMP/sites/hope" ]
|
||||
}
|
||||
|
||||
@test "a product the church doesn't have is refused before anything happens" {
|
||||
sed -i 's/^PRODUCTS=.*/PRODUCTS=accounts/' "$TMP/sites/hope/church.env"
|
||||
run cmd_uninstall hope events --confirm hope
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" == *"hope has no events"* ]]
|
||||
[ ! -s "$CALLS" ]
|
||||
run cmd_uninstall hope website2 --confirm hope
|
||||
[ "$status" -eq 1 ]
|
||||
}
|
||||
|
||||
@test "purge with a product: only what was kept of it, also while the church is still here" {
|
||||
run cmd_uninstall hope events --confirm hope
|
||||
[ "$status" -eq 0 ]
|
||||
kept="$(ls -d "$TMP/sites/.removed"/hope-*)"
|
||||
mkdir -p "$kept/accounts"
|
||||
run cmd_purge hope events --confirm hope
|
||||
[ "$status" -eq 0 ]
|
||||
[ ! -e "$kept/events" ]
|
||||
[ -d "$kept/accounts" ]
|
||||
# Accounts is still installed: its kept copy isn't purged, nor the whole church.
|
||||
run cmd_purge hope accounts --confirm hope
|
||||
[ "$status" -eq 1 ]
|
||||
run cmd_purge hope --confirm hope
|
||||
[ "$status" -eq 1 ]
|
||||
}
|
||||
Reference in New Issue
Block a user