gitoriaLog in with ident

components

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitd32b44e2d32b44e2antcolony#40: history (LOG.md), worker briefs (missions/) and reports moved here from antcolony, numbered per project; old numbers in antcolony docs/mission-map.mdmred32b44e2/deploy.sh

8.6 KB

  1. #!/usr/bin/env bash
  2. # deploy.sh — components.hybriel.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=components.hybriel.worldapi.org
  21. CONTAINER=components.hybriel.worldapi.org
  22. [email protected]:/CONTAINERS/projects/components.hybriel.worldapi.org
  23. DEFAULT_URL=https://components.hybriel.worldapi.org/
  24. GATES=(
  25. "node tests/browser.mjs"
  26. )
  27. # NEVER SENT (rsync patterns; a leading / anchors at the app folder)
  28. EXCLUDES=(
  29. /.git/ /.gitignore
  30. /storage/ /.sessions/ /.env /.env.* /.scratch/ /server.* /testapp/
  31. '*.log' '*.pid' node_modules/
  32. )
  33. # ---------------------------------------------------------------------------------------------
  34. DRY=0
  35. SKIP=0
  36. TARGET=${DEPLOY_TARGET:-$DEFAULT_TARGET}
  37. URL=${DEPLOY_URL:-$DEFAULT_URL}
  38. # Loreana's ssh config may not apply to Byrodin: -F /dev/null (ident STATUS)
  39. SSH=${DEPLOY_SSH:-ssh -F /dev/null -o BatchMode=yes -o ConnectTimeout=15}
  40. usage() { sed -n '2,15p' "$0" | sed 's/^# \{0,1\}//'; }
  41. while [ $# -gt 0 ]; do
  42. case "$1" in
  43. --dry-run) DRY=1 ;;
  44. --skip-tests) SKIP=1 ;;
  45. --target) TARGET=${2:?--target needs a value}; shift ;;
  46. --target=*) TARGET=${1#--target=} ;;
  47. --url) URL=${2:?--url needs a value}; shift ;;
  48. --url=*) URL=${1#--url=} ;;
  49. -h|--help) usage; exit 0 ;;
  50. *) echo "deploy: unknown argument: $1" >&2; usage >&2; exit 2 ;;
  51. esac
  52. shift
  53. done
  54. HERE=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
  55. cd "$HERE"
  56. step() { printf '\n==> %s\n' "$*"; }
  57. run() { printf ' $ %s\n' "$*" >&2; "$@"; }
  58. die() { printf '\ndeploy: REFUSED — %s\n' "$*" >&2; exit 1; }
  59. # a target `host:dir` is remote, a plain path is local
  60. if [[ "$TARGET" == *:* ]]; then
  61. REMOTE_HOST=${TARGET%%:*}
  62. REMOTE_DIR=${TARGET#*:}
  63. else
  64. REMOTE_HOST=
  65. REMOTE_DIR=$TARGET
  66. fi
  67. step "[0/5] $APP → $TARGET (dry run: $DRY, skip tests: $SKIP)"
  68. echo " from $HERE"
  69. echo " url $URL"
  70. [ -x bin/hybriel ] || die "bin/hybriel is missing here"
  71. [ -f docker-compose.yml ] || die "docker-compose.yml is missing here"
  72. [ -f project.hl ] || die "project.hl is missing here"
  73. command -v rsync >/dev/null || die "rsync is not installed"
  74. # ---- 1. the gates ---------------------------------------------------------------------------
  75. if [ "$SKIP" = 1 ]; then
  76. step "[1/5] GATES SKIPPED"
  77. printf ' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n'
  78. printf ' !! --skip-tests: NOTHING WAS TESTED. You deploy untested code. !!\n'
  79. printf ' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n'
  80. else
  81. step "[1/5] gates (${#GATES[@]})"
  82. # headless Chromes that exist BEFORE the gates are not ours (other sessions' test runs, e.g. Anton's)
  83. chromes() { ps -eo pid=,ppid=,args= | awk '/[h]l-browser-tier/ && /remote-debugging-port/ && !/--type=/ {print $1, $2}'; }
  84. before=" $(chromes | awk '{print $1}' | tr '\n' ' ') "
  85. for g in "${GATES[@]}"; do
  86. printf ' $ %s\n' "$g"
  87. out=$(mktemp)
  88. if ! $g > "$out" 2>&1; then
  89. grep -E '^FAIL|passed,' "$out" | sed 's/^/ /' || true
  90. echo " (full output: $out)"
  91. die "gate failed: $g"
  92. fi
  93. grep -E 'passed,' "$out" | tail -1 | sed 's/^/ /'
  94. rm -f "$out"
  95. done
  96. # a Chrome our gates LEFT: new since the gates began AND no longer owned by a running node test
  97. # (a Chrome whose parent is a live node process belongs to another session's test run right now)
  98. leaked=""
  99. while read -r pid ppid; do
  100. [ -z "$pid" ] && continue
  101. case "$before" in *" $pid "*) continue ;; esac
  102. ps -o args= -p "$ppid" 2>/dev/null | grep -q '^node\|/node ' && continue
  103. leaked="$leaked $pid"
  104. done < <(chromes)
  105. if [ -n "$leaked" ]; then
  106. die "a gate left a headless Chrome (pids:$leaked; ps -eo pid,args | grep hl-browser-tier)"
  107. fi
  108. fi
  109. # ---- 2. pre-deploy backup of the LIVE data (before anything is sent) ------------------------
  110. BACKUP_ROOT=${DEPLOY_BACKUP_ROOT:-/media/SLOW1TB2/deploy-backups}
  111. BACKUP_DIR="$BACKUP_ROOT/$APP"
  112. BACKUP_FILE="$BACKUP_DIR/$APP-$(date +%Y%m%d-%H%M).tgz"
  113. if [ "$DRY" = 1 ]; then
  114. step "[2/5] DRY RUN: backup skipped"
  115. else
  116. step "[2/5] backup live data ($APP) → $BACKUP_FILE"
  117. mkdir -p "$BACKUP_DIR"
  118. if [ -n "$REMOTE_HOST" ]; then
  119. present=$($SSH "$REMOTE_HOST" "cd '$REMOTE_DIR' 2>/dev/null && for p in storage .sessions .env; do [ -e \"\$p\" ] && echo \"\$p\"; done; true")
  120. else
  121. present=$(cd "$REMOTE_DIR" 2>/dev/null && for p in storage .sessions .env; do [ -e "$p" ] && echo "$p"; done; true)
  122. fi
  123. present=$(printf '%s' "$present" | tr '\n' ' ' | sed 's/ *$//')
  124. if [ -z "$present" ]; then
  125. echo " nothing to back up yet (no storage/.sessions/.env on the target)"
  126. else
  127. echo " taring: $present"
  128. if [ -n "$REMOTE_HOST" ]; then
  129. run $SSH "$REMOTE_HOST" "tar czf - -C '$REMOTE_DIR' $present" > "$BACKUP_FILE" \
  130. || { rm -f "$BACKUP_FILE"; die "backup failed (tar/ssh error) — refusing to deploy"; }
  131. else
  132. run tar czf "$BACKUP_FILE" -C "$REMOTE_DIR" $present \
  133. || { rm -f "$BACKUP_FILE"; die "backup failed (tar error) — refusing to deploy"; }
  134. fi
  135. [ -s "$BACKUP_FILE" ] || { rm -f "$BACKUP_FILE"; die "backup is empty — refusing to deploy"; }
  136. echo " $(du -h "$BACKUP_FILE" | cut -f1) $BACKUP_FILE"
  137. ls -1t "$BACKUP_DIR/$APP"-*.tgz 2>/dev/null | tail -n +6 | xargs -r rm -f --
  138. echo " keeping $(ls -1 "$BACKUP_DIR/$APP"-*.tgz 2>/dev/null | wc -l) archive(s) of $APP"
  139. fi
  140. fi
  141. # ---- 3. rsync the code ----------------------------------------------------------------------
  142. RSYNC=(rsync -az --no-owner --no-group --itemize-changes)
  143. for e in "${EXCLUDES[@]}"; do RSYNC+=("--exclude=$e"); done
  144. if [ -n "$REMOTE_HOST" ]; then
  145. RSYNC+=(-e "$SSH")
  146. else
  147. mkdir -p "$REMOTE_DIR"
  148. fi
  149. # the preview is ALWAYS made first (rsync -n): nothing that must stay on Byrodin may be in it
  150. step "[3/5] rsync preview (what would be sent)"
  151. preview=$(mktemp)
  152. run "${RSYNC[@]}" -n ./ "$TARGET/" > "$preview"
  153. sed 's/^/ /' "$preview"
  154. echo " ($(grep -c . "$preview" || true) lines)"
  155. if awk '{print $2}' "$preview" | grep -E '^(storage/|\.sessions/|\.env|\.scratch/|server\.|testapp/)|\.log$|\.pid$' ; then
  156. die "the preview holds a path that must never be sent (see above)"
  157. fi
  158. rm -f "$preview"
  159. if [ "$DRY" = 1 ]; then
  160. step "[3/5] DRY RUN: nothing sent"
  161. else
  162. step "[3/5] rsync"
  163. run "${RSYNC[@]}" ./ "$TARGET/" | sed 's/^/ /'
  164. fi
  165. # ---- 4. restart the container ---------------------------------------------------------------
  166. RESTART="cd '$REMOTE_DIR' && docker compose up -d --build && docker compose restart && docker compose ps"
  167. if [ "$DRY" = 1 ]; then
  168. step "[4/5] DRY RUN: would restart $CONTAINER"
  169. if [ -n "$REMOTE_HOST" ]; then echo " would run: $SSH $REMOTE_HOST \"$RESTART\""; else echo " would run: bash -c \"$RESTART\""; fi
  170. else
  171. step "[4/5] restart $CONTAINER"
  172. if [ -n "$REMOTE_HOST" ]; then
  173. run $SSH "$REMOTE_HOST" "$RESTART"
  174. else
  175. run bash -c "$RESTART"
  176. fi
  177. fi
  178. # ---- 5. the URL answers 200 -----------------------------------------------------------------
  179. if [ "$DRY" = 1 ]; then
  180. step "[5/5] DRY RUN: would check $URL answers 200"
  181. step "dry run done — nothing was sent, nothing restarted"
  182. exit 0
  183. fi
  184. step "[5/5] $URL must answer 200"
  185. code=000
  186. for i in $(seq 1 20); do
  187. code=$(curl -s -o /dev/null -w '%{http_code}' --max-time 10 "$URL" || true)
  188. echo " try $i: $code"
  189. [ "$code" = 200 ] && break
  190. sleep 1
  191. done
  192. if [ "$code" != 200 ]; then
  193. if [ -n "$REMOTE_HOST" ]; then die "$URL answered $code, not 200 — look: $SSH $REMOTE_HOST docker logs --tail 50 $CONTAINER"; fi
  194. die "$URL answered $code, not 200 — look: docker logs --tail 50 $CONTAINER"
  195. fi
  196. step "deployed $APP → $TARGET, $URL answers 200"

Branches

Latest commits

  • d32b44e2antcolony#40: history (LOG.md), worker briefs (missions/) and reports moved here from antcolony, numbered per project; old numbers in antcolony docs/mission-map.mdmre
  • 0fb80b57components: Hybriel master ff51cf46 (re-vendor round)mre
  • 57408b6ecomponents#14: installable app (manifest, service worker, offline index), own iconmre
  • 317bd09fdeploy.sh: back up live storage/.sessions/.env before every deploy (newest 5 kept)mre
  • becbd59bcomponents#13: modal dialog (<dialog> based, focus wrap, backdrop/Esc/x close with reason, scroll lock, footer buttons)mre
  • 367dd19adeploy.sh: never send .git or .gitignore to Byrodinmre
  • 4b4dabe5State of 2026-09-27, before the move to gitoriamre