From 1842c19a036cfc275cf995c2626f60fd18a2ff76 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Tue, 10 Feb 2015 12:21:42 +0000 Subject: [PATCH 1/5] Slightly improve "cleanup". - Use docker ps to filter containers by status to avoid building such a long pipe. --- dokku | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dokku b/dokku index 9b4bf7731..013384ad5 100755 --- a/dokku +++ b/dokku @@ -122,7 +122,7 @@ case "$1" in cleanup) # delete all non-running container - docker ps -a | grep 'Exit' | awk '{print $1}' | xargs docker rm &> /dev/null & + docker ps -a -f "status=exited" -q | xargs docker rm &> /dev/null & # delete unused images docker images | grep '' | awk '{print $3}' | xargs docker rmi &> /dev/null & ;; From 97ef925243bdd7e347a1a7a77825b03bf7447a05 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Tue, 10 Feb 2015 12:29:09 +0000 Subject: [PATCH 2/5] Simplify images cleanup too. Reviewed containers cleanup. - Images cleanup is a common practice taken from https://docs.docker.com/reference/commandline/cli/#filtering_1 --- dokku | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dokku b/dokku index 013384ad5..a59989f19 100755 --- a/dokku +++ b/dokku @@ -122,9 +122,9 @@ case "$1" in cleanup) # delete all non-running container - docker ps -a -f "status=exited" -q | xargs docker rm &> /dev/null & + docker rm $(docker ps -a -f "status=exited" -q) &> /dev/null & # delete unused images - docker images | grep '' | awk '{print $3}' | xargs docker rmi &> /dev/null & + docker rmi $(sudo docker images -f "dangling=true" -q) &> /dev/null & ;; plugins) From 0992719e3d936cd600d2322608441fe243da7478 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Tue, 10 Feb 2015 12:52:29 +0000 Subject: [PATCH 3/5] Remove unnecessary call to sudo. --- dokku | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dokku b/dokku index a59989f19..f70261d0e 100755 --- a/dokku +++ b/dokku @@ -1,4 +1,4 @@ -#!/usr/bin/env bash +7#!/usr/bin/env bash set -eo pipefail shopt -s nullglob @@ -124,7 +124,7 @@ case "$1" in # delete all non-running container docker rm $(docker ps -a -f "status=exited" -q) &> /dev/null & # delete unused images - docker rmi $(sudo docker images -f "dangling=true" -q) &> /dev/null & + docker rmi $(docker images -f "dangling=true" -q) &> /dev/null & ;; plugins) From 7e25e747792bcb8d9d188d956823a1ac2460aab2 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Tue, 10 Feb 2015 14:16:39 +0000 Subject: [PATCH 4/5] Fix typo. --- dokku | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dokku b/dokku index f70261d0e..00c25f915 100755 --- a/dokku +++ b/dokku @@ -1,4 +1,4 @@ -7#!/usr/bin/env bash +#!/usr/bin/env bash set -eo pipefail shopt -s nullglob From d79966af05aeef91f06f10661c9cf8e3ab0b50ab Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Fri, 13 Feb 2015 14:18:07 +0000 Subject: [PATCH 5/5] Cleanup unit test --- tests/unit/cleanup.bats | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 tests/unit/cleanup.bats diff --git a/tests/unit/cleanup.bats b/tests/unit/cleanup.bats new file mode 100644 index 000000000..3c21b490a --- /dev/null +++ b/tests/unit/cleanup.bats @@ -0,0 +1,35 @@ +#!/usr/bin/env bats + +load test_helper + +setup() { + deploy_app +} + +teardown() { + destroy_app +} + +@test "remove exited containers" { + run dokku run $TEST_APP hostname + echo "output: "$output + echo "status: "$status + container="$output" + assert_success + run bash -c "docker ps -a -f 'status=exited' -q --no-trunc=false | grep -q $container" + echo "output: "$output + echo "status: "$status + assert_success + run dokku cleanup + echo "output: "$output + echo "status: "$status + assert_success + run bash -c "docker ps -a -f 'status=exited' -q --no-trunc=false | grep -q $container" + echo "output: "$output + echo "status: "$status + assert_failure + run bash -c "docker ps -a -f 'status=exited' -q --no-trunc=false" + echo "output: "$output + echo "status: "$status + assert_output "" +}