Demo data from the Hub: crosscode demo, the seed-demo job, set-demo fixes
- scripts: `crosscode demo <church> --profile … [--admin-email …]`: backup, set-demo.js as the site's user, reload, wait for /health. - Contracts 1.7.0: the seed-demo job, DEMO_PROFILES, SEED_DEMO_MIN_AGENT. - Agent 1.2.0: seed-demo → crosscode demo. - Events 2.4.1: set-demo.js stops with a clear message when it can't read .env (was "client password must be a string", then a hang); keeps the oldest admin when the default one isn't on the site; --admin-email=. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+3
-1
@@ -96,6 +96,7 @@ crosscode: Cross Code installs on this server ($(git -C "${CROSSCODE_FROM:-.}" r
|
||||
crosscode adopt <church> --events-dir <old folder> move an existing Cross Events install here
|
||||
crosscode adopt <church> --undo go back to the old folder (within 14 days)
|
||||
crosscode restart <church> [events] reload the PM2 processes
|
||||
crosscode demo <church> --profile <profile> replace Events' data with a demo set
|
||||
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
|
||||
@@ -112,6 +113,7 @@ case "$command" in
|
||||
install) cmd_install "$@" ;;
|
||||
adopt) cmd_adopt "$@" ;;
|
||||
restart) cmd_restart "$@" ;;
|
||||
demo) cmd_demo "$@" ;;
|
||||
export) cmd_export "$@" ;;
|
||||
uninstall) cmd_uninstall "$@" ;;
|
||||
purge) cmd_purge "$@" ;;
|
||||
@@ -123,7 +125,7 @@ case "$command" in
|
||||
install) install_usage ;;
|
||||
add) add_usage ;;
|
||||
adopt) adopt_usage ;;
|
||||
restart|export|uninstall|purge) manage_usage ;;
|
||||
restart|demo|export|uninstall|purge) manage_usage ;;
|
||||
*) usage ;;
|
||||
esac ;;
|
||||
*) usage; die "Unknown command \"$command\"." ;;
|
||||
|
||||
@@ -6,6 +6,10 @@ CROSSCODE_REMOVED="${CROSSCODE_REMOVED:-$CROSSCODE_SITES/.removed}"
|
||||
manage_usage() {
|
||||
cat <<'EOF'
|
||||
crosscode restart <church> [events] reload the products' PM2 processes
|
||||
crosscode demo <church> --profile <profile> [--admin-email <email>]
|
||||
replace Cross Events' data with a demo set
|
||||
(small, events, ministry, multicampus); a backup
|
||||
is taken first, one admin account is kept
|
||||
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
|
||||
@@ -52,6 +56,52 @@ cmd_restart() {
|
||||
done
|
||||
}
|
||||
|
||||
DEMO_PROFILES="small events ministry multicampus"
|
||||
|
||||
# Replaces Events' data with a demo set (apps/events/backend/scripts/set-demo.js), as the site's
|
||||
# user (its .env is readable only by that user). The Hub sends it only for installs marked as demo
|
||||
# sites. Afterwards Events is reloaded: the demo switches campuses on or off.
|
||||
cmd_demo() {
|
||||
local church="" profile="" email="" dir user app
|
||||
while [ $# -gt 0 ]; do
|
||||
case "$1" in
|
||||
--profile) [ $# -ge 2 ] || die "--profile needs a value."; profile="$2"; shift 2 ;;
|
||||
--admin-email) [ $# -ge 2 ] || die "--admin-email needs a value."; email="$2"; shift 2 ;;
|
||||
-h|--help) manage_usage; return 0 ;;
|
||||
-*) die "Unknown option $1 (crosscode help demo)." ;;
|
||||
*) [ -z "$church" ] || die "One church at a time."; church="$1"; shift ;;
|
||||
esac
|
||||
done
|
||||
[ -n "$church" ] || die "Name the church."
|
||||
[[ " $DEMO_PROFILES " == *" $profile "* ]] || die "--profile: one of $DEMO_PROFILES."
|
||||
[ -z "$email" ] || [[ "$email" =~ ^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$ ]] || die "--admin-email: not an email address."
|
||||
require_root
|
||||
load_church "$church"
|
||||
church_has_product events || die "$church has no Cross Events."
|
||||
# shellcheck source=/dev/null
|
||||
source "$CROSSCODE_HOME/products/events.sh"
|
||||
dir="$(pvar events DIR)"
|
||||
user="$(pvar events USER)"
|
||||
app="$(events_app "$dir")"
|
||||
[ -f "$app/backend/scripts/set-demo.js" ] || die "This release of Events ($(checkout_ref "$dir" events)) has no demo data."
|
||||
|
||||
step "Events: backup first"
|
||||
events_backup "$dir" "$user" || die "The backup failed, so the demo data wasn't loaded."
|
||||
|
||||
step "Events: demo data ($profile)"
|
||||
local -a env_args=()
|
||||
# DEMO_ADMIN_EMAIL rather than --admin-email=: releases before 2.4.1 only read the variable.
|
||||
[ -z "$email" ] || env_args=("DEMO_ADMIN_EMAIL=$email")
|
||||
run_as "$user" "$app/backend" env "${env_args[@]+"${env_args[@]}"}" node scripts/set-demo.js "--profile=$profile" --yes ||
|
||||
die "The demo data wasn't loaded (see above). If it got as far as changing anything, the backup from just now has the data from before."
|
||||
|
||||
step "Events: reloading PM2"
|
||||
events_reload "$dir" "$user"
|
||||
step "Events: waiting for it to answer"
|
||||
wait_for_version "$(events_health_url)" "$(events_expected_version "$dir")" 60 ||
|
||||
die "Events didn't answer after the reload (sudo -u $user pm2 logs)."
|
||||
}
|
||||
|
||||
cmd_export() {
|
||||
local church="" out="" p tmp dir user bytes
|
||||
while [ $# -gt 0 ]; do
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
#!/usr/bin/env bats
|
||||
# crosscode demo (lib/manage.sh): argument checks, then backup → demo data as the site's user →
|
||||
# reload, in that order, and nothing after a step that fails.
|
||||
|
||||
setup() {
|
||||
export CROSSCODE_NO_UPDATE=1
|
||||
CC="$BATS_TEST_DIRNAME/../crosscode"
|
||||
TMP="$BATS_TEST_TMPDIR"
|
||||
CALLS="$TMP/calls"
|
||||
: > "$CALLS"
|
||||
# shellcheck source=../lib/common.sh
|
||||
source "$BATS_TEST_DIRNAME/../lib/common.sh"
|
||||
# shellcheck source=../lib/manage.sh
|
||||
source "$BATS_TEST_DIRNAME/../lib/manage.sh"
|
||||
|
||||
# A church with Events, and Events' hooks recording what they'd do.
|
||||
export CROSSCODE_HOME="$TMP/home"
|
||||
mkdir -p "$CROSSCODE_HOME/products" "$TMP/sites/hope/events/apps/events/backend/scripts"
|
||||
touch "$TMP/sites/hope/events/apps/events/backend/scripts/set-demo.js"
|
||||
cat > "$CROSSCODE_HOME/products/events.sh" <<EOF
|
||||
events_app() { printf '%s/apps/events' "\$1"; }
|
||||
events_backup() { echo "backup \$2" >> "$CALLS"; [ -z "\${FAIL_BACKUP:-}" ]; }
|
||||
events_reload() { echo "reload \$2" >> "$CALLS"; }
|
||||
events_health_url() { echo http://127.0.0.1:20001/health; }
|
||||
events_expected_version() { echo 2.4.1; }
|
||||
EOF
|
||||
require_root() { :; }
|
||||
load_church() { [ "$1" = hope ] || die "There's no church $1 here."; }
|
||||
church_has_product() { [ "$1" = events ]; }
|
||||
pvar() { case "$2" in DIR) echo "$TMP/sites/hope/events" ;; USER) echo cc-hope ;; esac; }
|
||||
checkout_ref() { echo events@2.4.1; }
|
||||
wait_for_version() { echo "wait $2" >> "$CALLS"; }
|
||||
run_as() { local user="$1" dir="$2"; shift 2; echo "run $user ${dir#"$TMP"/} $*" >> "$CALLS"; [ -z "${FAIL_SEED:-}" ]; }
|
||||
}
|
||||
|
||||
@test "the profile and admin email are checked before anything else" {
|
||||
CROSSCODE_HOME= run bash "$CC" demo hope
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" == *"--profile: one of small events ministry multicampus"* ]]
|
||||
CROSSCODE_HOME= run bash "$CC" demo hope --profile everything
|
||||
[ "$status" -eq 1 ]
|
||||
CROSSCODE_HOME= run bash "$CC" demo hope --profile small --admin-email 'x; rm -rf /'
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" == *"not an email address"* ]]
|
||||
CROSSCODE_HOME= run bash "$CC" help demo
|
||||
[[ "$output" == *"crosscode demo <church> --profile"* ]]
|
||||
}
|
||||
|
||||
@test "backup, then the demo as the site's user (no prompt), then a reload" {
|
||||
run cmd_demo hope --profile ministry
|
||||
[ "$status" -eq 0 ]
|
||||
[ "$(cat "$CALLS")" = "backup cc-hope
|
||||
run cc-hope sites/hope/events/apps/events/backend env node scripts/set-demo.js --profile=ministry --yes
|
||||
reload cc-hope
|
||||
wait 2.4.1" ]
|
||||
}
|
||||
|
||||
@test "the admin to keep goes in DEMO_ADMIN_EMAIL (older releases only read that)" {
|
||||
run cmd_demo hope --profile small --admin-email jane@hope.test
|
||||
[ "$status" -eq 0 ]
|
||||
grep -qx 'run cc-hope sites/hope/events/apps/events/backend env DEMO_ADMIN_EMAIL=jane@hope.test node scripts/set-demo.js --profile=small --yes' "$CALLS"
|
||||
}
|
||||
|
||||
@test "a failed backup stops it before the data is touched" {
|
||||
FAIL_BACKUP=1 run cmd_demo hope --profile small
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" == *"The backup failed"* ]]
|
||||
[ "$(cat "$CALLS")" = "backup cc-hope" ]
|
||||
}
|
||||
|
||||
@test "a failed demo isn't followed by a reload" {
|
||||
FAIL_SEED=1 run cmd_demo hope --profile small
|
||||
[ "$status" -eq 1 ]
|
||||
! grep -q reload "$CALLS"
|
||||
}
|
||||
|
||||
@test "a release without the demo script is refused" {
|
||||
rm "$TMP/sites/hope/events/apps/events/backend/scripts/set-demo.js"
|
||||
run cmd_demo hope --profile small
|
||||
[ "$status" -eq 1 ]
|
||||
[[ "$output" == *"has no demo data"* ]]
|
||||
[ ! -s "$CALLS" ]
|
||||
}
|
||||
Reference in New Issue
Block a user