gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit944ad51e944ad51eSK: report reports/SK-sec.md, LOGsk944ad51e/deploy.sh

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

Branches

Latest commits

  • 944ad51eSK: report reports/SK-sec.md, LOGsk
  • 415487c1SK: import budget per user + id type check, gate checkssk
  • 7b8a4f74security audit SK: nosniff + image sandbox headers, face id checks, adult follow, login budget + new session at login, guest-link budget, tests/security.mjssk
  • f0f26c5areports: T18 section (gates, boot-gate, 3x3 table, search + short-id compare)mre
  • 2a8682a1deploy.sh GATES: tests/warmstart.mjsmre
  • 3761338breports/T17-boot.md: handoff (phase table, fix, numbers so far, open proof)mre
  • 52e09eb2boot: sync window 1000 ms (the gates ask for a complete start), README boot-time sectionmre
  • c87149e8tests/warmstart.mjs: the progressive start proven over HTTP (note in the window, 503 short id, after: everything); bootBeginmre
  • 593a6350boot: resumable start-up scan, the server listens first (T17)mre
  • be4890a0reports: H2 hydration audit (no test change needed)mre
  • 1f9c8bf4re-vendor Hybriel 5406d146 (hl:web batches 9+10: #72 emits while the socket is closed delivered after reconnect, browser boot linear, faster list appends; contains int 6–14 + m406)mre
  • c1df88eddocker-compose: HL_GC_TABLE_LOAD=62 (tracker mission 048, T16 real-data numbers)mre
  • 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