gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Branchf2-api-bugs26117255|||PR apps API: review comments on files with spaces in their namemref2-api-bugs/deploy.sh

16.2 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/create-api.mjs" # gitoria#39: user-token repository creation
  26. "node tests/signin-return.mjs" # ident#31: sign in through the selector panel, back on the same page
  27. "node theme/check-theme.mjs ."
  28. "node tests/browser.mjs"
  29. "node tests/security.mjs"
  30. "node tests/private.mjs" # go34: private repos, organizations (158 checks)
  31. "node tests/ident-access-gitoria.mjs" # ident#27: an idt_ access key as the git password
  32. "node tests/apps.mjs" # gitoria#41: apps connected to a repo (URL allow-list, connect flow, add/remove/rotate)
  33. "node tests/pull-numbers.mjs" # gitoria#40: PR numbers, refs/pull/N/head, the changes of syncPulls, wasHeadOf
  34. "node tests/pull-ssh.mjs" # gitoria#40: the same over the sshd door (docker)
  35. "node tests/app-token.mjs" # gitoria#42: an app trades its key for a one-hour token; git read with it, no push
  36. "node tests/reviews.mjs" # gitoria#44: reviews with inline threads, replies, resolve, conversation (409/422/404, earlier head, paging, seq)
  37. "node tests/events.mjs" # gitoria#43: signed events to apps (HMAC, retries, Merge button, ping, delivery log, two processes, retention)
  38. "node tests/pr-page.mjs" # gitoria#45: the PR page (comments, threads, Review again, who hears of it, escaping, read-only for others)
  39. "node tests/status.mjs" # gitoria#46: statuses an app sets on a commit, required before merge, badges on /pulls
  40. "node tests/limits.mjs" # gitoria#47: status read route, pull.statuses/required, tickets list, 413/400, rate limits (429 + Retry-After)
  41. "node tests/status-keyed.mjs" # gitoria#46 v1.4: statuses keyed by app + context; an old-shape row (the old code on the same storage) is read and replaced
  42. "node tests/member-merge.mjs" # gitoria#48: a member merges a PR with a gtr_ token once the required checks pass (API + button, apps never)
  43. "node tests/protect-main.mjs" # gitoria#50: refuse direct pushes to the main branch (owner setting; https + ssh; branches, tags, merges stay)
  44. )
  45. # NEVER SENT (rsync patterns; a leading / anchors at the app folder)
  46. EXCLUDES=(
  47. /.git/ /.gitignore
  48. /storage/ /.sessions/ /.env /.env.* /.scratch/ /server.* /testapp/
  49. '*.log' '*.pid' node_modules/
  50. )
  51. # ---------------------------------------------------------------------------------------------
  52. DRY=0
  53. SKIP=0
  54. TARGET=${DEPLOY_TARGET:-$DEFAULT_TARGET}
  55. URL=${DEPLOY_URL:-$DEFAULT_URL}
  56. # how long the URL may take after the restart (seconds); a cold start compiles first (hybriel#148)
  57. URL_WAIT=${DEPLOY_URL_WAIT:-120}
  58. # Loreana's ssh config may not apply to Byrodin: -F /dev/null (ident STATUS)
  59. SSH=${DEPLOY_SSH:-ssh -F /dev/null -o BatchMode=yes -o ConnectTimeout=15}
  60. usage() { sed -n '2,15p' "$0" | sed 's/^# \{0,1\}//'; }
  61. while [ $# -gt 0 ]; do
  62. case "$1" in
  63. --dry-run) DRY=1 ;;
  64. --skip-tests) SKIP=1 ;;
  65. --target) TARGET=${2:?--target needs a value}; shift ;;
  66. --target=*) TARGET=${1#--target=} ;;
  67. --url) URL=${2:?--url needs a value}; shift ;;
  68. --url=*) URL=${1#--url=} ;;
  69. -h|--help) usage; exit 0 ;;
  70. *) echo "deploy: unknown argument: $1" >&2; usage >&2; exit 2 ;;
  71. esac
  72. shift
  73. done
  74. HERE=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
  75. cd "$HERE"
  76. step() { printf '\n==> %s\n' "$*"; }
  77. run() { printf ' $ %s\n' "$*" >&2; "$@"; }
  78. die() { printf '\ndeploy: REFUSED — %s\n' "$*" >&2; exit 1; }
  79. # a target `host:dir` is remote, a plain path is local
  80. if [[ "$TARGET" == *:* ]]; then
  81. REMOTE_HOST=${TARGET%%:*}
  82. REMOTE_DIR=${TARGET#*:}
  83. else
  84. REMOTE_HOST=
  85. REMOTE_DIR=$TARGET
  86. fi
  87. step "[0/5] $APP → $TARGET (dry run: $DRY, skip tests: $SKIP)"
  88. echo " from $HERE"
  89. echo " url $URL"
  90. [ -x bin/hybriel ] || die "bin/hybriel is missing here"
  91. [ -f docker-compose.yml ] || die "docker-compose.yml is missing here"
  92. [ -f project.hl ] || die "project.hl is missing here"
  93. command -v rsync >/dev/null || die "rsync is not installed"
  94. # ---- 1. the gates ---------------------------------------------------------------------------
  95. if [ "$SKIP" = 1 ]; then
  96. step "[1/5] GATES SKIPPED"
  97. printf ' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n'
  98. printf ' !! --skip-tests: NOTHING WAS TESTED. You deploy untested code. !!\n'
  99. printf ' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n'
  100. else
  101. step "[1/5] gates (${#GATES[@]})"
  102. # headless Chromes that exist BEFORE the gates are not ours (other sessions' test runs, e.g. Anton's)
  103. chromes() { ps -eo pid=,ppid=,args= | awk '/[h]l-browser-tier/ && /remote-debugging-port/ && !/--type=/ {print $1, $2}'; }
  104. before=" $(chromes | awk '{print $1}' | tr '\n' ' ') "
  105. for g in "${GATES[@]}"; do
  106. printf ' $ %s\n' "$g"
  107. out=$(mktemp)
  108. if ! $g > "$out" 2>&1; then
  109. grep -E '^FAIL|passed,' "$out" | sed 's/^/ /' || true
  110. echo " (full output: $out)"
  111. die "gate failed: $g"
  112. fi
  113. grep -E 'passed,|^check-theme:' "$out" | tail -1 | sed 's/^/ /' || true
  114. rm -f "$out"
  115. done
  116. # a Chrome our gates LEFT: new since the gates began AND no longer owned by a running node test
  117. # (a Chrome whose parent is a live node process belongs to another session's test run right now)
  118. leaked=""
  119. while read -r pid ppid; do
  120. [ -z "$pid" ] && continue
  121. case "$before" in *" $pid "*) continue ;; esac
  122. ps -o args= -p "$ppid" 2>/dev/null | grep -q '^node\|/node ' && continue
  123. leaked="$leaked $pid"
  124. done < <(chromes)
  125. if [ -n "$leaked" ]; then
  126. die "a gate left a headless Chrome (pids:$leaked; ps -eo pid,args | grep hl-browser-tier)"
  127. fi
  128. fi
  129. # ---- 2. pre-deploy backup of the LIVE data (before anything is sent) ------------------------
  130. BACKUP_ROOT=${DEPLOY_BACKUP_ROOT:-/media/SLOW1TB2/deploy-backups}
  131. BACKUP_DIR="$BACKUP_ROOT/$APP"
  132. BACKUP_FILE="$BACKUP_DIR/$APP-$(date +%Y%m%d-%H%M).tgz"
  133. if [ "$DRY" = 1 ]; then
  134. step "[2/5] DRY RUN: backup skipped"
  135. else
  136. step "[2/5] backup live data ($APP) → $BACKUP_FILE"
  137. mkdir -p "$BACKUP_DIR"
  138. if [ -n "$REMOTE_HOST" ]; then
  139. present=$($SSH "$REMOTE_HOST" "cd '$REMOTE_DIR' 2>/dev/null && for p in storage .sessions .env; do [ -e \"\$p\" ] && echo \"\$p\"; done; true")
  140. else
  141. present=$(cd "$REMOTE_DIR" 2>/dev/null && for p in storage .sessions .env; do [ -e "$p" ] && echo "$p"; done; true)
  142. fi
  143. present=$(printf '%s' "$present" | tr '\n' ' ' | sed 's/ *$//')
  144. if [ -z "$present" ]; then
  145. echo " nothing to back up yet (no storage/.sessions/.env on the target)"
  146. else
  147. echo " taring: $present"
  148. if [ -n "$REMOTE_HOST" ]; then
  149. # Order (Anton #203): directories, then every file except *.mpack (indexes, idxstate, sessions, .env), then the *.mpack
  150. # data last — a hot copy then never holds index offsets past the data it copied. tar exit 1 = "file changed as we
  151. # read it" / a file gone between find and tar (--ignore-failed-read) — a warning; the archive is checked with gzip -t.
  152. 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" \
  153. || { rm -f "$BACKUP_FILE"; die "backup failed (tar/ssh error) — refusing to deploy"; }
  154. else
  155. run tar czf "$BACKUP_FILE" --hard-dereference -C "$REMOTE_DIR" $present \
  156. || { rm -f "$BACKUP_FILE"; die "backup failed (tar error) — refusing to deploy"; }
  157. fi
  158. [ -s "$BACKUP_FILE" ] || { rm -f "$BACKUP_FILE"; die "backup is empty — refusing to deploy"; }
  159. echo " $(du -h "$BACKUP_FILE" | cut -f1) $BACKUP_FILE"
  160. ls -1t "$BACKUP_DIR/$APP"-*.tgz 2>/dev/null | tail -n +6 | xargs -r rm -f --
  161. echo " keeping $(ls -1 "$BACKUP_DIR/$APP"-*.tgz 2>/dev/null | wc -l) archive(s) of $APP"
  162. fi
  163. fi
  164. # ---- 3. rsync the code ----------------------------------------------------------------------
  165. RSYNC=(rsync -az --no-owner --no-group --itemize-changes)
  166. for e in "${EXCLUDES[@]}"; do RSYNC+=("--exclude=$e"); done
  167. if [ -n "$REMOTE_HOST" ]; then
  168. RSYNC+=(-e "$SSH")
  169. else
  170. mkdir -p "$REMOTE_DIR"
  171. fi
  172. # the preview is ALWAYS made first (rsync -n): nothing that must stay on Byrodin may be in it
  173. step "[3/5] rsync preview (what would be sent)"
  174. preview=$(mktemp)
  175. run "${RSYNC[@]}" -n ./ "$TARGET/" > "$preview"
  176. sed 's/^/ /' "$preview"
  177. echo " ($(grep -c . "$preview" || true) lines)"
  178. if awk '{print $2}' "$preview" | grep -E '^(storage/|\.sessions/|\.env|\.scratch/|server\.|testapp/)|\.log$|\.pid$' ; then
  179. die "the preview holds a path that must never be sent (see above)"
  180. fi
  181. rm -f "$preview"
  182. if [ "$DRY" = 1 ]; then
  183. step "[3/5] DRY RUN: nothing sent"
  184. else
  185. step "[3/5] rsync"
  186. run "${RSYNC[@]}" ./ "$TARGET/" | sed 's/^/ /'
  187. fi
  188. # ---- 4. restart the container, without a gap ------------------------------------------------
  189. # Apollo 2026-10-25: the main container is never restarted before the standby (port 45014, compose profile "standby") answers 200.
  190. # The shared helper (worldapi-conductor/standby-swap.sh) is piped to Byrodin: it starts the standby with the new code, waits for 200 on
  191. # /api/repos, restarts the main container, waits for 200 on 45004 and stops the standby. nginx serves from the standby meanwhile. If the
  192. # standby never comes up, nothing live was touched and the deploy stops here.
  193. # Apollo 2026-11-02 mpackdb freeze: DEPLOY_STOP_START=1 replaces ONLY this step with stop-then-start. Two gitoria processes must never
  194. # have the same mpackdb files open at once (the standby swap runs the old and the new process side by side); so the standby is made sure
  195. # to be down, the main container is stopped, and it is started again only after the old process is really gone (container not running AND
  196. # its old pid no longer exists). Without the variable the standby swap below stays the default (deploy-init.sh relies on the string
  197. # standby-swap.sh in this file to skip a second restart).
  198. SWAP=${DEPLOY_STANDBY_SWAP:-/media/STORAGE/projects/WorldAPI/worldapi-conductor/standby-swap.sh}
  199. SWAP_ARGS="gitoria gitoria gitoria-standby /api/repos 45004"
  200. STOP_WAIT=${DEPLOY_STOP_WAIT:-60}
  201. # runs on the target (as one script, so a failure before the start leaves the container stopped and the deploy fails): $1 = app dir
  202. stop_start_script() {
  203. cat <<'EOS'
  204. set -u
  205. cd "$APP_DIR" || exit 1
  206. echo " standby: making sure gitoria-standby is not running"
  207. docker compose --profile standby stop gitoria-standby || true
  208. cid=$(docker compose ps -a -q gitoria)
  209. oldpid=0
  210. if [ -n "$cid" ]; then oldpid=$(docker inspect -f '{{.State.Pid}}' "$cid" 2>/dev/null || echo 0); fi
  211. echo " old container ${cid:-none} pid $oldpid"
  212. echo " docker compose stop gitoria"
  213. docker compose stop gitoria || { echo " stop failed - NOT starting" >&2; exit 1; }
  214. i=0
  215. while :; do
  216. st=$(docker inspect -f '{{.State.Running}} {{.State.Pid}}' "$cid" 2>/dev/null) || st="gone 0"
  217. running=${st%% *}; pid=${st##* }
  218. alive=no
  219. # the pid docker shows now (0 once stopped) and the pid it had before the stop: neither may still exist
  220. for q in "$pid" "$oldpid"; do
  221. if [ -n "$q" ] && [ "$q" != 0 ] && [ -d "/proc/$q" ]; then alive=yes; fi
  222. done
  223. echo " wait ${i}s: running=$running pid=$pid oldpid=$oldpid still-there=$alive"
  224. if [ "$running" != true ] && [ "$alive" = no ]; then break; fi
  225. i=$((i + 1))
  226. if [ "$i" -gt "$STOP_WAIT" ]; then echo " the old gitoria process is still there after ${STOP_WAIT}s - NOT starting" >&2; exit 1; fi
  227. sleep 1
  228. done
  229. echo " old process gone: docker compose up -d gitoria"
  230. docker compose up -d gitoria
  231. EOS
  232. }
  233. [ -f "$SWAP" ] || die "the hand-over helper is missing: $SWAP"
  234. if [ "${DEPLOY_STOP_START:-}" = 1 ]; then
  235. if [ "$DRY" = 1 ]; then
  236. step "[4/5] DRY RUN: would stop $CONTAINER, wait until its old process is gone, then start it (DEPLOY_STOP_START=1, mpackdb freeze: no standby swap)"
  237. echo " would run on $TARGET: docker compose --profile standby stop gitoria-standby; docker compose stop gitoria; wait up to ${STOP_WAIT}s until docker inspect -f '{{.State.Running}} {{.State.Pid}}' shows not running AND the pid is gone (otherwise fail, do NOT start); docker compose up -d gitoria"
  238. else
  239. step "[4/5] stop $CONTAINER, wait until its old process is gone, start it (DEPLOY_STOP_START=1, mpackdb freeze: no standby swap)"
  240. if [ -n "$REMOTE_HOST" ]; then
  241. printf ' $ %s %s "APP_DIR=%s STOP_WAIT=%s bash -s" (stop-then-start script)\n' "$SSH" "$REMOTE_HOST" "$REMOTE_DIR" "$STOP_WAIT" >&2
  242. stop_start_script | $SSH "$REMOTE_HOST" "APP_DIR='$REMOTE_DIR' STOP_WAIT='$STOP_WAIT' bash -s" || die "stop-then-start failed - the container may be stopped, look: $SSH $REMOTE_HOST docker ps -a"
  243. else
  244. stop_start_script | APP_DIR="$REMOTE_DIR" STOP_WAIT="$STOP_WAIT" bash -s || die "stop-then-start failed - the container may be stopped"
  245. fi
  246. fi
  247. elif [ "$DRY" = 1 ]; then
  248. step "[4/5] DRY RUN: would hand over $CONTAINER through its standby (no gap)"
  249. echo " would run: $SWAP_ARGS with $SWAP on $TARGET"
  250. DRY_RUN=1 APP_DIR="$REMOTE_DIR" bash "$SWAP" $SWAP_ARGS 2>&1 | sed 's/^/ /' || true
  251. else
  252. step "[4/5] hand over $CONTAINER through its standby (no gap)"
  253. if [ -n "$REMOTE_HOST" ]; then
  254. printf ' $ %s %s "APP_DIR=%s bash -s -- %s" < %s\n' "$SSH" "$REMOTE_HOST" "$REMOTE_DIR" "$SWAP_ARGS" "$SWAP" >&2
  255. $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)"
  256. else
  257. APP_DIR="$REMOTE_DIR" bash "$SWAP" $SWAP_ARGS || die "the hand-over failed — see the log above"
  258. fi
  259. fi
  260. # 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
  261. if [ "$DRY" != 1 ]; then
  262. # 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)
  263. CHANGED="cd '$REMOTE_DIR' && docker compose build gitoria-sshd && docker compose up -d"
  264. if [ -n "$REMOTE_HOST" ]; then run $SSH "$REMOTE_HOST" "$CHANGED"; else run bash -c "$CHANGED"; fi
  265. fi
  266. # ---- 5. the URL answers 200 -----------------------------------------------------------------
  267. if [ "$DRY" = 1 ]; then
  268. step "[5/5] DRY RUN: would check $URL answers 200"
  269. step "dry run done — nothing was sent, nothing restarted"
  270. exit 0
  271. fi
  272. 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)"
  273. code=000
  274. start=$SECONDS
  275. i=0
  276. while :; do
  277. i=$((i + 1))
  278. code=$(curl -s -o /dev/null -w '%{http_code}' --max-time 10 "$URL" || true)
  279. echo " try $i ($((SECONDS - start)) s): $code"
  280. [ "$code" = 200 ] && break
  281. [ $((SECONDS - start)) -ge "$URL_WAIT" ] && break
  282. sleep 2
  283. done
  284. if [ "$code" != 200 ]; then
  285. if [ -n "$REMOTE_HOST" ]; then die "$URL answered $code, not 200 — look: $SSH $REMOTE_HOST docker logs --tail 50 $CONTAINER"; fi
  286. die "$URL answered $code, not 200 — look: docker logs --tail 50 $CONTAINER"
  287. fi
  288. step "deployed $APP → $TARGET, $URL answers 200"

Branches

Latest commits

  • 26117255|||PR apps API: review comments on files with spaces in their namemre
  • b8796cecMerge branch 'f1-broken-json' into mainGitoria
  • 2f2959c5|||PR apps API: broken JSON body gives 400 invalid_json, not 500mre
  • f7c6ca07Merge branch 'ident-chooser-selector' into mainGitoria
  • 691abb8e|||PR tests: find ident identity choice without the removed .choose classmre
  • 16a3530fMerge branch 'heap-revert' into mainGitoria
  • 220a80e0|||PR Remove temporary /__heap route and heap helper; logmre
  • 13f0c342Merge branch 'hl25-records' into mainGitoria
  • a002ecb7|||PR records: HL25 report, heap helper, logmre
  • 2492a55cTemporary keyed /__heap route for the live memory snapshotmre
  • fefaedd2log: tracker PR-only, HL25 startedmre
  • 21aa57calog: parkmre
  • e9f950a8reports: G25c soakmre
  • a7783686log: deploy 9f41ffcmre
  • 9f41ffc1log: W30b mergedmre
  • bef635c1Merge branch 'w30b'mre
  • 15b996a5Org list: keyed rows by @id, newest first, concat instead of hand loopmre
  • c4f6d9e3reports: G25c growth huntmre
  • b88e39f1log: privacy cleanup donemre
  • bfd7826ddocs: neutral example organisation namemre