gitoria

notes

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit4dd46dad4dd46dadsync-components: copy icons/icons.hl + README (F4); testsmre4dd46dad/deploy.sh

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

Branches

Latest commits

  • 4dd46dadsync-components: copy icons/icons.hl + README (F4); testsmre
  • c92e4a0ftests/e2e-outside.mjs: real-Chrome E2E, a note pushed from another device while open, then typingmre
  • 0516b4ccmd-editor.hl: verbatim copy of components f3-mdvalue (an older text handed back is kept)mre
  • 38cc11ecmd-editor.hl: verbatim copy of components f3-mdvalue (an outside value change is shown by the editor)mre
  • 3e7e79f5reports/H1-hydrate.mdmre
  • d98775e8tests/browser.mjs: wait for hydration before the clicks after a goto('/') (tables new note, phone sidebar)mre
  • 1f3198b6tests/cdp.mjs: HL_CPU_THROTTLE=<rate> optional CPU throttling for gate runsmre
  • 39cf9fdfre-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
  • db07aa84tests: short-id-switch ports from env (NOTES_SWITCH_IDENT_PORT/_PORT), defaults unchangedmre
  • ef9f9feere-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
  • 5485467cdocker-compose: nofile 65536 (Hybriel int 6+ boot opened >1024 files in the tracker)mre
  • 86ea4a85deploy: backup copies index files first and *.mpack data last (hot-copy skew, hybriel #203)mre
  • edeab20fnotes mission 015: mission, report, LOG, STATUSmre
  • 542a0383notes mission 015: e2e-chrome E2E_HOST (own cookie jar in the shared Chrome)mre
  • 8b30f510notes mission 015: main-menu from layouts 0bcc629, current = true kept (route marks only /), server-HTML marker checksmre
  • 84b574c4notes mission 015: re-vendor plugins from hybriel f9614007 (incl. web, #117)mre
  • 447da53dnotes mission 014: report — remaining gates and lisbeth E2E proofmre
  • fd9f92c3notes mission 014: report (partial), LOG, STATUSmre
  • b2501360notes mission 014: open() sets asking=false before flush(0) (hybriel#196)mre
  • 9414ce59notes mission 014: merge theme 76f99f6, components 3794c59, strict single-press openAsk (fails on old open())mre