summaryrefslogtreecommitdiff
path: root/tcwg-cleanup-stale-containers.sh
diff options
context:
space:
mode:
authorMaxim Kuvyrkov <maxim.kuvyrkov@linaro.org>2019-03-21 08:59:00 +0000
committerMaxim Kuvyrkov <maxim.kuvyrkov@linaro.org>2019-03-21 10:37:01 +0000
commita67558007d0617f6275b587dc3296f3da97848b5 (patch)
treef63d6f960d0f003238c653ec243d6cc6503ca662 /tcwg-cleanup-stale-containers.sh
parent211ed6deb1bd513c58b70074fbf6be4c676d8d16 (diff)
tcwg-cleanup-stale-containers.sh: Simplify image removal
The current code seems to miss some of the dependencies of dangling images, so we attempt to delete a used image. Rather than being more careful in not attempting to delete used images, we non-forcefully attempt to delete all images, and ignore failures. Change-Id: I240ab7faf6f21d7f60451375992805fcc9124f60
Diffstat (limited to 'tcwg-cleanup-stale-containers.sh')
-rwxr-xr-xtcwg-cleanup-stale-containers.sh35
1 files changed, 12 insertions, 23 deletions
diff --git a/tcwg-cleanup-stale-containers.sh b/tcwg-cleanup-stale-containers.sh
index d5d999d9..18710eb8 100755
--- a/tcwg-cleanup-stale-containers.sh
+++ b/tcwg-cleanup-stale-containers.sh
@@ -174,29 +174,18 @@ if [ ${#rm_volumes[@]} != 0 ]; then
fi
fi
-rm_images=()
-for image in $($DOCKER images -q -f dangling=true); do
- if ! $DOCKER ps -a --format "{{.Image}}" | grep -q $image; then
- rm_images=("${rm_images[@]}" $image)
- fi
-done
-
-if [ ${#rm_images[@]} != 0 ]; then
- echo "Removing unused images"
- if $cleanup_images; then
- for image in "${rm_images[@]}"; do
- $DOCKER rmi $image &
- res=0; wait $! || res=$?
- if [ $res != 0 ]; then
- echo "WARNING: $DOCKER rmi $image -- exit status: $res"
- status=$(($status|8))
- fi
- done
- else
- echo "Increasing exit code to indicate unused images"
- status=$(($status|8))
- echo "DRY_RUN: NOT REMOVING UNTAGGED IMAGES"
- fi
+if $cleanup_images; then
+ for image in $($DOCKER images -q -f dangling=true); do
+ # Try to remove $image. The command will fail if the image is
+ # used by something.
+ $DOCKER rmi $image &
+ res=0; wait $! || res=$?
+ if [ $res = 0 ]; then
+ echo "Removed image $image"
+ fi
+ done
+else
+ echo "DRY_RUN: NOT REMOVING UNTAGGED IMAGES"
fi
# This is a workaround for https://issues.jenkins-ci.org/browse/JENKINS-49097.