Update image-cleanup.sh

This commit is contained in:
HHF Technology 2024-12-06 23:00:25 +05:30
parent 57262655c9
commit beee3c3fe5

View file

@ -14,13 +14,14 @@ IMAGE_PATTERNS=(
"pterodactyl"
"pterodactyl/yolks"
"ghcr.io/pterodactyl"
"ghcr.io/parkervcp/games"
"ghcr.io/parkervcp/yolks"
"ghcr.io/parkervcp/steamcmd"
# Add new patterns below this line
#"pattern1"
#"pattern2"
)
# Log file setup
LOG_FILE="/var/log/image-cleanup.log"
LOG_FILE="/var/log/ptero-cleanup.log"
DOCKER_PATH=$(which docker)
# Function to log messages
@ -32,6 +33,18 @@ log_message() {
force_remove_image() {
local image=$1
log_message "Force removing image: $image"
# First try to remove dangling images specifically
if [[ $image == *"<none>"* ]]; then
local IMAGE_ID=$(echo $image | awk '{print $3}')
log_message "Detected dangling image with ID: $IMAGE_ID"
if $DOCKER_PATH rmi -f $IMAGE_ID > /dev/null 2>&1; then
log_message "Successfully removed dangling image: $IMAGE_ID"
return 0
fi
fi
# Regular force remove attempt
if $DOCKER_PATH rmi -f "$image" > /dev/null 2>&1; then
log_message "Successfully removed: $image"
return 0
@ -74,6 +87,19 @@ log_message "Starting image cleanup"
REMOVED_COUNT=0
FAILED_COUNT=0
# First, try to remove all dangling images specifically
log_message "Checking for dangling images first..."
DANGLING_IMAGES=$($DOCKER_PATH images -f "dangling=true" --format "{{.Repository}} {{.Tag}} {{.ID}}")
if [ ! -z "$DANGLING_IMAGES" ]; then
echo "$DANGLING_IMAGES" | while read -r image; do
if force_remove_image "$image"; then
((REMOVED_COUNT++))
else
((FAILED_COUNT++))
fi
done
fi
# Build grep pattern from IMAGE_PATTERNS array
GREP_PATTERN=$(IFS="|"; echo "${IMAGE_PATTERNS[*]}")
log_message "Using pattern: $GREP_PATTERN"
@ -81,20 +107,19 @@ 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
# Process all images
log_message "Processing target images"
echo "$TARGET_IMAGES" | while read -r image; do
if [ ! -z "$TARGET_IMAGES" ]; then
# 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
done
else
log_message "No target images found matching patterns"
fi
# Display summary
log_message "Cleanup completed"