docker_swarm_service: Use inspect_service to get service data (#52534)

* Use inspect_service to get service data

* Catch NotFound error

* Return None explicitly
This commit is contained in:
Hannes Ljungberg 2019-02-19 21:26:24 +01:00 committed by ansibot
parent f8d1fa80e9
commit af154e42ec

View file

@ -652,9 +652,13 @@ try:
from docker.utils import ( from docker.utils import (
parse_repository_tag, parse_repository_tag,
parse_env_file, parse_env_file,
format_environment format_environment,
)
from docker.errors import (
APIError,
DockerException,
NotFound,
) )
from docker.errors import APIError, DockerException
except ImportError: except ImportError:
# missing docker-py handled in ansible.module_utils.docker.common # missing docker-py handled in ansible.module_utils.docker.common
pass pass
@ -1320,20 +1324,10 @@ class DockerServiceManager(object):
return [{'name': n['Name'], 'id': n['Id']} for n in self.client.networks()] return [{'name': n['Name'], 'id': n['Id']} for n in self.client.networks()]
def get_service(self, name): def get_service(self, name):
# The Docker API allows filtering services by name but the filter looks try:
# for a substring match, not an exact match. (Filtering for "foo" would raw_data = self.client.inspect_service(name)
# return information for services "foobar" and "foobuzz" even if the except NotFound:
# service "foo" doesn't exist.) Avoid incorrectly determining that a
# service is present by filtering the list of services returned from the
# Docker API so that the name must be an exact match.
raw_data = [
service for service in self.client.services(filters={'name': name})
if service['Spec']['Name'] == name
]
if len(raw_data) == 0:
return None return None
raw_data = raw_data[0]
ds = DockerService() ds = DockerService()
task_template_data = raw_data['Spec']['TaskTemplate'] task_template_data = raw_data['Spec']['TaskTemplate']