gitoria

calendar

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commiteb86526feb86526fmission brief 004mreeb86526f/deploy.sh

8.8 KB

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

Branches

Latest commits

  • eb86526fmission brief 004mre
  • 2f953b16deploy.sh: wait up to DEPLOY_URL_WAIT (120 s) for 200 after the restart, as the other apps (cold compile, hybriel#148)mre
  • 9078e1b7calendar mission 003 (2/2): report, LOG, STATUS (cause: timers only in an open, front, touched page; gate old 5/2 new 7/0; Hybriel gaps Web Push + page-shown hook, STOP)mre
  • 31ffe910calendar mission 003 (1/2): calendar#9 — missed reminders shown when the app is back, timers set again when it is shownmre
  • 26c997d3mission brief 003mre
  • c6809c76calendar mission 002: report, LOG, STATUS (test bug; fixed gate 78/0 at 00:30 05:40 12:00 19:59 20:01; 23:30 not run, quota stop)mre
  • 00f72783calendar mission 002 (2/2): TEST bug — the reminder steps get their own identity (carol): alice's all-day Vacation (20th-22nd) from the events steps added 2 reminders from the 12th 20:00 to the 19th 23:00; Tokyo check no longer skipped in the Tokyo eveningmre
  • 54fb0ce1calendar mission 002 (1/2): gate clock override GATE_CLOCK=HH:MM (this process and every page get Date shifted to that local time today)mre
  • c838ee79mission brief 002mre
  • f2bc67ecdocker-compose: init: true (hybriel#156, PID 1 ignored SIGTERM, every restart waited 10 s)mre
  • 2881119fcalendar mission 001 (4/4): code order — README layout + same-output test, STATUS, LOG, report; tests/letcount.py (sub-folders), tests/realdata-baseline.mjs, tests/realdata-compare.pymre
  • 3b2a4cd0calendar mission 001 (3/4): code order — let only where reassigned (147 dropped incl. components/month-view; 110 left: 67 reassigned, 43 loop-bound); gates 78/0 (3 of 4 runs; 1 known 'next month' flake) + 18/0, live-data run = step 2mre
  • f5ce6b1dcalendar mission 001 (2/4): code order — topics, map, thin faces: lib/util.hl, lib/events(-helpers).hl (+calendarView/settingsView/soonOf), lib/settings.hl, lib/users.hl (+userOfLoginCode, tagOf), lib/api(-helpers).hl; project.hl = map; login route takes &req/&sessions (failed-login reason now kept); gates 78/0 + 18/0mre
  • c6fbd011calendar mission 001 (1/4): code order — files moved: lib/events.hl, lib/users.hl, components/styles.hl (imports only); gates 78/0 + 18/0, live-data run identicalmre
  • d6c59290calendar: Hybriel master 06617221 (plugin allocators 3a781359 + 413f60e4, mpackdb 2cb7ae5e, http1 773de63e); gates 78/0 + 18/0mre
  • 7bd0337ccalendar: Hybriel master 190aa11d (fc838894 GC correctness, #126 closure scopes, #127); gates 78/0 + 18/0mre
  • ff41310ccalendar: Hybriel master 8efba065 (#126 memory, #48 lambda copy; audit: no & needed)mre
  • 14ba08c7antcolony#40: mission references point to the moved missionsmre
  • 99c73346antcolony#40: history (LOG.md), worker briefs (missions/) and reports moved here from antcolony, numbered per project; old numbers in antcolony docs/mission-map.mdmre
  • 76edaa62calendar: Hybriel master ff51cf46 (re-vendor round, static workaround removed)mre