Update image-cleanup.sh
This commit is contained in:
parent
57262655c9
commit
beee3c3fe5
1 changed files with 41 additions and 16 deletions
|
@ -14,13 +14,14 @@ IMAGE_PATTERNS=(
|
||||||
"pterodactyl"
|
"pterodactyl"
|
||||||
"pterodactyl/yolks"
|
"pterodactyl/yolks"
|
||||||
"ghcr.io/pterodactyl"
|
"ghcr.io/pterodactyl"
|
||||||
|
"ghcr.io/parkervcp/games"
|
||||||
|
"ghcr.io/parkervcp/yolks"
|
||||||
|
"ghcr.io/parkervcp/steamcmd"
|
||||||
# Add new patterns below this line
|
# Add new patterns below this line
|
||||||
#"pattern1"
|
|
||||||
#"pattern2"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Log file setup
|
# Log file setup
|
||||||
LOG_FILE="/var/log/image-cleanup.log"
|
LOG_FILE="/var/log/ptero-cleanup.log"
|
||||||
DOCKER_PATH=$(which docker)
|
DOCKER_PATH=$(which docker)
|
||||||
|
|
||||||
# Function to log messages
|
# Function to log messages
|
||||||
|
@ -32,6 +33,18 @@ log_message() {
|
||||||
force_remove_image() {
|
force_remove_image() {
|
||||||
local image=$1
|
local image=$1
|
||||||
log_message "Force removing image: $image"
|
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
|
if $DOCKER_PATH rmi -f "$image" > /dev/null 2>&1; then
|
||||||
log_message "Successfully removed: $image"
|
log_message "Successfully removed: $image"
|
||||||
return 0
|
return 0
|
||||||
|
@ -74,6 +87,19 @@ log_message "Starting image cleanup"
|
||||||
REMOVED_COUNT=0
|
REMOVED_COUNT=0
|
||||||
FAILED_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
|
# Build grep pattern from IMAGE_PATTERNS array
|
||||||
GREP_PATTERN=$(IFS="|"; echo "${IMAGE_PATTERNS[*]}")
|
GREP_PATTERN=$(IFS="|"; echo "${IMAGE_PATTERNS[*]}")
|
||||||
log_message "Using pattern: $GREP_PATTERN"
|
log_message "Using pattern: $GREP_PATTERN"
|
||||||
|
@ -81,21 +107,20 @@ log_message "Using pattern: $GREP_PATTERN"
|
||||||
# Get all target images
|
# Get all target images
|
||||||
TARGET_IMAGES=$($DOCKER_PATH images --format "{{.Repository}}:{{.Tag}}" | grep -E "$GREP_PATTERN")
|
TARGET_IMAGES=$($DOCKER_PATH images --format "{{.Repository}}:{{.Tag}}" | grep -E "$GREP_PATTERN")
|
||||||
|
|
||||||
if [ -z "$TARGET_IMAGES" ]; then
|
if [ ! -z "$TARGET_IMAGES" ]; then
|
||||||
log_message "No target images found"
|
# Process all images
|
||||||
exit 0
|
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
|
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
|
# Display summary
|
||||||
log_message "Cleanup completed"
|
log_message "Cleanup completed"
|
||||||
log_message "Images removed: $REMOVED_COUNT"
|
log_message "Images removed: $REMOVED_COUNT"
|
||||||
|
|
Loading…
Reference in a new issue