gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit7c2277cc7c2277ccgitoria#38: worker reports W40–W46, logmre7c2277cc/deploy.sh

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

Branches

Latest commits

  • 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
  • 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
  • 8c8805f3merge main (gitoria#42), delivery log and redelivery routes, sweep fixmre
  • 5a996ea8gitoria#42: short-lived app tokens, read-only git fetch with them (merge w42-app-token)mre
  • afdb0cbfgitoria#43: rename the moved helper (name clash in the browser build)mre