gitoria

notes

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit7f6a40907f6a4090README: offline in Hybriel (hl:web offline, continuation, known md-editor gap); gate waits report instead of crash; realdata-offline checkmre7f6a4090/deploy.sh

9.0 KB

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

Branches

Latest commits

  • 7f6a4090README: offline in Hybriel (hl:web offline, continuation, known md-editor gap); gate waits report instead of crash; realdata-offline checkmre
  • 632223f2notes#4: delete notes-offline.js and sw.js (hl:web's generated worker and kept state replace them)mre
  • 52a2acadgate: offline checks on hl:web's worker/queue, Chrome offline via CDP (reload offline first), one-offline conflict, log-out forgets the kept copymre
  • e5785b03notes#4: the notes page in Hybriel — hl:web offline = [Notes], queued faces notesPut/notesDrop, push notesChanged, value-form notesPull on a tick; notes-offline.js/sw.js no longer loaded or routedmre
  • 510aff75notes#4: server continues a run of saves of one login (by + since), loser keeps writing into its copy; notesFor listmre
  • 0176c4b5mission brief 007mre
  • 06cfa870notes mission 006: re-vendor hybriel 0f396652 (static), reportmre
  • 6c07bd91mission brief w6mre
  • cf64d3d5notes mission 005: stopped at a Hybriel gap (for list repainted during hl:web boot throws, rowKidless); report, LOG, STATUSmre
  • a43b5d2dmission brief 005mre
  • 04a625edmission 004: re-vendor hybriel a832f73b (native bin + plugins as tickets), compose HL_CACHE_DIR volume + tmpfs /tmp, deploy URL wait 120 s, tests wait 120 s per start; gate 57/0 old and new; real-data same output. Not deployed.mre
  • aefb9d44mission brief w4mre
  • 202098b3docker-compose: init: true (hybriel#156, PID 1 ignored SIGTERM, every restart waited 10 s)mre
  • 304f6a9dnotes mission 003 (#5): report, STATUS, LOGmre
  • 9acc27f8notes mission 003 (#5): GitHub pipe tables in md-editor (parse, render, type, write) + gate checksmre
  • 2149e902notes mission 002 (4/4): code order — README file map + same-output test, STATUS, LOG, report; tests/letcount.py, tests/realdata-baseline.mjs, tests/realdata-compare.pymre
  • 47db4fc1notes mission 002 (3/4): code order — let only where reassigned (58 dropped; 34 left: 19 reassigned, 15 loop-bound); gates 50/0 + 18/0, live-data run = step 2mre
  • 3f8cb383notes mission 002 (2/4): code order — topics, map, thin wrappers: lib/util.hl, lib/notes(-helpers).hl, lib/users.hl (+userOfLoginCode, tagOf), lib/api(-helpers).hl; project.hl = map; login routes take &req/&sessions (failed-login reason now kept); dead notes#1 functions removed; gates 50/0 + 18/0mre
  • 8cda5412notes mission 002 (1/4): code order — files moved: lib/notes.hl, lib/users.hl, lib/jsoncheck.hl, components/styles.hl (imports only); gates 50/0 + 18/0, live-data run identicalmre
  • 47dad68bnotes: Hybriel master 06617221 (plugin allocators 3a781359 + 413f60e4, mpackdb 2cb7ae5e, http1 773de63e); gates 50/0 + 18/0mre