gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit7869f57b7869f57bident#7: delivery — the daily mail at 17:00 in the user's zone (digests table, once per local day, test clock IDENT_TEST_CLOCK), urgent mailed at once, push hookmre7869f57b/deploy.sh

9.1 KB

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

Branches

Latest commits

  • 7869f57bident#7: delivery — the daily mail at 17:00 in the user's zone (digests table, once per local day, test clock IDENT_TEST_CLOCK), urgent mailed at once, push hookmre
  • 371ef1b5ident#7: lib/mail.hl sendNotificationMail (sink: <sink>.mail, one JSON line per mail)mre
  • 39749e0bident#7: time zones through hl:time — server checks isZone, an account without a known zone reads as UTC, allAccountsmre
  • 7a54fe72mission brief 011mre
  • a6a874cdident mission 010: vendor hybriel 0f396652 (plugins + compose cache volume/tmpfs + deploy URL wait + report)mre
  • 4e7cf78emission brief 010mre
  • e8b2b0f7docker-compose: init: true (hybriel#156, PID 1 ignored SIGTERM, every restart waited 10 s)mre
  • 5fdbb6b2ident mission 009: report — scratch folder notemre
  • 74235815ident mission 009 (4/4): docs (README files map + same-output test, STATUS, LOG), report, tests/letcount.py + realdata-baseline/comparemre
  • fe183516ident mission 009 (3/4): let only where reassigned — 293 never-reassigned lets are plain declarations; kept: reassigned, loop bodies, names of a file member, a name declared twice in one function; same outputmre
  • d2e7f91bident mission 009 (2/4): one lib file per topic (login, accounts, identities, apps, invites, selector, notify + helpers, util), function routes as thin wrappers in lib/api.hl, project.hl = the map; same outputmre
  • 91017164ident mission 009 (1/4): file moves — the root .hl files into lib/ (api.hl → lib/api-helpers.hl), styles.hl → components/styles.hl; imports adjusted, no other changemre
  • f8bdcbc2ident: Hybriel master 06617221 (plugin allocators 3a781359 + 413f60e4, mpackdb 2cb7ae5e, http1 773de63e); all gates greenmre
  • ff78726cident: Hybriel master 190aa11d (fc838894 GC correctness, #127, #126 closure scopes); gates all greenmre
  • a3a7d21aident: Hybriel master 8efba065 (#126 GC by bytes, #48 lambda params copy); session-writing lambdas take &sessionmre
  • 98226b41antcolony#40: mission references point to the moved missionsmre
  • ff805b9aantcolony#40: history (LOG.md), worker briefs (missions/) and reports moved here from antcolony, numbered per project; old numbers in antcolony docs/mission-map.mdmre
  • 51a7bcdfident: Hybriel master 73267707 (#122); /code uses the new page() signature; pending address passed as parameter; once-checksmre
  • 836f644fident#24: installable app (manifest, service worker, data-free offline /start), own iconmre
  • 8bebbbf2deploy.sh: back up live storage/.sessions/.env before every deploy (newest 5 kept)mre