Update ptero-cleanup.sh
This commit is contained in:
parent
861ca5aa14
commit
ceceba1afe
1 changed files with 50 additions and 21 deletions
|
@ -21,36 +21,65 @@ if [ "$EUID" -ne 0 ]; then
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
log_message "Starting Pterodactyl image cleanup"
|
log_message "Starting 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
|
|
||||||
|
|
||||||
# Counter for removed images
|
# Counter for removed images
|
||||||
REMOVED_COUNT=0
|
REMOVED_COUNT=0
|
||||||
FAILED_COUNT=0
|
FAILED_COUNT=0
|
||||||
|
|
||||||
# Remove each unused Pterodactyl image
|
# First, try to remove hello-world images with force
|
||||||
echo "$PTERO_IMAGES" | while read -r image; do
|
log_message "Attempting to remove hello-world images"
|
||||||
# Check if image is being used by any container
|
HELLO_WORLD_IMAGES=$($DOCKER_PATH images --format "{{.Repository}}:{{.Tag}}" | grep "^hello-world")
|
||||||
if ! $DOCKER_PATH ps -a --format "{{.Image}}" | grep -q "^${image}$"; then
|
if [ ! -z "$HELLO_WORLD_IMAGES" ]; then
|
||||||
log_message "Attempting to remove image: $image"
|
echo "$HELLO_WORLD_IMAGES" | while read -r image; do
|
||||||
if $DOCKER_PATH rmi "$image" > /dev/null 2>&1; then
|
log_message "Force removing hello-world image: $image"
|
||||||
|
if $DOCKER_PATH rmi -f "$image" > /dev/null 2>&1; then
|
||||||
log_message "Successfully removed: $image"
|
log_message "Successfully removed: $image"
|
||||||
((REMOVED_COUNT++))
|
((REMOVED_COUNT++))
|
||||||
else
|
else
|
||||||
log_message "Failed to remove: $image (might be in use)"
|
# If force removal fails, try removing any stopped containers using this image
|
||||||
((FAILED_COUNT++))
|
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
|
fi
|
||||||
else
|
done
|
||||||
log_message "Skipping $image - currently in use"
|
fi
|
||||||
fi
|
|
||||||
done
|
# 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
|
# Display summary
|
||||||
log_message "Cleanup completed"
|
log_message "Cleanup completed"
|
||||||
|
|
Loading…
Reference in a new issue