From 94a88615d641dabfb3b8251b6714eb2b7ad96795 Mon Sep 17 00:00:00 2001 From: hhf Date: Fri, 6 Dec 2024 21:55:20 +0530 Subject: [PATCH] Update ptero-cleanup.sh --- ptero-cleanup.sh | 117 ++++++++++++++++++++++++++--------------------- 1 file changed, 66 insertions(+), 51 deletions(-) diff --git a/ptero-cleanup.sh b/ptero-cleanup.sh index 66c9163..05d04dd 100644 --- a/ptero-cleanup.sh +++ b/ptero-cleanup.sh @@ -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"