Files
image-builder/start.sh
2025-09-26 12:51:00 +02:00

116 lines
3.7 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
# Benötigte Kommandos (auch als Paketnamen verwendet)
REQUIRED_CMDS=(jq whiptail dialog sudo)
# Prüft, ob ein Kommando existiert
check_command() {
command -v "$1" >/dev/null 2>&1
}
# Sammle fehlende Pakete
MISSING_PKGS=()
for cmd in "${REQUIRED_CMDS[@]}"; do
if ! check_command "$cmd"; then
MISSING_PKGS+=("$cmd")
fi
done
# Helper: führe Befehl als root aus (wenn möglich)
run_as_root() {
if [ "$(id -u)" -eq 0 ]; then
"$@"
elif command -v sudo >/dev/null 2>&1; then
sudo "$@"
elif command -v su >/dev/null 2>&1; then
# su -c nimmt eine String-Command; wir bauen sie aus den args
su -c "$*"
else
echo "Fehler: Keine Möglichkeit, Befehle als root auszuführen (weder sudo noch su)."
return 2
fi
}
if [ ${#MISSING_PKGS[@]} -ne 0 ]; then
echo "❌ Fehlende Pakete: ${MISSING_PKGS[*]}"
# Interaktive Abfrage (CLI only)
while true; do
read -r -p "Möchtest du diese jetzt automatisch installieren? [Y/n] " REPLY
REPLY=${REPLY:-Y}
case "$REPLY" in
[Yy]|[Yy][Ee][Ss] )
# Paketmanager erkennen
if command -v apt-get >/dev/null 2>&1; then
INSTALL_CMD=(apt-get install -y)
PREPARE_CMD=(apt-get update)
elif command -v dnf >/dev/null 2>&1; then
INSTALL_CMD=(dnf install -y)
elif command -v yum >/dev/null 2>&1; then
INSTALL_CMD=(yum install -y)
elif command -v pacman >/dev/null 2>&1; then
INSTALL_CMD=(pacman -S --noconfirm)
elif command -v zypper >/dev/null 2>&1; then
INSTALL_CMD=(zypper install -y)
elif command -v apk >/dev/null 2>&1; then
INSTALL_CMD=(apk add --no-cache)
elif command -v brew >/dev/null 2>&1; then
INSTALL_CMD=(brew install)
else
echo "Kein unterstützter Paketmanager gefunden. Bitte installiere die Pakete manuell: ${MISSING_PKGS[*]}"
exit 1
fi
# Falls apt-get: update vorher ausführen
if [ "${INSTALL_CMD[0]}" = "apt-get" ]; then
echo "Führe apt-get update aus..."
if ! run_as_root "${PREPARE_CMD[@]}"; then
echo "Konnte 'apt-get update' nicht als root ausführen."
exit 1
fi
fi
echo "Starte Installation: ${MISSING_PKGS[*]}"
if ! run_as_root "${INSTALL_CMD[@]}" "${MISSING_PKGS[@]}"; then
echo "Installation fehlgeschlagen. Bitte installiere die Pakete manuell oder führe das Script als root aus."
exit 1
fi
# Erneut prüfen
STILL_MISSING=()
for cmd in "${MISSING_PKGS[@]}"; do
if ! check_command "$cmd"; then
STILL_MISSING+=("$cmd")
fi
done
if [ ${#STILL_MISSING[@]} -ne 0 ]; then
echo "Die folgenden Pakete konnten nicht installiert werden: ${STILL_MISSING[*]}"
echo "Bitte prüfe manuell oder installiere sie und starte das Script erneut."
exit 1
fi
echo "✅ Fehlende Pakete wurden installiert."
break
;;
[Nn]|[Nn][Oo] )
echo "Abbruch durch Benutzer. Bitte installiere die benötigten Pakete manuell: ${MISSING_PKGS[*]}"
exit 1
;;
* )
echo "Bitte mit Y oder N antworten."
;;
esac
done
else
echo "✅ Alle Voraussetzungen sind bereits erfüllt."
fi
# Am Ende image-builder.sh starten (das Script soll Docker/Buildx selbst behandeln)
if [ ! -x ./image-builder.sh ]; then
echo "Warnung: ./image-builder.sh nicht gefunden oder nicht ausführbar. Stelle sicher, dass image-builder.sh vorhanden ist und ausführbar ist."
fi
echo "🚀 Starte image-builder.sh ..."
exec ./scripts/image-builder.sh "$@"