Files
image-builder/scripts/image-builder.sh
2025-12-01 11:50:26 +00:00

854 lines
26 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
ROOT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
CONFIG_DIR="$ROOT_DIR/config"
GLOBAL_CONFIG="$CONFIG_DIR/global/config.json"
PROJECTS_DIR="./projects"
PROJECT_CONFIG_DIR="./config/projects"
REGISTRY_CONFIG_DIR="$CONFIG_DIR/registries"
LOGS_DIR="$ROOT_DIR/logs"
ensure_dirs() {
mkdir -p "$CONFIG_DIR/global" "$CONFIG_DIR/projects" "$CONFIG_DIR/registries" "$ROOT_DIR/projects" "$LOGS_DIR"
if [[ ! -f "$GLOBAL_CONFIG" ]]; then
cat >"$GLOBAL_CONFIG" <<EOF
{
"editor": "nano"
}
EOF
fi
}
ensure_prereqs() {
local bins=("git" "jq" "dialog")
for b in "${bins[@]}"; do
if ! command -v "$b" >/dev/null 2>&1; then
echo "[WARN] Benoetigtes Programm fehlt: $b"
fi
done
}
load_global_config() {
if [[ -f "$GLOBAL_CONFIG" ]]; then
EDITOR_CMD=$(jq -r '.editor // "nano"' "$GLOBAL_CONFIG")
else
EDITOR_CMD="nano"
fi
}
ensure_dirs
load_global_config
# -------------------------
# Konfigurations-Menue
# -------------------------
config_menu() {
while true; do
local builder_icon
builder_icon=$(get_builder_status_icon)
choice=$(dialog --title "Konfiguration" --menu "Bitte waehlen:" 20 70 10 \
1 "Abhaengigkeiten installieren (docker, git, jq, dialog)" \
2 "Buildx-Builder einrichten | Status: $builder_icon" \
3 "Globalen Editor setzen" \
4 "Standard-Registry setzen" \
0 "Zurueck" \
3>&1 1>&2 2>&3) || return
case $choice in
1) install_dependencies;;
2) install_docker_and_buildx;;
3) set_editor;;
4) set_default_registry ;;
0) return;;
esac
done
}
# BuildX-Builder Status Icon
get_builder_status_icon() {
if docker buildx ls 2>/dev/null | grep -q "multiarch-builder"; then
if docker buildx ls | grep "multiarch-builder" | grep -q "running"; then
echo "[OK]"
else
echo "[WARN]"
fi
else
echo "[ERR]"
fi
}
# BuildX-Builder-Status abrufen
get_builder_status() {
if docker buildx ls 2>/dev/null | grep -q "multiarch-builder"; then
if docker buildx ls | grep "multiarch-builder" | grep -q "running"; then
echo "[laeuft]"
else
echo "[vorhanden, aber gestoppt]"
fi
else
echo "[nicht eingerichtet]"
fi
}
# Abhaengigkeiten installieren
install_dependencies() {
dialog --msgbox "Installation von Abhaengigkeiten (Beispiel fuer Debian/Ubuntu). Bitte root-Rechte eingeben." 10 70
clear
sudo apt update
sudo apt install -y docker.io git jq dialog micro
}
# BuildX-Builder einrichten
install_docker_and_buildx() {
# Pruefen ob Docker installiert ist
if ! command -v docker >/dev/null 2>&1; then
dialog --msgbox "Docker ist nicht installiert! Bitte zuerst ueber 'Abhaengigkeiten installieren' einrichten." 10 70
return
fi
# Pruefen ob Buildx verfuegbar ist
if ! docker buildx version >/dev/null 2>&1; then
dialog --msgbox "Docker Buildx-Plugin fehlt! Installiere Buildx..." 10 70
clear
mkdir -p ~/.docker/cli-plugins
BUILDX_URL="https://github.com/docker/buildx/releases/latest/download/buildx-$(uname -s)-$(uname -m)"
curl -sSL "$BUILDX_URL" -o ~/.docker/cli-plugins/docker-buildx
chmod +x ~/.docker/cli-plugins/docker-buildx
fi
# Pruefen, ob multiarch-builder existiert
if ! docker buildx ls | grep -q "multiarch-builder"; then
dialog --msgbox "Richte Buildx-Builder 'multiarch-builder' ein..." 10 70
clear
docker run --rm --privileged tonistiigi/binfmt --install all
docker buildx create --name multiarch-builder --use
docker buildx inspect --bootstrap >/dev/null 2>&1
fi
# Finalpruefung: existiert Builder jetzt wirklich?
if docker buildx ls | grep -q "multiarch-builder"; then
dialog --msgbox "Buildx-Builder 'multiarch-builder' erfolgreich eingerichtet." 10 70
else
dialog --msgbox "Fehler: Buildx-Builder konnte nicht eingerichtet werden!" 10 70
fi
}
# Standard Editor setzen
set_editor() {
# editor -> paketname
declare -A editor_pkg
editor_pkg=( ["nano"]="nano" ["vim"]="vim" ["micro"]="micro" ["nvim"]="neovim" )
local options=()
declare -A editor_status
# Pruefe, ob Editor installiert ist
for cmd in "${!editor_pkg[@]}"; do
if command -v "$cmd" >/dev/null 2>&1; then
editor_status[$cmd]="(installiert)"
else
editor_status[$cmd]="(nicht installiert)"
fi
options+=("$cmd" "${editor_status[$cmd]}")
done
while true; do
choice=$(dialog --title "Editor waehlen" --menu "Bitte Editor auswaehlen:" 20 70 10 "${options[@]}" 3>&1 1>&2 2>&3) || return
if command -v "$choice" >/dev/null 2>&1; then
jq --arg ed "$choice" '.editor = $ed' "$GLOBAL_CONFIG" >"$GLOBAL_CONFIG.tmp" && mv "$GLOBAL_CONFIG.tmp" "$GLOBAL_CONFIG"
dialog --msgbox "Editor wurde auf '$choice' gesetzt." 10 60
return
else
install_choice=$(dialog --title "Editor nicht installiert" \
--yesno "Der Editor '$choice' ist nicht installiert. Moechten Sie ihn jetzt installieren?" 10 60 3>&1 1>&2 2>&3)
if [[ $? -eq 0 ]]; then
dialog --msgbox "Installation von ${editor_pkg[$choice]}..." 8 50
clear
sudo apt update
sudo apt install -y "${editor_pkg[$choice]}"
if command -v "$choice" >/dev/null 2>&1; then
# Speziell fuer nano: Syntax-Highlighting installieren
if [[ "$choice" == "nano" ]]; then
NANO_DIR="$HOME/.nano"
NANORC="$HOME/.nanorc"
if [[ -d "$NANO_DIR/.git" ]]; then
git -C "$NANO_DIR" pull --quiet
else
git clone https://github.com/scopatz/nanorc.git "$NANO_DIR"
fi
if ! grep -q "include $NANO_DIR" "$NANORC" 2>/dev/null; then
{
echo "## nano syntax highlighting"
echo "include $NANO_DIR/*.nanorc"
} >> "$NANORC"
fi
fi
dialog --msgbox "$choice erfolgreich installiert." 8 50
jq --arg ed "$choice" '.editor = $ed' "$GLOBAL_CONFIG" >"$GLOBAL_CONFIG.tmp" && mv "$GLOBAL_CONFIG.tmp" "$GLOBAL_CONFIG"
return
else
dialog --msgbox "Fehler: $choice konnte nicht installiert werden." 8 50
fi
fi
fi
done
}
# Standard-Registry setzen
set_default_registry() {
# Alle verfuegbaren Registries auslesen
local registries
registries=$(ls "$REGISTRY_CONFIG_DIR" 2>/dev/null)
[ -z "$registries" ] && { dialog --msgbox "Keine Registries verfuegbar. Bitte zuerst eine Registry erstellen." 10 60; return; }
# Menue-Liste vorbereiten
local menu_list=()
for r in $registries; do
menu_list+=("$r" "$r")
done
# Aktuelle Default-Registry auslesen
local current
current=$(jq -r '.default_registry // ""' "$GLOBAL_CONFIG")
# Auswahl per dialog
local choice
choice=$(dialog --title "Standard-Registry waehlen" \
--menu "Bitte Standard-Registry auswaehlen:" 20 70 10 \
"${menu_list[@]}" 3>&1 1>&2 2>&3) || return
# Speichern in global/config.json
jq --arg reg "$choice" '.default_registry = $reg' "$GLOBAL_CONFIG" >"$GLOBAL_CONFIG.tmp" && mv "$GLOBAL_CONFIG.tmp" "$GLOBAL_CONFIG"
dialog --msgbox "Standard-Registry auf '$choice' gesetzt." 10 60
}
# Standard-Registry auslesen
get_default_registry() {
if [[ -f "$GLOBAL_CONFIG" ]]; then
jq -r '.default_registry // ""' "$GLOBAL_CONFIG"
else
echo ""
fi
}
# ===============================
# Projektverwaltung
# ===============================
# Hauptmenue Projektverwaltung
project_menu() {
while true; do
choice=$(dialog --title "Projektverwaltung" --menu "Bitte waehlen:" 20 70 10 \
1 "Projekt erstellen" \
2 "Projekt loeschen" \
3 "Projekt bearbeiten" \
0 "Zurueck" \
3>&1 1>&2 2>&3) || return
case $choice in
1) create_project ;;
2) delete_project ;;
3) edit_project ;;
0) return ;;
esac
done
}
# Neues Projekt anlegen
create_project() {
project_name=$(dialog --inputbox "Projektname eingeben:" 10 60 3>&1 1>&2 2>&3) || return
[ -z "$project_name" ] && return
mkdir -p "$PROJECT_CONFIG_DIR/$project_name" "$PROJECTS_DIR/$project_name"
# Standardwerte setzen
default_registry=$(get_default_registry)
# Default Config-File
cat > "$PROJECT_CONFIG_DIR/$project_name/config-file" <<EOF
# Projektkonfiguration fuer $project_name
registry=$default_registry
image_name=$project_name
architectures=amd64
push=yes
version=1.0
latest=yes
auto_increment=no
git_repo=
EOF
# Dockerfile oder Git-Repo
if dialog --yesno "Soll ein Git-Repo geklont werden?" 10 60; then
repo_url=$(dialog --inputbox "Git-Repository-URL eingeben:" 10 60 3>&1 1>&2 2>&3) || return
if git clone "$repo_url" "$PROJECTS_DIR/$project_name"; then
sed -i "s|^git_repo=.*|git_repo=$repo_url|" "$PROJECT_CONFIG_DIR/$project_name/config-file"
else
dialog --msgbox "Fehler beim Klonen des Git-Repos!" 10 60
fi
else
echo -e "FROM debian:bookworm-slim\nCMD echo 'Hello from $project_name'" > "$PROJECTS_DIR/$project_name/Dockerfile"
fi
dialog --msgbox "Projekt $project_name wurde erstellt." 10 60
}
# Projekt loeschen
delete_project() {
projects=$(ls "$PROJECT_CONFIG_DIR" 2>/dev/null)
[ -z "$projects" ] && { dialog --msgbox "Keine Projekte vorhanden." 10 60; return; }
menu_list=()
for p in $projects; do
menu_list+=("$p" "")
done
project=$(dialog --menu "Projekt zum Loeschen auswaehlen:" 20 60 10 "${menu_list[@]}" 3>&1 1>&2 2>&3) || return
if dialog --yesno "Projekt $project wirklich loeschen?" 10 60; then
rm -rf "$PROJECT_CONFIG_DIR/$project" "$PROJECTS_DIR/$project"
dialog --msgbox "Projekt $project wurde geloescht." 10 60
fi
}
# Projekt bearbeiten
edit_project() {
projects=$(ls "$PROJECT_CONFIG_DIR" 2>/dev/null)
[ -z "$projects" ] && { dialog --msgbox "Keine Projekte vorhanden." 10 60; return; }
# Projekte fuer Menue vorbereiten (1 Spalte)
menu_list=()
for p in $projects; do
menu_list+=("$p" "")
done
project=$(dialog --menu "Projekt auswaehlen:" 20 60 10 "${menu_list[@]}" 3>&1 1>&2 2>&3) || return
local config_file="$PROJECT_CONFIG_DIR/$project/config-file"
local project_dir="$PROJECTS_DIR/$project"
while true; do
repo_url=$(grep "^git_repo=" "$config_file" | cut -d= -f2)
menu_items=(
1 "Registries auswaehlen"
2 "Architekturen auswaehlen"
3 "Push aktivieren/deaktivieren"
4 "Latest-Tag setzen/entfernen"
5 "Version/Subversion aendern"
6 "Automatische Subversion"
7 "Projektdateien bearbeiten"
8 "Image bauen"
)
[ -n "$repo_url" ] && menu_items+=("9" "Git Pull")
menu_items+=("0" "Zurueck")
choice=$(dialog --title "Projekt bearbeiten: $project" \
--menu "Bitte waehlen:" 20 70 12 \
"${menu_items[@]}" \
3>&1 1>&2 2>&3) || return
case $choice in
1) edit_project_registries "$config_file" ;;
2) edit_project_architectures "$config_file" ;;
3) edit_project_push "$config_file" ;;
4) edit_project_latest "$config_file" ;;
5) change_project_setting "$config_file" "version" ;;
6) edit_project_auto_subversion "$config_file" ;;
7) edit_project_files "$project" ;;
8) build_image "$project" ;;
9) gitpull_project "$project";;
0) return ;;
esac
done
}
# Registry fuer Projekt auswaehlen
edit_project_registries() {
local config_file=$1
# aktuelle registries im Projekt auslesen
local current=$(grep "^registry=" "$config_file" 2>/dev/null | cut -d= -f2 | tr ',' ' ')
# alle registries aus Registry-Verwaltung
local all_registries
all_registries=$(ls "$REGISTRY_CONFIG_DIR" 2>/dev/null)
[ -z "$all_registries" ] && { dialog --msgbox "Keine Registries verfuegbar. Bitte zuerst eine Registry erstellen." 10 60; return; }
# Default-Registry aus globaler Config
local default_registry
default_registry=$(get_default_registry)
# menu_list fuer dialog vorbereiten
local menu_list=()
for r in $all_registries; do
if [[ " $current " =~ " $r " ]]; then
menu_list+=("$r" "$r" ON)
elif [[ -z "$current" && "$r" == "$default_registry" ]]; then
# Projekt hat noch keine registries, Default wird ON
menu_list+=("$r" "$r" ON)
else
menu_list+=("$r" "$r" OFF)
fi
done
# dialog checklist
local selected
selected=$(dialog --title "Registries auswaehlen" --checklist "Mehrere auswaehlen (SPACE zum markieren)" 20 70 10 \
"${menu_list[@]}" 3>&1 1>&2 2>&3) || return
# dialog gibt die Auswahl in Anfuehrungszeichen zurueck, diese entfernen und Komma getrennt speichern
selected=$(echo $selected | tr -d '"' | tr ' ' ',')
# In Projekt-Config speichern
sed -i "s|^registry=.*|registry=$selected|" "$config_file"
}
# Schluessel im Config-File aendern
change_project_setting() {
local config_file=$1
local key=$2
local current=$(grep "^$key=" "$config_file" | cut -d= -f2)
new_value=$(dialog --inputbox "$key aendern (aktuell: $current)" 10 60 "$current" 3>&1 1>&2 2>&3) || return
sed -i "s|^$key=.*|$key=$new_value|" "$config_file"
}
# Aktuellen Editor aus global/config.json holen
get_editor_cmd() {
if [[ -f "$GLOBAL_CONFIG" ]]; then
jq -r '.editor // "nano"' "$GLOBAL_CONFIG"
else
echo "nano"
fi
}
# Projektdateien bearbeiten (Dateiauswahl + Editor)
edit_project_files() {
local project=$1
local project_dir="$PROJECTS_DIR/$project"
# Pruefen ob Projektordner existiert
if [[ ! -d "$project_dir" ]]; then
dialog --msgbox "Projektordner nicht gefunden." 10 60
return
fi
# Editor aus globaler Config holen
local editor_cmd
editor_cmd=$(get_editor_cmd)
# dialog-Dateiauswahl
local file
file=$(dialog --title "Datei auswaehlen" --fselect "$project_dir/" 20 70 3>&1 1>&2 2>&3) || return
[[ -z "$file" ]] && return
# Datei im Editor oeffnen
"$editor_cmd" "$file"
}
edit_project_architectures() {
local config_file=$1
local current=$(grep "^architectures=" "$config_file" | cut -d= -f2 | tr ',' ' ')
local all_archs=("amd64" "386" "arm64/v8" "arm/v7" "arm/v6" "arm/v5")
menu_list=()
for a in "${all_archs[@]}"; do
if [[ " $current " =~ " $a " ]]; then
menu_list+=("$a" "$a" ON)
else
menu_list+=("$a" "$a" OFF)
fi
done
selected=$(dialog --title "Architekturen auswaehlen" --checklist "Mehrere auswaehlen" 20 70 10 "${menu_list[@]}" 3>&1 1>&2 2>&3) || return
selected=$(echo $selected | tr -d '"')
sed -i "s|^architectures=.*|architectures=$selected|" "$config_file"
}
edit_project_push() {
local config_file=$1
local current=$(grep "^push=" "$config_file" | cut -d= -f2)
local value="OFF"
[ "$current" == "yes" ] && value="ON"
selected=$(dialog --title "Push aktivieren?" --checklist "Push auswaehlen" 10 50 1 push "Push aktivieren" $value 3>&1 1>&2 2>&3) || return
[[ $selected == *push* ]] && val="yes" || val="no"
sed -i "s|^push=.*|push=$val|" "$config_file"
}
edit_project_latest() {
local config_file=$1
local current=$(grep "^latest=" "$config_file" | cut -d= -f2)
local value="OFF"
[ "$current" == "yes" ] && value="ON"
selected=$(dialog --title "Latest-Tag setzen?" --checklist "Latest auswaehlen" 10 50 1 latest "Latest setzen" $value 3>&1 1>&2 2>&3) || return
[[ $selected == *latest* ]] && val="yes" || val="no"
sed -i "s|^latest=.*|latest=$val|" "$config_file"
}
edit_project_auto_subversion() {
local config_file=$1
local current=$(grep "^auto_subversion=" "$config_file" | cut -d= -f2)
local value="OFF"
[ "$current" == "yes" ] && value="ON"
selected=$(dialog --title "Subversion automatisch erhoehen?" --checklist "Auto Subversion auswaehlen" 10 50 1 auto "Automatisch erhoehen" $value 3>&1 1>&2 2>&3) || return
[[ $selected == *auto* ]] && val="yes" || val="no"
# falls auto_subversion key nicht existiert, hinzufuegen
if grep -q "^auto_subversion=" "$config_file"; then
sed -i "s|^auto_subversion=.*|auto_subversion=$val|" "$config_file"
else
echo "auto_subversion=$val" >> "$config_file"
fi
}
gitpull_project() {
local project=$1
local project_dir="$PROJECTS_DIR/$project"
local config_file="$PROJECT_CONFIG_DIR/$project/config-file"
# Prüfen ob git_repo gesetzt ist
local repo_url
repo_url=$(grep "^git_repo=" "$config_file" | cut -d= -f2)
if [[ -z "$repo_url" ]]; then
dialog --msgbox "Dieses Projekt ist nicht mit einem Git-Repository verknüpft." 10 60
return
fi
# Prüfen ob Ordner existiert
if [[ ! -d "$project_dir/.git" ]]; then
dialog --msgbox "Der Projektordner enthaelt kein Git-Repository.\nVielleicht wurde er gelöscht oder bearbeitet?" 12 60
return
fi
# Git Pull durchführen
{
echo "Starte git pull..."
git -C "$project_dir" pull
} >"$LOGS_DIR/gitpull_${project}_$(date +"%Y%m%d_%H%M%S").log" 2>&1
if [[ $? -eq 0 ]]; then
dialog --msgbox "Git Pull erfolgreich abgeschlossen." 10 60
else
dialog --msgbox "FEHLER beim Git Pull!\nSiehe Log-Datei unter logs/." 12 60
fi
}
# Hauptmenue - Registryverwaltung
registry_menu() {
while true; do
choice=$(dialog --title "Registry-Verwaltung" --menu "Bitte waehlen:" 20 70 10 \
1 "Registry erstellen" \
2 "Registry loeschen" \
3 "Registry bearbeiten" \
0 "Zurueck" \
3>&1 1>&2 2>&3) || return
case $choice in
1) create_registry ;;
2) delete_registry ;;
3) edit_registry ;;
0) return ;;
esac
done
}
# Registry anlegen
create_registry() {
local name url username password
name=$(dialog --inputbox "Name der Registry (z.B. docker.io):" 10 60 3>&1 1>&2 2>&3) || return
[ -z "$name" ] && return
url=$(dialog --inputbox "URL der Registry:" 10 60 3>&1 1>&2 2>&3) || return
username=$(dialog --inputbox "Benutzername:" 10 60 3>&1 1>&2 2>&3) || return
password=$(dialog --passwordbox "Passwort:" 10 60 3>&1 1>&2 2>&3) || return
mkdir -p "$REGISTRY_CONFIG_DIR/$name"
cat > "$REGISTRY_CONFIG_DIR/$name/config-file" <<EOF
url=$url
username=$username
password=$password
EOF
dialog --msgbox "Registry '$name' wurde erstellt." 10 60
}
# Registry loeschen
delete_registry() {
local registries
registries=$(ls "$REGISTRY_CONFIG_DIR" 2>/dev/null)
[ -z "$registries" ] && { dialog --msgbox "Keine Registries vorhanden." 10 60; return; }
menu_list=()
for r in $registries; do
menu_list+=("$r" "")
done
local reg
reg=$(dialog --menu "Registry zum Loeschen auswaehlen:" 20 60 10 "${menu_list[@]}" 3>&1 1>&2 2>&3) || return
if dialog --yesno "Registry $reg wirklich loeschen?" 10 60; then
rm -rf "$REGISTRY_CONFIG_DIR/$reg"
dialog --msgbox "Registry $reg wurde geloescht." 10 60
fi
}
# Registry bearbeiten
edit_registry() {
local registries
registries=$(ls "$REGISTRY_CONFIG_DIR" 2>/dev/null)
[ -z "$registries" ] && { dialog --msgbox "Keine Registries vorhanden." 10 60; return; }
menu_list=()
for r in $registries; do
menu_list+=("$r" "")
done
local reg
reg=$(dialog --menu "Registry zum Bearbeiten auswaehlen:" 20 60 10 "${menu_list[@]}" 3>&1 1>&2 2>&3) || return
local config_file="$REGISTRY_CONFIG_DIR/$reg/config-file"
local url username password
url=$(grep "^url=" "$config_file" | cut -d= -f2)
username=$(grep "^username=" "$config_file" | cut -d= -f2)
password=$(grep "^password=" "$config_file" | cut -d= -f2)
# Eingaben aendern
url=$(dialog --inputbox "URL:" 10 60 "$url" 3>&1 1>&2 2>&3) || return
username=$(dialog --inputbox "Benutzername:" 10 60 "$username" 3>&1 1>&2 2>&3) || return
password=$(dialog --passwordbox "Passwort:" 10 60 "$password" 3>&1 1>&2 2>&3) || return
cat > "$config_file" <<EOF
url=$url
username=$username
password=$password
EOF
dialog --msgbox "Registry '$reg' wurde aktualisiert." 10 60
}
# Projektparameter laden
load_project_config() {
local config_file="$1"
registry=$(grep "^registry=" "$config_file" | cut -d= -f2)
image_name=$(grep "^image_name=" "$config_file" | cut -d= -f2)
architectures=$(grep "^architectures=" "$config_file" | cut -d= -f2)
push=$(grep "^push=" "$config_file" | cut -d= -f2)
version=$(grep "^version=" "$config_file" | cut -d= -f2)
latest=$(grep "^latest=" "$config_file" | cut -d= -f2)
git_repo=$(grep "^git_repo=" "$config_file" | cut -d= -f2)
auto_subversion=$(grep "^auto_subversion=" "$config_file" | cut -d= -f2 || echo "no")
}
# Image bauen
build_image() {
local project=$1
local config_file="$PROJECT_CONFIG_DIR/$project/config-file"
load_project_config "$config_file"
local timestamp logfile
timestamp=$(date +"%Y%m%d_%H%M%S")
logfile="$LOGS_DIR/${project}_${timestamp}.log"
clear
{
echo "==== Build gestartet: $(date) ===="
echo "Projekt: $project"
echo "Registry: $registry"
echo "Image: $image_name"
echo "Architekturen: $architectures"
echo "Push: $push"
echo "Version: $version"
echo "Latest-Tag: $latest"
echo "===================================="
} | tee "$logfile"
# Architekturen in Buildx-Format
local platforms
platforms=$(echo "$architectures" | xargs -n1 | sed 's|^|linux/|' | paste -sd, -)
# Registry-URLs und Login
declare -A registry_url
IFS=',' read -ra registry_list <<< "$registry"
for r in "${registry_list[@]}"; do
r=$(echo "$r" | xargs) # führende/folgende Leerzeichen entfernen
config_path="$REGISTRY_CONFIG_DIR/$r/config-file"
if [[ ! -f "$config_path" ]]; then
echo "FEHLER: Registry-Konfigurationsdatei nicht gefunden: $config_path" | tee -a "$logfile"
continue
fi
url=$(grep "^url=" "$config_path" | cut -d= -f2)
username=$(grep "^username=" "$config_path" | cut -d= -f2)
password=$(grep "^password=" "$config_path" | cut -d= -f2)
registry_url["$r"]="$url"
if [[ "$push" == "yes" ]]; then
echo "Logging in to $url..."
echo "$password" | docker login "$url" -u "$username" --password-stdin 2>&1 | tee -a "$logfile"
fi
done
# Tags aufbauen
tags=()
for r in "${registry_list[@]}"; do
r=$(echo "$r" | xargs)
url="${registry_url[$r]}"
if [[ -z "$url" ]]; then
echo "WARNUNG: Registry $r konnte nicht gefunden werden, Tag wird uebersprungen." | tee -a "$logfile"
continue
fi
tags+=("-t" "${url}/${image_name}:${version}")
[[ "$latest" == "yes" ]] && tags+=("-t" "${url}/${image_name}:latest")
done
# Push-Flag
local push_flag=""
[[ "$push" == "yes" ]] && push_flag="--push"
# Build ausfuehren und Exit-Code speichern
if docker buildx build --platform "$platforms" "${tags[@]}" "$PROJECTS_DIR/$project" $push_flag 2>&1 | tee -a "$logfile"; then
build_status="erfolgreich"
else
build_status="FEHLGESCHLAGEN"
fi
echo "==== Build beendet: $(date) - Status: $build_status ====" | tee -a "$logfile"
# Auto-Subversion
if [[ "$auto_subversion" == "yes" ]]; then
local major minor
major=$(echo "$version" | cut -d. -f1)
minor=$(echo "$version" | cut -d. -f2)
minor=$((minor + 1))
sed -i "s/^version=.*/version=${major}.${minor}/" "$config_file"
echo "Subversion automatisch auf ${major}.${minor} erhoeht." | tee -a "$logfile"
fi
# Abfrage: Log anzeigen?
if dialog --yesno "Build $build_status. Log jetzt anzeigen?" 10 60; then
local editor_cmd
editor_cmd=$(get_editor_cmd)
$editor_cmd "$logfile"
fi
}
# Logs ansehen (optional gefiltert nach Projekt)
view_logs() {
#ensure_logs_dir
local project_filter="${1:-}" # wenn Projekt uebergeben, nur dessen Logs anzeigen
if [ -n "$project_filter" ]; then
logs=$(ls -1t "$LOGS_DIR/${project_filter}"_*.log 2>/dev/null | sed "s|$LOGS_DIR/||")
else
logs=$(ls -1t "$LOGS_DIR"/*.log 2>/dev/null | sed "s|$LOGS_DIR/||")
fi
[ -z "$logs" ] && { dialog --msgbox "Keine Logs vorhanden." 10 60; return; }
menu_list=()
for l in $logs; do
menu_list+=("$l" "")
done
log_file=$(dialog --menu "Log auswaehlen:" 20 70 12 "${menu_list[@]}" 3>&1 1>&2 2>&3) || return
# Editor aus globaler Config lesen
local editor=$(jq -r '.editor' "$GLOBAL_CONFIG")
${editor:-nano} "$LOGS_DIR/$log_file"
}
# Projektuebersicht anzeigen
project_overview() {
local project=$1
local config_file="$PROJECT_CONFIG_DIR/$project/config-file"
load_project_config "$config_file"
while true; do
choice=$(dialog --title "Projektuebersicht: $project" --menu "Parameter:" 20 70 10 \
"1" "Registry: $registry" \
"2" "Image-Name: $image_name" \
"3" "Architekturen: $architectures" \
"4" "Push: $push" \
"5" "Version: $version" \
"6" "Latest-Tag: $latest" \
"7" "Git-Repo: $git_repo" \
"B" "Bauen" \
"E" "Bearbeiten" \
"P" "Projektauswahl" \
"L" "Logs ansehen" \
"Z" "Zurueck" \
3>&1 1>&2 2>&3) || return
case $choice in
B) build_image "$project" ;;
E) edit_project "$project" ;; # Reuse aus Projektverwaltung
P) select_project_for_build ;;
L) view_logs "$project" ;;
Z) return ;;
esac
done
}
# Projektauswahl fuer Image-Bauen
select_project_for_build() {
local projects=($(ls "$PROJECT_CONFIG_DIR"))
[ ${#projects[@]} -eq 0 ] && { dialog --msgbox "Keine Projekte vorhanden." 10 60; return; }
menu_list=()
for p in "${projects[@]}"; do
menu_list+=("$p" "Projekt")
done
project=$(dialog --title "Projekt auswaehlen" --menu "Bitte Projekt auswaehlen:" 20 60 10 "${menu_list[@]}" 3>&1 1>&2 2>&3) || return
project_overview "$project"
}
# Menuepunkt im Hauptmenue
image_build_menu() {
select_project_for_build
}
# -------------------------
# Hauptmenue
# -------------------------
main_menu() {
while true; do
choice=$(dialog --title "Image Builder" --menu "Bitte waehlen:" 20 70 10 \
1 "Konfiguration" \
2 "Projektverwaltung" \
3 "Registryverwaltung" \
4 "Image-Bauen" \
5 "Logs ansehen" \
0 "Beenden" \
3>&1 1>&2 2>&3) || exit 0
case $choice in
1) config_menu;;
2) project_menu;;
3) registry_menu ;;
4) image_build_menu;;
5) view_logs ;; # <-- hier globale Logs
0) exit 0;;
esac
done
}
# -------------------------
# Main
# -------------------------
ensure_dirs
ensure_prereqs
main_menu