Files
crosscode-platform/scripts/lib/adopt.sh
T
joshuaandClaude Opus 5.5 c442fe0d42 crosscode script: deploy, status, install, adopt (Events 2.2.0, plan §4-5)
One script for installing, updating and moving church installs, replacing
hope-events' scripts/deploy.sh and install.sh:

- deploy <church> [events] / --every-church, with --version, --main, --check
  and --allow-local-changes: checks every product first (files changed by
  hand, Node, release requirements from WHATS-NEW.md headings), then each
  product in order with backup, checkout, install, migrate, build, PM2 reload
  and a /health version check; a failure rolls that product back and stops.
- status [<church>]: checked-out and latest release, /health, and the old
  folder kept after adopt.
- install <church> --products events --domain <address>: a new church under
  /var/www/sites/<church>, writing church.env.
- adopt <church> --events-dir <old> builds the monorepo checkout next to the
  running install and switches PM2 over; adopt --undo goes back to it.

Product hooks live in scripts/products/events.sh. shellcheck (lint:scripts)
and 47 bats tests (test:scripts) are part of npm run check.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 19:17:06 +02:00

290 lines
14 KiB
Bash

# shellcheck shell=bash
# crosscode adopt: moves an existing Cross Events install (a hope-events checkout at v2.1.x) onto
# the monorepo, into /var/www/sites/<church>/events (EVENTS-2.2-PLAN §5, decisions 25 and 26).
# The new checkout is built next to the running install, then PM2 switches over (seconds of
# downtime). The old folder isn't changed, so a failed switch just starts it again.
ADOPT_KEEP_DAYS=14
ADOPT_SUFFIX=".pre-2.2.0"
adopt_usage() {
cat <<'EOF'
crosscode adopt <church> --events-dir /var/www/<old folder> [options]
crosscode adopt <church> --undo
Moves an existing Cross Events install (hope-events at v2.1.x) to the monorepo, in
/var/www/sites/<church>/events. The old install keeps serving while the new one builds. The old
folder is kept as <old>.pre-2.2.0 for 14 days, for --undo.
--events-dir <dir> the existing install (required)
--version events@x.y.z the release to move to (default: the latest events@2.2.x)
--address <domain> the church's address, e.g. hopefamily.co.za (default: the Events
domain without "events.")
--undo go back to the old folder and the 2.1.x it ran
EOF
}
cmd_adopt() {
local church="" old="" version="" address="" undo=0
while [ $# -gt 0 ]; do
case "$1" in
--events-dir) [ $# -ge 2 ] || die "--events-dir needs a folder."; old="${2%/}"; shift 2 ;;
--version) [ $# -ge 2 ] || die "--version needs events@x.y.z."; version="$2"; shift 2 ;;
--address) [ $# -ge 2 ] || die "--address needs a domain."; address="$2"; shift 2 ;;
--undo) undo=1; shift ;;
-h|--help) adopt_usage; return 0 ;;
-*) die "Unknown option $1 (crosscode adopt --help)." ;;
*) [ -z "$church" ] || die "One church at a time."; church="$1"; shift ;;
esac
done
[ -n "$church" ] || { adopt_usage; die "Name the church."; }
valid_slug "$church" || die "\"$church\" isn't a church name (a-z, 0-9 and -, starting with a letter)."
[ "$undo" = 1 ] || [ -n "$old" ] || { adopt_usage; die "--events-dir is required."; }
[ -z "$version" ] || [[ "$version" =~ ^events@[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$ ]] || die "--version: expected events@x.y.z."
require_root
# shellcheck source=/dev/null
source "$CROSSCODE_HOME/products/events.sh"
if [ "$undo" = 1 ]; then
adopt_undo "$church"
else
adopt_run "$church" "$old" "$version" "$address"
fi
}
# rewrite_ecosystem <old dir> <new app dir>, PM2 config on stdin → stdout: absolute paths into
# the old install (e.g. cwd: '/var/www/hope-events/backend') point into the new one instead.
# __dirname-based paths need nothing. Only whole paths match (/var/www/hope-events2 doesn't).
rewrite_ecosystem() {
local old new
old="$(printf '%s' "${1%/}" | sed 's/[][\.*^$#/]/\\&/g')"
new="$(printf '%s' "${2%/}" | sed 's/[\&#]/\\&/g')"
sed -E "s#([\"'\`])${old}(/|[\"'\`])#\\1${new}\\2#g"
}
# Ignored files in a checkout's folder (the install's own data, not in git), one per line.
ignored_files_in() { git_in "$1" ls-files --others --ignored --exclude-standard -- "$2"; }
adopt_run() {
local church="$1" old="$2" version="$3" address="$4"
local sites new app user oldref changes backend frontend api web dburl db domain major
sites="$(church_dir "$church")"
new="$sites/events"
app="$new/apps/events"
# ── 1. Checks ──────────────────────────────────────────────────────────────────────────────
step "Checks"
[ -d "$old/.git" ] && [ -d "$old/backend" ] || die "$old isn't a Cross Events install (no .git or backend/)."
oldref="$(git_in "$old" describe --tags --exact-match 2>/dev/null || true)"
[[ "$oldref" =~ ^v2\.1\.[0-9]+$ ]] ||
die "$old is on ${oldref:-an untagged commit}, not a v2.1.x release. Update it with its scripts/deploy.sh to v2.1.1 first."
changes="$(local_changes "$old" | awk 'substr($0, 4) != "ecosystem.config.js"')"
if [ -n "$changes" ]; then
warn "Files in $old were changed or added by hand:"
printf '%s\n' "$changes" | sed 's/^/ /' >&2
die "Sort them out first (as scripts/deploy.sh --check explains). Nothing was changed."
fi
if [ -f "$(church_env_path "$church")" ]; then
church_env_read "$(church_env_path "$church")"
[ -n "$(pvar events ADOPTED_AT)" ] && { info "$church is already adopted ($(pvar events DIR)). Nothing to do."; return 0; }
die "$(church_env_path "$church") already exists: $church is already set up on this server."
fi
[ ! -e "$new" ] || die "$new exists from an adopt that didn't finish. The old install is still the one running: remove $new and run adopt again."
[ ! -e "$old$ADOPT_SUFFIX" ] || die "$old$ADOPT_SUFFIX already exists. Move it away first."
if grep -rlsF -- "$old" /etc/nginx/ >/dev/null 2>&1; then
warn "nginx refers to $old directly:"
grep -rlsF -- "$old" /etc/nginx/ | sed 's/^/ /' >&2
die "Change those to proxy to the app (or to the new folder, $app) first: the old folder gets renamed."
fi
[ -f "$old/ecosystem.config.js" ] || die "$old has no ecosystem.config.js, so its PM2 names aren't known."
major="$(node_major)"
[ "$major" -ge "$CROSSCODE_NODE_MIN" ] || die "This server runs Node $(node -v 2>/dev/null || echo '(none)'); 2.2.0 needs $CROSSCODE_NODE_MIN or later."
# ── 2. What the install already has ───────────────────────────────────────────────────────
user="$(stat -c %U "$old")"
id "$user" >/dev/null 2>&1 && [ "$user" != root ] || die "$old is owned by $user; expected the app user."
# shellcheck disable=SC2016
read -r backend frontend web < <(run_as "$user" "$old" node -e '
const apps = require(process.argv[1]).apps || [];
const b = apps.find(a => /backend/.test(a.name)) || apps[0] || {};
const f = apps.find(a => /frontend/.test(a.name)) || apps[1] || {};
const m = /-p\s+(\d+)/.exec([].concat(f.args || []).join(" "));
console.log(b.name || "", f.name || "", m ? m[1] : "3000");' "$old/ecosystem.config.js") ||
die "Couldn't read the PM2 names from $old/ecosystem.config.js."
[ -n "$backend" ] && [ -n "$frontend" ] || die "Couldn't find the backend and frontend apps in $old/ecosystem.config.js."
api="$(env_value "$old/backend/.env" PORT)"
api="${api:-5000}"
dburl="$(env_value "$old/backend/.env" DATABASE_URL)"
db="$(db_name_from_url "$dburl")"
[ -n "$db" ] || die "No DATABASE_URL in $old/backend/.env."
domain="$(host_from_url "$(env_value "$old/backend/.env" FRONTEND_URL)")"
if [ -z "$address" ]; then
[[ "$domain" == events.* ]] || die "The Events domain is \"$domain\"; give the church's address with --address."
address="${domain#events.}"
fi
info "App user $user, PM2 $backend + $frontend, ports api $api web $web, database $db, domain $domain"
# church.env is written at the end; until then the values live in CENV only.
CENV=()
church_env_set CHURCH "$church"
church_env_set PRODUCTS events
church_env_set ADDRESS "$address"
church_env_set EVENTS_DIR "$new"
church_env_set EVENTS_DOMAIN "$domain"
church_env_set EVENTS_PM2 "$backend $frontend"
church_env_set EVENTS_PORTS "$api $web"
church_env_set EVENTS_DB "$db"
church_env_set EVENTS_USER "$user"
# ── 3. The new checkout ────────────────────────────────────────────────────────────────────
step "New checkout in $new"
mkdir -p "$sites"
chmod 755 "$CROSSCODE_SITES" "$sites"
mkdir "$new"
chown "$user:$user" "$new"
run_as "$user" "$new" git clone --quiet "$CROSSCODE_REPO" .
if [ -z "$version" ]; then
version="$(git_in "$new" tag -l 'events@2.2.*' | latest_tag_from events)"
[ -n "$version" ] || die "No events@2.2.x release tag yet. Remove $new; nothing else changed."
fi
git_in "$new" rev-parse -q --verify "refs/tags/$version" >/dev/null || die "There's no tag $version. Remove $new; nothing else changed."
run_as "$user" "$new" git checkout -q --detach "$version"
info "At $version"
# ── 4. The install's own files ────────────────────────────────────────────────────────────
step "Copying the install's own files"
local f
cp -p "$old/backend/.env" "$app/backend/.env"
for f in "$old"/frontend/.env*; do
[ -f "$f" ] && [ "$(basename "$f")" != .env.example ] && cp -p "$f" "$app/frontend/"
done
rewrite_ecosystem "$old" "$app" < "$old/ecosystem.config.js" > "$app/ecosystem.config.js"
chown "$user:$user" "$app/ecosystem.config.js"
adopt_sync_data "$old" "$app" ""
info "Uploads (this can take a while; the site is still running)"
mkdir -p "$app/backend/public/uploads"
rsync -a "$old/backend/public/uploads/" "$app/backend/public/uploads/"
[ -z "$(local_changes "$new")" ] || die "The copied files show up as changes in $new (git status). Remove $new; nothing else changed."
# ── 5. Build, next to the running install ─────────────────────────────────────────────────
step "Installing and building $version (the old install keeps serving)"
events_install_deps "$new" "$user"
events_build "$new" "$user"
local expected
expected="$(events_expected_version "$new")"
if events_pending_migrations "$new" "$user"; then
die "$version has database migrations that aren't applied (or the database can't be read). adopt only moves the install; run it at a 2.2.x without migrations. Remove $new; nothing else changed."
fi
# ── 6. Switch over: the only downtime ─────────────────────────────────────────────────────
step "Switching over"
info "Database backup"
events_backup "$new" "$user"
info "Last copy of uploads and data"
adopt_sync_data "$old" "$app" --delete
rsync -a --delete "$old/backend/public/uploads/" "$app/backend/public/uploads/"
set +e
(
set -e
run_as "$user" "$new" pm2 delete "$backend" "$frontend"
run_as "$user" "$app" pm2 start ecosystem.config.js
run_as "$user" "$app" pm2 save
wait_for_version "$(events_health_url)" "$expected" 60
)
local rc=$?
set -e
if [ "$rc" != 0 ]; then
warn "The new install didn't come up. Starting the old one again."
run_as "$user" "$old" pm2 delete "$backend" "$frontend" >/dev/null 2>&1 || true
run_as "$user" "$old" pm2 start ecosystem.config.js
run_as "$user" "$old" pm2 save
if wait_for_version "$(events_health_url)" "${oldref#v}" 60; then
die "Back on $oldref from $old, as before. $new is left for a look (pm2 logs); remove it before trying again."
fi
die "The old install didn't come back either: check now (pm2 status, pm2 logs)."
fi
# ── 7. Keep the old folder for --undo, and record the church ──────────────────────────────
mv "$old" "$old$ADOPT_SUFFIX"
church_env_set EVENTS_OLD_DIR "$old$ADOPT_SUFFIX"
church_env_set EVENTS_OLD_REF "$oldref"
church_env_set EVENTS_ADOPTED_AT "$(date +%F)"
church_env_write "$(church_env_path "$church")"
step "Adopted: $church runs $version from $new"
info "The old folder is $old$ADOPT_SUFFIX, kept $ADOPT_KEEP_DAYS days for \`crosscode adopt $church --undo\`."
info "Updates from now on: crosscode deploy $church events"
}
# adopt_sync_data <from install> <to app dir> [--delete]: the backend's data/*.json that aren't
# in git, and its backups.
adopt_sync_data() {
local from="$1" to="$2" del="$3" f
while IFS= read -r f; do
[ -n "$f" ] || continue
mkdir -p "$(dirname "$to/$f")"
cp -p "$from/$f" "$to/$f"
done < <(ignored_files_in "$from" backend/data)
if [ -d "$from/backend/backups" ]; then
mkdir -p "$to/backend/backups"
# shellcheck disable=SC2086
rsync -a $del "$from/backend/backups/" "$to/backend/backups/"
fi
}
# adopt --undo: back to the old folder and the 2.1.x it ran, while it's kept.
adopt_undo() {
local church="$1" new app user oldkept orig oldref backend frontend extra
load_church "$church"
new="$(pvar events DIR)"
app="$new/apps/events"
user="$(pvar events USER)"
oldkept="$(pvar events OLD_DIR)"
oldref="$(pvar events OLD_REF)"
read -r backend frontend <<< "$(pvar events PM2)"
[ -n "$oldkept" ] || die "$church wasn't moved with adopt, so there's nothing to go back to."
[ -d "$oldkept" ] || die "The old folder $oldkept is gone, so adopt --undo can't go back any more."
orig="${oldkept%"$ADOPT_SUFFIX"}"
[ ! -e "$orig" ] || die "$orig exists again; move it away first."
step "Checks"
# The database must still suit 2.1.x: no migrations the old release doesn't have.
extra="$(comm -13 \
<(find "$oldkept/backend/prisma/migrations" -mindepth 1 -maxdepth 1 -type d -printf '%f\n' | sort) \
<(find "$app/backend/prisma/migrations" -mindepth 1 -maxdepth 1 -type d -printf '%f\n' | sort))"
[ -z "$extra" ] || die "$(checkout_ref "$new" events) has database migrations $oldref doesn't know ($(echo "$extra" | tr '\n' ' ')). Going back would break it; restore a backup instead."
step "Database backup"
events_backup "$new" "$user"
step "Going back to $oldref in $orig"
mv "$oldkept" "$orig"
info "Copying back uploads and data added since"
adopt_sync_data "$app" "$orig" ""
rsync -a "$app/backend/public/uploads/" "$orig/backend/public/uploads/"
set +e
(
set -e
run_as "$user" "$orig" pm2 delete "$backend" "$frontend"
run_as "$user" "$orig" pm2 start ecosystem.config.js
run_as "$user" "$orig" pm2 save
wait_for_version "$(events_health_url)" "${oldref#v}" 60
)
local rc=$?
set -e
if [ "$rc" != 0 ]; then
warn "$oldref didn't come up. Starting $(checkout_ref "$new" events) from $new again."
run_as "$user" "$app" pm2 delete "$backend" "$frontend" >/dev/null 2>&1 || true
mv "$orig" "$oldkept"
run_as "$user" "$app" pm2 start ecosystem.config.js
run_as "$user" "$app" pm2 save
die "Undo failed; $church runs from $new as before."
fi
# The church's folder goes aside (not deleted), so it's no longer a church here and adopt can
# be run again.
local aside
aside="$CROSSCODE_SITES/.undone-$church-$(date +%Y%m%d%H%M%S)"
mv "$(church_dir "$church")" "$aside"
step "Undone: $church runs $oldref from $orig again"
info "The monorepo checkout is in $aside; remove it when you no longer need it."
}