From ceceba1afe35b0258b5865759bfc202380ae42ad Mon Sep 17 00:00:00 2001 From: hhf Date: Fri, 6 Dec 2024 21:49:54 +0530 Subject: [PATCH] Update ptero-cleanup.sh --- ptero-cleanup.sh | 71 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 50 insertions(+), 21 deletions(-) diff --git a/ptero-cleanup.sh b/ptero-cleanup.sh index 4c680ad..66c9163 100644 --- a/ptero-cleanup.sh +++ b/ptero-cleanup.sh @@ -21,36 +21,65 @@ if [ "$EUID" -ne 0 ]; then exit 1 fi -log_message "Starting Pterodactyl image cleanup" - -# Get list of Pterodactyl-related images -PTERO_IMAGES=$($DOCKER_PATH images --format "{{.Repository}}:{{.Tag}}" | grep -i "pterodactyl\|pterodactyl/yolks\|ghcr.io/pterodactyl") - -if [ -z "$PTERO_IMAGES" ]; then - log_message "No Pterodactyl images found" - exit 0 -fi +log_message "Starting image cleanup" # Counter for removed images REMOVED_COUNT=0 FAILED_COUNT=0 -# Remove each unused Pterodactyl image -echo "$PTERO_IMAGES" | while read -r image; do - # Check if image is being used by any container - if ! $DOCKER_PATH ps -a --format "{{.Image}}" | grep -q "^${image}$"; then - log_message "Attempting to remove image: $image" - if $DOCKER_PATH rmi "$image" > /dev/null 2>&1; then +# First, try to remove hello-world images with force +log_message "Attempting to remove hello-world images" +HELLO_WORLD_IMAGES=$($DOCKER_PATH images --format "{{.Repository}}:{{.Tag}}" | grep "^hello-world") +if [ ! -z "$HELLO_WORLD_IMAGES" ]; then + echo "$HELLO_WORLD_IMAGES" | while read -r image; do + log_message "Force removing hello-world image: $image" + if $DOCKER_PATH rmi -f "$image" > /dev/null 2>&1; then log_message "Successfully removed: $image" ((REMOVED_COUNT++)) else - log_message "Failed to remove: $image (might be in use)" - ((FAILED_COUNT++)) + # If force removal fails, try removing any stopped containers using this image + CONTAINERS=$($DOCKER_PATH ps -a --filter "ancestor=$image" --format "{{.ID}}") + if [ ! -z "$CONTAINERS" ]; then + log_message "Removing stopped containers using $image" + echo "$CONTAINERS" | xargs -r $DOCKER_PATH rm -f + # Try removing the image again + if $DOCKER_PATH rmi -f "$image" > /dev/null 2>&1; then + log_message "Successfully removed: $image after container cleanup" + ((REMOVED_COUNT++)) + else + log_message "Failed to remove: $image even after container cleanup" + ((FAILED_COUNT++)) + fi + else + log_message "Failed to remove: $image" + ((FAILED_COUNT++)) + fi fi - else - log_message "Skipping $image - currently in use" - fi -done + done +fi + +# Get list of Pterodactyl images +PTERO_IMAGES=$($DOCKER_PATH images --format "{{.Repository}}:{{.Tag}}" | grep -E "pterodactyl|pterodactyl/yolks|ghcr.io/pterodactyl") + +if [ ! -z "$PTERO_IMAGES" ]; then + log_message "Processing Pterodactyl images" + # Remove each unused Pterodactyl image + echo "$PTERO_IMAGES" | while read -r image; do + # Check if image is being used by any container + if ! $DOCKER_PATH ps -a --format "{{.Image}}" | grep -q "^${image}$"; then + log_message "Attempting to remove image: $image" + if $DOCKER_PATH rmi "$image" > /dev/null 2>&1; then + log_message "Successfully removed: $image" + ((REMOVED_COUNT++)) + else + log_message "Failed to remove: $image (might be in use)" + ((FAILED_COUNT++)) + fi + else + log_message "Skipping $image - currently in use" + fi + done +fi # Display summary log_message "Cleanup completed"