diff options
author | Maxim Kuvyrkov <maxim.kuvyrkov@linaro.org> | 2017-04-25 10:50:27 +0000 |
---|---|---|
committer | Maxim Kuvyrkov <maxim.kuvyrkov@linaro.org> | 2017-04-26 06:38:42 +0000 |
commit | d66770fc93fc30d17015e631a3b8eeb402b57117 (patch) | |
tree | 7c9f6f4e8ad84d14d3ed7395f2633bacaf0d9227 /tcwg-cleanup-stale-containers.sh | |
parent | 81c3154aacab3a3fce181859e92a4346ad3c87ba (diff) |
tcwg-cleanup-stale-containers: Rework to stop running containers
... instead of removing them. This improvement makes cleanup job
to stop running containers after [default] 10 hours, and then
remove stopped containers after 10 days.
Change-Id: I1411a67788890b56cf6d08ee265beccdcdb7bd74
Diffstat (limited to 'tcwg-cleanup-stale-containers.sh')
-rwxr-xr-x | tcwg-cleanup-stale-containers.sh | 48 |
1 files changed, 34 insertions, 14 deletions
diff --git a/tcwg-cleanup-stale-containers.sh b/tcwg-cleanup-stale-containers.sh index a3d27235..ca3f1a7f 100755 --- a/tcwg-cleanup-stale-containers.sh +++ b/tcwg-cleanup-stale-containers.sh @@ -2,22 +2,22 @@ set -e -docker_ps_opts="" -hours="10" -verbose=false -cleanup_containers=false -cleanup_volumes=false +cleanup_running_hours="-10" +cleanup_stopped_hours="-240" cleanup_images=false +cleanup_volumes=false +verbose=false -OPTS="`getopt -l cleanup-containers:,cleanup-images:,cleanup-volumes:,docker_ps_opts:,hours:,verbose: -- "$@"`" -while test $# -gt 0; do +while [ $# -gt 0 ]; do case $1 in - --cleanup-containers) cleanup_containers="$2"; shift ;; + --cleanup-running-hours) cleanup_running_hours="$2"; shift ;; + --cleanup-stopped-hours) cleanup_stopped_hours="$2"; shift ;; --cleanup-images) cleanup_images="$2"; shift ;; --cleanup-volumes) cleanup_volumes="$2"; shift ;; --docker_ps_opts) docker_ps_opts="$2"; shift ;; --hours) hours="$2"; shift ;; --verbose) verbose="$2"; shift ;; + *) echo "ERROR: Wrong option: $1"; exit 1 ;; esac shift done @@ -26,10 +26,17 @@ if $verbose; then set -x fi -if [ "$hours" -lt "0" ]; then - echo "ERROR: Refusing to delete containers that are $hours hours old" - exit 1 -fi +do_cleanup_containers () +{ + local hours="$1" + local docker_ps_opts="$2" + local action="$3" + local cleanup_containers=true + + if [ "$hours" -lt "0" ]; then + hours="$((0-$hours))" + cleanup_containers=false + fi echo "Container report before:" docker ps $docker_ps_opts @@ -56,8 +63,8 @@ if [ ${#rm_containers[@]} != 0 ]; then if $cleanup_containers; then for container in "${rm_containers[@]}"; do echo "Removing container $container" - docker rm -f -v $container | cat - echo "docker rm exit status: ${PIPESTATUS[0]}" + docker $action $container | cat + echo "docker $action exit status: ${PIPESTATUS[0]}" done else echo "DRY_RUN: NOT REMOVING CONTAINERS" @@ -67,6 +74,19 @@ if [ ${#rm_containers[@]} != 0 ]; then docker ps $docker_ps_opts fi + exit $status +} + +res="0" +do_cleanup_containers $cleanup_running_hours "" "stop" & +wait $! || res=$? +status=$(($status+$res)) + +res="0" +do_cleanup_containers $cleanup_stopped_hours "-a" "rm -fv" & +wait $! || res=$? +status=$(($status+$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" |