From 2df3826291d28bc413c57e3f179d230a93ed971b Mon Sep 17 00:00:00 2001 From: Avi Deitcher Date: Sun, 6 Aug 2017 12:52:59 +0300 Subject: [PATCH] Add script to clean ip older images Signed-off-by: Avi Deitcher --- scripts/cleanup-older-images.sh | 88 +++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100755 scripts/cleanup-older-images.sh diff --git a/scripts/cleanup-older-images.sh b/scripts/cleanup-older-images.sh new file mode 100755 index 000000000..f1b919b04 --- /dev/null +++ b/scripts/cleanup-older-images.sh @@ -0,0 +1,88 @@ +#!/bin/sh +set -e + +## +# +# script to remove all older linuxkit images +# see usage() for usage and functionality +# + +usage() { + cat >&2 < + +Prune any images that start with except the most recent versions. +[-n] = dry-run, but report what it would remove and leave. + +Examples: + $0 linuxkit 2 + will remove all versions of any image starting with linuxkit except for the 2 most recent of each + would match "linuxkit/foo" and ALSO "linuxkitprojects/foo" + + $0 linuxkit/ 2 + will remove all versions of any image starting with linuxkit/ except for the 2 most recent of each + would match "linuxkit/foo" but NOT "linuxkitprojects/foo" + + $0 linuxkit/sshd 3 + will remove all versions of linuxkit/sshd except for the 3 most recent + +EOF +} + + +# backwards compatibility +dryrun=false +if [ "$1" = "-n" ]; then + dryrun=true + set -- "$2" "$3" +fi + +# sufficient arguments +if [ $# -ne 2 ] ; then + usage + exit 1 +fi + +imagebase="$1" +versions="$2" +unversions=$(( $versions + 1)) + +# make sure the imagebase is good +case "$imagebase" in + # has a slash in it + */*) + testimage="$imagebase*" + ;; + # no slash in it + *) + testimage="$imagebase*/*" + ;; +esac + +# find all of our images +IMAGELIST=$(docker image ls --filter=reference="$testimage"':*' --format '{{.Repository}} {{.Tag}} {{.CreatedAt}}') + +# find unique names for all images +uniqueimages=$(echo "$IMAGELIST" | awk '{print $1}' | sort | uniq) + +# now go through each image, find the list +for img in $uniqueimages; do + # get the unique list of each, and sort by date + sortedlist=$(echo "$IMAGELIST" | grep -w $img | sort -k 3,3 -r) + # now split + tokeep=$(echo "$sortedlist" | head -$versions | awk '{printf "%s:%s\t",$1,$2; $1=$2=""; print $0}') + todelete=$(echo "$sortedlist" | tail -n +$unversions | awk '{print $1":"$2}') + todeletedates=$(echo "$sortedlist" | tail -n +$unversions | awk '{printf "%s:%s\t",$1,$2; $1=$2=""; print $0}') + + echo "$tokeep" | while read i; do + echo "KEEP\t$i" + done + if [ -n "$todeletedates" ]; then + echo "$todeletedates" | while read i; do + echo "DELETE\t$i" + done + if ! "$dryrun"; then + docker image rm $todelete + fi + fi +done