gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit77829b7377829b73gitoria#43: signed events to connected apps (deliveries table, sender timer, pull_request events, ping)mre77829b73/deploy.sh

11.6 KB

  1. #!/usr/bin/env bash
  2. # deploy.sh — gitoria.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)
  11. # (backup: tars storage/.sessions/.env that exist on the target to Loreana's
  12. # /media/SLOW1TB2/deploy-backups/<app>/, keeps newest 5; --target DIR backs up DIR instead)
  13. #
  14. # THE FIRST DEPLOY is done by the architect on Byrodin (folder, data, nginx vhost, cert,
  15. # DNS). This script only updates the CODE: storage/, .sessions/, .env, .scratch/, server.*,
  16. # testapp/ and logs are never sent, so live data on Byrodin is never touched.
  17. # No --delete: a file removed here stays on Byrodin (harmless — nothing imports it).
  18. set -euo pipefail
  19. # ---- the app ------------------------------------------------------------------------------
  20. APP=gitoria.worldapi.org
  21. CONTAINER=gitoria.worldapi.org
  22. [email protected]:/CONTAINERS/projects/gitoria.worldapi.org
  23. DEFAULT_URL=https://gitoria.worldapi.org/
  24. GATES=(
  25. "node tests/signin-return.mjs" # ident#31: sign in through the selector panel, back on the same page
  26. "node theme/check-theme.mjs ."
  27. "node tests/browser.mjs"
  28. "node tests/security.mjs"
  29. "node tests/private.mjs" # go34: private repos, organizations (158 checks)
  30. "node tests/ident-access-gitoria.mjs" # ident#27: an idt_ access key as the git password
  31. "node tests/apps.mjs" # gitoria#41: apps connected to a repo (URL allow-list, connect flow, add/remove/rotate)
  32. "node tests/pull-numbers.mjs" # gitoria#40: PR numbers, refs/pull/N/head, the changes of syncPulls, wasHeadOf
  33. "node tests/pull-ssh.mjs" # gitoria#40: the same over the sshd door (docker)
  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. # Order (Anton #203): directories, then every file except *.mpack (indexes, idxstate, sessions, .env), then the *.mpack
  140. # data last — a hot copy then never holds index offsets past the data it copied. tar exit 1 = "file changed as we
  141. # read it" / a file gone between find and tar (--ignore-failed-read) — a warning; the archive is checked with gzip -t.
  142. 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" \
  143. || { rm -f "$BACKUP_FILE"; die "backup failed (tar/ssh error) — refusing to deploy"; }
  144. else
  145. run tar czf "$BACKUP_FILE" -C "$REMOTE_DIR" $present \
  146. || { rm -f "$BACKUP_FILE"; die "backup failed (tar error) — refusing to deploy"; }
  147. fi
  148. [ -s "$BACKUP_FILE" ] || { rm -f "$BACKUP_FILE"; die "backup is empty — refusing to deploy"; }
  149. echo " $(du -h "$BACKUP_FILE" | cut -f1) $BACKUP_FILE"
  150. ls -1t "$BACKUP_DIR/$APP"-*.tgz 2>/dev/null | tail -n +6 | xargs -r rm -f --
  151. echo " keeping $(ls -1 "$BACKUP_DIR/$APP"-*.tgz 2>/dev/null | wc -l) archive(s) of $APP"
  152. fi
  153. fi
  154. # ---- 3. rsync the code ----------------------------------------------------------------------
  155. RSYNC=(rsync -az --no-owner --no-group --itemize-changes)
  156. for e in "${EXCLUDES[@]}"; do RSYNC+=("--exclude=$e"); done
  157. if [ -n "$REMOTE_HOST" ]; then
  158. RSYNC+=(-e "$SSH")
  159. else
  160. mkdir -p "$REMOTE_DIR"
  161. fi
  162. # the preview is ALWAYS made first (rsync -n): nothing that must stay on Byrodin may be in it
  163. step "[3/5] rsync preview (what would be sent)"
  164. preview=$(mktemp)
  165. run "${RSYNC[@]}" -n ./ "$TARGET/" > "$preview"
  166. sed 's/^/ /' "$preview"
  167. echo " ($(grep -c . "$preview" || true) lines)"
  168. if awk '{print $2}' "$preview" | grep -E '^(storage/|\.sessions/|\.env|\.scratch/|server\.|testapp/)|\.log$|\.pid$' ; then
  169. die "the preview holds a path that must never be sent (see above)"
  170. fi
  171. rm -f "$preview"
  172. if [ "$DRY" = 1 ]; then
  173. step "[3/5] DRY RUN: nothing sent"
  174. else
  175. step "[3/5] rsync"
  176. run "${RSYNC[@]}" ./ "$TARGET/" | sed 's/^/ /'
  177. fi
  178. # ---- 4. restart the container, without a gap ------------------------------------------------
  179. # Apollo 2026-10-25: the main container is never restarted before the standby (port 45014, compose profile "standby") answers 200.
  180. # The shared helper (worldapi-conductor/standby-swap.sh) is piped to Byrodin: it starts the standby with the new code, waits for 200 on
  181. # /api/repos, restarts the main container, waits for 200 on 45004 and stops the standby. nginx serves from the standby meanwhile. If the
  182. # standby never comes up, nothing live was touched and the deploy stops here.
  183. SWAP=${DEPLOY_STANDBY_SWAP:-/media/STORAGE/projects/WorldAPI/worldapi-conductor/standby-swap.sh}
  184. SWAP_ARGS="gitoria gitoria gitoria-standby /api/repos 45004"
  185. [ -f "$SWAP" ] || die "the hand-over helper is missing: $SWAP"
  186. if [ "$DRY" = 1 ]; then
  187. step "[4/5] DRY RUN: would hand over $CONTAINER through its standby (no gap)"
  188. echo " would run: $SWAP_ARGS with $SWAP on $TARGET"
  189. DRY_RUN=1 APP_DIR="$REMOTE_DIR" bash "$SWAP" $SWAP_ARGS 2>&1 | sed 's/^/ /' || true
  190. else
  191. step "[4/5] hand over $CONTAINER through its standby (no gap)"
  192. if [ -n "$REMOTE_HOST" ]; then
  193. printf ' $ %s %s "APP_DIR=%s bash -s -- %s" < %s\n' "$SSH" "$REMOTE_HOST" "$REMOTE_DIR" "$SWAP_ARGS" "$SWAP" >&2
  194. $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)"
  195. else
  196. APP_DIR="$REMOTE_DIR" bash "$SWAP" $SWAP_ARGS || die "the hand-over failed — see the log above"
  197. fi
  198. fi
  199. # the helper only restarts the app container: bring the other service (the git-over-SSH sshd) up to date too, without restarting it when nothing changed
  200. if [ "$DRY" != 1 ]; then
  201. # the sshd image COPIES gitoria-shell / gitoria-keys / sshd_config: rebuild it first (cached when nothing changed), then `up -d` recreates the container only if its image changed (GO34c)
  202. CHANGED="cd '$REMOTE_DIR' && docker compose build gitoria-sshd && docker compose up -d"
  203. if [ -n "$REMOTE_HOST" ]; then run $SSH "$REMOTE_HOST" "$CHANGED"; else run bash -c "$CHANGED"; fi
  204. fi
  205. # ---- 5. the URL answers 200 -----------------------------------------------------------------
  206. if [ "$DRY" = 1 ]; then
  207. step "[5/5] DRY RUN: would check $URL answers 200"
  208. step "dry run done — nothing was sent, nothing restarted"
  209. exit 0
  210. fi
  211. 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)"
  212. code=000
  213. start=$SECONDS
  214. i=0
  215. while :; do
  216. i=$((i + 1))
  217. code=$(curl -s -o /dev/null -w '%{http_code}' --max-time 10 "$URL" || true)
  218. echo " try $i ($((SECONDS - start)) s): $code"
  219. [ "$code" = 200 ] && break
  220. [ $((SECONDS - start)) -ge "$URL_WAIT" ] && break
  221. sleep 2
  222. done
  223. if [ "$code" != 200 ]; then
  224. if [ -n "$REMOTE_HOST" ]; then die "$URL answered $code, not 200 — look: $SSH $REMOTE_HOST docker logs --tail 50 $CONTAINER"; fi
  225. die "$URL answered $code, not 200 — look: docker logs --tail 50 $CONTAINER"
  226. fi
  227. step "deployed $APP → $TARGET, $URL answers 200"

Branches

Latest commits

  • 77829b73gitoria#43: signed events to connected apps (deliveries table, sender timer, pull_request events, ping)mre
  • 1997e77dgitoria#40: stable PR numbers, refs/pull/N/head, ssh pushes reach the app (merge w40-pr-number)mre
  • 9364fb16gitoria#40: ssh pushes call /__git/pushed before and after (ensurePulls, notifyPush, syncPulls); gate over the sshd imagemre
  • fdb9ed28gitoria#41: repo owners can connect an app (merge w41-apps-connect)mre
  • 2ef23df0gitoria#40: the browser gate reads the title without the #Nmre
  • 9af07ecegitoria#41: connect an app to a repo (apps table, connect flow, manual add, allow-list, settings)mre
  • b9137ff0gitoria#40: pull requests are stored with a number, refs/pull/N/head, syncPulls and pullObject; /pulls shows #Nmre
  • 06d622daown image tag gitoria-runtime:debian12 for gitoria and standby (shared tag lost git)mre
  • 20caa33agitoria#37: media queries in the language's @media block (minWidth = '60rem') in list, codebrowser and repomre
  • c4175e6dgitoria: a startup error prints its message before exit 1 (hybriel#247)mre
  • c50d17efKeep function-route not-found style in shared stylesheetmre
  • e42d78d3Preserve ticket state CSS and document function-route style gapmre
  • c6b7154eWIP gitoria#26: move styles into nested component blocksmre
  • 77a2c31cgitoria#31: plain declarations instead of needless let in lib/ and tools/ (103 let removed, 137 kept because reassigned); no other changemre
  • 4b98ab62gitoria: an ident access key (idt_) works as the git password; read key clones only, bad key 401, ident down 503 (ident#27); gate tests/ident-access-gitoria.mjs in deploy.shmre
  • 1185f686gitoria#36: a failed return from tickets shows a note on the settings page and leaves one log line per outcome; no code is exchangedmre
  • 5d2c385agitoria: the sign-in callback keeps the session it finds and signs it in (same cookie, nothing signed out; ident#28); security gate 71+5 failing → 76 passed; deploy.sh runs tests/private.mjs toomre
  • 241ef3f9deploy.sh: rebuild the sshd image before up -d (GO34c)mre
  • 821cdbb1gitoria#34b: styles, create-repo tool, docs, push/ssh gates sign in through the selector, shots scriptmre
  • a81abdc9gitoria#34: org page via function route, member-reachable orgs, selects, reportmre