Update ptero-cleanup.sh
This commit is contained in:
parent
ceceba1afe
commit
94a88615d6
1 changed files with 66 additions and 51 deletions
117
ptero-cleanup.sh
117
ptero-cleanup.sh
|
@ -1,5 +1,24 @@
|
||||||
#!/bin/bash
|
#!/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 setup
|
||||||
LOG_FILE="/var/log/ptero-cleanup.log"
|
LOG_FILE="/var/log/ptero-cleanup.log"
|
||||||
DOCKER_PATH=$(which docker)
|
DOCKER_PATH=$(which docker)
|
||||||
|
@ -9,6 +28,34 @@ log_message() {
|
||||||
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
|
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
|
# Check if docker is installed
|
||||||
if [ -z "$DOCKER_PATH" ]; then
|
if [ -z "$DOCKER_PATH" ]; then
|
||||||
log_message "ERROR: Docker is not installed or not in PATH"
|
log_message "ERROR: Docker is not installed or not in PATH"
|
||||||
|
@ -27,59 +74,27 @@ log_message "Starting image cleanup"
|
||||||
REMOVED_COUNT=0
|
REMOVED_COUNT=0
|
||||||
FAILED_COUNT=0
|
FAILED_COUNT=0
|
||||||
|
|
||||||
# First, try to remove hello-world images with force
|
# Build grep pattern from IMAGE_PATTERNS array
|
||||||
log_message "Attempting to remove hello-world images"
|
GREP_PATTERN=$(IFS="|"; echo "${IMAGE_PATTERNS[*]}")
|
||||||
HELLO_WORLD_IMAGES=$($DOCKER_PATH images --format "{{.Repository}}:{{.Tag}}" | grep "^hello-world")
|
log_message "Using pattern: $GREP_PATTERN"
|
||||||
if [ ! -z "$HELLO_WORLD_IMAGES" ]; then
|
|
||||||
echo "$HELLO_WORLD_IMAGES" | while read -r image; do
|
# Get all target images
|
||||||
log_message "Force removing hello-world image: $image"
|
TARGET_IMAGES=$($DOCKER_PATH images --format "{{.Repository}}:{{.Tag}}" | grep -E "$GREP_PATTERN")
|
||||||
if $DOCKER_PATH rmi -f "$image" > /dev/null 2>&1; then
|
|
||||||
log_message "Successfully removed: $image"
|
if [ -z "$TARGET_IMAGES" ]; then
|
||||||
((REMOVED_COUNT++))
|
log_message "No target images found"
|
||||||
else
|
exit 0
|
||||||
# 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
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Get list of Pterodactyl images
|
# Process all images
|
||||||
PTERO_IMAGES=$($DOCKER_PATH images --format "{{.Repository}}:{{.Tag}}" | grep -E "pterodactyl|pterodactyl/yolks|ghcr.io/pterodactyl")
|
log_message "Processing target images"
|
||||||
|
echo "$TARGET_IMAGES" | while read -r image; do
|
||||||
if [ ! -z "$PTERO_IMAGES" ]; then
|
if force_remove_image "$image"; then
|
||||||
log_message "Processing Pterodactyl images"
|
((REMOVED_COUNT++))
|
||||||
# Remove each unused Pterodactyl image
|
else
|
||||||
echo "$PTERO_IMAGES" | while read -r image; do
|
((FAILED_COUNT++))
|
||||||
# Check if image is being used by any container
|
fi
|
||||||
if ! $DOCKER_PATH ps -a --format "{{.Image}}" | grep -q "^${image}$"; then
|
done
|
||||||
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