gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitfb9e7554fb9e7554gitoria#45: browser gate: a /pulls title links to the PR pagemrefb9e7554/deploy.sh

12.3 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. )
  40. # NEVER SENT (rsync patterns; a leading / anchors at the app folder)
  41. EXCLUDES=(
  42. /.git/ /.gitignore
  43. /storage/ /.sessions/ /.env /.env.* /.scratch/ /server.* /testapp/
  44. '*.log' '*.pid' node_modules/
  45. )
  46. # ---------------------------------------------------------------------------------------------
  47. DRY=0
  48. SKIP=0
  49. TARGET=${DEPLOY_TARGET:-$DEFAULT_TARGET}
  50. URL=${DEPLOY_URL:-$DEFAULT_URL}
  51. # how long the URL may take after the restart (seconds); a cold start compiles first (hybriel#148)
  52. URL_WAIT=${DEPLOY_URL_WAIT:-120}
  53. # Loreana's ssh config may not apply to Byrodin: -F /dev/null (ident STATUS)
  54. SSH=${DEPLOY_SSH:-ssh -F /dev/null -o BatchMode=yes -o ConnectTimeout=15}
  55. usage() { sed -n '2,15p' "$0" | sed 's/^# \{0,1\}//'; }
  56. while [ $# -gt 0 ]; do
  57. case "$1" in
  58. --dry-run) DRY=1 ;;
  59. --skip-tests) SKIP=1 ;;
  60. --target) TARGET=${2:?--target needs a value}; shift ;;
  61. --target=*) TARGET=${1#--target=} ;;
  62. --url) URL=${2:?--url needs a value}; shift ;;
  63. --url=*) URL=${1#--url=} ;;
  64. -h|--help) usage; exit 0 ;;
  65. *) echo "deploy: unknown argument: $1" >&2; usage >&2; exit 2 ;;
  66. esac
  67. shift
  68. done
  69. HERE=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
  70. cd "$HERE"
  71. step() { printf '\n==> %s\n' "$*"; }
  72. run() { printf ' $ %s\n' "$*" >&2; "$@"; }
  73. die() { printf '\ndeploy: REFUSED — %s\n' "$*" >&2; exit 1; }
  74. # a target `host:dir` is remote, a plain path is local
  75. if [[ "$TARGET" == *:* ]]; then
  76. REMOTE_HOST=${TARGET%%:*}
  77. REMOTE_DIR=${TARGET#*:}
  78. else
  79. REMOTE_HOST=
  80. REMOTE_DIR=$TARGET
  81. fi
  82. step "[0/5] $APP → $TARGET (dry run: $DRY, skip tests: $SKIP)"
  83. echo " from $HERE"
  84. echo " url $URL"
  85. [ -x bin/hybriel ] || die "bin/hybriel is missing here"
  86. [ -f docker-compose.yml ] || die "docker-compose.yml is missing here"
  87. [ -f project.hl ] || die "project.hl is missing here"
  88. command -v rsync >/dev/null || die "rsync is not installed"
  89. # ---- 1. the gates ---------------------------------------------------------------------------
  90. if [ "$SKIP" = 1 ]; then
  91. step "[1/5] GATES SKIPPED"
  92. printf ' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n'
  93. printf ' !! --skip-tests: NOTHING WAS TESTED. You deploy untested code. !!\n'
  94. printf ' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n'
  95. else
  96. step "[1/5] gates (${#GATES[@]})"
  97. # headless Chromes that exist BEFORE the gates are not ours (other sessions' test runs, e.g. Anton's)
  98. chromes() { ps -eo pid=,ppid=,args= | awk '/[h]l-browser-tier/ && /remote-debugging-port/ && !/--type=/ {print $1, $2}'; }
  99. before=" $(chromes | awk '{print $1}' | tr '\n' ' ') "
  100. for g in "${GATES[@]}"; do
  101. printf ' $ %s\n' "$g"
  102. out=$(mktemp)
  103. if ! $g > "$out" 2>&1; then
  104. grep -E '^FAIL|passed,' "$out" | sed 's/^/ /' || true
  105. echo " (full output: $out)"
  106. die "gate failed: $g"
  107. fi
  108. grep -E 'passed,|^check-theme:' "$out" | tail -1 | sed 's/^/ /' || true
  109. rm -f "$out"
  110. done
  111. # a Chrome our gates LEFT: new since the gates began AND no longer owned by a running node test
  112. # (a Chrome whose parent is a live node process belongs to another session's test run right now)
  113. leaked=""
  114. while read -r pid ppid; do
  115. [ -z "$pid" ] && continue
  116. case "$before" in *" $pid "*) continue ;; esac
  117. ps -o args= -p "$ppid" 2>/dev/null | grep -q '^node\|/node ' && continue
  118. leaked="$leaked $pid"
  119. done < <(chromes)
  120. if [ -n "$leaked" ]; then
  121. die "a gate left a headless Chrome (pids:$leaked; ps -eo pid,args | grep hl-browser-tier)"
  122. fi
  123. fi
  124. # ---- 2. pre-deploy backup of the LIVE data (before anything is sent) ------------------------
  125. BACKUP_ROOT=${DEPLOY_BACKUP_ROOT:-/media/SLOW1TB2/deploy-backups}
  126. BACKUP_DIR="$BACKUP_ROOT/$APP"
  127. BACKUP_FILE="$BACKUP_DIR/$APP-$(date +%Y%m%d-%H%M).tgz"
  128. if [ "$DRY" = 1 ]; then
  129. step "[2/5] DRY RUN: backup skipped"
  130. else
  131. step "[2/5] backup live data ($APP) → $BACKUP_FILE"
  132. mkdir -p "$BACKUP_DIR"
  133. if [ -n "$REMOTE_HOST" ]; then
  134. present=$($SSH "$REMOTE_HOST" "cd '$REMOTE_DIR' 2>/dev/null && for p in storage .sessions .env; do [ -e \"\$p\" ] && echo \"\$p\"; done; true")
  135. else
  136. present=$(cd "$REMOTE_DIR" 2>/dev/null && for p in storage .sessions .env; do [ -e "$p" ] && echo "$p"; done; true)
  137. fi
  138. present=$(printf '%s' "$present" | tr '\n' ' ' | sed 's/ *$//')
  139. if [ -z "$present" ]; then
  140. echo " nothing to back up yet (no storage/.sessions/.env on the target)"
  141. else
  142. echo " taring: $present"
  143. if [ -n "$REMOTE_HOST" ]; then
  144. # Order (Anton #203): directories, then every file except *.mpack (indexes, idxstate, sessions, .env), then the *.mpack
  145. # data last — a hot copy then never holds index offsets past the data it copied. tar exit 1 = "file changed as we
  146. # read it" / a file gone between find and tar (--ignore-failed-read) — a warning; the archive is checked with gzip -t.
  147. 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" \
  148. || { rm -f "$BACKUP_FILE"; die "backup failed (tar/ssh error) — refusing to deploy"; }
  149. else
  150. run tar czf "$BACKUP_FILE" -C "$REMOTE_DIR" $present \
  151. || { rm -f "$BACKUP_FILE"; die "backup failed (tar error) — refusing to deploy"; }
  152. fi
  153. [ -s "$BACKUP_FILE" ] || { rm -f "$BACKUP_FILE"; die "backup is empty — refusing to deploy"; }
  154. echo " $(du -h "$BACKUP_FILE" | cut -f1) $BACKUP_FILE"
  155. ls -1t "$BACKUP_DIR/$APP"-*.tgz 2>/dev/null | tail -n +6 | xargs -r rm -f --
  156. echo " keeping $(ls -1 "$BACKUP_DIR/$APP"-*.tgz 2>/dev/null | wc -l) archive(s) of $APP"
  157. fi
  158. fi
  159. # ---- 3. rsync the code ----------------------------------------------------------------------
  160. RSYNC=(rsync -az --no-owner --no-group --itemize-changes)
  161. for e in "${EXCLUDES[@]}"; do RSYNC+=("--exclude=$e"); done
  162. if [ -n "$REMOTE_HOST" ]; then
  163. RSYNC+=(-e "$SSH")
  164. else
  165. mkdir -p "$REMOTE_DIR"
  166. fi
  167. # the preview is ALWAYS made first (rsync -n): nothing that must stay on Byrodin may be in it
  168. step "[3/5] rsync preview (what would be sent)"
  169. preview=$(mktemp)
  170. run "${RSYNC[@]}" -n ./ "$TARGET/" > "$preview"
  171. sed 's/^/ /' "$preview"
  172. echo " ($(grep -c . "$preview" || true) lines)"
  173. if awk '{print $2}' "$preview" | grep -E '^(storage/|\.sessions/|\.env|\.scratch/|server\.|testapp/)|\.log$|\.pid$' ; then
  174. die "the preview holds a path that must never be sent (see above)"
  175. fi
  176. rm -f "$preview"
  177. if [ "$DRY" = 1 ]; then
  178. step "[3/5] DRY RUN: nothing sent"
  179. else
  180. step "[3/5] rsync"
  181. run "${RSYNC[@]}" ./ "$TARGET/" | sed 's/^/ /'
  182. fi
  183. # ---- 4. restart the container, without a gap ------------------------------------------------
  184. # Apollo 2026-10-25: the main container is never restarted before the standby (port 45014, compose profile "standby") answers 200.
  185. # The shared helper (worldapi-conductor/standby-swap.sh) is piped to Byrodin: it starts the standby with the new code, waits for 200 on
  186. # /api/repos, restarts the main container, waits for 200 on 45004 and stops the standby. nginx serves from the standby meanwhile. If the
  187. # standby never comes up, nothing live was touched and the deploy stops here.
  188. SWAP=${DEPLOY_STANDBY_SWAP:-/media/STORAGE/projects/WorldAPI/worldapi-conductor/standby-swap.sh}
  189. SWAP_ARGS="gitoria gitoria gitoria-standby /api/repos 45004"
  190. [ -f "$SWAP" ] || die "the hand-over helper is missing: $SWAP"
  191. if [ "$DRY" = 1 ]; then
  192. step "[4/5] DRY RUN: would hand over $CONTAINER through its standby (no gap)"
  193. echo " would run: $SWAP_ARGS with $SWAP on $TARGET"
  194. DRY_RUN=1 APP_DIR="$REMOTE_DIR" bash "$SWAP" $SWAP_ARGS 2>&1 | sed 's/^/ /' || true
  195. else
  196. step "[4/5] hand over $CONTAINER through its standby (no gap)"
  197. if [ -n "$REMOTE_HOST" ]; then
  198. printf ' $ %s %s "APP_DIR=%s bash -s -- %s" < %s\n' "$SSH" "$REMOTE_HOST" "$REMOTE_DIR" "$SWAP_ARGS" "$SWAP" >&2
  199. $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)"
  200. else
  201. APP_DIR="$REMOTE_DIR" bash "$SWAP" $SWAP_ARGS || die "the hand-over failed — see the log above"
  202. fi
  203. fi
  204. # 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
  205. if [ "$DRY" != 1 ]; then
  206. # 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)
  207. CHANGED="cd '$REMOTE_DIR' && docker compose build gitoria-sshd && docker compose up -d"
  208. if [ -n "$REMOTE_HOST" ]; then run $SSH "$REMOTE_HOST" "$CHANGED"; else run bash -c "$CHANGED"; fi
  209. fi
  210. # ---- 5. the URL answers 200 -----------------------------------------------------------------
  211. if [ "$DRY" = 1 ]; then
  212. step "[5/5] DRY RUN: would check $URL answers 200"
  213. step "dry run done — nothing was sent, nothing restarted"
  214. exit 0
  215. fi
  216. 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)"
  217. code=000
  218. start=$SECONDS
  219. i=0
  220. while :; do
  221. i=$((i + 1))
  222. code=$(curl -s -o /dev/null -w '%{http_code}' --max-time 10 "$URL" || true)
  223. echo " try $i ($((SECONDS - start)) s): $code"
  224. [ "$code" = 200 ] && break
  225. [ $((SECONDS - start)) -ge "$URL_WAIT" ] && break
  226. sleep 2
  227. done
  228. if [ "$code" != 200 ]; then
  229. if [ -n "$REMOTE_HOST" ]; then die "$URL answered $code, not 200 — look: $SSH $REMOTE_HOST docker logs --tail 50 $CONTAINER"; fi
  230. die "$URL answered $code, not 200 — look: docker logs --tail 50 $CONTAINER"
  231. fi
  232. step "deployed $APP → $TARGET, $URL answers 200"

Branches

Latest commits

  • 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
  • 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
  • 34143d2cgitoria#46: closing brace of appComments lost in the mergemre
  • 54927c9cgitoria#46: PR statuses and required checks before merge (merge w46-status)mre
  • 187e1871gitoria#46: badges without decorative bordersmre
  • 6b022798gitoria#46: tests/status.mjs in GATESmre
  • c6162f00gitoria#46: status badges on /pulls, Required before merge in Settingsmre
  • e5a0b64egitoria#46: PUT statuses/<sha>, required contexts block the merge on the servermre
  • 26a140e3gitoria#44: reviews, inline threads, replies, conversation for apps (merge w44-reviews)mre
  • 888dbf87gitoria#44: unknown API paths answer the contract's JSON 404 and sit above the page routesmre
  • ea587d90gitoria#44: reviews with inline threads, replies, resolve and the PR conversation for apps (lib/reviews.hl, tests/reviews.mjs)mre
  • 49533a50gitoria#43: signed events to connected apps (merge w43-events)mre
  • 1a1e87d5gitoria#43: the sender never writes the apps row; Settings shows the newest deliverymre
  • cd442066gitoria#43: app-auth unit test reads the apps table from its new filemre
  • 5dccbe38gitoria#43: in-flight rows are left alone by the other process; gate in deploy.sh; READMEmre