Update ptero-cleanup.sh

This commit is contained in:
HHF Technology 2024-12-06 21:55:20 +05:30
parent ceceba1afe
commit 94a88615d6

View file

@ -1,5 +1,24 @@
#!/bin/bash
###########################################
# Configuration - Add new patterns here
###########################################
# Add one pattern per line between the quotes
# Use regex patterns (grep -E compatible)
# Examples:
# "^nginx" - matches images starting with nginx
# "alpine:.*" - matches all alpine tagged images
# "mysql|redis" - matches mysql or redis images
IMAGE_PATTERNS=(
"^hello-world"
"pterodactyl"
"pterodactyl/yolks"
"ghcr.io/pterodactyl"
# Add new patterns below this line
#"pattern1"
#"pattern2"
)
# Log file setup
LOG_FILE="/var/log/ptero-cleanup.log"
DOCKER_PATH=$(which docker)
@ -9,6 +28,34 @@ log_message() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
}
# Function to force remove image and its containers
force_remove_image() {
local image=$1
log_message "Force removing image: $image"
if $DOCKER_PATH rmi -f "$image" > /dev/null 2>&1; then
log_message "Successfully removed: $image"
return 0
else
# If force removal fails, try removing any stopped containers using this image
local 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"
return 0
else
log_message "Failed to remove: $image even after container cleanup"
return 1
fi
else
log_message "Failed to remove: $image"
return 1
fi
fi
}
# Check if docker is installed
if [ -z "$DOCKER_PATH" ]; then
log_message "ERROR: Docker is not installed or not in PATH"
@ -27,59 +74,27 @@ log_message "Starting image cleanup"
REMOVED_COUNT=0
FAILED_COUNT=0
# 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
# 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
done
# Build grep pattern from IMAGE_PATTERNS array
GREP_PATTERN=$(IFS="|"; echo "${IMAGE_PATTERNS[*]}")
log_message "Using pattern: $GREP_PATTERN"
# Get all target images
TARGET_IMAGES=$($DOCKER_PATH images --format "{{.Repository}}:{{.Tag}}" | grep -E "$GREP_PATTERN")
if [ -z "$TARGET_IMAGES" ]; then
log_message "No target images found"
exit 0
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
# Process all images
log_message "Processing target images"
echo "$TARGET_IMAGES" | while read -r image; do
if force_remove_image "$image"; then
((REMOVED_COUNT++))
else
((FAILED_COUNT++))
fi
done
# Display summary
log_message "Cleanup completed"