gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitfafddbf9fafddbf9Merge hm64-catalog: tracker#64 catalog from packed entriesmrefafddbf9/deploy.sh

11.3 KB

  1. #!/usr/bin/env bash
  2. # deploy.sh — tracker.worldapi.org: Loreana (this folder) → Byrodin. Run ON LOREANA.
  3. #
  4. # ./deploy.sh gates → backup → rsync → hand over through the standby (no gap) → 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 (asked for up to 90 s)
  10. # (env: DEPLOY_TARGET, DEPLOY_URL, DEPLOY_SSH override the same defaults; DEPLOY_URL_WAIT the seconds the URL may take)
  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, .env with this app's ident
  15. # API key + secret — registered once in ident's /apps for origin https://tracker.worldapi.org
  16. # — nginx vhost, cert, DNS). This script only updates the CODE: storage/, .sessions/, .env,
  17. # .scratch/, server.*, tests/ fixtures and logs are never sent, so live data on Byrodin is
  18. # never touched. No --delete: a file removed here stays on Byrodin (harmless).
  19. set -euo pipefail
  20. # ---- the app ------------------------------------------------------------------------------
  21. APP=tracker.worldapi.org
  22. CONTAINER=tracker.worldapi.org
  23. [email protected]:/CONTAINERS/projects/tracker.worldapi.org
  24. DEFAULT_URL=https://tracker.worldapi.org/
  25. GATES=(
  26. "node tests/browser.mjs"
  27. "node tests/security.mjs"
  28. "node tests/kinds.mjs"
  29. "node tests/franchises.mjs"
  30. "node tests/pager.mjs"
  31. "node tests/franchiseseed.mjs"
  32. "node tests/signin-return.mjs" # ident#31: sign in through the selector panel, back on the same page
  33. "node theme/check-theme.mjs ."
  34. "node tests/person-lookup.mjs" # tracker#46: the person page reads no whole title records per request
  35. "node tests/mobile.mjs" # MK: the phone gate (390 px touch: tap targets, /franchises paging, a long season starts closed)
  36. "node tests/warmstart.mjs" # T17: the server listens before the boot scan is done (note, 503, then complete)
  37. )
  38. # NEVER SENT (rsync patterns; a leading / anchors at the app folder)
  39. EXCLUDES=(
  40. /.git/ /.gitignore
  41. /storage/ /.sessions/ /.env /.env.* /.scratch/ /server.* '*.log' '*.pid' node_modules/
  42. )
  43. # ---------------------------------------------------------------------------------------------
  44. DRY=0
  45. SKIP=0
  46. TARGET=${DEPLOY_TARGET:-$DEFAULT_TARGET}
  47. URL=${DEPLOY_URL:-$DEFAULT_URL}
  48. # Loreana's ssh config may not apply to Byrodin: -F /dev/null (ident STATUS)
  49. SSH=${DEPLOY_SSH:-ssh -F /dev/null -o BatchMode=yes -o ConnectTimeout=15}
  50. usage() { sed -n '2,15p' "$0" | sed 's/^# \{0,1\}//'; }
  51. while [ $# -gt 0 ]; do
  52. case "$1" in
  53. --dry-run) DRY=1 ;;
  54. --skip-tests) SKIP=1 ;;
  55. --target) TARGET=${2:?--target needs a value}; shift ;;
  56. --target=*) TARGET=${1#--target=} ;;
  57. --url) URL=${2:?--url needs a value}; shift ;;
  58. --url=*) URL=${1#--url=} ;;
  59. -h|--help) usage; exit 0 ;;
  60. *) echo "deploy: unknown argument: $1" >&2; usage >&2; exit 2 ;;
  61. esac
  62. shift
  63. done
  64. HERE=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
  65. cd "$HERE"
  66. step() { printf '\n==> %s\n' "$*"; }
  67. run() { printf ' $ %s\n' "$*" >&2; "$@"; }
  68. die() { printf '\ndeploy: REFUSED — %s\n' "$*" >&2; exit 1; }
  69. # a target `host:dir` is remote, a plain path is local
  70. if [[ "$TARGET" == *:* ]]; then
  71. REMOTE_HOST=${TARGET%%:*}
  72. REMOTE_DIR=${TARGET#*:}
  73. else
  74. REMOTE_HOST=
  75. REMOTE_DIR=$TARGET
  76. fi
  77. step "[0/5] $APP → $TARGET (dry run: $DRY, skip tests: $SKIP)"
  78. echo " from $HERE"
  79. echo " url $URL"
  80. [ -x bin/hybriel ] || die "bin/hybriel is missing here"
  81. [ -f docker-compose.yml ] || die "docker-compose.yml is missing here"
  82. [ -f project.hl ] || die "project.hl is missing here"
  83. command -v rsync >/dev/null || die "rsync is not installed"
  84. # ---- 1. the gates ---------------------------------------------------------------------------
  85. if [ "$SKIP" = 1 ]; then
  86. step "[1/5] GATES SKIPPED"
  87. printf ' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n'
  88. printf ' !! --skip-tests: NOTHING WAS TESTED. You deploy untested code. !!\n'
  89. printf ' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n'
  90. else
  91. step "[1/5] gates (${#GATES[@]})"
  92. # headless Chromes that exist BEFORE the gates are not ours (other sessions' test runs)
  93. chromes() { ps -eo pid=,ppid=,args= | awk '/[h]l-browser-tier/ && /remote-debugging-port/ && !/--type=/ {print $1, $2}'; }
  94. before=" $(chromes | awk '{print $1}' | tr '\n' ' ') "
  95. for g in "${GATES[@]}"; do
  96. printf ' $ %s\n' "$g"
  97. out=$(mktemp)
  98. if ! $g > "$out" 2>&1; then
  99. grep -E '^FAIL|passed,' "$out" | sed 's/^/ /' || true
  100. echo " (full output: $out)"
  101. die "gate failed: $g"
  102. fi
  103. grep -E 'passed,' "$out" | tail -1 | sed 's/^/ /' || true
  104. rm -f "$out"
  105. done
  106. # a Chrome our gates LEFT: new since the gates began AND no longer owned by a running node test
  107. leaked=""
  108. while read -r pid ppid; do
  109. [ -z "$pid" ] && continue
  110. case "$before" in *" $pid "*) continue ;; esac
  111. ps -o args= -p "$ppid" 2>/dev/null | grep -q '^node\|/node ' && continue
  112. leaked="$leaked $pid"
  113. done < <(chromes)
  114. if [ -n "$leaked" ]; then
  115. die "a gate left a headless Chrome (pids:$leaked; ps -eo pid,args | grep hl-browser-tier)"
  116. fi
  117. fi
  118. # ---- 2. pre-deploy backup of the LIVE data (before anything is sent) ------------------------
  119. BACKUP_ROOT=${DEPLOY_BACKUP_ROOT:-/media/SLOW1TB2/deploy-backups}
  120. BACKUP_DIR="$BACKUP_ROOT/$APP"
  121. BACKUP_FILE="$BACKUP_DIR/$APP-$(date +%Y%m%d-%H%M).tgz"
  122. if [ "$DRY" = 1 ]; then
  123. step "[2/5] DRY RUN: backup skipped"
  124. else
  125. step "[2/5] backup live data ($APP) → $BACKUP_FILE"
  126. mkdir -p "$BACKUP_DIR"
  127. if [ -n "$REMOTE_HOST" ]; then
  128. present=$($SSH "$REMOTE_HOST" "cd '$REMOTE_DIR' 2>/dev/null && for p in storage .sessions .env; do [ -e \"\$p\" ] && echo \"\$p\"; done; true")
  129. else
  130. present=$(cd "$REMOTE_DIR" 2>/dev/null && for p in storage .sessions .env; do [ -e "$p" ] && echo "$p"; done; true)
  131. fi
  132. present=$(printf '%s' "$present" | tr '\n' ' ' | sed 's/ *$//')
  133. if [ -z "$present" ]; then
  134. echo " nothing to back up yet (no storage/.sessions/.env on the target)"
  135. else
  136. echo " taring: $present"
  137. if [ -n "$REMOTE_HOST" ]; then
  138. # Order (Anton #203): directories, then every file except *.mpack (indexes, idxstate, sessions, .env), then the *.mpack
  139. # data last — a hot copy then never holds index offsets past the data it copied. tar exit 1 = "file changed as we
  140. # read it" / a file gone between find and tar (--ignore-failed-read) — a warning; the archive is checked with gzip -t.
  141. 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" \
  142. && gzip -t "$BACKUP_FILE" \
  143. || { rm -f "$BACKUP_FILE"; die "backup failed (tar/ssh error) — refusing to deploy"; }
  144. else
  145. run tar czf "$BACKUP_FILE" -C "$REMOTE_DIR" $present \
  146. || { rm -f "$BACKUP_FILE"; die "backup failed (tar error) — refusing to deploy"; }
  147. fi
  148. [ -s "$BACKUP_FILE" ] || { rm -f "$BACKUP_FILE"; die "backup is empty — refusing to deploy"; }
  149. echo " $(du -h "$BACKUP_FILE" | cut -f1) $BACKUP_FILE"
  150. ls -1t "$BACKUP_DIR/$APP"-*.tgz 2>/dev/null | tail -n +6 | xargs -r rm -f --
  151. echo " keeping $(ls -1 "$BACKUP_DIR/$APP"-*.tgz 2>/dev/null | wc -l) archive(s) of $APP"
  152. fi
  153. fi
  154. # ---- 3. rsync the code ----------------------------------------------------------------------
  155. RSYNC=(rsync -az --no-owner --no-group --itemize-changes)
  156. for e in "${EXCLUDES[@]}"; do RSYNC+=("--exclude=$e"); done
  157. if [ -n "$REMOTE_HOST" ]; then
  158. RSYNC+=(-e "$SSH")
  159. else
  160. mkdir -p "$REMOTE_DIR"
  161. fi
  162. # the preview is ALWAYS made first (rsync -n): nothing that must stay on Byrodin may be in it
  163. step "[3/5] rsync preview (what would be sent)"
  164. preview=$(mktemp)
  165. run "${RSYNC[@]}" -n ./ "$TARGET/" > "$preview"
  166. sed 's/^/ /' "$preview"
  167. echo " ($(grep -c . "$preview" || true) lines)"
  168. if awk '{print $2}' "$preview" | grep -E '^(storage/|\.sessions/|\.env|\.scratch/|server\.)|\.log$|\.pid$' ; then
  169. die "the preview holds a path that must never be sent (see above)"
  170. fi
  171. rm -f "$preview"
  172. if [ "$DRY" = 1 ]; then
  173. step "[3/5] DRY RUN: nothing sent"
  174. else
  175. step "[3/5] rsync"
  176. run "${RSYNC[@]}" ./ "$TARGET/" | sed 's/^/ /'
  177. fi
  178. # ---- 4. restart the container, without a gap ------------------------------------------------
  179. # Apollo 2026-10-25: the main container is never restarted before the standby (port 45018, compose profile "standby") answers 200.
  180. # The shared helper (worldapi-conductor/standby-swap.sh) is piped to Byrodin: it starts the standby with the new code, waits for 200,
  181. # restarts the main container, waits for 200 on 45008 and stops the standby. nginx serves from the standby meanwhile. If the standby never
  182. # comes up, nothing live was touched and the deploy stops here.
  183. SWAP=${DEPLOY_STANDBY_SWAP:-/media/STORAGE/projects/WorldAPI/worldapi-conductor/standby-swap.sh}
  184. SWAP_ARGS="tracker tracker tracker-standby / 45008"
  185. [ -f "$SWAP" ] || die "the hand-over helper is missing: $SWAP"
  186. if [ "$DRY" = 1 ]; then
  187. step "[4/5] DRY RUN: would hand over $CONTAINER through its standby (no gap)"
  188. echo " would run: $SWAP_ARGS with $SWAP on $TARGET"
  189. DRY_RUN=1 APP_DIR="$REMOTE_DIR" bash "$SWAP" $SWAP_ARGS 2>&1 | sed 's/^/ /' || true
  190. else
  191. step "[4/5] hand over $CONTAINER through its standby (no gap)"
  192. if [ -n "$REMOTE_HOST" ]; then
  193. printf ' $ %s %s "APP_DIR=%s bash -s -- %s" < %s\n' "$SSH" "$REMOTE_HOST" "$REMOTE_DIR" "$SWAP_ARGS" "$SWAP" >&2
  194. $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)"
  195. else
  196. APP_DIR="$REMOTE_DIR" bash "$SWAP" $SWAP_ARGS || die "the hand-over failed — see the log above"
  197. fi
  198. fi
  199. # ---- 5. the URL answers 200 -----------------------------------------------------------------
  200. if [ "$DRY" = 1 ]; then
  201. step "[5/5] DRY RUN: would check $URL answers 200"
  202. step "dry run done — nothing was sent, nothing restarted"
  203. exit 0
  204. fi
  205. # tracker#31 (mission 026): the boot is slower now (the search index over ~100k people, the list caches, the slug map) — nginx
  206. # answers 502 until the app listens. Ask every 2 s for up to URL_WAIT seconds (default 90), print only changes of the answer.
  207. URL_WAIT=${DEPLOY_URL_WAIT:-90}
  208. step "[5/5] $URL must answer 200 (waiting up to ${URL_WAIT} s for the boot)"
  209. code=000
  210. last=
  211. start=$(date +%s)
  212. while :; do
  213. code=$(curl -s -o /dev/null -w '%{http_code}' --max-time 10 "$URL" || true)
  214. waited=$(( $(date +%s) - start ))
  215. if [ "$code" != "$last" ] || [ "$code" = 200 ]; then echo " after ${waited} s: $code"; last=$code; fi
  216. [ "$code" = 200 ] && break
  217. [ "$waited" -ge "$URL_WAIT" ] && break
  218. sleep 2
  219. done
  220. if [ "$code" != 200 ]; then
  221. if [ -n "$REMOTE_HOST" ]; then die "$URL answered $code, not 200, after ${URL_WAIT} s — look: $SSH $REMOTE_HOST docker logs --tail 50 $CONTAINER"; fi
  222. die "$URL answered $code, not 200, after ${URL_WAIT} s — look: docker logs --tail 50 $CONTAINER"
  223. fi
  224. step "deployed $APP → $TARGET, $URL answers 200"

Branches

Latest commits

  • fafddbf9Merge hm64-catalog: tracker#64 catalog from packed entriesmre
  • b2e2206dlog: #63 livemre
  • 5831e0e0log: HM64 donemre
  • 4042b923Flatten catalog membership rows into paired slotsmre
  • ea8a759elog: #61 #62 live, DP63 and HM64 startedmre
  • 40439456Merge hm63-searchtext: tracker#63 packed search textmre
  • b7e6b48ePack normalized search text behind numeric offsetsmre
  • b35f53e1log: DP62 stale runtime decisionmre
  • f9593602log: Apollo deploy rule correctionmre
  • 3c95f7f3log: DP62 and HM63 startedmre
  • 9c4397c9mission HM63mre
  • 8dfe42c8mission DP62mre
  • e347fef4Merge rv44: tracker#61 #62 on Hybriel day44mre
  • 8274bcf1log: RV44 done, deploy held for the usage windowmre
  • 1ef7a3b1RV44: report day44 gates and migration/restart proofsmre
  • a5f62f6cRV44: re-vendor Hybriel set day44 (source 222edef7, bin 4f0f263e)mre
  • 6159a248Merge branch 'hm62-personindex' into rv44mre
  • a8923d93log: RV44 startedmre
  • cf28cf77mission RV44mre
  • 585782dfLog: tracker#60 Chrome look, STATUS rewrittenmre