225 lines
7.3 KiB
Bash
225 lines
7.3 KiB
Bash
#!/bin/bash
|
|
# nas-docker-up — Applique les mises à jour Docker via docker compose up -d
|
|
# Usage : nas-docker-up [stack_name]
|
|
# Sans argument : met à jour tous les conteneurs ayant une image plus récente
|
|
# Avec argument : met à jour uniquement la stack spécifiée
|
|
# Mode HA (non-interactif) : applique directement, pas de prompt
|
|
# Mode terminal : confirmation par stack (sauf si stack spécifiée en argument)
|
|
|
|
set -euo pipefail
|
|
|
|
if [ -t 1 ]; then INTERACTIVE=true; else INTERACTIVE=false; fi
|
|
|
|
TARGET_STACK="${1:-}"
|
|
|
|
# Couleurs (mode terminal uniquement)
|
|
if $INTERACTIVE; then
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
CYAN='\033[0;36m'
|
|
BOLD='\033[1m'
|
|
RESET='\033[0m'
|
|
else
|
|
RED='' GREEN='' YELLOW='' CYAN='' BOLD='' RESET=''
|
|
fi
|
|
|
|
# --- Détection de l'env global OMV ---
|
|
OMV_GLOBAL_ENV=""
|
|
if [ -f "/etc/omv-compose.env" ]; then
|
|
OMV_GLOBAL_ENV="/etc/omv-compose.env"
|
|
elif [ -f "/srv/omv-compose.env" ]; then
|
|
OMV_GLOBAL_ENV="/srv/omv-compose.env"
|
|
fi
|
|
|
|
# --- Collecte des conteneurs à mettre à jour ---
|
|
containers_to_update=()
|
|
images_to_update=()
|
|
compose_dirs=()
|
|
container_ids=()
|
|
old_versions=()
|
|
new_versions=()
|
|
|
|
if $INTERACTIVE; then
|
|
echo -e "${BOLD}--- Phase 1 : Recherche des mises à jour ---${RESET}"
|
|
fi
|
|
|
|
while IFS=: read -r container_id container_name; do
|
|
# Filtrer par stack si argument fourni
|
|
if [ -n "$TARGET_STACK" ] && [ "$container_name" != "$TARGET_STACK" ]; then
|
|
stack_label=$(docker inspect --format='{{index .Config.Labels "com.docker.compose.project"}}' "$container_id" 2>/dev/null | xargs)
|
|
if [ "$stack_label" != "$TARGET_STACK" ]; then
|
|
continue
|
|
fi
|
|
fi
|
|
|
|
compose_dir=$(docker inspect --format='{{index .Config.Labels "com.docker.compose.project.working_dir"}}' "$container_id" 2>/dev/null | xargs)
|
|
|
|
if [ -z "$compose_dir" ] || [ ! -d "$compose_dir" ]; then
|
|
continue
|
|
fi
|
|
|
|
image_name=$(docker inspect --format='{{.Config.Image}}' "$container_id")
|
|
old_image_id=$(docker inspect --format='{{.Image}}' "$container_id")
|
|
old_ver=$(docker inspect --format='{{index .Config.Labels "org.opencontainers.image.version"}}' "$container_id" 2>/dev/null || echo "")
|
|
[ -z "$old_ver" ] && old_ver="inconnue"
|
|
|
|
if $INTERACTIVE; then
|
|
echo -ne " Vérification de ${CYAN}${container_name}${RESET}... "
|
|
fi
|
|
|
|
if ! docker pull "$image_name" > /dev/null 2>&1; then
|
|
if $INTERACTIVE; then echo -e "${YELLOW}⚠ Erreur de pull (ignoré)${RESET}"; fi
|
|
continue
|
|
fi
|
|
|
|
new_image_id=$(docker inspect --format='{{.Id}}' "$image_name" 2>/dev/null)
|
|
|
|
if [ "$old_image_id" != "$new_image_id" ]; then
|
|
new_ver=$(docker inspect --format='{{index .Config.Labels "org.opencontainers.image.version"}}' "$image_name" 2>/dev/null || echo "")
|
|
[ -z "$new_ver" ] && new_ver="disponible"
|
|
|
|
if $INTERACTIVE; then
|
|
echo -e "${YELLOW}MAJ DISPONIBLE${RESET} : ${YELLOW}${old_ver}${RESET} → ${GREEN}${new_ver}${RESET}"
|
|
fi
|
|
|
|
containers_to_update+=("$container_name")
|
|
images_to_update+=("$image_name")
|
|
compose_dirs+=("$compose_dir")
|
|
container_ids+=("$container_id")
|
|
old_versions+=("$old_ver")
|
|
new_versions+=("$new_ver")
|
|
else
|
|
if $INTERACTIVE; then echo -e "${GREEN}✅ À jour${RESET}"; fi
|
|
fi
|
|
|
|
done < <(docker ps --format "{{.ID}}:{{.Names}}")
|
|
|
|
if [ ${#containers_to_update[@]} -eq 0 ]; then
|
|
if $INTERACTIVE; then
|
|
echo ""
|
|
echo -e "${GREEN}✅ Aucun conteneur à mettre à jour.${RESET}"
|
|
echo -e "${CYAN}🧹 Nettoyage des images orphelines...${RESET}"
|
|
fi
|
|
docker image prune -f > /dev/null
|
|
exit 0
|
|
fi
|
|
|
|
if $INTERACTIVE; then
|
|
echo ""
|
|
echo -e "${BOLD}--- Phase 2 : Bilan des mises à jour disponibles ---${RESET}"
|
|
for i in "${!containers_to_update[@]}"; do
|
|
echo -e " • ${CYAN}${containers_to_update[$i]}${RESET} : ${YELLOW}${old_versions[$i]}${RESET} → ${GREEN}${new_versions[$i]}${RESET}"
|
|
done
|
|
echo ""
|
|
|
|
# Si stack spécifique : pas de question sur le mode, on applique directement
|
|
if [ -z "$TARGET_STACK" ]; then
|
|
UPDATE_ALL=false
|
|
while true; do
|
|
read -p "Mettre à jour TOUS ces conteneurs ? [y]es (Tout) / [n]o (Choisir par conteneur) : " global_choice
|
|
case "$global_choice" in
|
|
[yY]*) UPDATE_ALL=true; break ;;
|
|
[nN]*) UPDATE_ALL=false; break ;;
|
|
*) echo "Veuillez répondre par y ou n." ;;
|
|
esac
|
|
done
|
|
else
|
|
UPDATE_ALL=true
|
|
fi
|
|
fi
|
|
|
|
# --- Phase 3 : Application ---
|
|
if $INTERACTIVE; then
|
|
echo ""
|
|
echo -e "${BOLD}--- Phase 3 : Application des mises à jour ---${RESET}"
|
|
fi
|
|
|
|
START_DIR=$(pwd)
|
|
upgraded=0
|
|
failed=0
|
|
|
|
for i in "${!containers_to_update[@]}"; do
|
|
c_name="${containers_to_update[$i]}"
|
|
c_dir="${compose_dirs[$i]}"
|
|
c_old_ver="${old_versions[$i]}"
|
|
c_new_ver="${new_versions[$i]}"
|
|
|
|
DO_UPDATE=false
|
|
|
|
if ! $INTERACTIVE; then
|
|
# Mode HA : toujours appliquer
|
|
DO_UPDATE=true
|
|
elif [ "$UPDATE_ALL" = true ]; then
|
|
DO_UPDATE=true
|
|
else
|
|
while true; do
|
|
read -p "Mettre à jour '${c_name}' (${c_old_ver} → ${c_new_ver}) ? [y/n] : " choice
|
|
case "$choice" in
|
|
[yY]*) DO_UPDATE=true; break ;;
|
|
[nN]*) DO_UPDATE=false; break ;;
|
|
*) echo "Veuillez répondre par y ou n." ;;
|
|
esac
|
|
done
|
|
fi
|
|
|
|
if [ "$DO_UPDATE" = true ]; then
|
|
if $INTERACTIVE; then
|
|
echo -e " 🚀 Mise à jour de ${CYAN}${c_name}${RESET} dans ${c_dir}..."
|
|
fi
|
|
|
|
if cd "$c_dir" 2>/dev/null; then
|
|
ENV_ARGS=""
|
|
|
|
# Env global OMV
|
|
if [ -n "$OMV_GLOBAL_ENV" ] && [ -f "$OMV_GLOBAL_ENV" ]; then
|
|
ENV_ARGS="--env-file $OMV_GLOBAL_ENV"
|
|
else
|
|
# Fallback : chercher global.env ou .env dans le dossier parent
|
|
PARENT_DIR=$(dirname "$c_dir")
|
|
if [ -f "$PARENT_DIR/global.env" ]; then
|
|
ENV_ARGS="--env-file $PARENT_DIR/global.env"
|
|
elif [ -f "$PARENT_DIR/.env" ]; then
|
|
ENV_ARGS="--env-file $PARENT_DIR/.env"
|
|
fi
|
|
fi
|
|
|
|
# .env local (toujours ajouté s'il existe)
|
|
if [ -f ".env" ]; then
|
|
ENV_ARGS="$ENV_ARGS --env-file .env"
|
|
fi
|
|
|
|
if docker compose $ENV_ARGS up -d --remove-orphans 2>&1; then
|
|
if $INTERACTIVE; then
|
|
echo -e " ${GREEN}✅ ${c_name} mis à jour avec succès.${RESET}"
|
|
fi
|
|
upgraded=$((upgraded + 1))
|
|
else
|
|
if $INTERACTIVE; then
|
|
echo -e " ${RED}❌ Échec de la mise à jour pour ${c_name}.${RESET}"
|
|
fi
|
|
failed=$((failed + 1))
|
|
fi
|
|
else
|
|
if $INTERACTIVE; then
|
|
echo -e " ${RED}❌ Impossible d'accéder à ${c_dir}.${RESET}"
|
|
fi
|
|
failed=$((failed + 1))
|
|
fi
|
|
else
|
|
if $INTERACTIVE; then
|
|
echo -e " ⏭ Mise à jour ignorée pour ${c_name}."
|
|
fi
|
|
fi
|
|
|
|
cd "$START_DIR"
|
|
done
|
|
|
|
if $INTERACTIVE; then
|
|
echo ""
|
|
echo -e "${BOLD}--- Terminé ---${RESET}"
|
|
echo -e " ${GREEN}✅ ${upgraded} mis à jour${RESET} ${RED}❌ ${failed} en échec${RESET}"
|
|
else
|
|
printf '{"upgraded":%d,"failed":%d}\n' "$upgraded" "$failed"
|
|
fi
|