waouw-update 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #!/bin/bash
  2. set -euo pipefail
  3. WAOUW_HOME="/opt/waouw"
  4. LOG_DIR="${LOG_DIR:-/var/log/waouw/updates}"
  5. ts() { date '+%Y-%m-%d %H:%M:%S'; }
  6. write_status() {
  7. local id="$1"; shift; local json="$1"
  8. mkdir -p "$LOG_DIR"
  9. echo "$json" > "$LOG_DIR/$id.json"
  10. }
  11. apply_update() {
  12. local ZIP="" ID=""
  13. while [[ $# -gt 0 ]]; do
  14. case "$1" in
  15. --zip) ZIP="$2"; shift 2;;
  16. --id) ID="$2"; shift 2;;
  17. *) shift;;
  18. esac
  19. done
  20. [[ -f "$ZIP" ]] || { echo "Archive introuvable: $ZIP"; exit 1; }
  21. [[ -n "$ID" ]] || ID=$(basename "$ZIP" .zip)
  22. local RUN_LOG="$LOG_DIR/$ID.log"
  23. mkdir -p "$LOG_DIR"
  24. write_status "$ID" "{\"id\":\"$ID\",\"state\":\"running\",\"start_time\":\"$(ts)\",\"log_path\":\"$RUN_LOG\"}"
  25. # All output goes to the log file AND stderr (for systemd journal)
  26. exec > >(tee -a "$RUN_LOG") 2>&1
  27. echo "=== WAOUW UPDATE START $(ts) ==="
  28. echo "ZIP: $ZIP ($(stat -c%s "$ZIP") bytes)"
  29. echo "ID: $ID"
  30. # ── Stage 1: Extract ────────────────────────────────────────────────
  31. STAGE="/tmp/waouw-update-$ID"
  32. rm -rf "$STAGE" && mkdir -p "$STAGE"
  33. unzip -oq "$ZIP" -d "$STAGE" || true
  34. echo "Extracted to $STAGE"
  35. # Fix Windows backslash paths (Compress-Archive bug)
  36. cd "$STAGE"
  37. {
  38. find . -name '*\\*' 2>/dev/null | sort -r | while IFS= read -r bad; do
  39. good=$(echo "$bad" | tr '\\' '/')
  40. mkdir -p "$(dirname "$good")"
  41. mv "$bad" "$good" 2>/dev/null || true
  42. done
  43. } || true
  44. find "$STAGE" -type d -empty -delete 2>/dev/null || true
  45. cd /
  46. # ── Stage 2: Verify manifest ────────────────────────────────────────
  47. MANI="$STAGE/manifest.yml"
  48. if [[ ! -f "$MANI" ]]; then
  49. echo "FATAL: Manifest manquant dans le package"
  50. write_status "$ID" "{\"id\":\"$ID\",\"state\":\"failed\",\"error\":\"manifest missing\",\"log_path\":\"$RUN_LOG\"}"
  51. exit 2
  52. fi
  53. echo "Manifest OK"
  54. # ── Stage 2b: Enforce complete package + SHA256 (Ed rule 2026-04-07) ─
  55. # A package MUST declare itself complete and ship a sha256 of files/.
  56. # Incremental patches are forbidden (cf feedback_update_system.md).
  57. if ! grep -qE '^complete:[[:space:]]*true' "$MANI"; then
  58. echo "FATAL: Le manifest ne déclare pas 'complete: true' — package incrémental refusé"
  59. echo " Règle Ed 2026-04-07 : chaque OTA DOIT être complète"
  60. write_status "$ID" "{\"id\":\"$ID\",\"state\":\"failed\",\"error\":\"package not declared complete\",\"log_path\":\"$RUN_LOG\"}"
  61. exit 2
  62. fi
  63. EXPECTED_SHA=$(grep -E '^files_sha256:' "$MANI" | awk '{print $2}' | tr -d '"' | tr -d "'")
  64. if [[ -z "$EXPECTED_SHA" ]]; then
  65. echo "FATAL: Le manifest ne contient pas 'files_sha256:' — package non vérifiable"
  66. write_status "$ID" "{\"id\":\"$ID\",\"state\":\"failed\",\"error\":\"manifest missing files_sha256\",\"log_path\":\"$RUN_LOG\"}"
  67. exit 2
  68. fi
  69. ACTUAL_SHA=$(cd "$STAGE/files" && find . -type f -print0 | sort -z | xargs -0 sha256sum | sha256sum | awk '{print $1}')
  70. if [[ "$EXPECTED_SHA" != "$ACTUAL_SHA" ]]; then
  71. echo "FATAL: SHA256 mismatch — package corrompu ou tronqué"
  72. echo " expected=$EXPECTED_SHA"
  73. echo " actual =$ACTUAL_SHA"
  74. write_status "$ID" "{\"id\":\"$ID\",\"state\":\"failed\",\"error\":\"sha256 mismatch\",\"log_path\":\"$RUN_LOG\"}"
  75. exit 2
  76. fi
  77. echo "Package complet et SHA256 vérifié ($ACTUAL_SHA)"
  78. # ── Stage 3: Verify files directory exists ──────────────────────────
  79. if [[ ! -d "$STAGE/files" ]]; then
  80. echo "FATAL: Dossier files/ manquant dans le package"
  81. write_status "$ID" "{\"id\":\"$ID\",\"state\":\"failed\",\"error\":\"files/ missing\",\"log_path\":\"$RUN_LOG\"}"
  82. exit 2
  83. fi
  84. # Count files to sync
  85. FILE_COUNT=$(find "$STAGE/files" -type f | wc -l)
  86. echo "Package contains $FILE_COUNT files"
  87. # ── Stage 4: Backup .env ────────────────────────────────────────────
  88. cp "$WAOUW_HOME/DEPLOY/.env" "/tmp/waouw-env-backup-$ID" 2>/dev/null || true
  89. cp "$WAOUW_HOME/.env" "/tmp/waouw-env-root-backup-$ID" 2>/dev/null || true
  90. echo ".env backed up"
  91. # ── Stage 5: Rsync overlay ─────────────────────────────────────────
  92. rsync -av \
  93. --exclude='.env' \
  94. --exclude='PASSWORDS.txt' \
  95. --exclude='sites-data/' \
  96. --exclude='*.sqlite' \
  97. --exclude='*.db' \
  98. "$STAGE/files/" "$WAOUW_HOME/"
  99. echo "Files synced (overlay mode, $FILE_COUNT files)"
  100. # ── Stage 6: Restore .env ──────────────────────────────────────────
  101. if [[ -f "/tmp/waouw-env-backup-$ID" ]]; then
  102. cp -f "/tmp/waouw-env-backup-$ID" "$WAOUW_HOME/DEPLOY/.env"
  103. echo ".env DEPLOY restored"
  104. fi
  105. if [[ -f "/tmp/waouw-env-root-backup-$ID" ]]; then
  106. cp -f "/tmp/waouw-env-root-backup-$ID" "$WAOUW_HOME/.env"
  107. echo ".env root restored"
  108. fi
  109. # ── Stage 7: Fix VERSION.json ──────────────────────────────────────
  110. if [[ -d "$WAOUW_HOME/VERSION.json" ]]; then
  111. rm -rf "$WAOUW_HOME/VERSION.json"
  112. fi
  113. if [[ -f "$STAGE/files/VERSION.json" ]]; then
  114. cp -f "$STAGE/files/VERSION.json" "$WAOUW_HOME/VERSION.json"
  115. echo "VERSION.json updated: $(cat "$WAOUW_HOME/VERSION.json")"
  116. fi
  117. # ── Stage 8: Fix permissions ───────────────────────────────────────
  118. chmod -R a+rX "$WAOUW_HOME/BACKEND/" "$WAOUW_HOME/UI/" 2>/dev/null || true
  119. echo "Permissions fixed"
  120. # ── Stage 9: Migrations ────────────────────────────────────────────
  121. if [[ -d "$STAGE/migrations" ]]; then
  122. echo "Running migrations..."
  123. chmod +x "$STAGE/migrations"/* 2>/dev/null || true
  124. for s in "$STAGE/migrations"/*; do
  125. [ -f "$s" ] && echo " Migration: $(basename "$s")" && bash "$s"
  126. done
  127. fi
  128. # ── Stage 10: Traefik config ───────────────────────────────────────
  129. if [[ -f "$WAOUW_HOME/DEPLOY/traefik/dynamic.yml" ]]; then
  130. cp -f "$WAOUW_HOME/DEPLOY/traefik/dynamic.yml" /etc/waouw/traefik/dynamic.yml 2>/dev/null || true
  131. echo "Traefik dynamic.yml updated"
  132. fi
  133. # ── Stage 11: Docker build ─────────────────────────────────────────
  134. echo "Building Docker images (--no-cache)..."
  135. pushd "$WAOUW_HOME/DEPLOY" >/dev/null
  136. if ! docker compose build --no-cache backend frontend; then
  137. echo "FATAL: docker compose build FAILED"
  138. write_status "$ID" "{\"id\":\"$ID\",\"state\":\"failed\",\"error\":\"docker build failed\",\"end_time\":\"$(ts)\",\"log_path\":\"$RUN_LOG\"}"
  139. cp -f "/tmp/waouw-env-backup-$ID" "$WAOUW_HOME/DEPLOY/.env" 2>/dev/null || true
  140. popd >/dev/null
  141. exit 3
  142. fi
  143. echo "Docker build succeeded"
  144. # ── Stage 12: Verify new image contains updated files ──────────────
  145. echo "Verifying new images..."
  146. if [[ -f "$WAOUW_HOME/BACKEND/app/api/routes.py" ]]; then
  147. EXPECTED_ROUTERS=$(grep -c 'include_router' "$WAOUW_HOME/BACKEND/app/api/routes.py" || echo 0)
  148. echo " routes.py has $EXPECTED_ROUTERS registered routers"
  149. fi
  150. # ── Stage 13: Restart containers ───────────────────────────────────
  151. echo "Restarting containers..."
  152. docker compose up -d 2>&1 || true
  153. sleep 10
  154. docker compose up -d frontend 2>&1 || true
  155. popd >/dev/null
  156. # ── Stage 14: Health check ─────────────────────────────────────────
  157. echo "Waiting for health checks..."
  158. sleep 15
  159. BACKEND_OK=$(docker inspect waouw_backend --format '{{.State.Health.Status}}' 2>/dev/null || echo "unknown")
  160. FRONTEND_OK=$(docker inspect waouw_frontend --format '{{.State.Health.Status}}' 2>/dev/null || echo "unknown")
  161. echo "Backend health: $BACKEND_OK"
  162. echo "Frontend health: $FRONTEND_OK"
  163. if [[ "$BACKEND_OK" != "healthy" ]]; then
  164. echo "WARNING: Backend not healthy after update"
  165. write_status "$ID" "{\"id\":\"$ID\",\"state\":\"warning\",\"error\":\"backend not healthy\",\"end_time\":\"$(ts)\",\"log_path\":\"$RUN_LOG\"}"
  166. exit 0
  167. fi
  168. echo "=== WAOUW UPDATE DONE $(ts) ==="
  169. write_status "$ID" "{\"id\":\"$ID\",\"state\":\"success\",\"start_time\":\"$(ts)\",\"end_time\":\"$(ts)\",\"log_path\":\"$RUN_LOG\"}"
  170. # Cleanup
  171. rm -rf "$STAGE"
  172. exit 0
  173. }
  174. case "${1:-}" in
  175. apply) shift; apply_update "$@" ;;
  176. *) echo "Usage: waouw-update apply --zip <archive> [--id <id>]"; exit 1 ;;
  177. esac