gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitbf1980f2bf1980f2Merge branch 'v56-day56' into mainIdentbf1980f2/deploy.sh

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

Branches

Latest commits

  • bf1980f2Merge branch 'v56-day56' into mainIdent
  • 62ae0adf|||PR ident: vendor Hybriel day56mre
  • 0d18abc4Merge branch 'f13-avatar' into mainIdent
  • 2a99730a|||PR ident#37 avatar in hlmre
  • fa4ed25bMerge branch 'f11-upsert' into mainIdent
  • 8f3d9bc9|||PR ident#42 keyed upsert and removemre
  • 675493b1ident#42: editing an identity upserts the one edited row (face editIdentity answers { identity, row }); a create or delete keeps the whole list because the rows' position-dependent parts (default, Delete, Identity n) changemre
  • 45e55303ident#42: an app write patches one row: upsert on create, edit and new secret, one-argument remove on delete; the faces answer the changed app (or the deleted id), not the whole listmre
  • 991641d4Merge branch 'f10-access' into mainIdent
  • e56d8273|||PR ident#34 part 4: access and inbox fixed classesmre
  • df7696dbident#34: tests: access ticks selector quoting fixmre
  • 7215903eident#34: access: the unstyled classes all, app, tick, apptick, plus are dropped; the gate finds the app ticks by input[name=app]mre
  • 69b0a0ceident#34: access: the pick buttons use Style.pickCard instead of class pickmre
  • dc702419Merge branch 'f9-choose' into mainIdent
  • 8caec771|||PR ident#34 restore .choose hookmre
  • 0dbff960Merge branch 'f8-shared' into mainIdent
  • d69665f6|||PR ident#34 part 3: shared look classes (wide, avatar, chip/used, conn, choose, who, inboxlink) moved to statics or droppedmre
  • 3a64dc48ident#34: chipLook: radius as the same var() string (a bare token name in a static does not compile); selector gate selectormre
  • eb039073ident#34: tests: signin and migration find the choice without the classmre
  • 4b94a594ident#34: tests: fix selector of the identity choicemre