#!/usr/bin/env bash set -u BASE_DIR="/srv/manifests" # Compose-Binary ermitteln if docker compose version >/dev/null 2>&1; then COMPOSE() { docker compose "$@"; } elif command -v docker-compose >/dev/null 2>&1; then COMPOSE() { docker-compose "$@"; } else echo "ERROR: Weder 'docker compose' noch 'docker-compose' gefunden." >&2 exit 1 fi compose_files=(docker-compose.yml docker-compose.yaml compose.yml compose.yaml) cd "$BASE_DIR" || { echo "ERROR: Kann $BASE_DIR nicht betreten"; exit 1; } # Ordnerliste (optional: nur übergebene) if [ "$#" -gt 0 ]; then dirs=("$@") else mapfile -t dirs < <(find . -maxdepth 1 -mindepth 1 -type d -printf "%P\n" | LC_ALL=C sort) fi overall_rc=0 detect_strategy() { local dir="$1" local strategy="pull" # default # 1) Markerdateien if [ -f "$dir/.update-with-build" ]; then strategy="build" elif [ -f "$dir/.update-with-pull" ]; then strategy="pull" # 2) .env Variable elif [ -f "$dir/.env" ]; then # Nur einfache, robuste Extraktion local val val="$(grep -E '^\s*UPDATE_STRATEGY\s*=' "$dir/.env" | tail -n1 | cut -d= -f2 | tr -d '[:space:]' || true)" if [ "$val" = "build" ]; then strategy="build" elif [ "$val" = "pull" ]; then strategy="pull" fi fi echo "$strategy" } has_compose_file() { local dir="$1" for f in "${compose_files[@]}"; do [ -f "$dir/$f" ] && return 0 done return 1 } for d in "${dirs[@]}"; do [ -d "$d" ] || { echo "SKIP: $d ist kein Ordner"; continue; } if ! has_compose_file "$d"; then echo "SKIP: $d (keine Compose-Datei gefunden)" continue fi strategy="$(detect_strategy "$d")" case "$strategy" in build) echo "==== $d: up -d --build ====" if ! (cd "$d" && COMPOSE up -d --build); then echo "WARN: $d -> up -d --build fehlgeschlagen" overall_rc=1 continue fi ;; pull|*) echo "==== $d: pull ====" if ! (cd "$d" && COMPOSE pull); then echo "WARN: $d -> pull fehlgeschlagen" overall_rc=1 # trotzdem versuchen zu starten fi echo "==== $d: up -d ====" if ! (cd "$d" && COMPOSE up -d); then echo "WARN: $d -> up -d fehlgeschlagen" overall_rc=1 continue fi ;; esac echo "OK: $d aktualisiert" done exit "$overall_rc"