gitoria
All repositories: gitoria
18.3 KB
#!/usr/bin/env bash# deploy.sh — gitoria.worldapi.org: Loreana (this folder) → Byrodin. Run ON LOREANA.## ./deploy.sh gates → backup → rsync → restart the container → public URL 200# ./deploy.sh --dry-run gates → rsync -n (shows what WOULD be sent), no backup, no restart, no URL check# ./deploy.sh --skip-tests skips the gates (LOUD warning) — only when you know why# ./deploy.sh --target DIR|HOST:DIR another destination (default below); a local DIR is# how the script is tested without touching Byrodin# ./deploy.sh --url URL the URL that must answer 200 after the restart# (env: DEPLOY_TARGET, DEPLOY_URL, DEPLOY_SSH override the same defaults)# (backup: tars storage/.sessions/.env that exist on the target to Loreana's# /media/SLOW1TB2/deploy-backups/<app>/, keeps newest 5; --target DIR backs up DIR instead)## THE FIRST DEPLOY is done by the architect on Byrodin (folder, data, nginx vhost, cert,# DNS). This script only updates the CODE: storage/, .sessions/, .env, .scratch/, server.*,# testapp/ and logs are never sent, so live data on Byrodin is never touched.# No --delete: a file removed here stays on Byrodin (harmless — nothing imports it).set -euo pipefail# ---- the app ------------------------------------------------------------------------------APP=gitoria.worldapi.orgCONTAINER=gitoria.worldapi.org[email protected]:/CONTAINERS/projects/gitoria.worldapi.orgDEFAULT_URL=https://gitoria.worldapi.org/GATES=("GITORIA_RENAME_PORT=${GITORIA_RENAME_PORT:-8728} GITORIA_GATE_CHROME=${GITORIA_GATE_CHROME:-8731-8733} node tests/repo-rename.mjs""GITORIA_PA_PORT=${GITORIA_PA_PORT:-8720} GITORIA_GATE_CHROME=${GITORIA_GATE_CHROME:-8725-8729} node tests/person-approval.mjs""GITORIA_CREATE_PORT=${GITORIA_CREATE_PORT:-8720} GITORIA_CREATE_IDENT_PORT=${GITORIA_CREATE_IDENT_PORT:-8721} GITORIA_GATE_CHROME=${GITORIA_GATE_CHROME:-8725-8729} node tests/create-api.mjs" # gitoria#39: user-token repository creation"GITORIA_SIGNIN_PORT=${GITORIA_SIGNIN_PORT:-8720} GITORIA_SIGNIN_CHROME=${GITORIA_SIGNIN_CHROME:-8723-8729} node tests/signin-return.mjs" # ident#31: sign in through the selector panel, back on the same page"node theme/check-theme.mjs .""GITORIA_GATE_PORT=${GITORIA_GATE_PORT:-8720} GITORIA_GATE_IDENT_PORT=${GITORIA_GATE_IDENT_PORT:-8721} GITORIA_GATE_TICKETS_PORT=${GITORIA_GATE_TICKETS_PORT:-8722} GITORIA_GATE_FIREFOX=${GITORIA_GATE_FIREFOX:-8730} GITORIA_GATE_CHROME=${GITORIA_GATE_CHROME:-8723-8729} node tests/browser.mjs""GITORIA_SEC_PORT=${GITORIA_SEC_PORT:-8720} GITORIA_SEC_PORT2=${GITORIA_SEC_PORT2:-8721} GITORIA_SEC_IDENT_PORT=${GITORIA_SEC_IDENT_PORT:-8722} node tests/security.mjs""GITORIA_PRIV_PORT=${GITORIA_PRIV_PORT:-8720} GITORIA_PRIV_IDENT_PORT=${GITORIA_PRIV_IDENT_PORT:-8721} node tests/private.mjs" # go34: private repos, organizations (158 checks)"GITORIA_IDA_PORT=${GITORIA_IDA_PORT:-8738} GITORIA_IDA_IDENT_PORT=${GITORIA_IDA_IDENT_PORT:-8739} node tests/ident-access-gitoria.mjs" # ident#27: an idt_ access key as the git password"GITORIA_APPS_PORT=${GITORIA_APPS_PORT:-8720} GITORIA_APPS_IDENT_PORT=${GITORIA_APPS_IDENT_PORT:-8721} node tests/apps.mjs" # gitoria#41: apps connected to a repo (URL allow-list, connect flow, add/remove/rotate)"GITORIA_PULLS_PORT=${GITORIA_PULLS_PORT:-8720} node tests/pull-numbers.mjs" # gitoria#40: PR numbers, refs/pull/N/head, the changes of syncPulls, wasHeadOf"GITORIA_PULLSSH_PORT=${GITORIA_PULLSSH_PORT:-8720} GITORIA_PULLSSH_SSH_PORT=${GITORIA_PULLSSH_SSH_PORT:-8721} node tests/pull-ssh.mjs" # gitoria#40: the same over the sshd door (docker)"GITORIA_TOKEN_PORT=${GITORIA_TOKEN_PORT:-8720} GITORIA_TOKEN_IDENT_PORT=${GITORIA_TOKEN_IDENT_PORT:-8721} node tests/app-token.mjs" # gitoria#42: an app trades its key for a one-hour token; git read with it, no push"GITORIA_REVIEWS_PORT=${GITORIA_REVIEWS_PORT:-8720} node tests/reviews.mjs" # gitoria#44: reviews with inline threads, replies, resolve, conversation (409/422/404, earlier head, paging, seq)"GITORIA_EVENTS_PORT=${GITORIA_EVENTS_PORT:-8720} node tests/events.mjs" # gitoria#43: signed events to apps (HMAC, retries, Merge button, ping, delivery log, two processes, retention)"GITORIA_PRPAGE_PORT=${GITORIA_PRPAGE_PORT:-8720} GITORIA_GATE_CHROME=${GITORIA_GATE_CHROME:-8725-8729} node tests/pr-page.mjs" # gitoria#45: the PR page (comments, threads, Review again, who hears of it, escaping, read-only for others)"GITORIA_STATUS_PORT=${GITORIA_STATUS_PORT:-8720} GITORIA_STATUS_IDENT_PORT=${GITORIA_STATUS_IDENT_PORT:-8721} node tests/status.mjs" # gitoria#46: statuses an app sets on a commit, required before merge, badges on /pulls"GITORIA_LIMITS_PORT=${GITORIA_LIMITS_PORT:-8720} GITORIA_LIMITS_IDENT_PORT=${GITORIA_LIMITS_IDENT_PORT:-8721} node tests/limits.mjs" # gitoria#47: status read route, pull.statuses/required, tickets list, 413/400, rate limits (429 + Retry-After)"GITORIA_KEYED_PORT=${GITORIA_KEYED_PORT:-8720} GITORIA_KEYED_IDENT_PORT=${GITORIA_KEYED_IDENT_PORT:-8721} node tests/status-keyed.mjs" # gitoria#46 v1.4: statuses keyed by app + context; an old-shape row (the old code on the same storage) is read and replaced"GITORIA_MM_PORT=${GITORIA_MM_PORT:-8720} GITORIA_GATE_CHROME=${GITORIA_GATE_CHROME:-8725-8729} node tests/member-merge.mjs" # gitoria#48: a member merges a PR with a gtr_ token once the required checks pass (API + button, apps never)"GITORIA_PM_PORT=${GITORIA_PM_PORT:-8720} node tests/protect-main.mjs" # gitoria#50: refuse direct pushes to the main branch (owner setting; https + ssh; branches, tags, merges stay))# NEVER SENT (rsync patterns; a leading / anchors at the app folder)EXCLUDES=(/.git/ /.gitignore/storage/ /.sessions/ /.env /.env.* /.scratch/ /server.* /testapp/'*.log' '*.pid' node_modules/)# ---------------------------------------------------------------------------------------------DRY=0SKIP=0TARGET=${DEPLOY_TARGET:-$DEFAULT_TARGET}URL=${DEPLOY_URL:-$DEFAULT_URL}# how long the URL may take after the restart (seconds); a cold start compiles first (hybriel#148)URL_WAIT=${DEPLOY_URL_WAIT:-120}# Loreana's ssh config may not apply to Byrodin: -F /dev/null (ident STATUS)SSH=${DEPLOY_SSH:-ssh -F /dev/null -o BatchMode=yes -o ConnectTimeout=15}usage() { sed -n '2,15p' "$0" | sed 's/^# \{0,1\}//'; }while [ $# -gt 0 ]; docase "$1" in--dry-run) DRY=1 ;;--skip-tests) SKIP=1 ;;--target) TARGET=${2:?--target needs a value}; shift ;;--target=*) TARGET=${1#--target=} ;;--url) URL=${2:?--url needs a value}; shift ;;--url=*) URL=${1#--url=} ;;-h|--help) usage; exit 0 ;;*) echo "deploy: unknown argument: $1" >&2; usage >&2; exit 2 ;;esacshiftdoneHERE=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)cd "$HERE"step() { printf '\n==> %s\n' "$*"; }run() { printf ' $ %s\n' "$*" >&2; "$@"; }die() { printf '\ndeploy: REFUSED — %s\n' "$*" >&2; exit 1; }# a target `host:dir` is remote, a plain path is localif [[ "$TARGET" == *:* ]]; thenREMOTE_HOST=${TARGET%%:*}REMOTE_DIR=${TARGET#*:}elseREMOTE_HOST=REMOTE_DIR=$TARGETfistep "[0/5] $APP → $TARGET (dry run: $DRY, skip tests: $SKIP)"echo " from $HERE"echo " url $URL"[ -x bin/hybriel ] || die "bin/hybriel is missing here"[ -f docker-compose.yml ] || die "docker-compose.yml is missing here"[ -f project.hl ] || die "project.hl is missing here"command -v rsync >/dev/null || die "rsync is not installed"# ---- 1. the gates ---------------------------------------------------------------------------if [ "$SKIP" = 1 ]; thenstep "[1/5] GATES SKIPPED"printf ' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n'printf ' !! --skip-tests: NOTHING WAS TESTED. You deploy untested code. !!\n'printf ' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n'elsestep "[1/5] gates (${#GATES[@]})"# headless Chromes that exist BEFORE the gates are not ours (other sessions' test runs, e.g. Anton's)chromes() { ps -eo pid=,ppid=,args= | awk '/[h]l-browser-tier/ && /remote-debugging-port/ && !/--type=/ {print $1, $2}'; }before=" $(chromes | awk '{print $1}' | tr '\n' ' ') "for g in "${GATES[@]}"; doprintf ' $ %s\n' "$g"out=$(mktemp)if ! env $g > "$out" 2>&1; thengrep -E '^FAIL|passed,' "$out" | sed 's/^/ /' || trueecho " (full output: $out)"die "gate failed: $g"figrep -E 'passed,|^check-theme:' "$out" | tail -1 | sed 's/^/ /' || truerm -f "$out"done# a Chrome our gates LEFT: new since the gates began AND no longer owned by a running node test# (a Chrome whose parent is a live node process belongs to another session's test run right now)leaked=""while read -r pid ppid; do[ -z "$pid" ] && continuecase "$before" in *" $pid "*) continue ;; esacps -o args= -p "$ppid" 2>/dev/null | grep -q '^node\|/node ' && continueleaked="$leaked $pid"done < <(chromes)if [ -n "$leaked" ]; thendie "a gate left a headless Chrome (pids:$leaked; ps -eo pid,args | grep hl-browser-tier)"fifi# ---- 2. pre-deploy backup of the LIVE data (before anything is sent) ------------------------BACKUP_ROOT=${DEPLOY_BACKUP_ROOT:-/media/SLOW1TB2/deploy-backups}BACKUP_DIR="$BACKUP_ROOT/$APP"BACKUP_FILE="$BACKUP_DIR/$APP-$(date +%Y%m%d-%H%M).tgz"if [ "$DRY" = 1 ]; thenstep "[2/5] DRY RUN: backup skipped"elsestep "[2/5] backup live data ($APP) → $BACKUP_FILE"mkdir -p "$BACKUP_DIR"if [ -n "$REMOTE_HOST" ]; thenpresent=$($SSH "$REMOTE_HOST" "cd '$REMOTE_DIR' 2>/dev/null && for p in storage .sessions .env; do [ -e \"\$p\" ] && echo \"\$p\"; done; true")elsepresent=$(cd "$REMOTE_DIR" 2>/dev/null && for p in storage .sessions .env; do [ -e "$p" ] && echo "$p"; done; true)fipresent=$(printf '%s' "$present" | tr '\n' ' ' | sed 's/ *$//')if [ -z "$present" ]; thenecho " nothing to back up yet (no storage/.sessions/.env on the target)"elseecho " taring: $present"if [ -n "$REMOTE_HOST" ]; then# Order (Anton #203): directories, then every file except *.mpack (indexes, idxstate, sessions, .env), then the *.mpack# data last — a hot copy then never holds index offsets past the data it copied. tar exit 1 = "file changed as we# read it" / a file gone between find and tar (--ignore-failed-read) — a warning; the archive is checked with gzip -t.run $SSH "$REMOTE_HOST" "cd '$REMOTE_DIR' && { find $present -type d; find $present -type f ! -name '*.mpack' | LC_ALL=C sort; find $present -type f -name '*.mpack' | LC_ALL=C sort; } | tar czf - --hard-dereference --warning=no-file-changed --ignore-failed-read --no-recursion -T -; r=\$?; [ \$r -le 1 ]" > "$BACKUP_FILE" \|| { rm -f "$BACKUP_FILE"; die "backup failed (tar/ssh error) — refusing to deploy"; }elserun tar czf "$BACKUP_FILE" --hard-dereference -C "$REMOTE_DIR" $present \|| { rm -f "$BACKUP_FILE"; die "backup failed (tar error) — refusing to deploy"; }fi[ -s "$BACKUP_FILE" ] || { rm -f "$BACKUP_FILE"; die "backup is empty — refusing to deploy"; }echo " $(du -h "$BACKUP_FILE" | cut -f1) $BACKUP_FILE"ls -1t "$BACKUP_DIR/$APP"-*.tgz 2>/dev/null | tail -n +6 | xargs -r rm -f --echo " keeping $(ls -1 "$BACKUP_DIR/$APP"-*.tgz 2>/dev/null | wc -l) archive(s) of $APP"fifi# ---- 3. rsync the code ----------------------------------------------------------------------RSYNC=(rsync -az --no-owner --no-group --itemize-changes)for e in "${EXCLUDES[@]}"; do RSYNC+=("--exclude=$e"); doneif [ -n "$REMOTE_HOST" ]; thenRSYNC+=(-e "$SSH")elsemkdir -p "$REMOTE_DIR"fi# the preview is ALWAYS made first (rsync -n): nothing that must stay on Byrodin may be in itstep "[3/5] rsync preview (what would be sent)"preview=$(mktemp)run "${RSYNC[@]}" -n ./ "$TARGET/" > "$preview"sed 's/^/ /' "$preview"echo " ($(grep -c . "$preview" || true) lines)"if awk '{print $2}' "$preview" | grep -E '^(storage/|\.sessions/|\.env|\.scratch/|server\.|testapp/)|\.log$|\.pid$' ; thendie "the preview holds a path that must never be sent (see above)"firm -f "$preview"if [ "$DRY" = 1 ]; thenstep "[3/5] DRY RUN: nothing sent"elsestep "[3/5] rsync"run "${RSYNC[@]}" ./ "$TARGET/" | sed 's/^/ /'fi# ---- 4. restart the container, without a gap ------------------------------------------------# Apollo 2026-10-25: the main container is never restarted before the standby (port 45014, compose profile "standby") answers 200.# The shared helper (worldapi-conductor/standby-swap.sh) is piped to Byrodin: it starts the standby with the new code, waits for 200 on# /api/repos, restarts the main container, waits for 200 on 45004 and stops the standby. nginx serves from the standby meanwhile. If the# standby never comes up, nothing live was touched and the deploy stops here.# Apollo 2026-11-02 mpackdb freeze: DEPLOY_STOP_START=1 replaces ONLY this step with stop-then-start. Two gitoria processes must never# have the same mpackdb files open at once (the standby swap runs the old and the new process side by side); so the standby is made sure# to be down, the main container is stopped, and it is started again only after the old process is really gone (container not running AND# its old pid no longer exists). Without the variable the standby swap below stays the default (deploy-init.sh relies on the string# standby-swap.sh in this file to skip a second restart).SWAP=${DEPLOY_STANDBY_SWAP:-/media/STORAGE/projects/WorldAPI/worldapi-conductor/standby-swap.sh}SWAP_ARGS="gitoria gitoria gitoria-standby /api/repos 45004"STOP_WAIT=${DEPLOY_STOP_WAIT:-60}# runs on the target (as one script, so a failure before the start leaves the container stopped and the deploy fails): $1 = app dirstop_start_script() {cat <<'EOS'set -ucd "$APP_DIR" || exit 1echo " standby: making sure gitoria-standby is not running"docker compose --profile standby stop gitoria-standby || truecid=$(docker compose ps -a -q gitoria)oldpid=0if [ -n "$cid" ]; then oldpid=$(docker inspect -f '{{.State.Pid}}' "$cid" 2>/dev/null || echo 0); fiecho " old container ${cid:-none} pid $oldpid"echo " docker compose stop gitoria"docker compose stop gitoria || { echo " stop failed - NOT starting" >&2; exit 1; }i=0while :; dost=$(docker inspect -f '{{.State.Running}} {{.State.Pid}}' "$cid" 2>/dev/null) || st="gone 0"running=${st%% *}; pid=${st##* }alive=no# the pid docker shows now (0 once stopped) and the pid it had before the stop: neither may still existfor q in "$pid" "$oldpid"; doif [ -n "$q" ] && [ "$q" != 0 ] && [ -d "/proc/$q" ]; then alive=yes; fidoneecho " wait ${i}s: running=$running pid=$pid oldpid=$oldpid still-there=$alive"if [ "$running" != true ] && [ "$alive" = no ]; then break; fii=$((i + 1))if [ "$i" -gt "$STOP_WAIT" ]; then echo " the old gitoria process is still there after ${STOP_WAIT}s - NOT starting" >&2; exit 1; fisleep 1doneecho " old process gone: docker compose up -d gitoria"docker compose up -d gitoriaEOS}[ -f "$SWAP" ] || die "the hand-over helper is missing: $SWAP"if [ "${DEPLOY_STOP_START:-}" = 1 ]; thenif [ "$DRY" = 1 ]; thenstep "[4/5] DRY RUN: would stop $CONTAINER, wait until its old process is gone, then start it (DEPLOY_STOP_START=1, mpackdb freeze: no standby swap)"echo " would run on $TARGET: docker compose --profile standby stop gitoria-standby; docker compose stop gitoria; wait up to ${STOP_WAIT}s until docker inspect -f '{{.State.Running}} {{.State.Pid}}' shows not running AND the pid is gone (otherwise fail, do NOT start); docker compose up -d gitoria"elsestep "[4/5] stop $CONTAINER, wait until its old process is gone, start it (DEPLOY_STOP_START=1, mpackdb freeze: no standby swap)"if [ -n "$REMOTE_HOST" ]; thenprintf ' $ %s %s "APP_DIR=%s STOP_WAIT=%s bash -s" (stop-then-start script)\n' "$SSH" "$REMOTE_HOST" "$REMOTE_DIR" "$STOP_WAIT" >&2stop_start_script | $SSH "$REMOTE_HOST" "APP_DIR='$REMOTE_DIR' STOP_WAIT='$STOP_WAIT' bash -s" || die "stop-then-start failed - the container may be stopped, look: $SSH $REMOTE_HOST docker ps -a"elsestop_start_script | APP_DIR="$REMOTE_DIR" STOP_WAIT="$STOP_WAIT" bash -s || die "stop-then-start failed - the container may be stopped"fifielif [ "$DRY" = 1 ]; thenstep "[4/5] DRY RUN: would hand over $CONTAINER through its standby (no gap)"echo " would run: $SWAP_ARGS with $SWAP on $TARGET"DRY_RUN=1 APP_DIR="$REMOTE_DIR" bash "$SWAP" $SWAP_ARGS 2>&1 | sed 's/^/ /' || trueelsestep "[4/5] hand over $CONTAINER through its standby (no gap)"if [ -n "$REMOTE_HOST" ]; thenprintf ' $ %s %s "APP_DIR=%s bash -s -- %s" < %s\n' "$SSH" "$REMOTE_HOST" "$REMOTE_DIR" "$SWAP_ARGS" "$SWAP" >&2$SSH "$REMOTE_HOST" "APP_DIR='$REMOTE_DIR' bash -s -- $SWAP_ARGS" < "$SWAP" || die "the hand-over failed — see the log above (the standby keeps serving if the main container did not come back)"elseAPP_DIR="$REMOTE_DIR" bash "$SWAP" $SWAP_ARGS || die "the hand-over failed — see the log above"fifi# the helper only restarts the app container: bring the other service (the git-over-SSH sshd) up to date too, without restarting it when nothing changedif [ "$DRY" != 1 ]; then# the sshd image COPIES gitoria-shell / gitoria-keys / sshd_config: rebuild it first (cached when nothing changed), then `up -d` recreates the container only if its image changed (GO34c)CHANGED="cd '$REMOTE_DIR' && docker compose build gitoria-sshd && docker compose up -d"if [ -n "$REMOTE_HOST" ]; then run $SSH "$REMOTE_HOST" "$CHANGED"; else run bash -c "$CHANGED"; fifi# ---- 5. the URL answers 200 -----------------------------------------------------------------if [ "$DRY" = 1 ]; thenstep "[5/5] DRY RUN: would check $URL answers 200"step "dry run done — nothing was sent, nothing restarted"exit 0fistep "[5/5] $URL must answer 200 (up to ${URL_WAIT}s: a new Hybriel compiles the app at its first start, ~20 s, hybriel#148)"code=000start=$SECONDSi=0while :; doi=$((i + 1))code=$(curl -s -o /dev/null -w '%{http_code}' --max-time 10 "$URL" || true)echo " try $i ($((SECONDS - start)) s): $code"[ "$code" = 200 ] && break[ $((SECONDS - start)) -ge "$URL_WAIT" ] && breaksleep 2doneif [ "$code" != 200 ]; thenif [ -n "$REMOTE_HOST" ]; then die "$URL answered $code, not 200 — look: $SSH $REMOTE_HOST docker logs --tail 50 $CONTAINER"; fidie "$URL answered $code, not 200 — look: docker logs --tail 50 $CONTAINER"fistep "deployed $APP → $TARGET, $URL answers 200"
Branches
Latest commits
- 74ec2af8Merge branch 'w54-repo-rename-pr' into mainGitoria
- 9f7ce8d8Preserve rename compatibility and live page titlesmre
- c3d95b6e|||PR separate names and addresses, rename repositories in Settings (#53)mre
- ce08920dMerge branch 'w52c-header-phone' into mainGitoria
- 0bab9e46|||PR header: ident status wraps on phones instead of clippingmre
- e7af3eceMerge branch 'w52b-approval-followup' into mainGitoria
- c01e66c6|||PR approvals follow-up: live Merge after status writes, helpers (#52)mre
- 841da497Merge branch 'w53-pulls-speed-pr' into mainGitoria
- 80307ae2|||PR pulls page cache: live PR numbers, complete scans onlymre
- 69141f65|||PR pulls page: cache the git part per repo by branch tipsmre
- 155861f6Merge branch 'w52-person-approval' into mainGitoria
- b5435de8|||PR person approvals: second Kunibert round (#52)mre
- 3a8b2fb8|||PR person approvals: Kunibert review fixes (#52)mre
- 2a89d8d0|||PR person approvals: Approve button for people; merge needs the required checks or a listed person's approval (#52)mre
- 83f0c953Report person approval verificationmre
- 3ae94b86Record browser failures and verify owned tab cleanupmre
- 54eec687Gate person approvals and merge alternativesmre
- 12b08829Add person approvals for pull request headsmre
- 55c84779Merge branch 'gp-gate-ports' into mainGitoria
- 57af42bbREADME: point to deploy.sh for the port listmre