79 lines
2.3 KiB
Bash
79 lines
2.3 KiB
Bash
|
|
#!/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"
|