From 47f4c3f2e5f8aae3b43e9c309494b5a132b0af0c Mon Sep 17 00:00:00 2001 From: Ash Wilson Date: Tue, 24 Mar 2015 11:07:12 -0400 Subject: [PATCH 1/2] Compare container images to Config.Image. --- cloud/docker/docker.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/cloud/docker/docker.py b/cloud/docker/docker.py index 158b8c8135d..4a8f4fef15c 100644 --- a/cloud/docker/docker.py +++ b/cloud/docker/docker.py @@ -420,6 +420,13 @@ def get_split_image_tag(image): return resource, tag +def normalize_image(image): + """ + Normalize a Docker image name to include the implied :latest tag. + """ + + return ":".join(get_split_image_tag(image)) + def is_running(container): '''Return True if an inspected container is in a state we consider "running."''' @@ -1109,11 +1116,13 @@ class DockerManager(object): if inspected: repo_tags = self.get_image_repo_tags() else: - image, tag = get_split_image_tag(self.module.params.get('image')) - repo_tags = [':'.join([image, tag])] + repo_tags = [normalize_image(self.module.params.get('image'))] for i in self.client.containers(all=True): - running_image = i['Image'] + details = self.client.inspect_container(i['Id']) + details = _docker_id_quirk(details) + + running_image = normalize_image(details['Config']['Image']) running_command = i['Command'].strip() if name: @@ -1128,9 +1137,6 @@ class DockerManager(object): matches = image_matches and command_matches if matches: - details = self.client.inspect_container(i['Id']) - details = _docker_id_quirk(details) - deployed.append(details) return deployed From 9ce2fc718a73c466c0e88028e2645490bc0daf7b Mon Sep 17 00:00:00 2001 From: Ash Wilson Date: Tue, 24 Mar 2015 11:18:15 -0400 Subject: [PATCH 2/2] Only fetch details when necessary. --- cloud/docker/docker.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/cloud/docker/docker.py b/cloud/docker/docker.py index 4a8f4fef15c..85eb0525a69 100644 --- a/cloud/docker/docker.py +++ b/cloud/docker/docker.py @@ -1119,15 +1119,17 @@ class DockerManager(object): repo_tags = [normalize_image(self.module.params.get('image'))] for i in self.client.containers(all=True): - details = self.client.inspect_container(i['Id']) - details = _docker_id_quirk(details) - - running_image = normalize_image(details['Config']['Image']) - running_command = i['Command'].strip() + details = None if name: matches = name in i.get('Names', []) else: + details = self.client.inspect_container(i['Id']) + details = _docker_id_quirk(details) + + running_image = normalize_image(details['Config']['Image']) + running_command = i['Command'].strip() + image_matches = running_image in repo_tags # if a container has an entrypoint, `command` will actually equal @@ -1137,6 +1139,10 @@ class DockerManager(object): matches = image_matches and command_matches if matches: + if not details: + details = self.client.inspect_container(i['Id']) + details = _docker_id_quirk(details) + deployed.append(details) return deployed