Skip to content

Commit

Permalink
fix remove_orphans using APIs exposed via AnsibleDockerClient (ansibl…
Browse files Browse the repository at this point in the history
…e#54316)

Co-Authored-By: sluther <[email protected]>
  • Loading branch information
sluther authored and resmo committed Mar 30, 2019
1 parent 90c067e commit 5517b03
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 2 additions & 0 deletions changelogs/fragments/26937-fix-remove-orphans.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- docker_compose - fixed an issue where ``remove_orphans`` doesn't work reliably.
21 changes: 20 additions & 1 deletion lib/ansible/modules/cloud/docker/docker_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@
from compose.cli.command import project_from_options
from compose.service import NoSuchImageError
from compose.cli.main import convergence_strategy_from_opts, build_action_from_opts, image_type_from_opt
from compose.const import DEFAULT_TIMEOUT
from compose.const import DEFAULT_TIMEOUT, LABEL_SERVICE, LABEL_PROJECT, LABEL_ONE_OFF
HAS_COMPOSE = True
HAS_COMPOSE_EXC = None
MINIMUM_COMPOSE_VERSION = '1.7.0'
Expand Down Expand Up @@ -717,6 +717,25 @@ def cmd_up(self):
result['changed'] = build_output['changed']
result['actions'] += build_output['actions']

if self.remove_orphans:
containers = self.client.containers(
filters={
'label': [
'{0}={1}'.format(LABEL_PROJECT, self.project.name),
'{0}={1}'.format(LABEL_ONE_OFF, "False")
],
}
)

orphans = []
for container in containers:
service_name = container.get('Labels', {}).get(LABEL_SERVICE)
if service_name not in self.project.service_names:
orphans.append(service_name)

if orphans:
result['changed'] = True

for service in self.project.services:
if not service_names or service.name in service_names:
plan = service.convergence_plan(strategy=converge)
Expand Down

0 comments on commit 5517b03

Please sign in to comment.