gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit50093aaf50093aafgitoria#46: statuses keyed by app + context (merge w46b-keyed)mre50093aaf/deploy.sh

12.6 KB

  1. #!/usr/bin/env bash
  2. # deploy.sh — gitoria.worldapi.org: Loreana (this folder) → Byrodin. Run ON LOREANA.
  3. #
  4. # ./deploy.sh gates → backup → rsync → restart the container → public URL 200
  5. # ./deploy.sh --dry-run gates → rsync -n (shows what WOULD be sent), no backup, no restart, no URL check
  6. # ./deploy.sh --skip-tests skips the gates (LOUD warning) — only when you know why
  7. # ./deploy.sh --target DIR|HOST:DIR another destination (default below); a local DIR is
  8. # how the script is tested without touching Byrodin
  9. # ./deploy.sh --url URL the URL that must answer 200 after the restart
  10. # (env: DEPLOY_TARGET, DEPLOY_URL, DEPLOY_SSH override the same defaults)
  11. # (backup: tars storage/.sessions/.env that exist on the target to Loreana's
  12. # /media/SLOW1TB2/deploy-backups/<app>/, keeps newest 5; --target DIR backs up DIR instead)
  13. #
  14. # THE FIRST DEPLOY is done by the architect on Byrodin (folder, data, nginx vhost, cert,
  15. # DNS). This script only updates the CODE: storage/, .sessions/, .env, .scratch/, server.*,
  16. # testapp/ and logs are never sent, so live data on Byrodin is never touched.
  17. # No --delete: a file removed here stays on Byrodin (harmless — nothing imports it).
  18. set -euo pipefail
  19. # ---- the app ------------------------------------------------------------------------------
  20. APP=gitoria.worldapi.org
  21. CONTAINER=gitoria.worldapi.org
  22. [email protected]:/CONTAINERS/projects/gitoria.worldapi.org
  23. DEFAULT_URL=https://gitoria.worldapi.org/
  24. GATES=(
  25. "node tests/signin-return.mjs" # ident#31: sign in through the selector panel, back on the same page
  26. "node theme/check-theme.mjs ."
  27. "node tests/browser.mjs"
  28. "node tests/security.mjs"
  29. "node tests/private.mjs" # go34: private repos, organizations (158 checks)
  30. "node tests/ident-access-gitoria.mjs" # ident#27: an idt_ access key as the git password
  31. "node tests/apps.mjs" # gitoria#41: apps connected to a repo (URL allow-list, connect flow, add/remove/rotate)
  32. "node tests/pull-numbers.mjs" # gitoria#40: PR numbers, refs/pull/N/head, the changes of syncPulls, wasHeadOf
  33. "node tests/pull-ssh.mjs" # gitoria#40: the same over the sshd door (docker)
  34. "node tests/app-token.mjs" # gitoria#42: an app trades its key for a one-hour token; git read with it, no push
  35. "node tests/reviews.mjs" # gitoria#44: reviews with inline threads, replies, resolve, conversation (409/422/404, earlier head, paging, seq)
  36. "node tests/events.mjs" # gitoria#43: signed events to apps (HMAC, retries, Merge button, ping, delivery log, two processes, retention)
  37. "node tests/pr-page.mjs" # gitoria#45: the PR page (comments, threads, Review again, who hears of it, escaping, read-only for others)
  38. "node tests/status.mjs" # gitoria#46: statuses an app sets on a commit, required before merge, badges on /pulls
  39. "node tests/limits.mjs" # gitoria#47: status read route, pull.statuses/required, tickets list, 413/400, rate limits (429 + Retry-After)
  40. "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
  41. )
  42. # NEVER SENT (rsync patterns; a leading / anchors at the app folder)
  43. EXCLUDES=(
  44. /.git/ /.gitignore
  45. /storage/ /.sessions/ /.env /.env.* /.scratch/ /server.* /testapp/
  46. '*.log' '*.pid' node_modules/
  47. )
  48. # ---------------------------------------------------------------------------------------------
  49. DRY=0
  50. SKIP=0
  51. TARGET=${DEPLOY_TARGET:-$DEFAULT_TARGET}
  52. URL=${DEPLOY_URL:-$DEFAULT_URL}
  53. # how long the URL may take after the restart (seconds); a cold start compiles first (hybriel#148)
  54. URL_WAIT=${DEPLOY_URL_WAIT:-120}
  55. # Loreana's ssh config may not apply to Byrodin: -F /dev/null (ident STATUS)
  56. SSH=${DEPLOY_SSH:-ssh -F /dev/null -o BatchMode=yes -o ConnectTimeout=15}
  57. usage() { sed -n '2,15p' "$0" | sed 's/^# \{0,1\}//'; }
  58. while [ $# -gt 0 ]; do
  59. case "$1" in
  60. --dry-run) DRY=1 ;;
  61. --skip-tests) SKIP=1 ;;
  62. --target) TARGET=${2:?--target needs a value}; shift ;;
  63. --target=*) TARGET=${1#--target=} ;;
  64. --url) URL=${2:?--url needs a value}; shift ;;
  65. --url=*) URL=${1#--url=} ;;
  66. -h|--help) usage; exit 0 ;;
  67. *) echo "deploy: unknown argument: $1" >&2; usage >&2; exit 2 ;;
  68. esac
  69. shift
  70. done
  71. HERE=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
  72. cd "$HERE"
  73. step() { printf '\n==> %s\n' "$*"; }
  74. run() { printf ' $ %s\n' "$*" >&2; "$@"; }
  75. die() { printf '\ndeploy: REFUSED — %s\n' "$*" >&2; exit 1; }
  76. # a target `host:dir` is remote, a plain path is local
  77. if [[ "$TARGET" == *:* ]]; then
  78. REMOTE_HOST=${TARGET%%:*}
  79. REMOTE_DIR=${TARGET#*:}
  80. else
  81. REMOTE_HOST=
  82. REMOTE_DIR=$TARGET
  83. fi
  84. step "[0/5] $APP → $TARGET (dry run: $DRY, skip tests: $SKIP)"
  85. echo " from $HERE"
  86. echo " url $URL"
  87. [ -x bin/hybriel ] || die "bin/hybriel is missing here"
  88. [ -f docker-compose.yml ] || die "docker-compose.yml is missing here"
  89. [ -f project.hl ] || die "project.hl is missing here"
  90. command -v rsync >/dev/null || die "rsync is not installed"
  91. # ---- 1. the gates ---------------------------------------------------------------------------
  92. if [ "$SKIP" = 1 ]; then
  93. step "[1/5] GATES SKIPPED"
  94. printf ' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n'
  95. printf ' !! --skip-tests: NOTHING WAS TESTED. You deploy untested code. !!\n'
  96. printf ' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n'
  97. else
  98. step "[1/5] gates (${#GATES[@]})"
  99. # headless Chromes that exist BEFORE the gates are not ours (other sessions' test runs, e.g. Anton's)
  100. chromes() { ps -eo pid=,ppid=,args= | awk '/[h]l-browser-tier/ && /remote-debugging-port/ && !/--type=/ {print $1, $2}'; }
  101. before=" $(chromes | awk '{print $1}' | tr '\n' ' ') "
  102. for g in "${GATES[@]}"; do
  103. printf ' $ %s\n' "$g"
  104. out=$(mktemp)
  105. if ! $g > "$out" 2>&1; then
  106. grep -E '^FAIL|passed,' "$out" | sed 's/^/ /' || true
  107. echo " (full output: $out)"
  108. die "gate failed: $g"
  109. fi
  110. grep -E 'passed,|^check-theme:' "$out" | tail -1 | sed 's/^/ /' || true
  111. rm -f "$out"
  112. done
  113. # a Chrome our gates LEFT: new since the gates began AND no longer owned by a running node test
  114. # (a Chrome whose parent is a live node process belongs to another session's test run right now)
  115. leaked=""
  116. while read -r pid ppid; do
  117. [ -z "$pid" ] && continue
  118. case "$before" in *" $pid "*) continue ;; esac
  119. ps -o args= -p "$ppid" 2>/dev/null | grep -q '^node\|/node ' && continue
  120. leaked="$leaked $pid"
  121. done < <(chromes)
  122. if [ -n "$leaked" ]; then
  123. die "a gate left a headless Chrome (pids:$leaked; ps -eo pid,args | grep hl-browser-tier)"
  124. fi
  125. fi
  126. # ---- 2. pre-deploy backup of the LIVE data (before anything is sent) ------------------------
  127. BACKUP_ROOT=${DEPLOY_BACKUP_ROOT:-/media/SLOW1TB2/deploy-backups}
  128. BACKUP_DIR="$BACKUP_ROOT/$APP"
  129. BACKUP_FILE="$BACKUP_DIR/$APP-$(date +%Y%m%d-%H%M).tgz"
  130. if [ "$DRY" = 1 ]; then
  131. step "[2/5] DRY RUN: backup skipped"
  132. else
  133. step "[2/5] backup live data ($APP) → $BACKUP_FILE"
  134. mkdir -p "$BACKUP_DIR"
  135. if [ -n "$REMOTE_HOST" ]; then
  136. present=$($SSH "$REMOTE_HOST" "cd '$REMOTE_DIR' 2>/dev/null && for p in storage .sessions .env; do [ -e \"\$p\" ] && echo \"\$p\"; done; true")
  137. else
  138. present=$(cd "$REMOTE_DIR" 2>/dev/null && for p in storage .sessions .env; do [ -e "$p" ] && echo "$p"; done; true)
  139. fi
  140. present=$(printf '%s' "$present" | tr '\n' ' ' | sed 's/ *$//')
  141. if [ -z "$present" ]; then
  142. echo " nothing to back up yet (no storage/.sessions/.env on the target)"
  143. else
  144. echo " taring: $present"
  145. if [ -n "$REMOTE_HOST" ]; then
  146. # Order (Anton #203): directories, then every file except *.mpack (indexes, idxstate, sessions, .env), then the *.mpack
  147. # data last — a hot copy then never holds index offsets past the data it copied. tar exit 1 = "file changed as we
  148. # read it" / a file gone between find and tar (--ignore-failed-read) — a warning; the archive is checked with gzip -t.
  149. 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 - --warning=no-file-changed --ignore-failed-read --no-recursion -T -; r=\$?; [ \$r -le 1 ]" > "$BACKUP_FILE" \
  150. || { rm -f "$BACKUP_FILE"; die "backup failed (tar/ssh error) — refusing to deploy"; }
  151. else
  152. run tar czf "$BACKUP_FILE" -C "$REMOTE_DIR" $present \
  153. || { rm -f "$BACKUP_FILE"; die "backup failed (tar error) — refusing to deploy"; }
  154. fi
  155. [ -s "$BACKUP_FILE" ] || { rm -f "$BACKUP_FILE"; die "backup is empty — refusing to deploy"; }
  156. echo " $(du -h "$BACKUP_FILE" | cut -f1) $BACKUP_FILE"
  157. ls -1t "$BACKUP_DIR/$APP"-*.tgz 2>/dev/null | tail -n +6 | xargs -r rm -f --
  158. echo " keeping $(ls -1 "$BACKUP_DIR/$APP"-*.tgz 2>/dev/null | wc -l) archive(s) of $APP"
  159. fi
  160. fi
  161. # ---- 3. rsync the code ----------------------------------------------------------------------
  162. RSYNC=(rsync -az --no-owner --no-group --itemize-changes)
  163. for e in "${EXCLUDES[@]}"; do RSYNC+=("--exclude=$e"); done
  164. if [ -n "$REMOTE_HOST" ]; then
  165. RSYNC+=(-e "$SSH")
  166. else
  167. mkdir -p "$REMOTE_DIR"
  168. fi
  169. # the preview is ALWAYS made first (rsync -n): nothing that must stay on Byrodin may be in it
  170. step "[3/5] rsync preview (what would be sent)"
  171. preview=$(mktemp)
  172. run "${RSYNC[@]}" -n ./ "$TARGET/" > "$preview"
  173. sed 's/^/ /' "$preview"
  174. echo " ($(grep -c . "$preview" || true) lines)"
  175. if awk '{print $2}' "$preview" | grep -E '^(storage/|\.sessions/|\.env|\.scratch/|server\.|testapp/)|\.log$|\.pid$' ; then
  176. die "the preview holds a path that must never be sent (see above)"
  177. fi
  178. rm -f "$preview"
  179. if [ "$DRY" = 1 ]; then
  180. step "[3/5] DRY RUN: nothing sent"
  181. else
  182. step "[3/5] rsync"
  183. run "${RSYNC[@]}" ./ "$TARGET/" | sed 's/^/ /'
  184. fi
  185. # ---- 4. restart the container, without a gap ------------------------------------------------
  186. # Apollo 2026-10-25: the main container is never restarted before the standby (port 45014, compose profile "standby") answers 200.
  187. # The shared helper (worldapi-conductor/standby-swap.sh) is piped to Byrodin: it starts the standby with the new code, waits for 200 on
  188. # /api/repos, restarts the main container, waits for 200 on 45004 and stops the standby. nginx serves from the standby meanwhile. If the
  189. # standby never comes up, nothing live was touched and the deploy stops here.
  190. SWAP=${DEPLOY_STANDBY_SWAP:-/media/STORAGE/projects/WorldAPI/worldapi-conductor/standby-swap.sh}
  191. SWAP_ARGS="gitoria gitoria gitoria-standby /api/repos 45004"
  192. [ -f "$SWAP" ] || die "the hand-over helper is missing: $SWAP"
  193. if [ "$DRY" = 1 ]; then
  194. step "[4/5] DRY RUN: would hand over $CONTAINER through its standby (no gap)"
  195. echo " would run: $SWAP_ARGS with $SWAP on $TARGET"
  196. DRY_RUN=1 APP_DIR="$REMOTE_DIR" bash "$SWAP" $SWAP_ARGS 2>&1 | sed 's/^/ /' || true
  197. else
  198. step "[4/5] hand over $CONTAINER through its standby (no gap)"
  199. if [ -n "$REMOTE_HOST" ]; then
  200. printf ' $ %s %s "APP_DIR=%s bash -s -- %s" < %s\n' "$SSH" "$REMOTE_HOST" "$REMOTE_DIR" "$SWAP_ARGS" "$SWAP" >&2
  201. $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)"
  202. else
  203. APP_DIR="$REMOTE_DIR" bash "$SWAP" $SWAP_ARGS || die "the hand-over failed — see the log above"
  204. fi
  205. fi
  206. # 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 changed
  207. if [ "$DRY" != 1 ]; then
  208. # 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)
  209. CHANGED="cd '$REMOTE_DIR' && docker compose build gitoria-sshd && docker compose up -d"
  210. if [ -n "$REMOTE_HOST" ]; then run $SSH "$REMOTE_HOST" "$CHANGED"; else run bash -c "$CHANGED"; fi
  211. fi
  212. # ---- 5. the URL answers 200 -----------------------------------------------------------------
  213. if [ "$DRY" = 1 ]; then
  214. step "[5/5] DRY RUN: would check $URL answers 200"
  215. step "dry run done — nothing was sent, nothing restarted"
  216. exit 0
  217. fi
  218. step "[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)"
  219. code=000
  220. start=$SECONDS
  221. i=0
  222. while :; do
  223. i=$((i + 1))
  224. code=$(curl -s -o /dev/null -w '%{http_code}' --max-time 10 "$URL" || true)
  225. echo " try $i ($((SECONDS - start)) s): $code"
  226. [ "$code" = 200 ] && break
  227. [ $((SECONDS - start)) -ge "$URL_WAIT" ] && break
  228. sleep 2
  229. done
  230. if [ "$code" != 200 ]; then
  231. if [ -n "$REMOTE_HOST" ]; then die "$URL answered $code, not 200 — look: $SSH $REMOTE_HOST docker logs --tail 50 $CONTAINER"; fi
  232. die "$URL answered $code, not 200 — look: docker logs --tail 50 $CONTAINER"
  233. fi
  234. step "deployed $APP → $TARGET, $URL answers 200"

Branches

Latest commits

  • 50093aafgitoria#46: statuses keyed by app + context (merge w46b-keyed)mre
  • dd0267edgitoria#46: fix a label in the keyed gatemre
  • b1dcb580gitoria#46: statuses keyed by app + context, required = app + context, old rows still readmre
  • 2287363egitoria#47: W47 report, logmre
  • 9f502db1gitoria#46: W46b briefmre
  • 9678a8f3gitoria#46: contract v1.4, statuses keyed by app + contextmre
  • bdcbbaa9gitoria#47: rate limits, status read route, statuses + required on the pull (merge w47-limits)mre
  • 772c401fgitoria#45: W45 report, logmre
  • b6b793afgitoria#45: PR page with reviews, threads, conversation, Review again (merge w45-pr-page)mre
  • fb9e7554gitoria#45: browser gate: a /pulls title links to the PR pagemre
  • f7198131gitoria#45: tests/pr-page.mjs gatemre
  • 483141d0gitoria#45: Settings shows the Apps heading only to the ownermre
  • e2eff8f6gitoria#45: the PR page /pulls/N with reviews, threads, verdicts, conversation, comment, reply, resolve and Review againmre
  • 7a602839gitoria#45: raiseEvent can send to one app only; people's comments, replies and resolving in reviews.hlmre
  • 5de7f578gitoria#47: rate limits per app (429, Retry-After), status read route, statuses and required on the pull, one JSON check for every write routemre
  • 19edc1f5gitoria#44: contract v1.3.1, what outdated meansmre
  • 7c2277ccgitoria#38: worker reports W40–W46, logmre
  • 1c3fa842gitoria#45 #47: worker briefsmre
  • 0a46bf7dgitoria#47: apps API contract v1.3 (status read route, statuses + required on the pull)mre
  • 3373f7c3gitoria#38: push API contract v1.2.1, reviews, worker briefs and logmre