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,204 @@
|
||||
# shellcheck shell=bash
|
||||
# crosscode restart / export / uninstall / purge (docs/AGENT-1.0-PLAN.md §4 and §9).
|
||||
|
||||
CROSSCODE_REMOVED="${CROSSCODE_REMOVED:-$CROSSCODE_SITES/.removed}"
|
||||
|
||||
manage_usage() {
|
||||
cat <<'EOF'
|
||||
crosscode restart <church> [events] reload the products' PM2 processes
|
||||
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
|
||||
/var/www/sites/.removed/<church>-<date>/ (30 days),
|
||||
then PM2, tunnels, database and folders removed
|
||||
crosscode purge <church> --confirm <church> delete what uninstall kept
|
||||
|
||||
All take --json (a report at the end; the log goes to stderr).
|
||||
EOF
|
||||
}
|
||||
|
||||
# The church's products that this script can handle, in deploy order.
|
||||
manage_products() {
|
||||
local p
|
||||
# shellcheck disable=SC2046 # PRODUCTS is a space-separated list
|
||||
for p in $(in_product_order $(cenv PRODUCTS)); do
|
||||
is_supported_product "$p" && echo "$p"
|
||||
done
|
||||
return 0
|
||||
}
|
||||
|
||||
cmd_restart() {
|
||||
local church="" p
|
||||
local -a named=()
|
||||
while [ $# -gt 0 ]; do
|
||||
case "$1" in
|
||||
-h|--help) manage_usage; return 0 ;;
|
||||
-*) die "Unknown option $1 (crosscode help restart)." ;;
|
||||
*) if [ -z "$church" ]; then church="$1"; else is_known_product "$1" || die "Unknown product \"$1\"."; named+=("$1"); fi; shift ;;
|
||||
esac
|
||||
done
|
||||
[ -n "$church" ] || die "Name the church."
|
||||
require_root
|
||||
load_church "$church"
|
||||
[ "${#named[@]}" -gt 0 ] || mapfile -t named < <(manage_products)
|
||||
for p in "${named[@]}"; do
|
||||
church_has_product "$p" || die "$church has no $p."
|
||||
# shellcheck source=/dev/null
|
||||
source "$CROSSCODE_HOME/products/$p.sh"
|
||||
step "$p: reloading PM2"
|
||||
"${p}_reload" "$(pvar "$p" DIR)" "$(pvar "$p" USER)"
|
||||
step "$p: waiting for it to answer"
|
||||
wait_for_version "$("${p}_health_url")" "$("${p}_expected_version" "$(pvar "$p" DIR)")" 60
|
||||
done
|
||||
}
|
||||
|
||||
cmd_export() {
|
||||
local church="" out="" p tmp dir user bytes
|
||||
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 ;;
|
||||
esac
|
||||
done
|
||||
[ -n "$church" ] || die "Name the church."
|
||||
[[ "$out" == /*.zip ]] || die "--out: an absolute path ending in .zip."
|
||||
[ ! -e "$out" ] || die "$out exists already."
|
||||
require_root
|
||||
load_church "$church"
|
||||
command -v zip >/dev/null || die "zip isn't installed (apt-get install zip)."
|
||||
tmp="$(mktemp -d)"
|
||||
chmod 755 "$tmp"
|
||||
local rc
|
||||
set +e
|
||||
(
|
||||
set -e
|
||||
for p in $(manage_products); do
|
||||
# shellcheck source=/dev/null
|
||||
source "$CROSSCODE_HOME/products/$p.sh"
|
||||
dir="$(pvar "$p" DIR)"; user="$(pvar "$p" USER)"
|
||||
step "$p: exporting the data"
|
||||
mkdir -p "$tmp/$p"
|
||||
chown "$user:$user" "$tmp/$p"
|
||||
"${p}_export" "$dir" "$user" "$tmp/$p"
|
||||
step "$p: adding the uploads"
|
||||
"${p}_copy_uploads" "$dir" "$tmp/$p/uploads"
|
||||
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" .)
|
||||
else
|
||||
(cd "$tmp" && zip -qr "$out" .)
|
||||
fi
|
||||
chmod 600 "$out"
|
||||
)
|
||||
rc=$?
|
||||
set -e
|
||||
rm -rf "$tmp"
|
||||
[ "$rc" = 0 ] || { rm -f "$out"; die "Export of $church failed."; }
|
||||
bytes="$(stat -c %s "$out")"
|
||||
report_result file "$out"
|
||||
report_result_json bytes "$bytes"
|
||||
step "Exported $church to $out ($bytes bytes)"
|
||||
}
|
||||
|
||||
cmd_uninstall() {
|
||||
local church="" confirm="" p dir user db kept old name
|
||||
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 ;;
|
||||
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"
|
||||
kept="$CROSSCODE_REMOVED/$church-$(date +%Y%m%d-%H%M%S)"
|
||||
mkdir -p "$kept"
|
||||
chmod 700 "$CROSSCODE_REMOVED" "$kept"
|
||||
report_result kept "$kept"
|
||||
|
||||
# 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
|
||||
# shellcheck source=/dev/null
|
||||
source "$CROSSCODE_HOME/products/$p.sh"
|
||||
dir="$(pvar "$p" DIR)"; user="$(pvar "$p" USER)"
|
||||
step "$p: final backup"
|
||||
"${p}_backup" "$dir" "$user"
|
||||
step "$p: keeping the backup, uploads and settings in $kept/$p"
|
||||
"${p}_keep" "$dir" "$kept/$p"
|
||||
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
|
||||
|
||||
step "Removing $(church_dir "$church")"
|
||||
user="$(pvar events 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
|
||||
pm2 unstartup systemd -u "$user" >/dev/null 2>&1 || true
|
||||
pkill -u "$user" 2>/dev/null || true
|
||||
userdel -r "$user" >/dev/null 2>&1 || warn "The user $user couldn't be removed (userdel -r $user)."
|
||||
fi
|
||||
step "Uninstalled $church. Kept until purged (30 days): $kept"
|
||||
}
|
||||
|
||||
cmd_purge() {
|
||||
local church="" confirm="" d n=0
|
||||
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 ;;
|
||||
esac
|
||||
done
|
||||
[ -n "$church" ] || die "Name the church."
|
||||
valid_slug "$church" || die "\"$church\" isn't a church name."
|
||||
[ "$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."
|
||||
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))
|
||||
done
|
||||
report_result_json deleted "$n"
|
||||
[ "$n" -gt 0 ] || info "Nothing kept for $church."
|
||||
}
|
||||
Reference in New Issue
Block a user