Skip to content

Commit

Permalink
remove obsolete images (close #25) (#44)
Browse files Browse the repository at this point in the history
* remove obsolete images

* remove obsolte images after container update

* do not try remove obsolete images on the remote side
  • Loading branch information
renskiy committed Jul 21, 2016
1 parent 9e6d362 commit 6b8de7c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
12 changes: 9 additions & 3 deletions fabricio/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,7 @@ def pull(self, tag=None):
local_port=self.local_registry.port,
local_host=self.local_registry.host,
):
fabricio.run('docker pull {image}'.format(
image=self.image[self.registry:tag]),
)
DockerTasks.pull(self, tag=tag)

@fab.task(task_class=IgnoreHostsTask)
def prepare(self, tag=None):
Expand All @@ -255,6 +253,7 @@ def prepare(self, tag=None):
quiet=False,
use_cache=True,
)
self.remove_obsolete_images()

@fab.task(default=True, task_class=IgnoreHostsTask)
def deploy(self, force=False, tag=None, *args, **kwargs):
Expand All @@ -265,6 +264,12 @@ def deploy(self, force=False, tag=None, *args, **kwargs):
fab.execute(self.push, tag=tag)
DockerTasks.deploy(self, force=force, tag=tag, *args, **kwargs)

@staticmethod
def remove_obsolete_images():
fabricio.local(
'docker rmi $(docker images --filter "dangling=true" --quiet)',
)


class BuildDockerTasks(PullDockerTasks):

Expand All @@ -289,6 +294,7 @@ def prepare(self, tag=None, no_cache=False):
quiet=False,
use_cache=True,
)
self.remove_obsolete_images()

@fab.task(default=True, task_class=IgnoreHostsTask)
def deploy(self, force=False, tag=None, no_cache=False, *args, **kwargs):
Expand Down
16 changes: 16 additions & 0 deletions tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ def test_deploy(self, remote_tunnel, run, local, backup, migrate, update):
deploy_kwargs=dict(),
expected_calls=[
mock.call.local('docker pull test:latest', quiet=False, use_cache=True),
mock.call.local('docker rmi $(docker images --filter "dangling=true" --quiet)'),
mock.call.local('docker tag test:latest localhost:5000/test:latest', use_cache=True),
mock.call.local('docker push localhost:5000/test:latest', quiet=False, use_cache=True),
mock.call.local('docker rmi localhost:5000/test:latest', use_cache=True),
Expand All @@ -307,6 +308,7 @@ def test_deploy(self, remote_tunnel, run, local, backup, migrate, update):
deploy_kwargs=dict(),
expected_calls=[
mock.call.local('docker pull test:latest', quiet=False, use_cache=True),
mock.call.local('docker rmi $(docker images --filter "dangling=true" --quiet)'),
mock.call.local('docker tag test:latest localhost:5000/test:latest', use_cache=True),
mock.call.local('docker push localhost:5000/test:latest', quiet=False, use_cache=True),
mock.call.local('docker rmi localhost:5000/test:latest', use_cache=True),
Expand All @@ -322,6 +324,7 @@ def test_deploy(self, remote_tunnel, run, local, backup, migrate, update):
deploy_kwargs=dict(),
expected_calls=[
mock.call.local('docker pull test:latest', quiet=False, use_cache=True),
mock.call.local('docker rmi $(docker images --filter "dangling=true" --quiet)'),
mock.call.local('docker tag test:latest host:1234/test:latest', use_cache=True),
mock.call.local('docker push host:1234/test:latest', quiet=False, use_cache=True),
mock.call.local('docker rmi host:1234/test:latest', use_cache=True),
Expand All @@ -337,6 +340,7 @@ def test_deploy(self, remote_tunnel, run, local, backup, migrate, update):
deploy_kwargs=dict(force='yes'),
expected_calls=[
mock.call.local('docker pull test:latest', quiet=False, use_cache=True),
mock.call.local('docker rmi $(docker images --filter "dangling=true" --quiet)'),
mock.call.local('docker tag test:latest localhost:5000/test:latest', use_cache=True),
mock.call.local('docker push localhost:5000/test:latest', quiet=False, use_cache=True),
mock.call.local('docker rmi localhost:5000/test:latest', use_cache=True),
Expand All @@ -351,6 +355,7 @@ def test_deploy(self, remote_tunnel, run, local, backup, migrate, update):
deploy_kwargs=dict(tag='tag'),
expected_calls=[
mock.call.local('docker pull test:tag', quiet=False, use_cache=True),
mock.call.local('docker rmi $(docker images --filter "dangling=true" --quiet)'),
mock.call.local('docker tag test:tag localhost:5000/test:tag', use_cache=True),
mock.call.local('docker push localhost:5000/test:tag', quiet=False, use_cache=True),
mock.call.local('docker rmi localhost:5000/test:tag', use_cache=True),
Expand All @@ -365,6 +370,7 @@ def test_deploy(self, remote_tunnel, run, local, backup, migrate, update):
deploy_kwargs=dict(backup='no'),
expected_calls=[
mock.call.local('docker pull test:latest', quiet=False, use_cache=True),
mock.call.local('docker rmi $(docker images --filter "dangling=true" --quiet)'),
mock.call.local('docker tag test:latest localhost:5000/test:latest', use_cache=True),
mock.call.local('docker push localhost:5000/test:latest', quiet=False, use_cache=True),
mock.call.local('docker rmi localhost:5000/test:latest', use_cache=True),
Expand All @@ -378,6 +384,7 @@ def test_deploy(self, remote_tunnel, run, local, backup, migrate, update):
deploy_kwargs=dict(migrate='no'),
expected_calls=[
mock.call.local('docker pull test:latest', quiet=False, use_cache=True),
mock.call.local('docker rmi $(docker images --filter "dangling=true" --quiet)'),
mock.call.local('docker tag test:latest localhost:5000/test:latest', use_cache=True),
mock.call.local('docker push localhost:5000/test:latest', quiet=False, use_cache=True),
mock.call.local('docker rmi localhost:5000/test:latest', use_cache=True),
Expand Down Expand Up @@ -423,6 +430,7 @@ def test_deploy(self, remote_tunnel, run, local, backup, migrate, update):
deploy_kwargs=dict(),
expected_calls=[
mock.call.local('docker build --tag test:latest .', quiet=False, use_cache=True),
mock.call.local('docker rmi $(docker images --filter "dangling=true" --quiet)'),
mock.call.local('docker tag test:latest localhost:5000/test:latest', use_cache=True),
mock.call.local('docker push localhost:5000/test:latest', quiet=False, use_cache=True),
mock.call.local('docker rmi localhost:5000/test:latest', use_cache=True),
Expand All @@ -437,6 +445,7 @@ def test_deploy(self, remote_tunnel, run, local, backup, migrate, update):
deploy_kwargs=dict(no_cache=True),
expected_calls=[
mock.call.local('docker build --tag test:latest --no-cache .', quiet=False, use_cache=True),
mock.call.local('docker rmi $(docker images --filter "dangling=true" --quiet)'),
mock.call.local('docker tag test:latest localhost:5000/test:latest', use_cache=True),
mock.call.local('docker push localhost:5000/test:latest', quiet=False, use_cache=True),
mock.call.local('docker rmi localhost:5000/test:latest', use_cache=True),
Expand All @@ -451,6 +460,7 @@ def test_deploy(self, remote_tunnel, run, local, backup, migrate, update):
deploy_kwargs=dict(),
expected_calls=[
mock.call.local('docker build --tag test:latest build/path', quiet=False, use_cache=True),
mock.call.local('docker rmi $(docker images --filter "dangling=true" --quiet)'),
mock.call.local('docker tag test:latest localhost:5000/test:latest', use_cache=True),
mock.call.local('docker push localhost:5000/test:latest', quiet=False, use_cache=True),
mock.call.local('docker rmi localhost:5000/test:latest', use_cache=True),
Expand All @@ -466,6 +476,7 @@ def test_deploy(self, remote_tunnel, run, local, backup, migrate, update):
deploy_kwargs=dict(),
expected_calls=[
mock.call.local('docker build --tag test:latest .', quiet=False, use_cache=True),
mock.call.local('docker rmi $(docker images --filter "dangling=true" --quiet)'),
mock.call.local('docker tag test:latest localhost:5000/test:latest', use_cache=True),
mock.call.local('docker push localhost:5000/test:latest', quiet=False, use_cache=True),
mock.call.local('docker rmi localhost:5000/test:latest', use_cache=True),
Expand All @@ -481,6 +492,7 @@ def test_deploy(self, remote_tunnel, run, local, backup, migrate, update):
deploy_kwargs=dict(),
expected_calls=[
mock.call.local('docker build --tag test:latest .', quiet=False, use_cache=True),
mock.call.local('docker rmi $(docker images --filter "dangling=true" --quiet)'),
mock.call.local('docker tag test:latest host:1234/test:latest', use_cache=True),
mock.call.local('docker push host:1234/test:latest', quiet=False, use_cache=True),
mock.call.local('docker rmi host:1234/test:latest', use_cache=True),
Expand All @@ -496,6 +508,7 @@ def test_deploy(self, remote_tunnel, run, local, backup, migrate, update):
deploy_kwargs=dict(force='yes'),
expected_calls=[
mock.call.local('docker build --tag test:latest .', quiet=False, use_cache=True),
mock.call.local('docker rmi $(docker images --filter "dangling=true" --quiet)'),
mock.call.local('docker tag test:latest localhost:5000/test:latest', use_cache=True),
mock.call.local('docker push localhost:5000/test:latest', quiet=False, use_cache=True),
mock.call.local('docker rmi localhost:5000/test:latest', use_cache=True),
Expand All @@ -510,6 +523,7 @@ def test_deploy(self, remote_tunnel, run, local, backup, migrate, update):
deploy_kwargs=dict(tag='tag'),
expected_calls=[
mock.call.local('docker build --tag test:tag .', quiet=False, use_cache=True),
mock.call.local('docker rmi $(docker images --filter "dangling=true" --quiet)'),
mock.call.local('docker tag test:tag localhost:5000/test:tag', use_cache=True),
mock.call.local('docker push localhost:5000/test:tag', quiet=False, use_cache=True),
mock.call.local('docker rmi localhost:5000/test:tag', use_cache=True),
Expand All @@ -524,6 +538,7 @@ def test_deploy(self, remote_tunnel, run, local, backup, migrate, update):
deploy_kwargs=dict(backup='no'),
expected_calls=[
mock.call.local('docker build --tag test:latest .', quiet=False, use_cache=True),
mock.call.local('docker rmi $(docker images --filter "dangling=true" --quiet)'),
mock.call.local('docker tag test:latest localhost:5000/test:latest', use_cache=True),
mock.call.local('docker push localhost:5000/test:latest', quiet=False, use_cache=True),
mock.call.local('docker rmi localhost:5000/test:latest', use_cache=True),
Expand All @@ -537,6 +552,7 @@ def test_deploy(self, remote_tunnel, run, local, backup, migrate, update):
deploy_kwargs=dict(migrate='no'),
expected_calls=[
mock.call.local('docker build --tag test:latest .', quiet=False, use_cache=True),
mock.call.local('docker rmi $(docker images --filter "dangling=true" --quiet)'),
mock.call.local('docker tag test:latest localhost:5000/test:latest', use_cache=True),
mock.call.local('docker push localhost:5000/test:latest', quiet=False, use_cache=True),
mock.call.local('docker rmi localhost:5000/test:latest', use_cache=True),
Expand Down

0 comments on commit 6b8de7c

Please sign in to comment.