# shellcheck shell=bash # crosscode install: a new church on this server (from hope-events' scripts/install.sh 2.1.x). # Events only in 2.2.0; Accounts, Website and App follow as their products/*.sh are added. install_usage() { cat <<'EOF' crosscode install --products events --domain
[options] A new church: system packages (Node 24, PostgreSQL, PM2, nginx, certbot) if missing, then per product an app user, database, checkout, .env, PM2 processes, nginx site and certificate. Writes /var/www/sites//church.env. DNS must already point here (it prints the records). --products events the products to install (only events in this version) --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 --email for Let's Encrypt notices (required unless --no-ssl) --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-) --api-port Events backend port (default: the first free one from 5000) --web-port Events frontend port (default: the first free one from 3000) --no-ssl no certificate (behind another proxy that does HTTPS) EOF } # Ports in use: every church's *_PORTS on this server, and anything listening now. used_ports() { local f for f in "$CROSSCODE_SITES"/*/church.env; do [ -f "$f" ] && sed -nE 's/^[A-Z]+_PORTS="?([0-9 ]+)"?.*/\1/p' "$f" | tr ' ' '\n' done ss -ltnH 2>/dev/null | awk '{ n = split($4, a, ":"); print a[n] }' return 0 } # free_port : the first port from not in the list. free_port() { local p="$1" shift while printf '%s\n' "$@" | grep -qx "$p"; do p=$((p + 1)); done echo "$p" } random_secret() { openssl rand -base64 64 | tr -d '/+=\n' | head -c "${1:-48}"; } cmd_install() { local church="" products="" address="" events_domain="" email="" version="" main=0 user="" api="" web="" ssl=1 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 ;; --email) email="$2"; shift 2 ;; --version) version="$2"; shift 2 ;; --main) main=1; shift ;; --user) user="$2"; shift 2 ;; --api-port) api="$2"; shift 2 ;; --web-port) web="$2"; shift 2 ;; --no-ssl) ssl=0; shift ;; -h|--help) install_usage; return 0 ;; -*) die "Unknown option $1 (crosscode install --help)." ;; *) [ -z "$church" ] || die "One church at a time."; church="$1"; shift ;; esac done [ -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 for p in $products; do is_known_product "$p" || die "Unknown product \"$p\"." [ "$p" = events ] || die "This version of crosscode can only install events." 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}" [ "$ssl" = 0 ] || [ -n "$email" ] || die "--email is required for the certificate (or use --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." 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)." local taken mapfile -t taken < <(used_ports) 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." install_system_packages "$ssl" step "App user ($user)" id "$user" >/dev/null 2>&1 || useradd --create-home --shell /bin/bash "$user" CENV=() church_env_set CHURCH "$church" church_env_set PRODUCTS "events" church_env_set ADDRESS "$address" install_events "$church" "$user" "$events_domain" "$api" "$web" "$version" "$main" "$ssl" "$email" church_env_write "$(church_env_path "$church")" step "Done" info "DNS (if not set yet): $events_domain → this server (A record, or a CNAME)." info "Open https://$events_domain/setup to create the admin account, then enter the licence key." info "Updates: crosscode deploy $church events" } install_system_packages() { local ssl="$1" step "System packages" export DEBIAN_FRONTEND=noninteractive apt-get update -q apt-get install -y -q curl git rsync nginx postgresql postgresql-client openssl build-essential ca-certificates gnupg [ "$ssl" = 0 ] || apt-get install -y -q certbot python3-certbot-nginx # Node 24 is the LTS line supported until April 2028. if ! command -v node >/dev/null || [ "$(node_major)" -lt 24 ]; then step "Node.js 24" curl -fsSL https://deb.nodesource.com/setup_24.x | bash - apt-get install -y -q nodejs fi command -v pm2 >/dev/null || npm install -g pm2 } install_events() { local church="$1" user="$2" domain="$3" api="$4" web="$5" version="$6" main="$7" ssl="$8" email="$9" local dir app db pass url name scheme # shellcheck source=/dev/null source "$CROSSCODE_HOME/products/events.sh" dir="$(church_dir "$church")/events" app="$dir/apps/events" db="$(printf '%s_events' "$church" | tr '-' '_')" name="$church-events" scheme=https [ "$ssl" = 1 ] || scheme=http url="$scheme://$domain" church_env_set EVENTS_DIR "$dir" church_env_set EVENTS_DOMAIN "$domain" church_env_set EVENTS_PM2 "$name-backend $name-frontend" church_env_set EVENTS_PORTS "$api $web" church_env_set EVENTS_DB "$db" church_env_set EVENTS_USER "$user" step "Events: code ($dir)" mkdir -p "$(church_dir "$church")" chmod 755 "$CROSSCODE_SITES" "$(church_dir "$church")" if [ ! -d "$dir/.git" ]; then mkdir -p "$dir" chown "$user:$user" "$dir" run_as "$user" "$dir" git clone --quiet "$CROSSCODE_REPO" . fi run_as "$user" "$dir" git fetch --tags --quiet origin if [ "$main" = 1 ]; then version=origin/main elif [ -z "$version" ]; then version="$(latest_tag "$dir" events)" [ -n "$version" ] || die "No events@x.y.z release tags yet (use --version or --main)." fi run_as "$user" "$dir" git checkout -q --detach "$version" info "At $(checkout_ref "$dir" events)" step "Events: database ($db)" # Re-running keeps the password already in backend/.env; otherwise a new one. pass="" if [ -f "$app/backend/.env" ]; then pass="$(env_value "$app/backend/.env" DATABASE_URL | sed -nE 's#^postgresql://[^:]+:([^@]+)@.*#\1#p')" fi pass="${pass:-$(random_secret 32)}" sudo -u postgres psql -v ON_ERROR_STOP=1 -q < "$app/backend/.env" < "$app/frontend/.env.production" chown "$user:$user" "$app/backend/.env" "$app/frontend/.env.production" chmod 600 "$app/backend/.env" events_ensure_pm2_config "$dir" "$user" step "Events: install, migrate, build" events_install_deps "$dir" "$user" events_migrate "$dir" "$user" events_build "$dir" "$user" step "Events: PM2" run_as "$user" "$app" pm2 startOrReload ecosystem.config.js run_as "$user" "$app" pm2 save env PATH="$PATH:/usr/bin" pm2 startup systemd -u "$user" --hp "$(getent passwd "$user" | cut -d: -f6)" >/dev/null step "Events: nginx ($domain)" install_events_nginx "$name" "$domain" "$api" "$web" if [ "$ssl" = 1 ]; then step "Events: certificate" certbot --nginx -d "$domain" --non-interactive --agree-tos -m "$email" --redirect fi step "Events: checking" wait_for_version "$(events_health_url)" "$(events_expected_version "$dir")" 60 || warn "The backend isn't answering yet: sudo -u $user pm2 logs" } install_events_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" <