gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commite874e44de874e44dtests/status-keyed: app token lives 60 s so it outlives the gate's two restartsmree874e44d/deploy.sh

15.8 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. )
  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 - --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" -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 45014, 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. # /api/repos, restarts the main container, waits for 200 on 45004 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. # Apollo 2026-11-02 mpackdb freeze: DEPLOY_STOP_START=1 replaces ONLY this step with stop-then-start. Two gitoria processes must never
  192. # 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
  193. # 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
  194. # its old pid no longer exists). Without the variable the standby swap below stays the default (deploy-init.sh relies on the string
  195. # standby-swap.sh in this file to skip a second restart).
  196. SWAP=${DEPLOY_STANDBY_SWAP:-/media/STORAGE/projects/WorldAPI/worldapi-conductor/standby-swap.sh}
  197. SWAP_ARGS="gitoria gitoria gitoria-standby /api/repos 45004"
  198. STOP_WAIT=${DEPLOY_STOP_WAIT:-60}
  199. # runs on the target (as one script, so a failure before the start leaves the container stopped and the deploy fails): $1 = app dir
  200. stop_start_script() {
  201. cat <<'EOS'
  202. set -u
  203. cd "$APP_DIR" || exit 1
  204. echo " standby: making sure gitoria-standby is not running"
  205. docker compose --profile standby stop gitoria-standby || true
  206. cid=$(docker compose ps -a -q gitoria)
  207. oldpid=0
  208. if [ -n "$cid" ]; then oldpid=$(docker inspect -f '{{.State.Pid}}' "$cid" 2>/dev/null || echo 0); fi
  209. echo " old container ${cid:-none} pid $oldpid"
  210. echo " docker compose stop gitoria"
  211. docker compose stop gitoria || { echo " stop failed - NOT starting" >&2; exit 1; }
  212. i=0
  213. while :; do
  214. st=$(docker inspect -f '{{.State.Running}} {{.State.Pid}}' "$cid" 2>/dev/null) || st="gone 0"
  215. running=${st%% *}; pid=${st##* }
  216. alive=no
  217. # the pid docker shows now (0 once stopped) and the pid it had before the stop: neither may still exist
  218. for q in "$pid" "$oldpid"; do
  219. if [ -n "$q" ] && [ "$q" != 0 ] && [ -d "/proc/$q" ]; then alive=yes; fi
  220. done
  221. echo " wait ${i}s: running=$running pid=$pid oldpid=$oldpid still-there=$alive"
  222. if [ "$running" != true ] && [ "$alive" = no ]; then break; fi
  223. i=$((i + 1))
  224. if [ "$i" -gt "$STOP_WAIT" ]; then echo " the old gitoria process is still there after ${STOP_WAIT}s - NOT starting" >&2; exit 1; fi
  225. sleep 1
  226. done
  227. echo " old process gone: docker compose up -d gitoria"
  228. docker compose up -d gitoria
  229. EOS
  230. }
  231. [ -f "$SWAP" ] || die "the hand-over helper is missing: $SWAP"
  232. if [ "${DEPLOY_STOP_START:-}" = 1 ]; then
  233. if [ "$DRY" = 1 ]; then
  234. 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)"
  235. 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"
  236. else
  237. step "[4/5] stop $CONTAINER, wait until its old process is gone, start it (DEPLOY_STOP_START=1, mpackdb freeze: no standby swap)"
  238. if [ -n "$REMOTE_HOST" ]; then
  239. printf ' $ %s %s "APP_DIR=%s STOP_WAIT=%s bash -s" (stop-then-start script)\n' "$SSH" "$REMOTE_HOST" "$REMOTE_DIR" "$STOP_WAIT" >&2
  240. 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"
  241. else
  242. stop_start_script | APP_DIR="$REMOTE_DIR" STOP_WAIT="$STOP_WAIT" bash -s || die "stop-then-start failed - the container may be stopped"
  243. fi
  244. fi
  245. elif [ "$DRY" = 1 ]; then
  246. step "[4/5] DRY RUN: would hand over $CONTAINER through its standby (no gap)"
  247. echo " would run: $SWAP_ARGS with $SWAP on $TARGET"
  248. DRY_RUN=1 APP_DIR="$REMOTE_DIR" bash "$SWAP" $SWAP_ARGS 2>&1 | sed 's/^/ /' || true
  249. else
  250. step "[4/5] hand over $CONTAINER through its standby (no gap)"
  251. if [ -n "$REMOTE_HOST" ]; then
  252. printf ' $ %s %s "APP_DIR=%s bash -s -- %s" < %s\n' "$SSH" "$REMOTE_HOST" "$REMOTE_DIR" "$SWAP_ARGS" "$SWAP" >&2
  253. $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)"
  254. else
  255. APP_DIR="$REMOTE_DIR" bash "$SWAP" $SWAP_ARGS || die "the hand-over failed — see the log above"
  256. fi
  257. fi
  258. # 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
  259. if [ "$DRY" != 1 ]; then
  260. # 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)
  261. CHANGED="cd '$REMOTE_DIR' && docker compose build gitoria-sshd && docker compose up -d"
  262. if [ -n "$REMOTE_HOST" ]; then run $SSH "$REMOTE_HOST" "$CHANGED"; else run bash -c "$CHANGED"; fi
  263. fi
  264. # ---- 5. the URL answers 200 -----------------------------------------------------------------
  265. if [ "$DRY" = 1 ]; then
  266. step "[5/5] DRY RUN: would check $URL answers 200"
  267. step "dry run done — nothing was sent, nothing restarted"
  268. exit 0
  269. fi
  270. 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)"
  271. code=000
  272. start=$SECONDS
  273. i=0
  274. while :; do
  275. i=$((i + 1))
  276. code=$(curl -s -o /dev/null -w '%{http_code}' --max-time 10 "$URL" || true)
  277. echo " try $i ($((SECONDS - start)) s): $code"
  278. [ "$code" = 200 ] && break
  279. [ $((SECONDS - start)) -ge "$URL_WAIT" ] && break
  280. sleep 2
  281. done
  282. if [ "$code" != 200 ]; then
  283. if [ -n "$REMOTE_HOST" ]; then die "$URL answered $code, not 200 — look: $SSH $REMOTE_HOST docker logs --tail 50 $CONTAINER"; fi
  284. die "$URL answered $code, not 200 — look: docker logs --tail 50 $CONTAINER"
  285. fi
  286. step "deployed $APP → $TARGET, $URL answers 200"

Branches

Latest commits

  • e874e44dtests/status-keyed: app token lives 60 s so it outlives the gate's two restartsmre
  • 270b8d61deploy.sh: DEPLOY_STOP_START=1 stops, waits for the old pid, then starts (mpackdb freeze, replaces the standby swap)mre
  • 3f9235b8project.hl: drop the dummy /pulls/:n component route (hybriel#294 is fixed in day37)mre
  • c90daf19Re-vendor gitoria to Hybriel set-day37 (f6b3d45b)mre
  • 03b5bb08W39 report to reports/, drop scratch repro, mission + logmre
  • 154d333fMerge w39-create-api: POST /api/repos with a user token (#39)mre
  • 149dd512Rewrite W39 reportmre
  • e5385075Expect 401 for POST /api/repos without a tokenmre
  • 1a76055cDocument repository creation APImre
  • 4d779b3bAdd create-api gatemre
  • 88563122Add POST /api/repos for user tokensmre
  • b00860e3Create repositories with member checks in createRepoInmre
  • 0a38f236Report repository creation API blockermre
  • b5f72187Record malformed JSON blocker for repository creationmre
  • a2ae8302logmre
  • 6947cd58logmre
  • 2175e48clogmre
  • 40eb3ca1gitoria#38: review package, logmre
  • 06a5a50egitoria#38: deploy b8252ca, logmre
  • b8252ca0gitoria#46: W46b report, logmre