diff --git a/docker_custodian/args.py b/docker_custodian/args.py index 674266d..a664388 100644 --- a/docker_custodian/args.py +++ b/docker_custodian/args.py @@ -5,7 +5,7 @@ from pytimeparse import timeparse def timedelta_type(value): - """Retrun the :class:`datetime.datetime.DateTime` for a time in the past. + """Return the :class:`datetime.datetime.DateTime` for a time in the past. :param value: a string containing a time format supported by :mod:`pytimeparse` """ diff --git a/docker_custodian/docker_gc.py b/docker_custodian/docker_gc.py index 121096d..aa458a7 100644 --- a/docker_custodian/docker_gc.py +++ b/docker_custodian/docker_gc.py @@ -26,7 +26,8 @@ def cleanup_containers(client, max_container_age, dry_run): for container_summary in reversed(all_containers): container = api_call(client.inspect_container, container_summary['Id']) - if not container or not is_container_old(container, max_container_age): + if not container or not should_remove_container(container, + max_container_age): continue log.info("Removing container %s %s %s" % ( @@ -38,24 +39,26 @@ def cleanup_containers(client, max_container_age, dry_run): api_call(client.remove_container, container['Id']) -def is_container_old(container, min_date): +def should_remove_container(container, min_date): state = container.get('State', {}) + if state.get('Running'): return False if state.get('Ghost'): return True - # Container was created, but never used - if (state.get('FinishedAt') == YEAR_ZERO and - dateutil.parser.parse(container['Created']) < min_date): - return True + # Container was created, but never started + if state.get('FinishedAt') == YEAR_ZERO: + created_date = dateutil.parser.parse(container['Created']) + return created_date < min_date - return dateutil.parser.parse(state['FinishedAt']) < min_date + finished_date = dateutil.parser.parse(state['FinishedAt']) + return finished_date < min_date def get_all_containers(client): - log.info("Getting all continers") + log.info("Getting all containers") containers = client.containers(all=True) log.info("Found %s containers", len(containers)) return containers @@ -174,6 +177,7 @@ def main(): if args.max_container_age: cleanup_containers(client, args.max_container_age, args.dry_run) + if args.max_image_age: exclude_set = build_exclude_set( args.exclude_image, diff --git a/tests/docker_gc_test.py b/tests/docker_gc_test.py index 0081a4a..b6a79bd 100644 --- a/tests/docker_gc_test.py +++ b/tests/docker_gc_test.py @@ -11,26 +11,32 @@ import requests.exceptions from docker_custodian import docker_gc -class TestIsOldContainer(object): +class TestShouldRemoveContainer(object): def test_is_running(self, container, now): container['State']['Running'] = True - assert not docker_gc.is_container_old(container, now) + assert not docker_gc.should_remove_container(container, now) def test_is_ghost(self, container, now): container['State']['Ghost'] = True - assert docker_gc.is_container_old(container, now) + assert docker_gc.should_remove_container(container, now) - def test_old_never_run(self, container, now): + def test_old_never_run(self, container, now, earlier_time): + container['Created'] = str(earlier_time) container['State']['FinishedAt'] = docker_gc.YEAR_ZERO - assert docker_gc.is_container_old(container, now) + assert docker_gc.should_remove_container(container, now) + + def test_not_old_never_run(self, container, now, earlier_time): + container['Created'] = str(now) + container['State']['FinishedAt'] = docker_gc.YEAR_ZERO + assert not docker_gc.should_remove_container(container, now) def test_old_stopped(self, container, now): - assert docker_gc.is_container_old(container, now) + assert docker_gc.should_remove_container(container, now) def test_not_old(self, container, now): container['State']['FinishedAt'] = '2014-01-21T00:00:00Z' - assert not docker_gc.is_container_old(container, now) + assert not docker_gc.should_remove_container(container, now) def test_cleanup_containers(mock_client, now):