gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Branchrecords-park55f567f025|||PR records: park #55, no Claude workers ruleTrackerrecords-park55/deploy.sh

11.9 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 2 (Apollo 2026-11-11: ~20 GB each); --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. # gate ports in Tracker's loreana slot 8716-8719 (the tests' defaults 8700/8701 hit other apps); env overrides win
  96. export LC_ALL=C
  97. export TRACKER_GATE_PORT="${TRACKER_GATE_PORT:-8717}" TRACKER_GATE_IDENT_PORT="${TRACKER_GATE_IDENT_PORT:-8716}" TRACKER_GATE_TMDB_PORT="${TRACKER_GATE_TMDB_PORT:-8719}"
  98. export TRACKER_GATE_CHROME="${TRACKER_GATE_CHROME:-8718-8718}" TRACKER_SIGNIN_PORT="${TRACKER_SIGNIN_PORT:-8716}" TRACKER_SIGNIN_CHROME="${TRACKER_SIGNIN_CHROME:-8718-8718}"
  99. export HL_PORT_BASE="${HL_PORT_BASE:-8518}" TRACKER_COLD_MS="${TRACKER_COLD_MS:-180000}"
  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,' "$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. 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 - --hard-dereference --warning=no-file-changed --ignore-failed-read --no-recursion -T -; r=\$?; [ \$r -le 1 ]" > "$BACKUP_FILE" \
  147. && gzip -t "$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 +3 | 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\.)|\.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 45018, 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,
  186. # restarts the main container, waits for 200 on 45008 and stops the standby. nginx serves from the standby meanwhile. If the standby never
  187. # 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="tracker tracker tracker-standby / 45008"
  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. # ---- 5. the URL answers 200 -----------------------------------------------------------------
  205. if [ "$DRY" = 1 ]; then
  206. step "[5/5] DRY RUN: would check $URL answers 200"
  207. step "dry run done — nothing was sent, nothing restarted"
  208. exit 0
  209. fi
  210. # tracker#31 (mission 026): the boot is slower now (the search index over ~100k people, the list caches, the slug map) — nginx
  211. # answers 502 until the app listens. Ask every 2 s for up to URL_WAIT seconds (default 90), print only changes of the answer.
  212. URL_WAIT=${DEPLOY_URL_WAIT:-90}
  213. step "[5/5] $URL must answer 200 (waiting up to ${URL_WAIT} s for the boot)"
  214. code=000
  215. last=
  216. start=$(date +%s)
  217. while :; do
  218. code=$(curl -s -o /dev/null -w '%{http_code}' --max-time 10 "$URL" || true)
  219. waited=$(( $(date +%s) - start ))
  220. if [ "$code" != "$last" ] || [ "$code" = 200 ]; then echo " after ${waited} s: $code"; last=$code; fi
  221. [ "$code" = 200 ] && break
  222. [ "$waited" -ge "$URL_WAIT" ] && break
  223. sleep 2
  224. done
  225. if [ "$code" != 200 ]; then
  226. 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
  227. die "$URL answered $code, not 200, after ${URL_WAIT} s — look: docker logs --tail 50 $CONTAINER"
  228. fi
  229. step "deployed $APP → $TARGET, $URL answers 200"

Branches

Latest commits

  • f567f025|||PR records: park #55, no Claude workers ruleTracker
  • c21aff6bMerge branch 'records-hm55c' into mainTracker
  • 9ba258d3|||PR records: HM55b resultTracker
  • 64f2d300Merge branch 'records-hm55b2' into mainTracker
  • ae3b80dc|||PR records: HM55b briefTracker
  • 7ef3cb3bMerge branch 'records-st55' into mainTracker
  • fec2e080|||PR records: STATUS HM55Tracker
  • 8e174918Merge branch 'records-hm55b' into mainTracker
  • b1ecee57|||PR records: HM55 stateTracker
  • 28eab08cMerge branch 'records-hm55' into mainTracker
  • d3edaa05|||PR records: HM55 start, PROCEDURES.mdTracker
  • 3059f86cMerge branch 'records-design-rule' into mainTracker
  • 421a4809|||PR records: his-design rule (Apollo 2026-11-13)Tracker
  • d1390692Merge branch 'records-dp75b' into mainTracker
  • a4d1a7d2|||PR records: later steps checked, branch auditTracker
  • e070e16fMerge branch 'records-dp75' into mainTracker
  • 642b4fa8|||PR records: DP75 day62 live, AP62 briefTracker
  • bfc4ea4dMerge branch 'ap62' into mainTracker
  • 2c95ac46|||PR AP62: re-vendor Hybriel set day62 (source 70341ad2, bin a54f5f08)Tracker
  • b9c7c3c8Merge branch 'records-dp74' into mainTracker