summaryrefslogtreecommitdiff
path: root/tcwg-cleanup-stale-containers.sh
diff options
context:
space:
mode:
Diffstat (limited to 'tcwg-cleanup-stale-containers.sh')
-rwxr-xr-xtcwg-cleanup-stale-containers.sh25
1 files changed, 20 insertions, 5 deletions
diff --git a/tcwg-cleanup-stale-containers.sh b/tcwg-cleanup-stale-containers.sh
index e160337c..a7ae8ab7 100755
--- a/tcwg-cleanup-stale-containers.sh
+++ b/tcwg-cleanup-stale-containers.sh
@@ -63,14 +63,28 @@ DOCKER="docker"
do_cleanup_containers ()
{
local hours="$1"
- local docker_ps_opts="$2"
+ local filter="$2"
local action="$3"
local action_msg="$4"
local msg="$5"
local cleanup_containers=true
local dryrun_msg=""
+
+ # We stop only jenkins build containers, and remove all old stopped
+ # containers.
local only_jenkins_containers=true
+ local docker_ps_opts=""
+ local date_field="{{.Created}}"
+ case "$filter" in
+ "jenkins-running")
+ ;;
+ "all-exited")
+ only_jenkins_containers=false
+ docker_ps_opts="--filter status=exited"
+ date_field="{{.State.FinishedAt}}"
+ ;;
+ esac
if [ "$hours" -eq "0" ]; then
exit 0
@@ -90,11 +104,10 @@ do_cleanup_containers ()
for container in $($DOCKER ps $docker_ps_opts --format "{{.ID}}"); do
local container_date container_seconds
- container_date=$(date +%s --date="$($DOCKER inspect --format "{{.Created}}" $container)")
+ container_date=$(date +%s --date="$($DOCKER inspect --format "$date_field" $container)")
container_seconds=$((curdate-container_date))
if [ "$(($container_seconds/3600))" -gt "$hours" ]; then
-
# Do we want to remove all containers, or only those
# started by Jenkins jobs?
if ${only_jenkins_containers}; then
@@ -145,12 +158,14 @@ do_cleanup_containers ()
}
res="0"
-do_cleanup_containers $cleanup_running_hours "" "stop" "stop" "Stopping long-running containers" &
+do_cleanup_containers $cleanup_running_hours "jenkins-running" \
+ "stop" "stop" "Stopping long-running containers" &
wait $! || res=$?
status=$res
res="0"
-do_cleanup_containers $cleanup_stopped_hours "-a" "rm -fv" "remove" "Removing containers stopped long ago" &
+do_cleanup_containers $cleanup_stopped_hours "all-exited" \
+ "rm -fv" "remove" "Removing containers stopped long ago" &
wait $! || res=$?
status=$(($status|(2*$res)))