From beee3c3fe523133d5cf8a75a59074c1dff500c74 Mon Sep 17 00:00:00 2001 From: hhf Date: Fri, 6 Dec 2024 23:00:25 +0530 Subject: [PATCH] Update image-cleanup.sh --- image-cleanup.sh | 57 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 41 insertions(+), 16 deletions(-) diff --git a/image-cleanup.sh b/image-cleanup.sh index f23c019..8002311 100644 --- a/image-cleanup.sh +++ b/image-cleanup.sh @@ -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 == *""* ]]; 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,21 +107,20 @@ 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 +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 +else + log_message "No target images found matching patterns" 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" log_message "Images removed: $REMOVED_COUNT"