gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitd0a82867d0a82867mission 048: report, LOG (HL_GC_TABLE_LOAD=62 measured)mred0a82867/deploy.sh

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

Branches

Latest commits

  • d0a82867mission 048: report, LOG (HL_GC_TABLE_LOAD=62 measured)mre
  • 2d4be66fre-vendor Hybriel 5df4eb7e (GC: HL_GC_TABLE_LOAD knob, mission 406)mre
  • 99e7c8b9tests: realdata-m035-radii, repro-t38, realdata-060-backfill ports from env (REAL_PORT, REAL_CHROME, REPRO_CHROME), defaults unchangedmre
  • 4625aa22re-vendor Hybriel 6f8a0689 (int 6–11: streaming cursors, #203 index heal + close() fix, fd403 listDir/cursor fds, hl:time browser side, hl:web batches 5+6)mre
  • a6dfd6ecdeploy: person-lookup gate (tracker#46)mre
  • 33ad4bd5Merge commit 'f57f64f'mre
  • 7d38cec4docker-compose: nofile 65536 (default soft 1024 refused boot: errno 24 in fs.listDir, Hybriel 528bc89f)mre
  • f57f64f0mission 046: report, LOGmre
  • 5965963adeploy: backup copies index files first and *.mpack data last (hot-copy skew, hybriel #203)mre
  • efe1b485mission 046: re-vendor Hybriel master 60b2817b (integration 8, #203): plugins mpackdb + web (bin/ git-ignored: hybriel, libhlrt.a, hlc.o, hlrt/ from native/zig-out)mre
  • 53099ffbmission 046: person page reads a slim per-title credit row cache (no whole-record fetch per credit); gate tests/person-lookup.mjsmre
  • 9b3828aemission 046a: report, LOGmre
  • 4b37356emission 046: franchise seed supersedeCollections reads the curated timelines once (10 s one-tick block at the end of boot)mre
  • ac6268e3mission 046a: re-vendor Hybriel 528bc89f (integration 6): plugins mpackdb/time/web (bin/ git-ignored)mre
  • 322b3293mission 046: photoQueue guard for non-row fetch results (damaged persons index)mre
  • e5b99aecmission 046: photoQueue in slices (people.hl photoQueueStart/Step, jobs.hl photoTick), tests/photoqueue.hlmre
  • 31dcf2admission 045: report, LOGmre
  • 9c3971d0tests/persons-skip-count.hlmre
  • 4d72e365mission 045mre
  • b9cae4c9slices: skip and count non-row values from range(); merge persons pass falls back to iterate; guard testmre