diff --git a/scripts/image-builder.sh b/scripts/image-builder.sh index a4b8b89..197928c 100755 --- a/scripts/image-builder.sh +++ b/scripts/image-builder.sh @@ -5,8 +5,8 @@ IFS=$'\n\t' ROOT_DIR="$(cd "$(dirname "$0")/.." && pwd)" CONFIG_DIR="$ROOT_DIR/config" GLOBAL_CONFIG="$CONFIG_DIR/global/config.json" -PROJECTS_DIR="$ROOT_DIR/projects" -PROJECT_CONFIG_DIR="$CONFIG_DIR/projects" +PROJECTS_DIR="./projects" +PROJECT_CONFIG_DIR="./config/projects" REGISTRY_CONFIG_DIR="$CONFIG_DIR/registries" LOGS_DIR="$ROOT_DIR/logs" @@ -598,184 +598,82 @@ EOF # Projektparameter laden load_project_config() { - local config_file="$1" - if [[ -f "$config_file" ]]; then - echo "Lade Konfiguration aus $config_file" - image_name=$(grep "^image_name=" "$config_file" | cut -d= -f2) - dockerfile=$(grep "^dockerfile=" "$config_file" | cut -d= -f2) - context=$(grep "^context=" "$config_file" | cut -d= -f2) - version=$(grep "^version=" "$config_file" | cut -d= -f2) - architectures=$(grep "^architectures=" "$config_file" | cut -d= -f2) - registry=$(grep "^registry=" "$config_file" | cut -d= -f2) - push=$(grep "^push=" "$config_file" | cut -d= -f2) - latest=$(grep "^latest=" "$config_file" | cut -d= -f2) - auto_subversion=$(grep "^auto_subversion=" "$config_file" | cut -d= -f2) - - architectures="${architectures//,/ }" - architectures=$(echo "$architectures" | xargs) - - echo "Geladen: image=$image_name, version=$version, archs=$architectures, registry=$registry" - else - echo "Konfigurationsdatei $config_file nicht gefunden!" - exit 1 - fi + 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") } - - -# Gibt die URL einer Registry anhand des Config-Files zurück -get_registry_url() { - local reg=$1 - local reg_file="$REGISTRY_CONFIG_DIR/$reg/config-file" - if [[ ! -f "$reg_file" ]]; then - echo "" - return - fi - grep -E '^url=' "$reg_file" | cut -d'=' -f2- -} - -# Führt docker login für eine Registry anhand des Config-Files aus -registry_login() { - local reg=$1 - local reg_file="$REGISTRY_CONFIG_DIR/$reg/config-file" - - if [[ ! -f "$reg_file" ]]; then - echo "Registry-Konfiguration '$reg' fehlt!" - return 1 - fi - - local url username password - url=$(grep -E '^url=' "$reg_file" | cut -d'=' -f2-) - username=$(grep -E '^username=' "$reg_file" | cut -d'=' -f2-) - password=$(grep -E '^password=' "$reg_file" | cut -d'=' -f2-) - - if [[ -z "$url" || -z "$username" || -z "$password" ]]; then - echo "Ungültige Registry-Konfig für $reg (url/username/password fehlt)!" - return 1 - fi - - echo "$password" | docker login "$url" -u "$username" --password-stdin -} - -# Docker-Image bauen +# Image bauen build_image() { local project=$1 local config_file="$PROJECT_CONFIG_DIR/$project/config-file" - - # ---- Konfiguration laden ---- load_project_config "$config_file" - # Prüfen, ob Registry gesetzt ist - if [[ -z "$registry" ]]; then - echo "Keine Registry definiert! Build abgebrochen." - whiptail --msgbox "Keine Registry definiert für Projekt $project. Build abgebrochen." 10 60 - return 1 - fi - - # Architekturen prüfen - if [[ -z "$architectures" ]]; then - echo "Keine Architekturen definiert! Build abgebrochen." - whiptail --msgbox "Keine Architekturen definiert für Projekt $project. Build abgebrochen." 10 60 - return 1 - fi - - local timestamp logfile - timestamp=$(date +"%Y%m%d_%H%M%S") - logfile="$LOGS_DIR/${project}_${timestamp}.log" - - echo "==== Build gestartet: $(date) ====" | tee -a "$logfile" - echo "Projekt: $project" | tee -a "$logfile" - echo "Registry: $registry" | tee -a "$logfile" - echo "Image: $image_name" | tee -a "$logfile" - echo "Architekturen: $architectures" | tee -a "$logfile" - echo "Push: $push" | tee -a "$logfile" - echo "Version: $version" | tee -a "$logfile" - echo "Latest: $latest" | tee -a "$logfile" - echo "====================================" | tee -a "$logfile" - - # ---- Plattformen vorbereiten ---- - local platforms arch_array - read -r -a arch_array <<< "${architectures//,/ }" - - platforms="" - for arch in "${arch_array[@]}"; do - case "$arch" in - amd64) platforms+="linux/amd64," ;; - arm64) platforms+="linux/arm64," ;; - armhf) platforms+="linux/arm/v7," ;; - x86) platforms+="linux/386," ;; - *) echo "WARNUNG: Unbekannte Architektur '$arch' wird ignoriert." | tee -a "$logfile" ;; - esac - done - platforms="${platforms%,}" # letztes Komma entfernen - - if [[ -z "$platforms" ]]; then - echo "Keine gültigen Architekturen gefunden. Build abgebrochen." | tee -a "$logfile" - return 1 - fi - - echo "DEBUG: platforms=$platforms" | tee -a "$logfile" - - # ---- Tags für Registry vorbereiten ---- - IFS=',' read -ra reg_array <<< "$registry" - tags=() - for reg in "${reg_array[@]}"; do - reg=$(echo "$reg" | xargs) # trim - [[ -z "$reg" ]] && continue - - # Login falls Registry-Config existiert - local reg_config="$REGISTRY_CONFIG_DIR/$reg/config-file" - if [[ -f "$reg_config" ]]; then - local reg_user reg_pass - reg_user=$(grep "^username=" "$reg_config" | cut -d= -f2) - reg_pass=$(grep "^password=" "$reg_config" | cut -d= -f2) - if [[ -n "$reg_user" && -n "$reg_pass" ]]; then - echo "$reg_pass" | docker login "$reg" -u "$reg_user" --password-stdin 2>>"$logfile" || { - echo "WARNUNG: Login bei $reg fehlgeschlagen!" | tee -a "$logfile" - } - fi - fi - - tags+=("-t" "$reg/$image_name:$version") - [[ "$latest" == "yes" ]] && tags+=("-t" "$reg/$image_name:latest") + # Build-Parameter aus Config + local regs=() + IFS=',' read -r -a regs <<< "$registry" # Registries als Array + local tags=() + for r in "${regs[@]}"; do + [[ -n "$r" ]] && tags+=("-t $r/$image_name:$version") done - if [[ ${#tags[@]} -eq 0 ]]; then - echo "Keine Registry-Tags definiert. Build abgebrochen." | tee -a "$logfile" - return 1 - fi + local archs + archs=$(echo "$architectures" | sed 's/ /,/g') - # ---- Docker Build-Befehl ---- - local cmd - if [[ "$push" == "yes" ]]; then - cmd=(docker buildx build --platform "$platforms" "${tags[@]}" "$PROJECTS_DIR/$project" --push) - else - cmd=(docker buildx build --platform "$platforms" "${tags[@]}" "$PROJECTS_DIR/$project" --load) - fi + local push_flag="" + [[ "$push" == "yes" ]] && push_flag="--push" - echo "Build-Befehl: ${cmd[*]}" | tee -a "$logfile" + local logfile="$LOGS_DIR/${project}_$(date +"%Y%m%d_%H%M%S").log" - # ---- Build ausführen ---- - if "${cmd[@]}" >>"$logfile" 2>&1; then - echo "==== Build beendet: $(date) ====" | tee -a "$logfile" + echo "==== Build gestartet: $(date) ====" > "$logfile" + echo "Projekt: $project" >> "$logfile" + echo "Registries: ${registry}" >> "$logfile" + echo "Image: $image_name" >> "$logfile" + echo "Architekturen: $architectures" >> "$logfile" + echo "Push: $push" >> "$logfile" + echo "Version: $version" >> "$logfile" + echo "Latest: $latest" >> "$logfile" + echo "====================================" >> "$logfile" - # Auto-Subversion erhöhen - if [[ "$auto_subversion" == "yes" ]]; then - local major minor new_version - major=$(echo "$version" | cut -d. -f1) - minor=$(echo "$version" | cut -d. -f2) - minor=$((minor + 1)) - new_version="${major}.${minor}" - sed -i "s/^version=.*/version=$new_version/" "$config_file" - echo "Subversion automatisch auf $new_version erhöht." | tee -a "$logfile" + # Build starten + local build_cmd + build_cmd="docker buildx build --platform linux/$archs ${tags[*]} ./projects/$project $push_flag" + echo "$build_cmd" >> "$logfile" + + if $build_cmd >> "$logfile" 2>&1; then + echo "==== Build beendet: $(date) ====" >> "$logfile" + + # Latest-Tag setzen + if [[ "$latest" == "yes" ]]; then + for r in "${regs[@]}"; do + [[ -n "$r" ]] && docker tag "$r/$image_name:$version" "$r/$image_name:latest" >> "$logfile" 2>&1 + done + fi + + # Subversion erhöhen + if grep -q "^auto_subversion=yes" "$config_file"; then + local main_ver sub_ver + main_ver=$(echo "$version" | cut -d. -f1) + sub_ver=$(echo "$version" | cut -d. -f2) + sub_ver=$((sub_ver+1)) + version="$main_ver.$sub_ver" + sed -i "s|^version=.*|version=$version|" "$config_file" + echo "Subversion automatisch auf $version erhöht." >> "$logfile" fi else - echo "==== Build fehlgeschlagen: $(date) ====" | tee -a "$logfile" - whiptail --msgbox "Build für Projekt $project fehlgeschlagen. Details im Log: $logfile" 10 70 - return 1 + echo "==== Build fehlgeschlagen: $(date) ====" >> "$logfile" fi + + whiptail --msgbox "Build beendet. Log-Datei: $logfile" 15 70 } + # Logs ansehen (optional gefiltert nach Projekt) view_logs() { #ensure_logs_dir @@ -896,4 +794,4 @@ main_menu() { # ------------------------- ensure_dirs ensure_prereqs -main_menu +main_menu \ No newline at end of file