fix: cleanup_lxc.sh löscht Nginx-Proxy-Einträge vor LXC-Löschung
- 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 <noreply@anthropic.com>
This commit is contained in:
78
cleanup_lxc.sh
Executable file
78
cleanup_lxc.sh
Executable file
@@ -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"
|
||||
Reference in New Issue
Block a user