summaryrefslogtreecommitdiff
path: root/tcwg-cleanup-stale-containers.sh
blob: e03c00fa4c6fbafdc4c17825700e00bba5005899 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/bin/bash

set -e

cleanup_running_hours="-10"
cleanup_stopped_hours="-240"
cleanup_images=false
cleanup_volumes=false
verbose=false

while [ $# -gt 0 ]; do
    case $1 in
	--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

if $verbose; then
    set -x
fi

if [ x"$DOCKER" = x"" ]; then
    DOCKER="docker"
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

rm_containers=()
for container in $($DOCKER ps --format "{{.ID}} {{.RunningFor}}" $docker_ps_opts | grep "hour\|day" | cut -d" " -f 1); do
    container_days="$($DOCKER ps --format "{{.ID}} {{.RunningFor}}" $docker_ps_opts | grep "$container [0-9]\+ day" | cut -d" " -f 2)"
    if [ x"$container_days" = x"" ]; then container_days="0"; fi

    container_hours="$($DOCKER ps --format "{{.ID}} {{.RunningFor}}" $docker_ps_opts | grep "$container [0-9]\+ hour" | cut -d" " -f 2)"
    if [ x"$container_hours" = x"" ]; then container_hours="0"; fi

    container_hours=$(($container_days*24 + $container_hours))
    if [ "$container_hours" -gt "$hours" ]; then
        rm_containers=("${rm_containers[@]}" $container)
    fi
done

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]}"
        done
    else
        echo "DRY_RUN: NOT REMOVING CONTAINERS"
    fi

    echo "Containers report after:"
    $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"
    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]}"
    else
        echo "DRY_RUN: NOT REMOVING DANGLING VOLUMES"
    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"
    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]}"
	done
    else
        echo "DRY_RUN: NOT REMOVING UNTAGGED IMAGES"
    fi
fi

exit $status