# shellcheck shell=bash # Shared helpers for the crosscode script: logging, users, church.env, tags, versions, health. # Only functions and defaults: sourcing this file changes nothing (the bats tests rely on that). CROSSCODE_SITES="${CROSSCODE_SITES:-/var/www/sites}" CROSSCODE_REPO="${CROSSCODE_REPO:-https://c-git.crosscode.co.za/joshua/crosscode-platform.git}" # Every deploy runs the products in this order (ROADMAP §8): Accounts first, since others sign in # through it. PRODUCT_ORDER=(accounts events website app) # Products this version of the script can deploy (a products/.sh exists for each). SUPPORTED_PRODUCTS=(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 # ── Logging ────────────────────────────────────────────────────────────────────────────────── step() { printf '==> %s\n' "$*"; } info() { printf ' %s\n' "$*"; } warn() { printf '!!> %s\n' "$*" >&2; } die() { printf '!!> %s\n' "$*" >&2; exit 1; } # ── Small checks ───────────────────────────────────────────────────────────────────────────── require_root() { # The bats tests run deploys against stub products as an ordinary user. [ "${CROSSCODE_TEST:-}" = 1 ] && return 0 [ "$(id -u)" = 0 ] || die "Run this as root (sudo crosscode …): it reads church.env (root only) and runs each app as its own user." } # A church's short name: lower case letters, digits and -, starting with a letter. valid_slug() { [[ "$1" =~ ^[a-z][a-z0-9-]{0,39}$ ]]; } is_known_product() { local p for p in "${PRODUCT_ORDER[@]}"; do [ "$p" = "$1" ] && return 0; done return 1 } is_supported_product() { local p for p in "${SUPPORTED_PRODUCTS[@]}"; do [ "$p" = "$1" ] && return 0; done return 1 } # A product's place in the deploy order (0 for accounts). product_index() { local i for i in "${!PRODUCT_ORDER[@]}"; do [ "${PRODUCT_ORDER[$i]}" = "$1" ] && { echo "$i"; return 0; }; done echo 99 } # The products given (any order, repeats allowed), printed in deploy order, one per line. in_product_order() { local p q for p in "${PRODUCT_ORDER[@]}"; do for q in "$@"; do if [ "$p" = "$q" ]; then echo "$p"; break; fi done done } # Upper case for church.env keys: events → EVENTS. upper() { printf '%s' "$1" | tr '[:lower:]-' '[:upper:]_'; } # ── Running things as the app user ─────────────────────────────────────────────────────────── # run_as : runs the command in as (directly when we already # are that user), with that user's HOME so PM2 and npm use their own folders. run_as() { local user="$1" dir="$2" shift 2 if [ "$(id -un)" = "$user" ]; then (cd -- "$dir" && "$@") else sudo -u "$user" -H -- bash -c 'cd -- "$1" && shift && exec "$@"' crosscode-run "$dir" "$@" fi } # ── church.env ─────────────────────────────────────────────────────────────────────────────── # /var/www/sites//church.env holds names, folders, domains and ports (never secrets). It is # read as data, never sourced: only KEY=VALUE lines with known keys are taken, into CENV. declare -gA CENV=() church_dir() { printf '%s/%s' "$CROSSCODE_SITES" "$1"; } church_env_path() { printf '%s/%s/church.env' "$CROSSCODE_SITES" "$1"; } church_env_key_ok() { [[ "$1" =~ ^(CHURCH|PRODUCTS|ADDRESS)$ ]] || [[ "$1" =~ ^(ACCOUNTS|EVENTS|WEBSITE|APP)_[A-Z0-9_]+$ ]] } # church_env_read : fills CENV. Values may be bare or in double or single quotes; a # "# comment" after the value is ignored. Unknown keys and other lines are skipped. church_env_read() { local file="$1" line key value CENV=() [ -f "$file" ] || return 1 while IFS= read -r line || [ -n "$line" ]; do line="${line%$'\r'}" [[ "$line" =~ ^[[:space:]]*([A-Z][A-Z0-9_]*)=(.*)$ ]] || continue key="${BASH_REMATCH[1]}" value="${BASH_REMATCH[2]}" church_env_key_ok "$key" || continue if [[ "$value" =~ ^\"([^\"]*)\" ]] || [[ "$value" =~ ^\'([^\']*)\' ]]; then value="${BASH_REMATCH[1]}" else value="${value%%#*}" value="${value%"${value##*[![:space:]]}"}" fi CENV[$key]="$value" done < "$file" } # cenv [default]: a value from CENV. cenv() { printf '%s' "${CENV[$1]:-${2:-}}"; } # pvar [default]: a product's value, e.g. pvar events DIR → CENV[EVENTS_DIR]. pvar() { cenv "$(upper "$1")_$2" "${3:-}"; } church_env_set() { church_env_key_ok "$1" || die "church.env: unknown key $1" [[ "$2" != *[\"\'\$\`\\]* && "$2" != *$'\n'* ]] || die "church.env: $1 can't contain quotes, \$, \` , \\ or newlines" CENV[$1]="$2" } church_env_unset() { unset 'CENV[$1]'; } # church_env_write : writes CENV in a fixed order (CHURCH, PRODUCTS, ADDRESS, then each # product's keys in deploy order), atomically, mode 600. church_env_write() { local file="$1" tmp key p prefix local -a keys=() for key in CHURCH PRODUCTS ADDRESS; do [ -n "${CENV[$key]+x}" ] && keys+=("$key"); done for p in "${PRODUCT_ORDER[@]}"; do prefix="$(upper "$p")_" while IFS= read -r key; do [ -n "$key" ] && keys+=("$key"); done < <( for key in "${!CENV[@]}"; do [[ "$key" == "$prefix"* ]] && echo "$key"; done | LC_ALL=C sort ) done tmp="$(mktemp "$(dirname "$file")/.church.env.XXXXXX")" { echo "# Written by crosscode. Names, folders, domains and ports only: secrets stay in each product's .env." for key in "${keys[@]}"; do if [[ "${CENV[$key]}" =~ [[:space:]#] || -z "${CENV[$key]}" ]]; then printf '%s="%s"\n' "$key" "${CENV[$key]}" else printf '%s=%s\n' "$key" "${CENV[$key]}" fi done } > "$tmp" chmod 600 "$tmp" mv -f "$tmp" "$file" } # Church names on this server: every folder in $CROSSCODE_SITES with a church.env. list_churches() { local f for f in "$CROSSCODE_SITES"/*/church.env; do [ -f "$f" ] && basename "$(dirname "$f")" done return 0 } # Loads a church's church.env into CENV, or stops. load_church() { valid_slug "$1" || die "\"$1\" isn't a church name (a-z, 0-9 and -)." church_env_read "$(church_env_path "$1")" || die "No church \"$1\" on this server ($(church_env_path "$1") is missing)." } church_has_product() { local p for p in $(cenv PRODUCTS); do [ "$p" = "$1" ] && return 0; done return 1 } # ── Versions and tags ──────────────────────────────────────────────────────────────────────── # version_ge : a >= b, for x.y.z with an optional -prerelease (1.2.0-rc1 < 1.2.0). version_ge() { local a="$1" b="$2" an bn ap bp i x y an="${a%%-*}"; bn="${b%%-*}" ap=""; bp="" [[ "$a" == *-* ]] && ap="${a#*-}" [[ "$b" == *-* ]] && bp="${b#*-}" local -a A B IFS=. read -r -a A <<< "$an" IFS=. read -r -a B <<< "$bn" for i in 0 1 2; do x="${A[$i]:-0}"; y="${B[$i]:-0}" [[ "$x" =~ ^[0-9]+$ && "$y" =~ ^[0-9]+$ ]] || return 2 if ((10#$x > 10#$y)); then return 0; fi if ((10#$x < 10#$y)); then return 1; fi done # Same x.y.z: a release beats its prereleases; prereleases compare as text. if [ -z "$ap" ]; then return 0; fi if [ -z "$bp" ]; then return 1; fi [[ "$ap" > "$bp" || "$ap" == "$bp" ]] } # tag_version → x.y.z tag_version() { printf '%s' "${1#*@}"; } # latest_tag , reading tag names on stdin: the newest released product@x.y.z # (prerelease and test tags such as events@2.2.0-test are never "latest"). latest_tag_from() { local p="$1" t best="" while IFS= read -r t; do [[ "$t" =~ ^${p}@[0-9]+\.[0-9]+\.[0-9]+$ ]] || continue if [ -z "$best" ] || version_ge "$(tag_version "$t")" "$(tag_version "$best")"; then best="$t"; fi done printf '%s' "$best" } # git_in : git for reading a checkout owned by the app user (as root, git would # otherwise refuse it as "dubious ownership"). Anything that writes runs as the app user instead. git_in() { local dir="$1" shift git -c safe.directory="$dir" -C "$dir" "$@" } # latest_tag : from the checkout's tags (fetch first). latest_tag() { git_in "$1" tag -l "$2@*" | latest_tag_from "$2"; } # checkout_ref : the product tag the checkout is on, or the short commit. checkout_ref() { git_in "$1" describe --tags --exact-match --match "$2@*" 2>/dev/null || git_in "$1" rev-parse --short HEAD } # whatsnew_requires , reading a WHATS-NEW.md on stdin: the requirements in that # version's heading, one "product x.y.z" per line. Same grammar as the Hub's release sync # (events-hub releaseSync.js): "## 3.0.0 — requires accounts 1.2.0, website 1.1.0 — urgent". # HTML comments are skipped (the file's own instructions contain example headings). whatsnew_requires() { local version="$1" awk -v want="$version" ' BEGIN { products = "accounts|events|website|app" } { line = $0 sub(/\r$/, "", line) # Drop comments, which can span lines. out = "" while (line != "") { if (incomment) { e = index(line, "-->") if (e == 0) { line = ""; break } line = substr(line, e + 3); incomment = 0 } else { s = index(line, "