gitoria

layouts

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit635234df635234dfcomponents re-synced to components.hybriel 5a98d77 (#15 black/white base look); gate 73/0, check-theme 0, screenshots unchangedmre635234df/deploy.sh

8.6 KB

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

Branches

Latest commits

  • 635234dfcomponents re-synced to components.hybriel 5a98d77 (#15 black/white base look); gate 73/0, check-theme 0, screenshots unchangedmre
  • 876b2064docs: workaround note for hybriel#130 (slot marker mid-line)mre
  • 85b56541layouts mission 002 3/3: same-output tests, letcount, shots ports, docs, reportmre
  • 0f325d3blayouts mission 002 2/3: code rules — styles.hl -> components/styles.hl, the one never-reassigned let dropped; gate 73/0, same outputmre
  • a3e74c8alayouts mission 002 1/3: components re-copied byte-verbatim from components.hybriel 3f93d71 by tools/sync-components.sh (+ --check, components/SOURCE); gate 73/0, check-theme 0mre
  • 80ce6ba9report 001 (mission 075)mre
  • 5c6e338flayouts.worldapi.org in Hybriel: WorldAPI theme (tokens, borderless elements, themed components.hybriel components), shells (header, main/user menu, sidebar overlay, content-first, list-first, three-pane), showcase, check-theme helper; gate 73/0 (mission 075)mre