Clean up and fixes post API version 1.20 testing.

This commit is contained in:
chouseknecht 2016-04-25 15:12:28 -04:00
parent 6eba1d6fb3
commit e888abbcb0
No known key found for this signature in database
GPG key ID: 78F1698C5705A81B

View file

@ -39,11 +39,11 @@ options:
description: description:
- Use with state 'present' to provide an alternate name for the Dockerfile to use when building an image. - Use with state 'present' to provide an alternate name for the Dockerfile to use when building an image.
default: Dockerfile default: Dockerfile
version_added: "2.0"
force: force:
description: description:
- Use with absent state to un-tag and remove all images matching the specified name. Use with states 'present' - Use with absent state to un-tag and remove all images matching the specified name. Use with states 'present'
and 'tagged' to take action even when an image already exists. If archive_path is specified, the force option and 'tagged' to take action even when an image already exists.
will cause an existing archive to be overwritten.
default: false default: false
http_timeout: http_timeout:
description: description:
@ -218,6 +218,7 @@ class ImageManager(DockerBaseClass):
self.state = parameters.get('state') self.state = parameters.get('state')
self.tag = parameters.get('tag') self.tag = parameters.get('tag')
self.http_timeout = parameters.get('http_timeout') self.http_timeout = parameters.get('http_timeout')
self.debug = parameters.get('debug')
self.push = False self.push = False
if self.state in ['present', 'build']: if self.state in ['present', 'build']:
@ -239,74 +240,35 @@ class ImageManager(DockerBaseClass):
if not image or self.force: if not image or self.force:
if self.path: if self.path:
# build the image # Build the image
self.push = True image_name = self.name
params = dict(
path=self.path,
tag=self.name,
rm=self.rm,
nocache=self.nocache,
stream=True,
timeout=self.http_timeout,
pull=self.pull,
forcerm=self.rm,
dockerfile=self.dockerfile,
decode=True
)
if self.tag: if self.tag:
params['tag'] = "%s:%s" % (self.name, self.tag) image_name = "%s:%s" % (self.name, self.tag)
if self.container_limits: self.log("Building image %s" % image_name)
params['container_limits'] = self.container_limits, self.results['actions'].append("Built image %s from %s" % (image_name, self.path))
self.log("Building image %s" % (params['tag']))
self.results['actions'].append("Built image %s from %s" % (params['tag'], self.path))
self.results['changed'] = True self.results['changed'] = True
self.push = True
if not self.check_mode: if not self.check_mode:
for line in self.client.build(**params): self.results['image'] = self.build_image()
self.log(line, pretty_print=True)
image = self.client.find_image(name=self.name, tag=self.tag)
if image:
self.results['image'] = image
elif self.load_path: elif self.load_path:
# Load the image from an archive # Load the image from an archive
if not os.path.isfile(self.load_path): if not os.path.isfile(self.load_path):
self.fail("Error loading image %s. Specified path %s does not exist." % (self.name, self.fail("Error loading image %s. Specified path %s does not exist." % (self.name,
self.load_path)) self.load_path))
self.push = True self.push = True
name = self.name image_name = self.name
if self.tag: if self.tag:
name = "%s:%s" % (self.name, self.tag) image_name = "%s:%s" % (self.name, self.tag)
self.results['actions'].append("Loaded image %s from %s" % (image_name, self.load_path))
self.results['actions'].append("Loaded image %s from %s" % (name, self.load_path))
self.results['changed'] = True self.results['changed'] = True
if not self.check_mode: if not self.check_mode:
try: self.results['image'] = self.load_image()
self.log("Reading image data from %s" % (self.load_path))
image_tar = open(self.load_path, 'r')
image_data = image_tar.read()
image_tar.close()
except Exception as exc:
self.fail("Error reading image data %s - %s" % (self.load_path, str(exc)))
try:
self.log("Loading image from %s" % (self.load_path))
self.client.load_image(image_data)
except Exception as exc:
self.fail("Error loading image %s - %s" % (name, str(exc)))
image = self.client.find_image(self.name, self.tag)
if image:
self.results['image'] = image
else: else:
# pull the image # pull the image
if not self.check_mode:
self.results['actions'].append('Pulled image %s:%s' % (self.name, self.tag)) self.results['actions'].append('Pulled image %s:%s' % (self.name, self.tag))
self.client.pull_image(self.name, tag=self.tag)
self.results['changed'] = True self.results['changed'] = True
image = self.client.find_image(name=self.name, tag=self.tag) if not self.check_mode:
if image: self.results['image'] = self.client.pull_image(self.name, tag=self.tag)
self.results['image'] = image
if self.archive_path: if self.archive_path:
self.archive_image(self.name, self.tag) self.archive_image(self.name, self.tag)
@ -314,7 +276,7 @@ class ImageManager(DockerBaseClass):
if self.push and not self.repository: if self.push and not self.repository:
self.push_image(self.name, self.tag) self.push_image(self.name, self.tag)
elif self.repository: elif self.repository:
self.tag_image(self.name, self.tag, self.repository, force=self.force, push=self.push) self.tag_image(self.name, self.tag, self.repository, force=self.force)
def absent(self): def absent(self):
''' '''
@ -344,23 +306,25 @@ class ImageManager(DockerBaseClass):
:param name - name of the image. Type: str :param name - name of the image. Type: str
:return None :return None
''' '''
if not tag: if not tag:
tag = "latest" tag = "latest"
image = self.client.find_image(name=name, tag=tag) image = self.client.find_image(name=name, tag=tag)
self.log("archive image:") if not image:
self.log(image, pretty_print=True) self.log("archive image: image %s:%s not found" % (name, tag))
if image: return
if not os.path.isfile(self.archive_path) or self.force:
image_name = "%s:%s" % (name, tag) image_name = "%s:%s" % (name, tag)
self.results['actions'].append('Archived image %s to %s' % (image_name, self.archive_path))
self.results['changed'] = True
if not self.check_mode:
self.log("Getting archive of image %s" % image_name)
try: try:
self.log("Getting archive of image %s" % (image_name))
image = self.client.get_image(image_name) image = self.client.get_image(image_name)
except Exception as exc: except Exception as exc:
self.fail("Error getting image %s - %s" % (image_name, str(exc))) self.fail("Error getting image %s - %s" % (image_name, str(exc)))
self.results['actions'].append('Archived image %s to %s' % (image_name, self.archive_path))
self.results['changed'] = True
if not self.check_mode:
try: try:
image_tar = open(self.archive_path, 'w') image_tar = open(self.archive_path, 'w')
image_tar.write(image.data) image_tar.write(image.data)
@ -387,35 +351,33 @@ class ImageManager(DockerBaseClass):
registry, repo_name = auth.resolve_repository_name(repository) registry, repo_name = auth.resolve_repository_name(repository)
if re.search('/', repository): if re.search('/', repository):
if registry: if registry:
config = auth.load_config() config = auth.load_config()
if not auth.resolve_authconfig(config, registry): if not auth.resolve_authconfig(config, registry):
self.fail("Error: configuration for %s not found. Try logging into %s first." % registry) self.fail("Error: configuration for %s not found. Try logging into %s first." % registry)
try: self.log("pushing image %s" % repository)
self.log("pushing image %s" % (repository))
status = None
self.results['actions'].append("Pushed image %s to %s:%s" % (self.name, self.repository, self.tag)) self.results['actions'].append("Pushed image %s to %s:%s" % (self.name, self.repository, self.tag))
self.results['changed'] = True self.results['changed'] = True
if not self.check_mode: if not self.check_mode:
status = None
try:
for line in self.client.push(repository, tag=tag, stream=True): for line in self.client.push(repository, tag=tag, stream=True):
response = json.loads(line) line = json.loads(line)
self.log(response, pretty_print=True) self.log(line, pretty_print=True)
if response.get('errorDetail'): if line.get('errorDetail'):
# there was an error raise Exception(line['errorDetail']['message'])
raise Exception(response['errorDetail']['message']) status = line.get('status')
status = response.get('status')
image = self.client.find_image(name=repository, tag=tag)
if image:
self.results['image'] = image
self.results['image']['push_status'] = status
except Exception as exc: except Exception as exc:
if re.search('unauthorized', str(exc)): if re.search('unauthorized', str(exc)):
self.fail("Error pushing image %s: %s. Does the repository exist?" % (repository, str(exc))) self.fail("Error pushing image %s: %s. Does the repository exist?" % (repository, str(exc)))
self.fail("Error pushing image %s: %s" % (repository, str(exc))) self.fail("Error pushing image %s: %s" % (repository, str(exc)))
self.results['image'] = self.client.find_image(name=repository, tag=tag)
if not self.results['image']:
self.results['image'] = dict()
self.results['image']['push_status'] = status
def tag_image(self, name, tag, repository, force=False, push=False): def tag_image(self, name, tag, repository, force=False):
''' '''
Tag an image into a repository. Tag an image into a repository.
@ -431,11 +393,11 @@ class ImageManager(DockerBaseClass):
found = 'found' if image else 'not found' found = 'found' if image else 'not found'
self.log("image %s was %s" % (repo, found)) self.log("image %s was %s" % (repo, found))
if not image or force: if not image or force:
try:
self.log("tagging %s:%s to %s" % (name, tag, repository)) self.log("tagging %s:%s to %s" % (name, tag, repository))
self.results['changed'] = True self.results['changed'] = True
self.results['actions'].append("Tagged image %s:%s to %s" % (name, tag, repository)) self.results['actions'].append("Tagged image %s:%s to %s" % (name, tag, repository))
if not self.check_mode: if not self.check_mode:
try:
# Finding the image does not always work, especially running a localhost registry. In those # Finding the image does not always work, especially running a localhost registry. In those
# cases, if we don't set force=True, it errors. # cases, if we don't set force=True, it errors.
image_name = name image_name = name
@ -444,14 +406,69 @@ class ImageManager(DockerBaseClass):
tag_status = self.client.tag(image_name, repository, tag=tag, force=True) tag_status = self.client.tag(image_name, repository, tag=tag, force=True)
if not tag_status: if not tag_status:
raise Exception("Tag operation failed.") raise Exception("Tag operation failed.")
image = self.client.find_image(name=repository, tag=tag)
if image:
self.results['image'] = image
except Exception as exc: except Exception as exc:
self.fail("Error: failed to tag image %s - %s" % (name, str(exc))) self.fail("Error: failed to tag image %s - %s" % (name, str(exc)))
self.results['image'] = self.client.find_image(name=repository, tag=tag)
self.push_image(repository, tag) self.push_image(repository, tag)
def build_image(self):
'''
Build an image
:return: image dict
'''
self.push = True
params = dict(
path=self.path,
tag=self.name,
rm=self.rm,
nocache=self.nocache,
stream=True,
timeout=self.http_timeout,
pull=self.pull,
forcerm=self.rm,
dockerfile=self.dockerfile,
decode=True
)
if self.tag:
params['tag'] = "%s:%s" % (self.name, self.tag)
if self.container_limits:
params['container_limits'] = self.container_limits,
for line in self.client.build(**params):
# line = json.loads(line)
self.log(line, pretty_print=True)
if line.get('error'):
if line.get('errorDetail'):
errorDetail = line.get('errorDetail')
self.fail("Error building %s - code: %s message: %s" % (self.name,
errorDetail.get('code'),
errorDetail.get('message')))
else:
self.fail("Error building %s - %s" % (self.name, line.get('error')))
return self.client.find_image(name=self.name, tag=self.tag)
def load_image(self):
'''
Load an image from a .tar archive
:return: image dict
'''
try:
self.log("Reading image data from %s" % self.load_path)
image_tar = open(self.load_path, 'r')
image_data = image_tar.read()
image_tar.close()
except Exception as exc:
self.fail("Error reading image data %s - %s" % (self.load_path, str(exc)))
try:
self.log("Loading image from %s" % self.load_path)
self.client.load_image(image_data)
except Exception as exc:
self.fail("Error loading image %s - %s" % (self.name, str(exc)))
return self.client.find_image(self.name, self.tag)
def main(): def main():
argument_spec = dict( argument_spec = dict(