gitoriaLog in with ident

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit81b15b7b81b15b7bState of 2026-09-27, before the move to gitoriamre81b15b7b/deploy.sh

6.9 KB

  1. #!/usr/bin/env bash
  2. # deploy.sh — ident.worldapi.org: Loreana (this folder) → Byrodin. Run ON LOREANA.
  3. #
  4. # ./deploy.sh gates → rsync → restart the container → public URL 200
  5. # ./deploy.sh --dry-run gates → rsync -n (shows what WOULD be sent), 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. #
  12. # THE FIRST DEPLOY is done by the architect on Byrodin (folder, .env with SMTP, nginx vhost,
  13. # cert, DNS). This script only updates the CODE: storage/, .sessions/, .env, .scratch/,
  14. # server.*, testapp/ and logs are never sent, so live data on Byrodin is never touched.
  15. # No --delete: a file removed here stays on Byrodin (harmless — nothing imports it).
  16. set -euo pipefail
  17. # ---- the app ------------------------------------------------------------------------------
  18. APP=ident.worldapi.org
  19. CONTAINER=ident.worldapi.org
  20. [email protected]:/CONTAINERS/projects/ident.worldapi.org
  21. DEFAULT_URL=https://ident.worldapi.org/
  22. GATES=(
  23. "node tests/browser.mjs"
  24. "node tests/apps.mjs"
  25. "node tests/selector.mjs"
  26. "node tests/migration.mjs"
  27. "node tests/iplimit.mjs"
  28. "node tests/notify.mjs"
  29. )
  30. # NEVER SENT (rsync patterns; a leading / anchors at the app folder)
  31. EXCLUDES=(
  32. /storage/ /.sessions/ /.env /.env.* /.scratch/ /server.* /testapp/
  33. '*.log' '*.pid' node_modules/
  34. )
  35. # ---------------------------------------------------------------------------------------------
  36. DRY=0
  37. SKIP=0
  38. TARGET=${DEPLOY_TARGET:-$DEFAULT_TARGET}
  39. URL=${DEPLOY_URL:-$DEFAULT_URL}
  40. # Loreana's ssh config may not apply to Byrodin: -F /dev/null (ident STATUS)
  41. SSH=${DEPLOY_SSH:-ssh -F /dev/null -o BatchMode=yes -o ConnectTimeout=15}
  42. usage() { sed -n '2,13p' "$0" | sed 's/^# \{0,1\}//'; }
  43. while [ $# -gt 0 ]; do
  44. case "$1" in
  45. --dry-run) DRY=1 ;;
  46. --skip-tests) SKIP=1 ;;
  47. --target) TARGET=${2:?--target needs a value}; shift ;;
  48. --target=*) TARGET=${1#--target=} ;;
  49. --url) URL=${2:?--url needs a value}; shift ;;
  50. --url=*) URL=${1#--url=} ;;
  51. -h|--help) usage; exit 0 ;;
  52. *) echo "deploy: unknown argument: $1" >&2; usage >&2; exit 2 ;;
  53. esac
  54. shift
  55. done
  56. HERE=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
  57. cd "$HERE"
  58. step() { printf '\n==> %s\n' "$*"; }
  59. run() { printf ' $ %s\n' "$*" >&2; "$@"; }
  60. die() { printf '\ndeploy: REFUSED — %s\n' "$*" >&2; exit 1; }
  61. # a target `host:dir` is remote, a plain path is local
  62. if [[ "$TARGET" == *:* ]]; then
  63. REMOTE_HOST=${TARGET%%:*}
  64. REMOTE_DIR=${TARGET#*:}
  65. else
  66. REMOTE_HOST=
  67. REMOTE_DIR=$TARGET
  68. fi
  69. step "[0/4] $APP → $TARGET (dry run: $DRY, skip tests: $SKIP)"
  70. echo " from $HERE"
  71. echo " url $URL"
  72. [ -x bin/hybriel ] || die "bin/hybriel is missing here"
  73. [ -f docker-compose.yml ] || die "docker-compose.yml is missing here"
  74. [ -f project.hl ] || die "project.hl is missing here"
  75. command -v rsync >/dev/null || die "rsync is not installed"
  76. # ---- 1. the gates ---------------------------------------------------------------------------
  77. if [ "$SKIP" = 1 ]; then
  78. step "[1/4] GATES SKIPPED"
  79. printf ' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n'
  80. printf ' !! --skip-tests: NOTHING WAS TESTED. You deploy untested code. !!\n'
  81. printf ' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n'
  82. else
  83. step "[1/4] gates (${#GATES[@]})"
  84. # headless Chromes that exist BEFORE the gates are not ours (other sessions' test runs, e.g. Anton's)
  85. chromes() { ps -eo pid=,ppid=,args= | awk '/[h]l-browser-tier/ && /remote-debugging-port/ && !/--type=/ {print $1, $2}'; }
  86. before=" $(chromes | awk '{print $1}' | tr '\n' ' ') "
  87. for g in "${GATES[@]}"; do
  88. printf ' $ %s\n' "$g"
  89. out=$(mktemp)
  90. if ! $g > "$out" 2>&1; then
  91. grep -E '^FAIL|passed,' "$out" | sed 's/^/ /' || true
  92. echo " (full output: $out)"
  93. die "gate failed: $g"
  94. fi
  95. grep -E 'passed,' "$out" | tail -1 | sed 's/^/ /'
  96. rm -f "$out"
  97. done
  98. # a Chrome our gates LEFT: new since the gates began AND no longer owned by a running node test
  99. # (a Chrome whose parent is a live node process belongs to another session's test run right now)
  100. leaked=""
  101. while read -r pid ppid; do
  102. [ -z "$pid" ] && continue
  103. case "$before" in *" $pid "*) continue ;; esac
  104. ps -o args= -p "$ppid" 2>/dev/null | grep -q '^node\|/node ' && continue
  105. leaked="$leaked $pid"
  106. done < <(chromes)
  107. if [ -n "$leaked" ]; then
  108. die "a gate left a headless Chrome (pids:$leaked; ps -eo pid,args | grep hl-browser-tier)"
  109. fi
  110. fi
  111. # ---- 2. rsync the code ----------------------------------------------------------------------
  112. RSYNC=(rsync -az --no-owner --no-group --itemize-changes)
  113. for e in "${EXCLUDES[@]}"; do RSYNC+=("--exclude=$e"); done
  114. if [ -n "$REMOTE_HOST" ]; then
  115. RSYNC+=(-e "$SSH")
  116. else
  117. mkdir -p "$REMOTE_DIR"
  118. fi
  119. # the preview is ALWAYS made first (rsync -n): nothing that must stay on Byrodin may be in it
  120. step "[2/4] rsync preview (what would be sent)"
  121. preview=$(mktemp)
  122. run "${RSYNC[@]}" -n ./ "$TARGET/" > "$preview"
  123. sed 's/^/ /' "$preview"
  124. echo " ($(grep -c . "$preview" || true) lines)"
  125. if awk '{print $2}' "$preview" | grep -E '^(storage/|\.sessions/|\.env|\.scratch/|server\.|testapp/)|\.log$|\.pid$' ; then
  126. die "the preview holds a path that must never be sent (see above)"
  127. fi
  128. rm -f "$preview"
  129. if [ "$DRY" = 1 ]; then
  130. step "[2/4] DRY RUN: nothing sent"
  131. else
  132. step "[2/4] rsync"
  133. run "${RSYNC[@]}" ./ "$TARGET/" | sed 's/^/ /'
  134. fi
  135. # ---- 3. restart the container ---------------------------------------------------------------
  136. RESTART="cd '$REMOTE_DIR' && docker compose up -d --build && docker compose restart && docker compose ps"
  137. if [ "$DRY" = 1 ]; then
  138. step "[3/4] DRY RUN: would restart $CONTAINER"
  139. if [ -n "$REMOTE_HOST" ]; then echo " would run: $SSH $REMOTE_HOST \"$RESTART\""; else echo " would run: bash -c \"$RESTART\""; fi
  140. else
  141. step "[3/4] restart $CONTAINER"
  142. if [ -n "$REMOTE_HOST" ]; then
  143. run $SSH "$REMOTE_HOST" "$RESTART"
  144. else
  145. run bash -c "$RESTART"
  146. fi
  147. fi
  148. # ---- 4. the URL answers 200 -----------------------------------------------------------------
  149. if [ "$DRY" = 1 ]; then
  150. step "[4/4] DRY RUN: would check $URL answers 200"
  151. step "dry run done — nothing was sent, nothing restarted"
  152. exit 0
  153. fi
  154. step "[4/4] $URL must answer 200"
  155. code=000
  156. for i in $(seq 1 20); do
  157. code=$(curl -s -o /dev/null -w '%{http_code}' --max-time 10 "$URL" || true)
  158. echo " try $i: $code"
  159. [ "$code" = 200 ] && break
  160. sleep 1
  161. done
  162. if [ "$code" != 200 ]; then
  163. if [ -n "$REMOTE_HOST" ]; then die "$URL answered $code, not 200 — look: $SSH $REMOTE_HOST docker logs --tail 50 $CONTAINER"; fi
  164. die "$URL answered $code, not 200 — look: docker logs --tail 50 $CONTAINER"
  165. fi
  166. step "deployed $APP → $TARGET, $URL answers 200"

Branches

Latest commits

  • 81b15b7bState of 2026-09-27, before the move to gitoriamre