Update ptero-cleanup.sh

This commit is contained in:
HHF Technology 2024-12-06 21:49:54 +05:30
parent 861ca5aa14
commit ceceba1afe

View file

@ -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"