feat(website): crosscode website.sh and signing in through Accounts
crosscode: scripts/products/website.sh (ports 5300/3300, uploads in the export), crosscode add <church> website (needs Accounts; www.<address> or the Cross Code address), one install_app for Accounts and the Website, env template copy, bats tests. Backend: Hub client (protocol v2, product website), crosscode.link/1 with automatic connect to the church's Accounts, sign-in through @crosscode/auth-client with the Website's own session and roles, the first Owner from HUB_ADMIN_EMAIL (verified), 2FA for Owners and Admins, back-channel logout, Accounts' user.* calls, and the church export. Frontend: admin sign-in/out and the sign-in problem page. 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=(accounts events)
|
||||
SUPPORTED_PRODUCTS=(accounts events website)
|
||||
# Releases older than this get a warning; older than CROSSCODE_NODE_MIN the deploy is refused.
|
||||
CROSSCODE_NODE_MIN=20
|
||||
CROSSCODE_NODE_WARN=22
|
||||
@@ -358,8 +358,8 @@ node_major() { node -p 'process.versions.node.split(".")[0]' 2>/dev/null || echo
|
||||
# 1.0–1.1 has no .gitignore for its ecosystem.config.cjs). Nor do the files in the folders this
|
||||
# script writes into (Accounts below 1.3.0 doesn't ignore the database dumps made before each
|
||||
# deploy, so the dump would stop the very deploy that brings the .gitignore).
|
||||
CROSSCODE_OWN_FILES=(apps/events/ecosystem.config.js apps/accounts/ecosystem.config.cjs)
|
||||
CROSSCODE_OWN_DIRS=(apps/accounts/backend/backups/)
|
||||
CROSSCODE_OWN_FILES=(apps/events/ecosystem.config.js apps/accounts/ecosystem.config.cjs apps/website/ecosystem.config.cjs)
|
||||
CROSSCODE_OWN_DIRS=(apps/accounts/backend/backups/ apps/website/backend/backups/)
|
||||
local_changes() {
|
||||
git_in "$1" status --porcelain --untracked-files=all |
|
||||
awk -v own="${CROSSCODE_OWN_FILES[*]}" -v dirs="${CROSSCODE_OWN_DIRS[*]}" '
|
||||
|
||||
+80
-45
@@ -1,7 +1,8 @@
|
||||
# shellcheck shell=bash
|
||||
# crosscode install: a new church on this server (from hope-events' scripts/install.sh 2.1.x;
|
||||
# behind frp from the Hub in agent 1.0, docs/AGENT-1.0-PLAN.md §7).
|
||||
# Events only so far; Accounts, Website and App follow as their products/*.sh are added.
|
||||
# A new church starts with Events or Accounts; the Website is added next to Accounts with
|
||||
# `crosscode add` (WEBSITE-1.0-PLAN §11). The App follows as its products/app.sh is added.
|
||||
|
||||
install_usage() {
|
||||
cat <<'EOF'
|
||||
@@ -160,6 +161,21 @@ cmd_install() {
|
||||
install_done "$church" "$product" "$pdomain" "$api" "$web" "$frp"
|
||||
}
|
||||
|
||||
# Where a product goes on the church's address: <product>.<address>, except the Website, which is
|
||||
# the church's own site: www.<address>, or the Cross Code address itself (hope.crosscode.co.za), as
|
||||
# the Hub does (Hub 2.21.0).
|
||||
default_domain() {
|
||||
local product="$1" address="$2"
|
||||
if [ "$product" = website ]; then
|
||||
case "$address" in
|
||||
*.crosscode.co.za) printf '%s' "$address" ;;
|
||||
*) printf 'www.%s' "$address" ;;
|
||||
esac
|
||||
else
|
||||
printf '%s.%s' "$product" "$address"
|
||||
fi
|
||||
}
|
||||
|
||||
# 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"
|
||||
@@ -197,6 +213,7 @@ install_product() {
|
||||
case "$product" in
|
||||
events) install_events "$@" ;;
|
||||
accounts) install_accounts "$@" ;;
|
||||
website) install_website "$@" ;;
|
||||
*) die "This version of crosscode can't install $product." ;;
|
||||
esac
|
||||
}
|
||||
@@ -224,7 +241,10 @@ install_done() {
|
||||
else
|
||||
info "DNS (if not set yet): $domain → this server (A record, or a CNAME)."
|
||||
fi
|
||||
info "Open https://$domain/setup to create the admin account."
|
||||
case "$product" in
|
||||
website) info "Open https://$domain/admin and sign in with the admin email the Hub was given: that account becomes the first Owner." ;;
|
||||
*) info "Open https://$domain/setup to create the admin account." ;;
|
||||
esac
|
||||
if [ "$product" = accounts ]; then info "Updates: crosscode deploy $church (Accounts is always updated first)"; else info "Updates: crosscode deploy $church $product"; fi
|
||||
}
|
||||
|
||||
@@ -232,11 +252,13 @@ 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
|
||||
Adds a product to a church already installed here (Accounts next to Events, the Website next to
|
||||
Accounts), 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>)
|
||||
--domain <domain> where it's served (default: <product>.<the church's address>; the
|
||||
Website: www.<address>, or the address itself on a Cross Code 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)
|
||||
@@ -276,15 +298,17 @@ cmd_add() {
|
||||
[ -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."
|
||||
case "$product" in events|accounts|website) ;; *) die "This version of crosscode can only add events, accounts and website." ;; esac
|
||||
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)."
|
||||
# The Website signs in through the church's Accounts and can't be reached without it (WEBSITE-1.0-PLAN §11).
|
||||
[ "$product" != website ] || church_has_product accounts || die "The Website needs the church's Accounts: add it first (crosscode add $church accounts)."
|
||||
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="${domain:-$(default_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.
|
||||
@@ -422,24 +446,34 @@ install_events() {
|
||||
die "The backend didn't answer with its version (sudo -u $user pm2 logs $name-backend)."
|
||||
}
|
||||
|
||||
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
|
||||
install_accounts() { install_app accounts Accounts ACCOUNTS_SECRET "$@"; }
|
||||
install_website() { install_app website Website WEBSITE_SECRET "$@"; }
|
||||
|
||||
step "Accounts: code ($dir)"
|
||||
# install_app <product> <title> <generated secret> <church> <user> <domain> <api> <web> <version>
|
||||
# <main> <ssl> <email> <frp>: Accounts and the Website (and later the App), which share one shape:
|
||||
# a checkout of crosscode-platform, apps/<product>/{backend,frontend}, ecosystem.config.cjs, and
|
||||
# one secret generated here that never leaves the server.
|
||||
install_app() {
|
||||
local product="$1" title="$2" secret="$3"
|
||||
shift 3
|
||||
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 prefix
|
||||
# shellcheck source=/dev/null
|
||||
source "$CROSSCODE_HOME/products/$product.sh"
|
||||
prefix="$(printf '%s' "$product" | tr '[:lower:]' '[:upper:]')"
|
||||
dir="$(church_dir "$church")/$product"
|
||||
app="$dir/apps/$product"
|
||||
db="$(printf '%s_%s' "$church" "$product" | tr '-' '_')"
|
||||
name="$church-$product"
|
||||
church_env_set "${prefix}_DIR" "$dir"
|
||||
church_env_set "${prefix}_DOMAIN" "$domain"
|
||||
church_env_set "${prefix}_PM2" "$name-backend $name-frontend"
|
||||
church_env_set "${prefix}_PORTS" "$api $web"
|
||||
church_env_set "${prefix}_DB" "$db"
|
||||
church_env_set "${prefix}_USER" "$user"
|
||||
[ "$frp" = 0 ] || church_env_set "${prefix}_FRP" 1
|
||||
|
||||
step "$title: code ($dir)"
|
||||
mkdir -p "$dir"
|
||||
undo_add dir "$dir"
|
||||
chown "$user:$user" "$dir"
|
||||
@@ -448,63 +482,64 @@ install_accounts() {
|
||||
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)."
|
||||
version="$(latest_tag "$dir" "$product")"
|
||||
[ -n "$version" ] || die "No $product@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)"
|
||||
ref="$(checkout_ref "$dir" "$product")"
|
||||
info "At $ref"
|
||||
report_result_json versions "{\"accounts\":\"$ref\"}"
|
||||
report_result_json versions "{\"$product\":\"$ref\"}"
|
||||
|
||||
step "Accounts: database ($db on $(db_host))"
|
||||
step "$title: 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"
|
||||
step "$title: 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)"
|
||||
local generated
|
||||
generated="$(random_secret 64)"
|
||||
export "CROSSCODE_GEN_$secret=$generated"
|
||||
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" \
|
||||
env_apply "$app" "$product" "$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"
|
||||
"${product}_ensure_pm2_config" "$dir" "$user"
|
||||
|
||||
step "Accounts: install, migrate, build"
|
||||
accounts_install_deps "$dir" "$user"
|
||||
accounts_migrate "$dir" "$user"
|
||||
accounts_build "$dir" "$user"
|
||||
step "$title: install, migrate, build"
|
||||
"${product}_install_deps" "$dir" "$user"
|
||||
"${product}_migrate" "$dir" "$user"
|
||||
"${product}_build" "$dir" "$user"
|
||||
|
||||
step "Accounts: PM2"
|
||||
step "$title: 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
|
||||
step "$title: frpc tunnels"
|
||||
frpc_add "$church" "$product" "$web" "$api"
|
||||
undo_add frpc "$church" "$product"
|
||||
frpc_reload
|
||||
else
|
||||
step "Accounts: nginx ($domain)"
|
||||
step "$title: nginx ($domain)"
|
||||
install_site_nginx "$name" "$domain" "$api" "$web"
|
||||
undo_add nginx "$name"
|
||||
if [ "$ssl" = 1 ]; then
|
||||
step "Accounts: certificate"
|
||||
step "$title: 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 ||
|
||||
step "$title: checking it answers"
|
||||
wait_for_version "$("${product}_health_url")" "$("${product}_expected_version" "$dir")" 90 ||
|
||||
die "The backend didn't answer with its version (sudo -u $user pm2 logs $name-backend)."
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user