Accounts: deploy hooks, env template, export, docs
scripts/products/accounts.sh (backup with pg_dump, build with the shared packages, PM2, export, keep, failed-migration recovery); accounts in SUPPORTED_PRODUCTS so deploy updates it where a church has it. Env template in both places, PM2 example, export script, /api/health version from package.json. README, CHANGELOG, WHATS-NEW; the plan's changes while building and what's still to do. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -8,7 +8,7 @@ CROSSCODE_REPO="${CROSSCODE_REPO:-https://c-git.crosscode.co.za/joshua/crosscode
|
||||
# through it.
|
||||
PRODUCT_ORDER=(accounts events website app)
|
||||
# Products this version of the script can deploy (a products/<name>.sh exists for each).
|
||||
SUPPORTED_PRODUCTS=(events)
|
||||
SUPPORTED_PRODUCTS=(accounts events)
|
||||
# Releases older than this get a warning; older than CROSSCODE_NODE_MIN the deploy is refused.
|
||||
CROSSCODE_NODE_MIN=20
|
||||
CROSSCODE_NODE_WARN=22
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"schema": "crosscode.env-template/1",
|
||||
"product": "accounts",
|
||||
"files": {
|
||||
"backend/.env": [
|
||||
{ "name": "NODE_ENV", "layer": "generated", "required": true, "description": "production" },
|
||||
{ "name": "PORT", "layer": "install", "from": "{apiPort}", "required": true, "description": "The backend's port (from the Hub's port list)" },
|
||||
{ "name": "DATABASE_URL", "layer": "generated", "required": true, "description": "Made on the server with a new password; never leaves it" },
|
||||
{ "name": "ACCOUNTS_SECRET", "layer": "generated", "required": true, "description": "Encrypts stored secrets (signing keys, 2FA secrets, link secrets) and signs cookies: never change it" },
|
||||
{ "name": "ACCOUNTS_URL", "layer": "install", "from": "https://{domain}", "required": true, "description": "The public address; the OIDC issuer is <this>/api/oidc" },
|
||||
{ "name": "TRUST_PROXY", "layer": "server", "required": true, "description": "Behind frp: loopback" },
|
||||
{ "name": "APP_TIMEZONE", "layer": "install", "default": "Africa/Johannesburg" },
|
||||
{ "name": "HUB_URL", "layer": "defaults", "default": "https://hub.crosscode.co.za" },
|
||||
{ "name": "HUB_PUBLIC_KEY", "layer": "defaults", "description": "Only for a Hub other than production" },
|
||||
{ "name": "HUB_LICENCE_KEY", "layer": "install", "description": "A Hub install registers by itself with this (CCA-…)" },
|
||||
{ "name": "HUB_ADMIN_NAME", "layer": "install" },
|
||||
{ "name": "HUB_ADMIN_EMAIL", "layer": "install", "description": "Setup only lets this address become the first admin" },
|
||||
{ "name": "JOBS_ENABLED", "layer": "defaults", "default": "true" }
|
||||
],
|
||||
"frontend/.env.production": [
|
||||
{ "name": "ACCOUNTS_API_URL", "layer": "install", "from": "http://127.0.0.1:{apiPort}", "required": true, "description": "Where the pages reach the backend on this server" }
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
# shellcheck shell=bash
|
||||
# Accounts' hooks for the crosscode script (docs/ACCOUNTS-1.0-PLAN.md §9), the same hooks as
|
||||
# products/events.sh. Every hook takes the product's checkout (ACCOUNTS_DIR, a checkout of
|
||||
# crosscode-platform) and the app user. Values come from church.env (CENV): ACCOUNTS_DIR,
|
||||
# ACCOUNTS_DOMAIN, ACCOUNTS_PM2 ("<backend> <frontend>"), ACCOUNTS_PORTS ("<api> <web>"),
|
||||
# ACCOUNTS_DB, ACCOUNTS_USER.
|
||||
|
||||
accounts_app() { printf '%s/apps/accounts' "$1"; }
|
||||
|
||||
accounts_pm2_names() { pvar accounts PM2; }
|
||||
|
||||
accounts_api_port() { local p; read -r p _ <<< "$(pvar accounts PORTS)"; printf '%s' "${p:-5200}"; }
|
||||
accounts_web_port() { local p; read -r _ p <<< "$(pvar accounts PORTS)"; printf '%s' "${p:-3200}"; }
|
||||
accounts_health_url() { printf 'http://127.0.0.1:%s/api/health' "$(accounts_api_port)"; }
|
||||
|
||||
accounts_expected_version() {
|
||||
node -p 'require(process.argv[1]).version' "$(accounts_app "$1")/backend/package.json"
|
||||
}
|
||||
|
||||
# The shared packages Accounts is built with (contracts, auth-client for its tests, ui for its pages).
|
||||
accounts_shared_packages() {
|
||||
local p
|
||||
for p in packages/contracts packages/ui packages/auth-client; do
|
||||
[ -f "$1/$p/package.json" ] && printf '%s\n' "$p"
|
||||
done
|
||||
}
|
||||
|
||||
# A database dump into backend/backups/ (the last 14 are kept), before every deploy and uninstall.
|
||||
accounts_backup() {
|
||||
local dir="$1" user="$2" backend
|
||||
backend="$(accounts_app "$dir")/backend"
|
||||
# shellcheck disable=SC2016 # the app user's shell expands these
|
||||
run_as "$user" "$backend" bash -c '
|
||||
set -euo pipefail
|
||||
url="$(grep -E "^DATABASE_URL=" .env | head -1 | cut -d= -f2- | sed -e "s/^\"//" -e "s/\"$//")"
|
||||
mkdir -p backups
|
||||
file="backups/accounts-$(date +%Y%m%d-%H%M%S).sql.gz"
|
||||
pg_dump --no-owner --no-privileges "$url" | gzip > "$file"
|
||||
chmod 600 "$file"
|
||||
ls -1t backups/accounts-*.sql.gz | tail -n +15 | xargs -r rm -f
|
||||
echo " $file"'
|
||||
}
|
||||
|
||||
accounts_install_deps() {
|
||||
local -a ws=()
|
||||
local p
|
||||
while IFS= read -r p; do ws+=(-w "$p"); done < <(accounts_shared_packages "$1")
|
||||
run_as "$2" "$1" npm ci --include=dev --no-audit --no-fund --include-workspace-root "${ws[@]}" -w apps/accounts/backend -w apps/accounts/frontend
|
||||
}
|
||||
|
||||
accounts_migrate() {
|
||||
run_as "$2" "$(accounts_app "$1")/backend" npm run prisma:deploy
|
||||
}
|
||||
|
||||
accounts_build() {
|
||||
local p
|
||||
for p in packages/contracts packages/auth-client; do
|
||||
[ ! -f "$1/$p/package.json" ] || run_as "$2" "$1/$p" npm run build || return 1
|
||||
done
|
||||
run_as "$2" "$(accounts_app "$1")/backend" npm run build &&
|
||||
run_as "$2" "$(accounts_app "$1")/frontend" npm run build
|
||||
}
|
||||
|
||||
accounts_rollback_build() {
|
||||
accounts_install_deps "$1" "$2" && accounts_build "$1" "$2"
|
||||
}
|
||||
|
||||
# The install's own PM2 config (not in git), made from the example the first time.
|
||||
accounts_ensure_pm2_config() {
|
||||
local dir="$1" user="$2" app conf backend frontend
|
||||
app="$(accounts_app "$dir")"
|
||||
conf="$app/ecosystem.config.cjs"
|
||||
[ -f "$conf" ] && return 0
|
||||
read -r backend frontend <<< "$(accounts_pm2_names)"
|
||||
sed -e "s/'accounts-backend'/'${backend}'/" \
|
||||
-e "s/'accounts-frontend'/'${frontend}'/" \
|
||||
-e "s/-p 3200/-p $(accounts_web_port)/" \
|
||||
"$app/ecosystem.config.example.cjs" > "$conf"
|
||||
chown "$user:$user" "$conf" 2>/dev/null || true
|
||||
info "Created $conf from the example."
|
||||
}
|
||||
|
||||
accounts_reload() {
|
||||
accounts_ensure_pm2_config "$1" "$2"
|
||||
run_as "$2" "$(accounts_app "$1")" pm2 reload ecosystem.config.cjs --update-env
|
||||
}
|
||||
|
||||
# accounts.json (the church profile and the people, no secrets) into <out dir>.
|
||||
accounts_export() {
|
||||
local app
|
||||
app="$(accounts_app "$1")"
|
||||
run_as "$2" "$app/backend" npx tsx scripts/export-church.ts --out-dir "$3"
|
||||
}
|
||||
|
||||
# Accounts has no uploads (the logo is an address).
|
||||
accounts_copy_uploads() { mkdir -p "$2"; }
|
||||
|
||||
# What uninstall keeps for 30 days: the backups and the .env (its ACCOUNTS_SECRET decrypts what's stored).
|
||||
accounts_keep() {
|
||||
local app dest="$2"
|
||||
app="$(accounts_app "$1")"
|
||||
mkdir -p "$dest"
|
||||
[ ! -d "$app/backend/backups" ] || rsync -a "$app/backend/backups/" "$dest/backups/"
|
||||
cp -p "$app/backend/.env" "$dest/backend.env" 2>/dev/null || true
|
||||
chmod -R go-rwx "$dest"
|
||||
}
|
||||
|
||||
accounts_env_vars() {
|
||||
printf '%s\n' --var "domain=$(pvar accounts DOMAIN)" --var "apiPort=$(accounts_api_port)" --var "webPort=$(accounts_web_port)" --var "church=$(cenv CHURCH)"
|
||||
}
|
||||
|
||||
# As events_resolve_failed_migrations: a migration that failed in this release and isn't in the
|
||||
# previous one is marked as rolled back (the backup taken first has the database as it was).
|
||||
accounts_resolve_failed_migrations() {
|
||||
local dir="$1" user="$2" prev="$3" backend name
|
||||
backend="$(accounts_app "$dir")/backend"
|
||||
local -a failed
|
||||
# shellcheck disable=SC2016 # SQL through psql, run as the app user
|
||||
mapfile -t failed < <(run_as "$user" "$backend" bash -c '
|
||||
url="$(grep -E "^DATABASE_URL=" .env | head -1 | cut -d= -f2- | sed -e "s/^\"//" -e "s/\"$//")"
|
||||
psql "$url" -At -c "select migration_name from _prisma_migrations where finished_at is null and rolled_back_at is null"')
|
||||
for name in "${failed[@]}"; do
|
||||
[ -n "$name" ] || continue
|
||||
if git_in "$dir" cat-file -e "$prev:apps/accounts/backend/prisma/migrations/$name/migration.sql" 2>/dev/null; then
|
||||
warn "accounts: $name failed but $prev has it too; left for a look (npx prisma migrate status)."
|
||||
continue
|
||||
fi
|
||||
run_as "$user" "$backend" npx prisma migrate resolve --rolled-back "$name" >/dev/null
|
||||
info "accounts: marked the failed migration $name as rolled back."
|
||||
report_result "resolvedMigration" "$name"
|
||||
done
|
||||
}
|
||||
Reference in New Issue
Block a user