Clean up the docker module just a little so that we can unittest the get_split_image_tag function

pull/18777/head
Toshio Kuratomi 10 years ago committed by Matt Clay
parent f45e70fdbc
commit 9472a27345

@ -336,9 +336,10 @@ try:
except ImportError, e: except ImportError, e:
HAS_DOCKER_PY = False HAS_DOCKER_PY = False
try: if HAS_DOCKER_PY:
try:
from docker.errors import APIError as DockerAPIError from docker.errors import APIError as DockerAPIError
except ImportError: except ImportError:
from docker.client import APIError as DockerAPIError from docker.client import APIError as DockerAPIError
@ -369,6 +370,25 @@ def _docker_id_quirk(inspect):
del inspect['ID'] del inspect['ID']
return inspect return inspect
def get_split_image_tag(image):
# If image contains a host or org name, omit that from our check
if '/' in image:
registry, resource = image.rsplit('/', 1)
else:
registry, resource = None, image
# now we can determine if image has a tag
if ':' in resource:
resource, tag = resource.split(':', 1)
if registry:
resource = '/'.join((registry, resource))
else:
tag = "latest"
resource = image
return resource, tag
class DockerManager: class DockerManager:
counters = {'created':0, 'started':0, 'stopped':0, 'killed':0, 'removed':0, 'restarted':0, 'pull':0} counters = {'created':0, 'started':0, 'stopped':0, 'killed':0, 'removed':0, 'restarted':0, 'pull':0}
@ -505,24 +525,6 @@ class DockerManager:
return binds return binds
def get_split_image_tag(self, image):
# If image contains a host or org name, omit that from our check
if '/' in image:
registry, resource = image.rsplit('/', 1)
else:
registry, resource = None, image
# now we can determine if image has a tag
if ':' in resource:
resource, tag = resource.split(':', 1)
if registry:
resource = '/'.join((registry, resource))
else:
tag = "latest"
resource = image
return resource, tag
def get_summary_counters_msg(self): def get_summary_counters_msg(self):
msg = "" msg = ""
for k, v in self.counters.iteritems(): for k, v in self.counters.iteritems():
@ -562,10 +564,10 @@ class DockerManager:
# if we weren't given a tag with the image, we need to only compare on the image name, as that # if we weren't given a tag with the image, we need to only compare on the image name, as that
# docker will give us back the full image name including a tag in the container list if one exists. # docker will give us back the full image name including a tag in the container list if one exists.
image, tag = self.get_split_image_tag(image) image, tag = get_split_image_tag(image)
for i in self.client.containers(all=True): for i in self.client.containers(all=True):
running_image, running_tag = self.get_split_image_tag(i['Image']) running_image, running_tag = get_split_image_tag(i['Image'])
running_command = i['Command'].strip() running_command = i['Command'].strip()
name_matches = False name_matches = False
@ -623,7 +625,7 @@ class DockerManager:
containers = do_create(count, params) containers = do_create(count, params)
except: except:
resource = self.module.params.get('image') resource = self.module.params.get('image')
image, tag = self.get_split_image_tag(resource) image, tag = get_split_image_tag(resource)
if self.module.params.get('username'): if self.module.params.get('username'):
try: try:
self.client.login( self.client.login(
@ -851,4 +853,5 @@ def main():
# import module snippets # import module snippets
from ansible.module_utils.basic import * from ansible.module_utils.basic import *
main() if __name__ == '__main__':
main()

Loading…
Cancel
Save