From 3bf49b66b626e2810d9cef45715be6841637edb8 Mon Sep 17 00:00:00 2001 From: pi-farm Date: Sat, 27 Sep 2025 20:14:25 +0200 Subject: [PATCH] add function build_image and view_logs --- scripts/image-builder.sh | 150 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 147 insertions(+), 3 deletions(-) diff --git a/scripts/image-builder.sh b/scripts/image-builder.sh index 84bd161..98bff40 100755 --- a/scripts/image-builder.sh +++ b/scripts/image-builder.sh @@ -8,9 +8,10 @@ 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" + mkdir -p "$CONFIG_DIR/global" "$CONFIG_DIR/projects" "$CONFIG_DIR/registries" "$ROOT_DIR/projects" "$LOGS_DIR" if [[ ! -f "$GLOBAL_CONFIG" ]]; then cat >"$GLOBAL_CONFIG" </dev/null 2>&1; then echo "[WARN] Benötigtes Programm fehlt: $b" @@ -316,7 +317,7 @@ edit_project() { projects=$(ls "$PROJECT_CONFIG_DIR" 2>/dev/null) [ -z "$projects" ] && { whiptail --msgbox "Keine Projekte vorhanden." 10 60; return; } - # Projekte für Menü vorbereiten (immer 2 Spalten) + # Projekte für Menü vorbereiten (1 Spalte) menu_list=() for p in $projects; do menu_list+=("$p" "") @@ -337,6 +338,7 @@ edit_project() { 5 "Version/Subversion ändern" 6 "Automatische Subversion" 7 "Projektdateien bearbeiten" + 9 "Image bauen" ) [ -n "$repo_url" ] && menu_items+=("8" "Git Pull") @@ -357,11 +359,13 @@ edit_project() { 6) edit_project_auto_subversion "$config_file" ;; 7) edit_project_files "$project" ;; 8) [ -n "$repo_url" ] && (cd "$project_dir" && git pull || whiptail --msgbox "Git Pull fehlgeschlagen." 10 60) ;; + 9) build_image "$project" ;; 0) return ;; esac done } + # Registry für Projekt auswählen edit_project_registries() { local config_file=$1 @@ -592,6 +596,144 @@ EOF whiptail --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) + auto_increment=$(grep "^auto_increment=" "$config_file" | cut -d= -f2 2>/dev/null || echo "no") + git_repo=$(grep "^git_repo=" "$config_file" | cut -d= -f2) +} + +# Image bauen +build_image() { + local project=$1 + local config_file="$PROJECT_CONFIG_DIR/$project/config-file" + local project_dir="$PROJECTS_DIR/$project" + + load_project_config "$config_file" + + local tags="-t $registry/$image_name:$version" + [ "$latest" = "yes" ] && tags="$tags -t $registry/$image_name:latest" + + local timestamp + timestamp=$(date +"%Y%m%d_%H%M%S") + local logfile="$LOGS_DIR/${project}_${timestamp}.log" + + whiptail --msgbox "Starte Build:\n\nProjekt: $project\nRegistry: $registry\nImage: $image_name\nArchitekturen: $architectures\nPush: $push\nVersion: $version\nTags: $tags\n\nBuild-Log: $logfile" 20 70 + + # Build ausführen und alles ins Log schreiben + { + echo "==== Build gestartet: $(date) ====" + echo "Projekt: $project" + echo "Registry: $registry" + echo "Image: $image_name" + echo "Architekturen: $architectures" + echo "Push: $push" + echo "Version: $version" + echo "Tags: $tags" + echo "====================================" + docker buildx build --platform "$architectures" $tags "$project_dir" $( [ "$push" = "yes" ] && echo "--push" ) + echo "==== Build abgeschlossen: $(date) ====" + } &> "$logfile" + + # Version hochzählen falls auto_increment aktiv + if [ "$auto_increment" = "yes" ]; then + base=$(echo "$version" | cut -d. -f1) + sub=$(echo "$version" | cut -d. -f2) + new_sub=$((sub+1)) + new_version="$base.$new_sub" + sed -i "s|^version=.*|version=$new_version|" "$config_file" + fi + + # Nachfrage, ob Log angezeigt werden soll + if whiptail --yesno "Build abgeschlossen!\n\nMöchten Sie die Logdatei jetzt ansehen?\n\n$logfile" 15 70; then + EDITOR_CMD=$(get_editor_cmd) + $EDITOR_CMD "$logfile" + else + whiptail --msgbox "Build abgeschlossen!\n\nLogdatei: $logfile" 12 70 + fi +} + +# Logs ansehen (optional gefiltert nach Projekt) +view_logs() { + ensure_logs_dir + + local project_filter="${1:-}" # wenn Projekt übergeben, 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" ] && { whiptail --msgbox "Keine Logs vorhanden." 10 60; return; } + + menu_list=() + for l in $logs; do + menu_list+=("$l" "") + done + + log_file=$(whiptail --menu "Log auswählen:" 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" +} + +# Projektübersicht anzeigen +project_overview() { + local project=$1 + local config_file="$PROJECT_CONFIG_DIR/$project/config-file" + load_project_config "$config_file" + + while true; do + choice=$(whiptail --title "Projektübersicht: $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" "Zurück" \ + 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 für Image-Bauen +select_project_for_build() { + local projects=($(ls "$PROJECT_CONFIG_DIR")) + [ ${#projects[@]} -eq 0 ] && { whiptail --msgbox "Keine Projekte vorhanden." 10 60; return; } + + menu_list=() + for p in "${projects[@]}"; do + menu_list+=("$p" "Projekt") + done + + project=$(whiptail --title "Projekt auswählen" --menu "Bitte Projekt auswählen:" 20 60 10 "${menu_list[@]}" 3>&1 1>&2 2>&3) || return + project_overview "$project" +} + +# Menüpunkt im Hauptmenü +image_build_menu() { + select_project_for_build +} # ------------------------- # Hauptmenü @@ -604,6 +746,7 @@ main_menu() { 3 "Registryverwaltung" \ 4 "Image-Bauen" \ 5 "Image-Verwaltung" \ + 6 "Logs ansehen" \ 0 "Beenden" \ 3>&1 1>&2 2>&3) || exit 0 @@ -613,6 +756,7 @@ main_menu() { 3) registry_menu ;; 4) whiptail --msgbox "Image-Bauen (noch nicht implementiert)" 10 70;; 5) whiptail --msgbox "Image-Verwaltung (noch nicht implementiert)" 10 70;; + 6) view_logs ;; # <-- hier globale Logs 0) exit 0;; esac done