diff --git a/scripts/redeploy-containers.sh b/scripts/redeploy-containers.sh index 76192d4..b7290aa 100644 --- a/scripts/redeploy-containers.sh +++ b/scripts/redeploy-containers.sh @@ -1,35 +1,37 @@ #!/bin/bash -# Auto-update Docker-Compose stacks nach Git-Pull -# Läuft als dockeradmin, prüft geänderte Compose-Dateien -# Stillgelegte Container werden ignoriert +# Auto-Restart Script für geänderte Docker-Compose Stacks +# Nur laufende, aktive Container werden neu gestartet +# Inaktive Container bleiben unberührt +# Logs im Repo-Verzeichnis REPO_DIR="/srv/docker" -LOGFILE="/var/log/docker-update.log" +LOGFILE="$REPO_DIR/scripts/docker-update.log" -INACTIVE_CONTAINERS=("adguard" "kea" "caddy" "wikijs") +# Liste der inaktiven Container +INACTIVE_CONTAINERS=("adguard" "kea" "caddy") log() { echo "$(date '+%Y-%m-%d %H:%M:%S') | $*" | tee -a "$LOGFILE" } -log "===== Starting Auto-Update (changed Compose only) =====" +log "===== Starting Auto-Restart (final) =====" cd "$REPO_DIR" || { log "ERROR: Cannot enter $REPO_DIR"; exit 1; } -# 1️⃣ Pull neueste Änderungen +# 1️⃣ Git Pull + Hard Reset (VM exakt auf Remote-Stand bringen) git fetch --all &>/dev/null git reset --hard origin/main &>/dev/null -log "Pulled latest changes from Git." +log "Pulled latest changes and reset VM to remote state." # 2️⃣ Geänderte Compose-Dateien ermitteln CHANGED=$(git diff --name-only HEAD~1 HEAD | grep -E '^compose/.+/docker-compose\.yml$' || true) if [ -z "$CHANGED" ]; then - log "No Compose files changed. Nothing to update." + log "No Compose files changed. Nothing to restart." exit 0 fi -# 3️⃣ Nur geänderte Container updaten +# 3️⃣ Nur laufende, geänderte Container neu starten for FILE in $CHANGED; do CONTAINER_NAME=$(echo "$FILE" | cut -d'/' -f2) @@ -45,16 +47,22 @@ for FILE in $CHANGED; do continue fi - log "Updating container: $CONTAINER_NAME" - cd "$COMPOSE_DIR" || continue - docker compose pull &>/dev/null - docker compose up -d &>/dev/null + # Prüfen, ob Container läuft + RUNNING=$(docker compose -f "$COMPOSE_DIR/docker-compose.yml" ps -q) + if [ -z "$RUNNING" ]; then + log "Container $CONTAINER_NAME is stopped. Skipping restart." + continue + fi + log "Restarting running container: $CONTAINER_NAME" + cd "$COMPOSE_DIR" || continue + + docker compose up -d &>/dev/null if [ $? -eq 0 ]; then - log "✅ $CONTAINER_NAME updated successfully" + log "✅ $CONTAINER_NAME restarted successfully" else - log "❌ Failed to update $CONTAINER_NAME" + log "❌ Failed to restart $CONTAINER_NAME" fi done -log "===== Auto-Update Completed =====" \ No newline at end of file +log "===== Auto-Restart Completed =====" \ No newline at end of file