gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit734ac0a9734ac0a9ident mission 013 step 1: re-vendor Hybriel ce6f2e0a (plugins whole; bin/ is git-ignored)mre734ac0a9/deploy.sh

9.2 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. "node tests/delivery.mjs"
  33. "node theme/check-theme.mjs ."
  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.* /testapp/
  39. '*.log' '*.pid' node_modules/
  40. )
  41. # ---------------------------------------------------------------------------------------------
  42. DRY=0
  43. SKIP=0
  44. TARGET=${DEPLOY_TARGET:-$DEFAULT_TARGET}
  45. URL=${DEPLOY_URL:-$DEFAULT_URL}
  46. # how long the URL may take after the restart (seconds); a cold start compiles first (hybriel#148)
  47. URL_WAIT=${DEPLOY_URL_WAIT:-120}
  48. # Loreana's ssh config may not apply to Byrodin: -F /dev/null (ident STATUS)
  49. SSH=${DEPLOY_SSH:-ssh -F /dev/null -o BatchMode=yes -o ConnectTimeout=15}
  50. usage() { sed -n '2,15p' "$0" | sed 's/^# \{0,1\}//'; }
  51. while [ $# -gt 0 ]; do
  52. case "$1" in
  53. --dry-run) DRY=1 ;;
  54. --skip-tests) SKIP=1 ;;
  55. --target) TARGET=${2:?--target needs a value}; shift ;;
  56. --target=*) TARGET=${1#--target=} ;;
  57. --url) URL=${2:?--url needs a value}; shift ;;
  58. --url=*) URL=${1#--url=} ;;
  59. -h|--help) usage; exit 0 ;;
  60. *) echo "deploy: unknown argument: $1" >&2; usage >&2; exit 2 ;;
  61. esac
  62. shift
  63. done
  64. HERE=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
  65. cd "$HERE"
  66. step() { printf '\n==> %s\n' "$*"; }
  67. run() { printf ' $ %s\n' "$*" >&2; "$@"; }
  68. die() { printf '\ndeploy: REFUSED — %s\n' "$*" >&2; exit 1; }
  69. # a target `host:dir` is remote, a plain path is local
  70. if [[ "$TARGET" == *:* ]]; then
  71. REMOTE_HOST=${TARGET%%:*}
  72. REMOTE_DIR=${TARGET#*:}
  73. else
  74. REMOTE_HOST=
  75. REMOTE_DIR=$TARGET
  76. fi
  77. step "[0/5] $APP → $TARGET (dry run: $DRY, skip tests: $SKIP)"
  78. echo " from $HERE"
  79. echo " url $URL"
  80. [ -x bin/hybriel ] || die "bin/hybriel is missing here"
  81. [ -f docker-compose.yml ] || die "docker-compose.yml is missing here"
  82. [ -f project.hl ] || die "project.hl is missing here"
  83. command -v rsync >/dev/null || die "rsync is not installed"
  84. # ---- 1. the gates ---------------------------------------------------------------------------
  85. if [ "$SKIP" = 1 ]; then
  86. step "[1/5] GATES SKIPPED"
  87. printf ' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n'
  88. printf ' !! --skip-tests: NOTHING WAS TESTED. You deploy untested code. !!\n'
  89. printf ' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n'
  90. else
  91. step "[1/5] gates (${#GATES[@]})"
  92. # headless Chromes that exist BEFORE the gates are not ours (other sessions' test runs, e.g. Anton's)
  93. chromes() { ps -eo pid=,ppid=,args= | awk '/[h]l-browser-tier/ && /remote-debugging-port/ && !/--type=/ {print $1, $2}'; }
  94. before=" $(chromes | awk '{print $1}' | tr '\n' ' ') "
  95. for g in "${GATES[@]}"; do
  96. printf ' $ %s\n' "$g"
  97. out=$(mktemp)
  98. if ! $g > "$out" 2>&1; then
  99. grep -E '^FAIL|passed,' "$out" | sed 's/^/ /' || true
  100. echo " (full output: $out)"
  101. die "gate failed: $g"
  102. fi
  103. grep -E 'passed,|^check-theme:' "$out" | tail -1 | sed 's/^/ /' || true
  104. rm -f "$out"
  105. done
  106. # a Chrome our gates LEFT: new since the gates began AND no longer owned by a running node test
  107. # (a Chrome whose parent is a live node process belongs to another session's test run right now)
  108. leaked=""
  109. while read -r pid ppid; do
  110. [ -z "$pid" ] && continue
  111. case "$before" in *" $pid "*) continue ;; esac
  112. ps -o args= -p "$ppid" 2>/dev/null | grep -q '^node\|/node ' && continue
  113. leaked="$leaked $pid"
  114. done < <(chromes)
  115. if [ -n "$leaked" ]; then
  116. die "a gate left a headless Chrome (pids:$leaked; ps -eo pid,args | grep hl-browser-tier)"
  117. fi
  118. fi
  119. # ---- 2. pre-deploy backup of the LIVE data (before anything is sent) ------------------------
  120. BACKUP_ROOT=${DEPLOY_BACKUP_ROOT:-/media/SLOW1TB2/deploy-backups}
  121. BACKUP_DIR="$BACKUP_ROOT/$APP"
  122. BACKUP_FILE="$BACKUP_DIR/$APP-$(date +%Y%m%d-%H%M).tgz"
  123. if [ "$DRY" = 1 ]; then
  124. step "[2/5] DRY RUN: backup skipped"
  125. else
  126. step "[2/5] backup live data ($APP) → $BACKUP_FILE"
  127. mkdir -p "$BACKUP_DIR"
  128. if [ -n "$REMOTE_HOST" ]; then
  129. present=$($SSH "$REMOTE_HOST" "cd '$REMOTE_DIR' 2>/dev/null && for p in storage .sessions .env; do [ -e \"\$p\" ] && echo \"\$p\"; done; true")
  130. else
  131. present=$(cd "$REMOTE_DIR" 2>/dev/null && for p in storage .sessions .env; do [ -e "$p" ] && echo "$p"; done; true)
  132. fi
  133. present=$(printf '%s' "$present" | tr '\n' ' ' | sed 's/ *$//')
  134. if [ -z "$present" ]; then
  135. echo " nothing to back up yet (no storage/.sessions/.env on the target)"
  136. else
  137. echo " taring: $present"
  138. if [ -n "$REMOTE_HOST" ]; then
  139. run $SSH "$REMOTE_HOST" "tar czf - -C '$REMOTE_DIR' $present" > "$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\.|testapp/)|\.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. 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)"
  195. code=000
  196. start=$SECONDS
  197. i=0
  198. while :; do
  199. i=$((i + 1))
  200. code=$(curl -s -o /dev/null -w '%{http_code}' --max-time 10 "$URL" || true)
  201. echo " try $i ($((SECONDS - start)) s): $code"
  202. [ "$code" = 200 ] && break
  203. [ $((SECONDS - start)) -ge "$URL_WAIT" ] && break
  204. sleep 2
  205. done
  206. if [ "$code" != 200 ]; then
  207. if [ -n "$REMOTE_HOST" ]; then die "$URL answered $code, not 200 — look: $SSH $REMOTE_HOST docker logs --tail 50 $CONTAINER"; fi
  208. die "$URL answered $code, not 200 — look: docker logs --tail 50 $CONTAINER"
  209. fi
  210. step "deployed $APP → $TARGET, $URL answers 200"

Branches

Latest commits

  • 734ac0a9ident mission 013 step 1: re-vendor Hybriel ce6f2e0a (plugins whole; bin/ is git-ignored)mre
  • b37875ccident mission 012 part 3: real-Chrome fixes (brand 44 px, identity form width), e2e walk, report, logmre
  • f989c2ebident mission 012: stopgap reload after in-place sign-in/sign-out (until hybriel#193), gates wait for cold start, welcome end reloadsmre
  • 443291d4ident mission 012: /start on its own shell (no account), browser gate 152/0, reportmre
  • 021dd0fcident mission 012: browser gate waits cold start (IDENT_COLD_MS), header selectors, e2e checks; shell events from a page do not reach it (probe), current not marked, account-bar link 44 pxmre
  • 97f77b07ident mission 012 (WIP): onto the layouts theme dbe3de7, shell on AppHeader/MainMenu/UserMenu/Sidebar + ContentFirst, styles cut, check-theme gatemre
  • 8e4f435bpaths: projects moved into /media/STORAGE/projects/WorldAPI/mre
  • fa288f63ident mission 011: report wording (Intl zone names)mre
  • 961fae01ident mission 011: report, LOG, STATUSmre
  • 65348039ident#7: delivery gate — console check split at the gate's own server restart (49 checks)mre
  • dfa7b783ident#7: README (Delivery, env, gate, files, data), deploy.sh runs tests/delivery.mjsmre
  • ab6331f4ident#7: tests/delivery.mjs — fake clock gate: Vienna/New York/Tokyo/UTC 17:00, urgent, mail off, restart, DST days, zone changemre
  • 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