diff options
author | Maxim Kuvyrkov <maxim.kuvyrkov@linaro.org> | 2017-04-26 07:00:26 +0000 |
---|---|---|
committer | Maxim Kuvyrkov <maxim.kuvyrkov@linaro.org> | 2017-05-02 17:36:33 +0000 |
commit | fd33ebf0b931c3c52466cdeea4d8f8ace8be33fb (patch) | |
tree | 196b80354052372c891248530dad76cf30ae2f9c /tcwg-cleanup-stale-containers.sh | |
parent | 80881f7d36dc0507622b51116a3e808f7aef256a (diff) |
tcwg-cleanup-stale-containers: Improve handling of exit code
Exit code is now set to non-zero only when unexpected events happen.
Expected cleanups do not increase exit code anymore.
Change-Id: I009825e7b8d7ba16303ec3dd2aeb7ae69110168b
Diffstat (limited to 'tcwg-cleanup-stale-containers.sh')
-rwxr-xr-x | tcwg-cleanup-stale-containers.sh | 45 |
1 files changed, 30 insertions, 15 deletions
diff --git a/tcwg-cleanup-stale-containers.sh b/tcwg-cleanup-stale-containers.sh index b605d13d..6c28b8dc 100755 --- a/tcwg-cleanup-stale-containers.sh +++ b/tcwg-cleanup-stale-containers.sh @@ -77,19 +77,24 @@ do_cleanup_containers () fi done - status="0" + local res + local status="0" if [ ${#rm_containers[@]} != 0 ]; then echo "Removing containers: ${rm_containers[@]}" - echo "Increasing exit code by 1 to indicate stale containers" - status="$(($status+1))" if $cleanup_containers; then for container in "${rm_containers[@]}"; do echo "Removing container $container" - $DOCKER $action $container | cat - echo "$DOCKER $action exit status: ${PIPESTATUS[0]}" + $DOCKER $action $container & + res=0; wait $! || res=$? + if [ $res != 0 ]; then + echo "WARNING: $DOCKER $action $container -- exit status: $res" + status="1" + fi done else echo "DRY_RUN: NOT REMOVING CONTAINERS" + echo "Increasing exit code to indicate stale containers" + status="1" fi echo "Containers report after:" @@ -102,21 +107,27 @@ do_cleanup_containers () res="0" do_cleanup_containers $cleanup_running_hours "" "stop" & wait $! || res=$? -status=$(($status+$res)) +status=$res res="0" do_cleanup_containers $cleanup_stopped_hours "-a" "rm -fv" & wait $! || res=$? -status=$(($status+$res)) +status=$(($status|(2*$res))) if [ x"$($DOCKER volume ls -q -f dangling=true)" != x"" ]; then echo "Removing dangling volumes" - echo "Increasing exit code by 2 to indicate dangling volumes" - status="$(($status+2))" if $cleanup_volumes; then - $DOCKER volume ls -q -f dangling=true | xargs $DOCKER volume rm | cat - echo "xargs $DOCKER volume rm exit status: ${PIPESTATUS[1]}" + for volume in $($DOCKER volume ls -q -f dangling=true); do + $DOCKER volume rm $volume & + res=0; wait $! || res=$? + if [ $res != 0 ]; then + echo "WARNING: $DOCKER volume rm $volume -- exit status: $res" + status=$(($status|4)) + fi + done else + echo "Increasing exit code to indicate dangling volumes" + status=$(($status|4)) echo "DRY_RUN: NOT REMOVING DANGLING VOLUMES" fi fi @@ -130,14 +141,18 @@ done if [ ${#rm_images[@]} != 0 ]; then echo "Removing unused images" - echo "Increasing exit code by 4 to indicate unused images" - status="$(($status+4))" if $cleanup_images; then for image in "${rm_images[@]}"; do - $DOCKER rmi $image | cat - echo "$DOCKER rmi exit status: ${PIPESTATUS[0]}" + $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 fi |