From 6a5669e77d98f2acdb4b590d71a82162ad1a3fcb Mon Sep 17 00:00:00 2001 From: Wolfgang Date: Sun, 1 Mar 2026 18:41:50 +0100 Subject: [PATCH] =?UTF-8?q?fix:=20cleanup=5Flxc.sh=20l=C3=B6scht=20Nginx-P?= =?UTF-8?q?roxy-Eintr=C3=A4ge=20vor=20LXC-L=C3=B6schung?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Subshell-Bug behoben: while-Loop nutzt nun Process Substitution statt Pipe - Spaltenindex korrigiert: awk '{print $2}' statt $3 für Container-Status - Nginx-Proxy-Einträge werden vor LXC-Löschung via delete_nginx_proxy.sh entfernt - Proxy-Ergebnis (JSON) wird pro Container im Output eingebettet Co-Authored-By: Claude Sonnet 4.6 --- cleanup_lxc.sh | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100755 cleanup_lxc.sh diff --git a/cleanup_lxc.sh b/cleanup_lxc.sh new file mode 100755 index 0000000..77c95e8 --- /dev/null +++ b/cleanup_lxc.sh @@ -0,0 +1,78 @@ +#!/bin/bash + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +# JSON-Output initialisieren +output="{" +output="$output\"result\": \"success\"," +output="$output\"deleted_containers\": [" + +first=true +containers_deleted=0 +total_containers=0 + +# Zähle Gesamtzahl der Container +total_containers=$(pct list | grep -E '^[0-9]+' | wc -l) + +# Wenn keine Container vorhanden sind +if [ "$total_containers" -eq 0 ]; then + output="$output]," + output="$output\"message\": \"Keine Container gefunden\"," + output="$output\"total_containers\": 0," + output="$output\"deleted_count\": 0," + output="$output\"status\": \"no_containers\"" + output="$output}" + echo "$output" + exit 0 +fi + +# Verarbeite jeden Container +while read -r line; do + container=$(echo "$line" | awk '{print $1}') + status=$(echo "$line" | awk '{print $2}') + + if [ "$status" = "stopped" ]; then + # Proxy-Eintrag zuerst löschen + echo "Lösche Nginx-Proxy für Container $container..." + proxy_json=$(bash "$SCRIPT_DIR/delete_nginx_proxy.sh" --ctid "$container" 2>/dev/null || echo "{\"error\": \"proxy script failed\"}") + echo "Proxy-Ergebnis: $proxy_json" + + # Container löschen + echo "Lösche Container $container..." + pct destroy $container -f + + if [ $? -eq 0 ]; then + echo "Container $container erfolgreich gelöscht" + ((containers_deleted++)) + lxc_status="deleted" + else + echo "Fehler beim Löschen von Container $container" + lxc_status="error" + fi + + # JSON-Output für diesen Container + entry="{\"id\": \"$container\", \"status\": \"$lxc_status\", \"proxy\": $proxy_json}" + if [ "$first" = true ]; then + output="$output$entry" + first=false + else + output="$output,$entry" + fi + fi +done < <(pct list | grep -E '^[0-9]+') + +# Abschluss des JSON-Outputs +output="$output]," +output="$output\"message\": \"Löschung abgeschlossen\"," +output="$output\"total_containers\": $total_containers," +output="$output\"deleted_count\": $containers_deleted," + +# Überprüfe, ob überhaupt Container gelöscht wurden +if [ "$containers_deleted" -eq 0 ]; then + output="$output\"status\": \"no_deletions\"" +else + output="$output\"status\": \"completed\"" +fi + +output="$output}" +echo "$output"