summaryrefslogtreecommitdiff
path: root/tcwg-cleanup-stale-containers.sh
blob: b605d13d35ee14ba64ed60a3acb4d21513c5709a (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/bin/bash

set -e

usage ()
{
    cat <<EOF
Options:
  --cleanup-running-hours HOURS
  --cleanup-stopped-hours HOURS
	Cleanup running/stopped containers that have been created more
	than HOURS ago.  Setting HOURS to negative values will
	run the cleanup in dry-run mode.

  --cleanup-images true/false
	Whether to cleanup untagged images

  --cleanup-volumes true/false
	Whether to cleanup dangling volumes

  --verbose true/false
	Whether to run in verbose mode
EOF
    exit 1
}

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 ;;
	--verbose) verbose="$2"; shift ;;
	*) echo "ERROR: Wrong option: $1"; usage ;;
    esac
    shift
done

if $verbose; then
    set -x
fi

DOCKER="docker"

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