-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_ghost.sh
More file actions
executable file
·84 lines (69 loc) · 2.78 KB
/
Copy pathupdate_ghost.sh
File metadata and controls
executable file
·84 lines (69 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/bin/bash
set -euo pipefail
##### INSTRUCTION
#
# Mise a jour de la stack Ghost, avec sauvegarde prealable.
#
# sauvegarde borg -> docker compose pull -> up -d -> attente du healthcheck
#
# Au premier demarrage d'une nouvelle version, Ghost joue ses migrations de
# schema : elles ne sont PAS reversibles. D'ou la sauvegarde avant le pull.
#
# Usage :
# ./update_ghost.sh # backup puis mise a jour
# ./update_ghost.sh --no-backup # mise a jour seule (a eviter)
#
# Prerequis : backup_ghost.sh configure (voir son en-tete).
#####
SCRIPT_DIR="$(cd -- "$(dirname -- "$0")" && pwd)"
GHOST_DIR="${GHOST_DIR:-$(dirname "$SCRIPT_DIR")}"
COMPOSE_FILE="$GHOST_DIR/docker-compose.yml"
# Duree max d'attente du retour au vert de Ghost, en secondes.
HEALTH_TIMEOUT=180
[ -f "$COMPOSE_FILE" ] || { echo "[ghost-update] compose introuvable : $COMPOSE_FILE" >&2; exit 1; }
compose() { docker compose -f "$COMPOSE_FILE" "$@"; }
#### 1. SAUVEGARDE ####
if [ "${1:-}" = "--no-backup" ]; then
echo "[ghost-update] sauvegarde IGNOREE (--no-backup)"
else
echo "[ghost-update] sauvegarde avant mise a jour..."
bash "$SCRIPT_DIR/backup_ghost.sh"
fi
#### 2. MISE A JOUR ####
VERSION_AVANT="$(compose exec -T ghost sh -c 'cat current/package.json' 2>/dev/null \
| grep -m1 '"version"' | cut -d'"' -f4 || echo 'inconnue')"
echo "[ghost-update] version actuelle : $VERSION_AVANT"
echo "[ghost-update] recuperation des images..."
compose pull
echo "[ghost-update] redemarrage de la stack..."
compose up -d
#### 3. VERIFICATION ####
echo "[ghost-update] attente du healthcheck Ghost (max ${HEALTH_TIMEOUT}s)..."
# On resout l'ID du conteneur via compose : son nom depend du COMPOSE_PROJECT_NAME
# de cette instance, plusieurs Ghost pouvant tourner sur le meme serveur.
CONTENEUR="$(compose ps -q ghost)"
[ -n "$CONTENEUR" ] || { echo "[ghost-update] conteneur ghost introuvable" >&2; exit 1; }
ATTENTE=0
while [ "$ATTENTE" -lt "$HEALTH_TIMEOUT" ]; do
ETAT="$(docker inspect --format '{{.State.Health.Status}}' "$CONTENEUR" 2>/dev/null || echo 'absent')"
case "$ETAT" in
healthy)
VERSION_APRES="$(compose exec -T ghost sh -c 'cat current/package.json' 2>/dev/null \
| grep -m1 '"version"' | cut -d'"' -f4 || echo 'inconnue')"
echo "[ghost-update] OK — Ghost est en bonne sante."
echo "[ghost-update] $VERSION_AVANT -> $VERSION_APRES"
exit 0
;;
unhealthy)
break
;;
esac
sleep 5
ATTENTE=$((ATTENTE + 5))
done
echo "[ghost-update] ECHEC : Ghost n'est pas revenu en bonne sante (etat : ${ETAT:-inconnu})." >&2
echo "[ghost-update] Derniers logs :" >&2
compose logs --tail 50 ghost >&2 || true
echo "[ghost-update] Sauvegarde disponible dans borgwarehouse." >&2
echo "[ghost-update] Procedure de restauration : voir la section 'Restauration' du README." >&2
exit 1