From e1c6a4cce16eefcb00c98a8f7063a57d88dae86b Mon Sep 17 00:00:00 2001 From: Joshua Oosthuizen Date: Wed, 18 Nov 2026 14:31:36 +0200 Subject: [PATCH] crosscode: install and add Accounts; agent: Accounts installs and add-product crosscode install takes --products accounts (one product per install); crosscode add installs a product next to a church's others, as its app user, undoing only its own folder, database, PM2 and site if a step fails. install_events/install_accounts share the checks, ports and nginx site. Agent 1.1.0: install jobs for Accounts, and add-product jobs (behind frp, with the Hub's ports and release). Co-Authored-By: Claude Opus 5.5 --- apps/agent/package.json | 2 +- apps/agent/src/command.ts | 28 +-- apps/agent/tests/command.test.ts | 15 ++ scripts/crosscode | 4 +- scripts/lib/install.sh | 296 ++++++++++++++++++++++++++----- scripts/tests/cli.bats | 32 ++++ 6 files changed, 322 insertions(+), 55 deletions(-) diff --git a/apps/agent/package.json b/apps/agent/package.json index c52d2ef..39cf93f 100644 --- a/apps/agent/package.json +++ b/apps/agent/package.json @@ -1,6 +1,6 @@ { "name": "@crosscode/agent", - "version": "1.0.1", + "version": "1.1.0", "private": true, "description": "The server agent: runs the crosscode script for signed jobs from Cross Code Hub, reports stats (docs/AGENT-1.0-PLAN.md)", "license": "UNLICENSED", diff --git a/apps/agent/src/command.ts b/apps/agent/src/command.ts index c428f01..f8ca2d6 100644 --- a/apps/agent/src/command.ts +++ b/apps/agent/src/command.ts @@ -11,21 +11,27 @@ export function crosscodeArgs(job: agent.AgentJob, opts: { exportFile?: string } switch (job.type) { case "install": { const a = job.args; - if (a.products.length !== 1 || a.products[0] !== "events") throw new Error("This agent can only install Events so far."); - const domain = a.domains.events; - const ports = a.ports.events; - if (!domain || !ports) throw new Error("An Events install needs its domain and ports."); - // The church's address: the Events domain without "events." (crosscode uses it for the - // church.env ADDRESS; the domain itself goes in --events-domain). - const address = domain.startsWith("events.") ? domain.slice("events.".length) : domain; - const out = ["install", a.church, "--products", "events", "--domain", address, "--events-domain", domain, + const product = a.products[0]; + if (a.products.length !== 1 || (product !== "events" && product !== "accounts")) throw new Error("This agent installs one product at a time: Events or Accounts."); + const domain = a.domains[product]; + const ports = a.ports[product]; + if (!domain || !ports) throw new Error(`An install of ${product} needs its domain and ports.`); + // The church's address: the product's domain without "." (crosscode keeps it in + // church.env ADDRESS for the products added later; the domain itself goes in ---domain). + const address = domain.startsWith(`${product}.`) ? domain.slice(product.length + 1) : domain; + const out = ["install", a.church, "--products", product, "--domain", address, `--${product}-domain`, domain, "--api-port", String(ports.api), "--web-port", String(ports.web), "--frp"]; - const v = a.versions?.events; + const v = a.versions?.[product]; if (v) out.push("--version", v); return out; } - case "add-product": - return ["add", job.args.church, job.args.product]; + case "add-product": { + // A product next to the church's others (phase 3: Accounts next to Events), behind frp. + const a = job.args; + const out = ["add", a.church, a.product, "--domain", a.domain, "--api-port", String(a.ports.api), "--web-port", String(a.ports.web), "--frp"]; + if (a.version) out.push("--version", a.version); + return out; + } case "deploy": { const out = ["deploy", job.args.church, ...job.args.products]; for (const v of job.args.versions ?? []) out.push("--version", v); diff --git a/apps/agent/tests/command.test.ts b/apps/agent/tests/command.test.ts index e6ea869..aa4b091 100644 --- a/apps/agent/tests/command.test.ts +++ b/apps/agent/tests/command.test.ts @@ -12,6 +12,21 @@ describe("crosscode arguments for each job", () => { }))).toEqual(["install", "hope", "--products", "events", "--domain", "hope.co.za", "--events-domain", "events.hope.co.za", "--api-port", "20011", "--web-port", "20010", "--frp", "--version", "events@2.4.1"]); }); + it("install: Accounts as a church's first product", () => { + expect(crosscodeArgs(parsed({ + type: "install", + args: { church: "grace", products: ["accounts"], domains: { accounts: "accounts.grace.crosscode.co.za" }, ports: { accounts: { web: 20020, api: 20021 } } }, + }))).toEqual(["install", "grace", "--products", "accounts", "--domain", "grace.crosscode.co.za", "--accounts-domain", "accounts.grace.crosscode.co.za", "--api-port", "20021", "--web-port", "20020", "--frp"]); + expect(() => crosscodeArgs(parsed({ type: "install", args: { church: "grace", products: ["website"], domains: { website: "www.grace.co.za" }, ports: { website: { web: 20030, api: 20031 } } } }))).toThrow(/one product at a time/); + }); + + it("add-product: Accounts next to Events, behind frp", () => { + expect(crosscodeArgs(parsed({ + type: "add-product", + args: { church: "hope", product: "accounts", version: "accounts@1.0.0", domain: "accounts.hope.co.za", ports: { web: 20012, api: 20013 } }, + }))).toEqual(["add", "hope", "accounts", "--domain", "accounts.hope.co.za", "--api-port", "20013", "--web-port", "20012", "--frp", "--version", "accounts@1.0.0"]); + }); + it("deploy, adopt, restart, uninstall, purge, status", () => { expect(crosscodeArgs(parsed({}))).toEqual(["deploy", "hope", "events", "--version", "events@2.4.1"]); expect(crosscodeArgs(parsed({ args: { church: "hope", products: ["events"], check: true } }))).toEqual(["deploy", "hope", "events", "--check"]); diff --git a/scripts/crosscode b/scripts/crosscode index 72692cb..27fb239 100755 --- a/scripts/crosscode +++ b/scripts/crosscode @@ -92,6 +92,7 @@ crosscode: Cross Code installs on this server ($(git -C "${CROSSCODE_FROM:-.}" r crosscode deploy … --check only the checks; changes nothing crosscode status [] versions, latest releases, health crosscode install --products events --domain
a new church + crosscode add accounts [--domain …] add a product to a church here crosscode adopt --events-dir move an existing Cross Events install here crosscode adopt --undo go back to the old folder (within 14 days) crosscode restart [events] reload the PM2 processes @@ -114,12 +115,13 @@ case "$command" in export) cmd_export "$@" ;; uninstall) cmd_uninstall "$@" ;; purge) cmd_purge "$@" ;; - add) die "\`crosscode add\` comes with the second product (phase 3)." ;; + add) cmd_add "$@" ;; help|-h|--help) case "${1:-}" in deploy) deploy_usage ;; status) status_usage ;; install) install_usage ;; + add) add_usage ;; adopt) adopt_usage ;; restart|export|uninstall|purge) manage_usage ;; *) usage ;; diff --git a/scripts/lib/install.sh b/scripts/lib/install.sh index 6873e4f..fa1bb16 100644 --- a/scripts/lib/install.sh +++ b/scripts/lib/install.sh @@ -5,7 +5,7 @@ install_usage() { cat <<'EOF' -crosscode install --products events --domain
[options] +crosscode install --products events|accounts --domain
[options] A new church: system packages if missing, then per product an app user, database, checkout, .env files (from the release's env.template.json), PM2 processes, and either an nginx site with @@ -13,10 +13,12 @@ a certificate (a server with its own address) or frpc tunnels (--frp, behind the server). Writes /var/www/sites//church.env. If a step fails, everything this run created is removed again. - --products events the products to install (only events in this version) + --products the product to install first (events or accounts; add more with + `crosscode add`) --domain
the church's address: hopefamily.co.za or hopefamily.crosscode.co.za; - Events is served on events.
- --events-domain serve Events somewhere else instead + the product is served on .
+ --events-domain serve the product somewhere other than .
+ --accounts-domain (the same, for Accounts) --version events@x.y.z that release (default: the latest) --main the latest main (staging churches only) --user the Linux user the apps run as (default: cc-) @@ -75,12 +77,12 @@ install_undo() { } cmd_install() { - local church="" products="" address="" events_domain="" email="" version="" main=0 user="" api="" web="" ssl=1 frp=0 keep=0 + local church="" products="" address="" pdomain="" email="" version="" main=0 user="" api="" web="" ssl=1 frp=0 keep=0 while [ $# -gt 0 ]; do case "$1" in --products) products="$(printf '%s' "$2" | tr ',' ' ')"; shift 2 ;; --domain) address="$2"; shift 2 ;; - --events-domain) events_domain="$2"; shift 2 ;; + --events-domain|--accounts-domain) pdomain="$2"; shift 2 ;; --email) email="$2"; shift 2 ;; --version) version="$2"; shift 2 ;; --main) main=1; shift ;; @@ -98,40 +100,24 @@ cmd_install() { [ -n "$church" ] || { install_usage; die "Name the church."; } valid_slug "$church" || die "\"$church\" isn't a church name (a-z, 0-9 and -, starting with a letter)." [ -n "$products" ] || die "--products is required (e.g. --products events)." - local p + local p product="" for p in $products; do is_known_product "$p" || die "Unknown product \"$p\"." - [ "$p" = events ] || die "This version of crosscode can only install events." + [ "$p" = events ] || [ "$p" = accounts ] || die "This version of crosscode can only install events and accounts." + [ -z "$product" ] || die "One product per install; add the others with \`crosscode add\`." + product="$p" done [[ "$address" =~ ^[a-z0-9]([a-z0-9.-]*[a-z0-9])?\.[a-z]{2,}$ ]] || die "--domain: give the church's address, e.g. hopefamily.co.za." - events_domain="${events_domain:-events.$address}" - [[ "$events_domain" =~ ^[a-z0-9]([a-z0-9.-]*[a-z0-9])?\.[a-z]{2,}$ ]] || die "--events-domain: expected a domain." - [ "$ssl" = 0 ] || [ -n "$email" ] || die "--email is required for the certificate (or use --frp or --no-ssl)." - [ -z "$version" ] || [[ "$version" =~ ^events@[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$ ]] || die "--version: expected events@x.y.z." - [ "$main" = 0 ] || [ -z "$version" ] || die "Use --main or --version, not both." - if [ "$frp" = 1 ]; then - [ -n "$api" ] && [ -n "$web" ] || die "--frp needs --api-port and --web-port (from the Hub's port list)." - fi + pdomain="${pdomain:-$product.$address}" + [[ "$pdomain" =~ ^[a-z0-9]([a-z0-9.-]*[a-z0-9])?\.[a-z]{2,}$ ]] || die "--$product-domain: expected a domain." + install_check_options "$product" "$ssl" "$email" "$version" "$main" "$frp" "$api" "$web" user="${user:-cc-$church}" user="${user:0:32}" require_root - [ ! -f "$(church_env_path "$church")" ] || die "$church is already installed here (crosscode status $church). New products for it come with \`crosscode add\` (phase 3)." + [ ! -f "$(church_env_path "$church")" ] || die "$church is already installed here (crosscode status $church). Add products with \`crosscode add $church \`." [ ! -e "$(church_dir "$church")" ] || die "$(church_dir "$church") exists without a church.env. Move it away first." - - local taken - mapfile -t taken < <(used_ports) - if [ -n "$api" ] && printf '%s\n' "${taken[@]}" | grep -qx "$api"; then die "Port $api is already in use here."; fi - if [ -n "$web" ] && printf '%s\n' "${taken[@]}" | grep -qx "$web"; then die "Port $web is already in use here."; fi - api="${api:-$(free_port 5000 "${taken[@]}")}" - web="${web:-$(free_port 3000 "${taken[@]}" "$api")}" - [[ "$api" =~ ^[0-9]+$ && "$web" =~ ^[0-9]+$ && "$api" != "$web" ]] || die "Ports must be two different numbers." - if [ "$frp" = 1 ]; then - command -v "$FRPC_BIN" >/dev/null || die "frpc isn't installed here ($FRPC_BIN)." - frpc_check_format - fi + read -r api web <<< "$(install_ports "$api" "$web")" report_result church "$church" - report_result_json ports "{\"events\":{\"web\":$web,\"api\":$api}}" - report_result_json domains "{\"events\":\"$events_domain\"}" INSTALL_UNDO="$(mktemp)" local rc @@ -144,15 +130,66 @@ cmd_install() { useradd --create-home --shell /bin/bash "$user" undo_add user "$user" fi + mkdir -p "$(church_dir "$church")" + undo_add dir "$(church_dir "$church")" + chmod 755 "$CROSSCODE_SITES" "$(church_dir "$church")" CENV=() church_env_set CHURCH "$church" - church_env_set PRODUCTS "events" + church_env_set PRODUCTS "$product" church_env_set ADDRESS "$address" - install_events "$church" "$user" "$events_domain" "$api" "$web" "$version" "$main" "$ssl" "$email" "$frp" + install_product "$product" "$church" "$user" "$pdomain" "$api" "$web" "$version" "$main" "$ssl" "$email" "$frp" church_env_write "$(church_env_path "$church")" ) rc=$? set -e + install_finish "$rc" "$keep" "$church" + install_done "$church" "$product" "$pdomain" "$api" "$web" "$frp" +} + +# The checks every install and add share, before anything is touched. +install_check_options() { + local product="$1" ssl="$2" email="$3" version="$4" main="$5" frp="$6" api="$7" web="$8" + [ "$ssl" = 0 ] || [ -n "$email" ] || die "--email is required for the certificate (or use --frp or --no-ssl)." + [ -z "$version" ] || [[ "$version" =~ ^$product@[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$ ]] || die "--version: expected $product@x.y.z." + [ "$main" = 0 ] || [ -z "$version" ] || die "Use --main or --version, not both." + if [ "$frp" = 1 ]; then + [ -n "$api" ] && [ -n "$web" ] || die "--frp needs --api-port and --web-port (from the Hub's port list)." + fi + return 0 +} + +# " ": the ones given (checked free), or the first free from 5000 / 3000. +install_ports() { + local api="$1" web="$2" taken + mapfile -t taken < <(used_ports) + if [ -n "$api" ] && printf '%s\n' "${taken[@]}" | grep -qx "$api"; then die "Port $api is already in use here."; fi + if [ -n "$web" ] && printf '%s\n' "${taken[@]}" | grep -qx "$web"; then die "Port $web is already in use here."; fi + api="${api:-$(free_port 5000 "${taken[@]}")}" + web="${web:-$(free_port 3000 "${taken[@]}" "$api")}" + [[ "$api" =~ ^[0-9]+$ && "$web" =~ ^[0-9]+$ && "$api" != "$web" ]] || die "Ports must be two different numbers." + printf '%s %s\n' "$api" "$web" +} + +# install_product
+install_product() { + local product="$1" + shift + if [ "${10}" = 1 ]; then + command -v "$FRPC_BIN" >/dev/null || die "frpc isn't installed here ($FRPC_BIN)." + frpc_check_format + fi + report_result_json ports "{\"$product\":{\"web\":$5,\"api\":$4}}" + report_result_json domains "{\"$product\":\"$3\"}" + case "$product" in + events) install_events "$@" ;; + accounts) install_accounts "$@" ;; + *) die "This version of crosscode can't install $product." ;; + esac +} + +# After the subshell: undo what was made if it failed (unless --keep-on-failure). +install_finish() { + local rc="$1" keep="$2" church="$3" if [ "$rc" != 0 ]; then if [ "$keep" = 1 ]; then warn "Install failed; --keep-on-failure: what was made is left in place ($(church_dir "$church"))." @@ -163,15 +200,106 @@ cmd_install() { die "Install of $church failed." fi rm -f "$INSTALL_UNDO" +} +install_done() { + local church="$1" product="$2" domain="$3" api="$4" web="$5" frp="$6" step "Done" if [ "$frp" = 1 ]; then - info "Tunnels: $church-events-web → $web, $church-events-api → $api. Set up the site on the central server." + info "Tunnels: $church-$product-web → $web, $church-$product-api → $api. Set up the site on the central server." else - info "DNS (if not set yet): $events_domain → this server (A record, or a CNAME)." + info "DNS (if not set yet): $domain → this server (A record, or a CNAME)." fi - info "Open https://$events_domain/setup to create the admin account." - info "Updates: crosscode deploy $church events" + info "Open https://$domain/setup to create the admin account." + if [ "$product" = accounts ]; then info "Updates: crosscode deploy $church (Accounts is always updated first)"; else info "Updates: crosscode deploy $church $product"; fi +} + +add_usage() { + cat <<'EOF' +crosscode add [options] + +Adds a product to a church already installed here (from phase 3: Accounts next to Events), in +its own folder, database, PM2 processes and site, run by the church's app user. If a step fails, +what this run made is removed again; the church's other products aren't touched. + + --domain where it's served (default: .) + --version @x.y.z that release (default: the latest) + --main the latest main (staging churches only) + --api-port backend port (default: the first free one from 5000) + --web-port frontend port (default: the first free one from 3000) + --frp behind frp: tunnels in frpc.ini (ports must be given); no nginx here + --email for Let's Encrypt notices (required unless --frp or --no-ssl) + --no-ssl no certificate (behind another proxy that does HTTPS) + --config the .env values by layer (JSON; given by the agent) + --keep-on-failure leave what was made if a step fails + --json print a report (JSON) at the end +EOF +} + +cmd_add() { + local church="" product="" domain="" email="" version="" main=0 api="" web="" ssl=1 frp=0 keep=0 + while [ $# -gt 0 ]; do + case "$1" in + --domain) domain="$2"; shift 2 ;; + --email) email="$2"; shift 2 ;; + --version) version="$2"; shift 2 ;; + --main) main=1; shift ;; + --api-port) api="$2"; shift 2 ;; + --web-port) web="$2"; shift 2 ;; + --no-ssl) ssl=0; shift ;; + --frp) frp=1; ssl=0; shift ;; + --keep-on-failure) keep=1; shift ;; + -h|--help) add_usage; return 0 ;; + -*) die "Unknown option $1 (crosscode add --help)." ;; + *) + if [ -z "$church" ]; then church="$1" + elif [ -z "$product" ]; then product="$1" + else die "crosscode add : one product at a time." + fi + shift ;; + esac + done + [ -n "$church" ] && [ -n "$product" ] || { add_usage; die "Name the church and the product."; } + valid_slug "$church" || die "\"$church\" isn't a church name." + is_known_product "$product" || die "Unknown product \"$product\"." + [ "$product" = events ] || [ "$product" = accounts ] || die "This version of crosscode can only add events and accounts." + install_check_options "$product" "$ssl" "$email" "$version" "$main" "$frp" "$api" "$web" + require_root + load_church "$church" + ! church_has_product "$product" || die "$church already has $product (crosscode status $church)." + local address user="" existing + address="$(cenv ADDRESS)" + [ -n "$domain" ] || [ -n "$address" ] || die "--domain is needed: the church has no address in church.env." + domain="${domain:-$product.$address}" + [[ "$domain" =~ ^[a-z0-9]([a-z0-9.-]*[a-z0-9])?\.[a-z]{2,}$ ]] || die "--domain: expected a domain." + [ ! -e "$(church_dir "$church")/$product" ] || die "$(church_dir "$church")/$product exists already. Move it away first." + # The church's app user: the one its other products run as. + for existing in $(cenv PRODUCTS); do + user="$(pvar "$existing" USER)" + [ -z "$user" ] || break + done + user="${user:-cc-$church}" + id "$user" >/dev/null 2>&1 || die "The church's app user $user doesn't exist." + read -r api web <<< "$(install_ports "$api" "$web")" + report_result church "$church" + + INSTALL_UNDO="$(mktemp)" + local rc + set +e + ( + set -e + command -v pm2 >/dev/null || install_system_packages "$ssl" "$frp" + install_product "$product" "$church" "$user" "$domain" "$api" "$web" "$version" "$main" "$ssl" "$email" "$frp" + local -a all + # shellcheck disable=SC2046 # the product names are single words + mapfile -t all < <(in_product_order $(cenv PRODUCTS) "$product") + church_env_set PRODUCTS "${all[*]}" + church_env_write "$(church_env_path "$church")" + ) + rc=$? + set -e + install_finish "$rc" "$keep" "$church" + install_done "$church" "$product" "$domain" "$api" "$web" "$frp" } install_system_packages() { @@ -211,10 +339,8 @@ install_events() { [ "$frp" = 0 ] || church_env_set EVENTS_FRP 1 step "Events: code ($dir)" - mkdir -p "$(church_dir "$church")" - undo_add dir "$(church_dir "$church")" - chmod 755 "$CROSSCODE_SITES" "$(church_dir "$church")" mkdir -p "$dir" + undo_add dir "$dir" chown "$user:$user" "$dir" run_as "$user" "$dir" git clone --quiet "$CROSSCODE_REPO" . run_as "$user" "$dir" git fetch --tags --force --quiet origin @@ -269,7 +395,7 @@ install_events() { frpc_reload else step "Events: nginx ($domain)" - install_events_nginx "$name" "$domain" "$api" "$web" + install_site_nginx "$name" "$domain" "$api" "$web" undo_add nginx "$name" if [ "$ssl" = 1 ]; then step "Events: certificate" @@ -282,7 +408,93 @@ install_events() { die "The backend didn't answer with its version (sudo -u $user pm2 logs $name-backend)." } -install_events_nginx() { +install_accounts() { + local church="$1" user="$2" domain="$3" api="$4" web="$5" version="$6" main="$7" ssl="$8" email="$9" frp="${10}" + local dir app db pass name ref + # shellcheck source=/dev/null + source "$CROSSCODE_HOME/products/accounts.sh" + dir="$(church_dir "$church")/accounts" + app="$dir/apps/accounts" + db="$(printf '%s_accounts' "$church" | tr '-' '_')" + name="$church-accounts" + church_env_set ACCOUNTS_DIR "$dir" + church_env_set ACCOUNTS_DOMAIN "$domain" + church_env_set ACCOUNTS_PM2 "$name-backend $name-frontend" + church_env_set ACCOUNTS_PORTS "$api $web" + church_env_set ACCOUNTS_DB "$db" + church_env_set ACCOUNTS_USER "$user" + [ "$frp" = 0 ] || church_env_set ACCOUNTS_FRP 1 + + step "Accounts: code ($dir)" + mkdir -p "$dir" + undo_add dir "$dir" + chown "$user:$user" "$dir" + run_as "$user" "$dir" git clone --quiet "$CROSSCODE_REPO" . + run_as "$user" "$dir" git fetch --tags --force --quiet origin + if [ "$main" = 1 ]; then + version=origin/main + elif [ -z "$version" ]; then + version="$(latest_tag "$dir" accounts)" + [ -n "$version" ] || die "No accounts@x.y.z release tags yet (use --version or --main)." + fi + run_as "$user" "$dir" git checkout -q --detach "$version" + ref="$(checkout_ref "$dir" accounts)" + info "At $ref" + report_result_json versions "{\"accounts\":\"$ref\"}" + + step "Accounts: database ($db on $(db_host))" + ! db_exists "$db" || die "A database or role named $db already exists on $(db_host). Remove it first if it's left over, or choose another church name." + pass="$(random_secret 32)" + undo_add db "$db" + db_create "$db" "$pass" + + step "Accounts: configuration" + local trust=1 + [ "$frp" = 0 ] || trust=loopback + ( + export CROSSCODE_GEN_NODE_ENV=production + export CROSSCODE_GEN_ACCOUNTS_SECRET + CROSSCODE_GEN_ACCOUNTS_SECRET="$(random_secret 64)" + export CROSSCODE_GEN_DATABASE_URL="postgresql://$db:$pass@$(db_host):$(db_port)/$db" + env_apply "$app" accounts "$ref" --var "domain=$domain" --var "apiPort=$api" --var "webPort=$web" --var "church=$church" \ + --fallback "TRUST_PROXY=$trust" + ) | sed 's/^/ /' + [ "${PIPESTATUS[0]}" = 0 ] || die "The .env files couldn't be written (see above)." + chown "$user:$user" "$app/backend/.env" "$app/frontend/.env.production" + chmod 600 "$app/backend/.env" "$app/frontend/.env.production" + accounts_ensure_pm2_config "$dir" "$user" + + step "Accounts: install, migrate, build" + accounts_install_deps "$dir" "$user" + accounts_migrate "$dir" "$user" + accounts_build "$dir" "$user" + + step "Accounts: PM2" + undo_add pm2 "$user" "$name-backend $name-frontend" + run_as "$user" "$app" pm2 startOrReload ecosystem.config.cjs + run_as "$user" "$app" pm2 save + + if [ "$frp" = 1 ]; then + step "Accounts: frpc tunnels" + frpc_add "$church" accounts "$web" "$api" + undo_add frpc "$church" accounts + frpc_reload + else + step "Accounts: nginx ($domain)" + install_site_nginx "$name" "$domain" "$api" "$web" + undo_add nginx "$name" + if [ "$ssl" = 1 ]; then + step "Accounts: certificate" + certbot --nginx -d "$domain" --non-interactive --agree-tos -m "$email" --redirect + fi + fi + + step "Accounts: checking it answers" + wait_for_version "$(accounts_health_url)" "$(accounts_expected_version "$dir")" 90 || + die "The backend didn't answer with its version (sudo -u $user pm2 logs $name-backend)." +} + +install_site_nginx() { local name="$1" domain="$2" api="$3" web="$4" # Only this domain is served by this site (no catch-all), as ROADMAP §8 asks. cat > "/etc/nginx/sites-available/$name" < "* ]] +}