Files
crosscode-platform/scripts/lib/status.sh
T
joshuaandClaude Opus 5.5 e9e9b5a97f 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>
2026-11-17 12:50:19 +02:00

102 lines
3.8 KiB
Bash

# shellcheck shell=bash
# crosscode status [<church>]: each product's version, latest release, health and last backup.
status_usage() {
cat <<'EOF'
crosscode status [<church>] [--no-fetch]
Each product's checked-out release, the latest release, and what /health reports (version,
database, jobs, last backup), for one church or every church on this server.
--no-fetch don't fetch tags first (faster; "latest" may be out of date)
EOF
}
cmd_status() {
local church="" fetch=1
while [ $# -gt 0 ]; do
case "$1" in
--no-fetch) fetch=0; shift ;;
-h|--help) status_usage; return 0 ;;
-*) die "Unknown option $1 (crosscode status --help)." ;;
*) [ -z "$church" ] || die "One church at a time."; church="$1"; shift ;;
esac
done
require_root
local -a churches
if [ -n "$church" ]; then
churches=("$church")
else
mapfile -t churches < <(list_churches)
[ "${#churches[@]}" -gt 0 ] || { info "No churches on this server ($CROSSCODE_SITES/*/church.env)."; return 0; }
fi
local c
STATUS_JSON=""
for c in "${churches[@]}"; do status_church "$c" "$fetch"; done
report_result_json churches "[$STATUS_JSON]"
}
STATUS_JSON=""
# jstr <text>: a JSON string.
jstr() { node -e 'process.stdout.write(JSON.stringify(process.argv[1]))' "$1"; }
status_church() {
local church="$1" fetch="$2" p dir user ref latest url json version db jobs backup line
load_church "$church"
echo
step "$church ($(cenv ADDRESS '(no address)'))"
local pj="" hj
# shellcheck disable=SC2046 # PRODUCTS is a space-separated list of names
for p in $(in_product_order $(cenv PRODUCTS)); do
dir="$(pvar "$p" DIR)"
user="$(pvar "$p" USER)"
if [ ! -d "$dir/.git" ]; then
info "$(printf '%-9s %s' "$p" "missing checkout: $dir")"
pj+="${pj:+,}{\"product\":\"$p\",\"ref\":null,\"latest\":null,\"health\":null,\"problem\":\"missing checkout\"}"
continue
fi
if [ "$fetch" = 1 ]; then
run_as "$user" "$dir" timeout 20 git fetch --tags --quiet origin 2>/dev/null || true
fi
ref="$(checkout_ref "$dir" "$p")"
latest="$(latest_tag "$dir" "$p")"
line="$(printf '%-9s %-16s latest %-16s' "$p" "$ref" "${latest:-none}")"
[ -n "$latest" ] && [ "$ref" != "$latest" ] && line="$line (update available)"
info "$line"
hj=null
if is_supported_product "$p"; then
# shellcheck source=/dev/null
source "$CROSSCODE_HOME/products/$p.sh"
url="$("${p}_health_url")"
if json="$(health_json "$url")"; then
version="$(printf '%s' "$json" | json_get version)"
db="$(printf '%s' "$json" | json_get db)"
jobs="$(printf '%s' "$json" | json_get jobs)"
backup="$(printf '%s' "$json" | json_get lastBackupAt)"
info " health: version ${version:-?}, database ${db:-?}, jobs ${jobs:-?}, last backup ${backup:-never}"
hj="$json"
else
warn " $p isn't answering on $url (pm2 status; pm2 logs)."
fi
fi
pj+="${pj:+,}{\"product\":\"$p\",\"ref\":$(jstr "$ref"),\"latest\":$(jstr "$latest"),\"health\":$hj,\"ports\":$(jstr "$(pvar "$p" PORTS)"),\"domain\":$(jstr "$(pvar "$p" DOMAIN)")}"
status_old_folder "$p"
done
STATUS_JSON+="${STATUS_JSON:+,}{\"church\":\"$church\",\"address\":$(jstr "$(cenv ADDRESS)"),\"products\":[$pj]}"
}
# After `adopt`, the old folder is kept for 14 days so `adopt --undo` can go back to it.
status_old_folder() {
local p="$1" old at age
old="$(pvar "$p" OLD_DIR)"
at="$(pvar "$p" ADOPTED_AT)"
[ -n "$old" ] && [ -d "$old" ] || return 0
age=$(( ($(date +%s) - $(date -d "$at" +%s 2>/dev/null || date +%s)) / 86400 ))
if [ "$age" -ge 14 ]; then
warn " The old folder $old is $age days old, past the 14 days kept for \`adopt --undo\`: remove it (rm -rf $old)."
else
info " Old folder kept for \`adopt --undo\`: $old (adopted $at, $((14 - age)) days left)."
fi
}