crosscode: install and add Accounts; agent: Accounts installs and add-product

crosscode install takes --products accounts (one product per install);
crosscode add <church> <product> 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 <noreply@anthropic.com>
This commit is contained in:
2026-11-18 14:31:36 +02:00
co-authored by Claude Opus 5.5
parent 7f34e184e1
commit e1c6a4cce1
6 changed files with 322 additions and 55 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "@crosscode/agent", "name": "@crosscode/agent",
"version": "1.0.1", "version": "1.1.0",
"private": true, "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)", "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", "license": "UNLICENSED",
+17 -11
View File
@@ -11,21 +11,27 @@ export function crosscodeArgs(job: agent.AgentJob, opts: { exportFile?: string }
switch (job.type) { switch (job.type) {
case "install": { case "install": {
const a = job.args; 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 product = a.products[0];
const domain = a.domains.events; if (a.products.length !== 1 || (product !== "events" && product !== "accounts")) throw new Error("This agent installs one product at a time: Events or Accounts.");
const ports = a.ports.events; const domain = a.domains[product];
if (!domain || !ports) throw new Error("An Events install needs its domain and ports."); const ports = a.ports[product];
// The church's address: the Events domain without "events." (crosscode uses it for the if (!domain || !ports) throw new Error(`An install of ${product} needs its domain and ports.`);
// church.env ADDRESS; the domain itself goes in --events-domain). // The church's address: the product's domain without "<product>." (crosscode keeps it in
const address = domain.startsWith("events.") ? domain.slice("events.".length) : domain; // church.env ADDRESS for the products added later; the domain itself goes in --<product>-domain).
const out = ["install", a.church, "--products", "events", "--domain", address, "--events-domain", 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"]; "--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); if (v) out.push("--version", v);
return out; return out;
} }
case "add-product": case "add-product": {
return ["add", job.args.church, job.args.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": { case "deploy": {
const out = ["deploy", job.args.church, ...job.args.products]; const out = ["deploy", job.args.church, ...job.args.products];
for (const v of job.args.versions ?? []) out.push("--version", v); for (const v of job.args.versions ?? []) out.push("--version", v);
+15
View File
@@ -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"]); }))).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", () => { it("deploy, adopt, restart, uninstall, purge, status", () => {
expect(crosscodeArgs(parsed({}))).toEqual(["deploy", "hope", "events", "--version", "events@2.4.1"]); 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"]); expect(crosscodeArgs(parsed({ args: { church: "hope", products: ["events"], check: true } }))).toEqual(["deploy", "hope", "events", "--check"]);
+3 -1
View File
@@ -92,6 +92,7 @@ crosscode: Cross Code installs on this server ($(git -C "${CROSSCODE_FROM:-.}" r
crosscode deploy <church> … --check only the checks; changes nothing crosscode deploy <church> … --check only the checks; changes nothing
crosscode status [<church>] versions, latest releases, health crosscode status [<church>] versions, latest releases, health
crosscode install <church> --products events --domain <address> a new church crosscode install <church> --products events --domain <address> a new church
crosscode add <church> accounts [--domain …] add a product to a church here
crosscode adopt <church> --events-dir <old folder> move an existing Cross Events install here 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 adopt <church> --undo go back to the old folder (within 14 days)
crosscode restart <church> [events] reload the PM2 processes crosscode restart <church> [events] reload the PM2 processes
@@ -114,12 +115,13 @@ case "$command" in
export) cmd_export "$@" ;; export) cmd_export "$@" ;;
uninstall) cmd_uninstall "$@" ;; uninstall) cmd_uninstall "$@" ;;
purge) cmd_purge "$@" ;; purge) cmd_purge "$@" ;;
add) die "\`crosscode add\` comes with the second product (phase 3)." ;; add) cmd_add "$@" ;;
help|-h|--help) help|-h|--help)
case "${1:-}" in case "${1:-}" in
deploy) deploy_usage ;; deploy) deploy_usage ;;
status) status_usage ;; status) status_usage ;;
install) install_usage ;; install) install_usage ;;
add) add_usage ;;
adopt) adopt_usage ;; adopt) adopt_usage ;;
restart|export|uninstall|purge) manage_usage ;; restart|export|uninstall|purge) manage_usage ;;
*) usage ;; *) usage ;;
+254 -42
View File
@@ -5,7 +5,7 @@
install_usage() { install_usage() {
cat <<'EOF' cat <<'EOF'
crosscode install <church> --products events --domain <address> [options] crosscode install <church> --products events|accounts --domain <address> [options]
A new church: system packages if missing, then per product an app user, database, checkout, 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 .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>/church.env. If a step fails, everything this run created server). Writes /var/www/sites/<church>/church.env. If a step fails, everything this run created
is removed again. is removed again.
--products events the products to install (only events in this version) --products <product> the product to install first (events or accounts; add more with
`crosscode add`)
--domain <address> the church's address: hopefamily.co.za or hopefamily.crosscode.co.za; --domain <address> the church's address: hopefamily.co.za or hopefamily.crosscode.co.za;
Events is served on events.<address> the product is served on <product>.<address>
--events-domain <domain> serve Events somewhere else instead --events-domain <domain> serve the product somewhere other than <product>.<address>
--accounts-domain <domain> (the same, for Accounts)
--version events@x.y.z that release (default: the latest) --version events@x.y.z that release (default: the latest)
--main the latest main (staging churches only) --main the latest main (staging churches only)
--user <name> the Linux user the apps run as (default: cc-<church>) --user <name> the Linux user the apps run as (default: cc-<church>)
@@ -75,12 +77,12 @@ install_undo() {
} }
cmd_install() { 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 while [ $# -gt 0 ]; do
case "$1" in case "$1" in
--products) products="$(printf '%s' "$2" | tr ',' ' ')"; shift 2 ;; --products) products="$(printf '%s' "$2" | tr ',' ' ')"; shift 2 ;;
--domain) address="$2"; 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 ;; --email) email="$2"; shift 2 ;;
--version) version="$2"; shift 2 ;; --version) version="$2"; shift 2 ;;
--main) main=1; shift ;; --main) main=1; shift ;;
@@ -98,40 +100,24 @@ cmd_install() {
[ -n "$church" ] || { install_usage; die "Name the church."; } [ -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)." 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)." [ -n "$products" ] || die "--products is required (e.g. --products events)."
local p local p product=""
for p in $products; do for p in $products; do
is_known_product "$p" || die "Unknown product \"$p\"." 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 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." [[ "$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}" pdomain="${pdomain:-$product.$address}"
[[ "$events_domain" =~ ^[a-z0-9]([a-z0-9.-]*[a-z0-9])?\.[a-z]{2,}$ ]] || die "--events-domain: expected a domain." [[ "$pdomain" =~ ^[a-z0-9]([a-z0-9.-]*[a-z0-9])?\.[a-z]{2,}$ ]] || die "--$product-domain: expected a domain."
[ "$ssl" = 0 ] || [ -n "$email" ] || die "--email is required for the certificate (or use --frp or --no-ssl)." install_check_options "$product" "$ssl" "$email" "$version" "$main" "$frp" "$api" "$web"
[ -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
user="${user:-cc-$church}" user="${user:-cc-$church}"
user="${user:0:32}" user="${user:0:32}"
require_root 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 <product>\`."
[ ! -e "$(church_dir "$church")" ] || die "$(church_dir "$church") exists without a church.env. Move it away first." [ ! -e "$(church_dir "$church")" ] || die "$(church_dir "$church") exists without a church.env. Move it away first."
read -r api web <<< "$(install_ports "$api" "$web")"
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
report_result church "$church" report_result church "$church"
report_result_json ports "{\"events\":{\"web\":$web,\"api\":$api}}"
report_result_json domains "{\"events\":\"$events_domain\"}"
INSTALL_UNDO="$(mktemp)" INSTALL_UNDO="$(mktemp)"
local rc local rc
@@ -144,15 +130,66 @@ cmd_install() {
useradd --create-home --shell /bin/bash "$user" useradd --create-home --shell /bin/bash "$user"
undo_add user "$user" undo_add user "$user"
fi fi
mkdir -p "$(church_dir "$church")"
undo_add dir "$(church_dir "$church")"
chmod 755 "$CROSSCODE_SITES" "$(church_dir "$church")"
CENV=() CENV=()
church_env_set CHURCH "$church" church_env_set CHURCH "$church"
church_env_set PRODUCTS "events" church_env_set PRODUCTS "$product"
church_env_set ADDRESS "$address" 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")" church_env_write "$(church_env_path "$church")"
) )
rc=$? rc=$?
set -e 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
}
# "<api> <web>": 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 <product> <church> <user> <domain> <api> <web> <version> <main> <ssl> <email> <frp>
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 [ "$rc" != 0 ]; then
if [ "$keep" = 1 ]; then if [ "$keep" = 1 ]; then
warn "Install failed; --keep-on-failure: what was made is left in place ($(church_dir "$church"))." 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." die "Install of $church failed."
fi fi
rm -f "$INSTALL_UNDO" rm -f "$INSTALL_UNDO"
}
install_done() {
local church="$1" product="$2" domain="$3" api="$4" web="$5" frp="$6"
step "Done" step "Done"
if [ "$frp" = 1 ]; then 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 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 fi
info "Open https://$events_domain/setup to create the admin account." info "Open https://$domain/setup to create the admin account."
info "Updates: crosscode deploy $church events" 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 <church> <product> [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 <domain> where it's served (default: <product>.<the church's address>)
--version <product>@x.y.z that release (default: the latest)
--main the latest main (staging churches only)
--api-port <n> backend port (default: the first free one from 5000)
--web-port <n> frontend port (default: the first free one from 3000)
--frp behind frp: tunnels in frpc.ini (ports must be given); no nginx here
--email <email> for Let's Encrypt notices (required unless --frp or --no-ssl)
--no-ssl no certificate (behind another proxy that does HTTPS)
--config <file> 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 <church> <product>: 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() { install_system_packages() {
@@ -211,10 +339,8 @@ install_events() {
[ "$frp" = 0 ] || church_env_set EVENTS_FRP 1 [ "$frp" = 0 ] || church_env_set EVENTS_FRP 1
step "Events: code ($dir)" 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" mkdir -p "$dir"
undo_add dir "$dir"
chown "$user:$user" "$dir" chown "$user:$user" "$dir"
run_as "$user" "$dir" git clone --quiet "$CROSSCODE_REPO" . run_as "$user" "$dir" git clone --quiet "$CROSSCODE_REPO" .
run_as "$user" "$dir" git fetch --tags --force --quiet origin run_as "$user" "$dir" git fetch --tags --force --quiet origin
@@ -269,7 +395,7 @@ install_events() {
frpc_reload frpc_reload
else else
step "Events: nginx ($domain)" step "Events: nginx ($domain)"
install_events_nginx "$name" "$domain" "$api" "$web" install_site_nginx "$name" "$domain" "$api" "$web"
undo_add nginx "$name" undo_add nginx "$name"
if [ "$ssl" = 1 ]; then if [ "$ssl" = 1 ]; then
step "Events: certificate" 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)." 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" local name="$1" domain="$2" api="$3" web="$4"
# Only this domain is served by this site (no catch-all), as ROADMAP §8 asks. # Only this domain is served by this site (no catch-all), as ROADMAP §8 asks.
cat > "/etc/nginx/sites-available/$name" <<NGINX cat > "/etc/nginx/sites-available/$name" <<NGINX
+32
View File
@@ -85,3 +85,35 @@ setup() {
[ "$status" -eq 1 ] [ "$status" -eq 1 ]
[[ "$output" == *"expected events@x.y.z"* ]] [[ "$output" == *"expected events@x.y.z"* ]]
} }
@test "install takes accounts too, one product at a time" {
run bash "$CC" install hope --products events,accounts --domain hope.co.za --no-ssl
[ "$status" -eq 1 ]
[[ "$output" == *"One product per install"* ]]
run bash "$CC" install hope --products accounts --domain hope.co.za --version events@2.4.0 --no-ssl
[ "$status" -eq 1 ]
[[ "$output" == *"expected accounts@x.y.z"* ]]
run bash "$CC" install hope --products accounts --domain hope.co.za --frp
[ "$status" -eq 1 ]
[[ "$output" == *"--frp needs --api-port and --web-port"* ]]
}
@test "add checks its arguments before anything needs root" {
run bash "$CC" add hope
[ "$status" -eq 1 ]
[[ "$output" == *"Name the church and the product"* ]]
run bash "$CC" add hope website --no-ssl
[ "$status" -eq 1 ]
[[ "$output" == *"can only add events and accounts"* ]]
run bash "$CC" add hope accounts
[ "$status" -eq 1 ]
[[ "$output" == *"--email is required"* ]]
run bash "$CC" help add
[ "$status" -eq 0 ]
[[ "$output" == *"crosscode add <church> <product>"* ]]
}