mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-22 21:00:22 -07:00
Rework imgadm module: (#20535)
- get rid of changed, rc, stderr, stdout juggling - use get_bin_path() to get 'imgadm' path - move actual implementation to Imgadm class - fix 'changed' for imported images
This commit is contained in:
parent
82dbe9853c
commit
899e336b1e
1 changed files with 115 additions and 124 deletions
|
@ -1,7 +1,7 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
# (c) 2016, Jasper Lievisse Adriaanse <j@jasper.la>
|
# (c) 2016, 2017 Jasper Lievisse Adriaanse <j@jasper.la>
|
||||||
#
|
#
|
||||||
# This file is part of Ansible
|
# This file is part of Ansible
|
||||||
#
|
#
|
||||||
|
@ -130,134 +130,146 @@ import re
|
||||||
# -E option to return any errors in JSON, the generated JSON does not play well
|
# -E option to return any errors in JSON, the generated JSON does not play well
|
||||||
# with the JSON parsers of Python. The returned message contains '\n' as part of
|
# with the JSON parsers of Python. The returned message contains '\n' as part of
|
||||||
# the stacktrace, which breaks the parsers.
|
# the stacktrace, which breaks the parsers.
|
||||||
IMGADM = 'imgadm'
|
|
||||||
|
|
||||||
|
|
||||||
# Helper method to massage stderr
|
class Imgadm(object):
|
||||||
def errmsg(stderr):
|
def __init__(self, module):
|
||||||
match = re.match('^imgadm .*?: error \(\w+\): (.*): .*', stderr)
|
self.module = module
|
||||||
if match:
|
self.params = module.params
|
||||||
return match.groups()[0]
|
self.cmd = module.get_bin_path('imgadm', required=True)
|
||||||
else:
|
self.changed = False
|
||||||
return 'Unexpected failure'
|
self.uuid = module.params['uuid']
|
||||||
|
|
||||||
|
# Since there are a number of (natural) aliases, prevent having to look
|
||||||
|
# them up everytime we operate on `state`.
|
||||||
|
if self.params['state'] in ['present', 'imported', 'updated']:
|
||||||
|
self.present = True
|
||||||
|
else:
|
||||||
|
self.present = False
|
||||||
|
|
||||||
def update_images(module):
|
# Perform basic UUID validation upfront.
|
||||||
uuid = module.params['uuid']
|
if self.uuid and self.uuid != '*':
|
||||||
cmd = IMGADM + ' update'
|
if not re.match('^[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$', self.uuid, re.IGNORECASE):
|
||||||
|
module.fail_json(msg='Provided value for uuid option is not a valid UUID.')
|
||||||
|
|
||||||
if uuid != '*':
|
# Helper method to massage stderr
|
||||||
cmd = '{0} {1}'.format(cmd, uuid)
|
def errmsg(self, stderr):
|
||||||
|
match = re.match('^imgadm .*?: error \(\w+\): (.*): .*', stderr)
|
||||||
|
if match:
|
||||||
|
return match.groups()[0]
|
||||||
|
else:
|
||||||
|
return 'Unexpected failure'
|
||||||
|
|
||||||
(rc, stdout, stderr) = module.run_command(cmd)
|
def update_images(self):
|
||||||
|
if self.uuid == '*':
|
||||||
|
cmd = '{0} update'.format(self.cmd)
|
||||||
|
else:
|
||||||
|
cmd = '{0} update {1}'.format(self.cmd, self.uuid)
|
||||||
|
|
||||||
# There is no feedback from imgadm(1M) to determine if anything
|
(rc, stdout, stderr) = self.module.run_command(cmd)
|
||||||
# was actually changed. So treat this as an 'always-changes' operation.
|
|
||||||
# Note that 'imgadm -v' produces unparseable JSON...
|
|
||||||
return rc, stdout, errmsg(stderr), True
|
|
||||||
|
|
||||||
|
if rc != 0:
|
||||||
|
self.module.fail_json(msg='Failed to update images: {0}'.format(self.errmsg(stderr)))
|
||||||
|
|
||||||
def manage_sources(module, present):
|
# There is no feedback from imgadm(1M) to determine if anything
|
||||||
force = module.params['force']
|
# was actually changed. So treat this as an 'always-changes' operation.
|
||||||
source = module.params['source']
|
# Note that 'imgadm -v' produces unparseable JSON...
|
||||||
imgtype = module.params['type']
|
self.changed = True
|
||||||
|
|
||||||
cmd = IMGADM + ' sources'
|
def manage_sources(self):
|
||||||
|
force = self.params['force']
|
||||||
|
source = self.params['source']
|
||||||
|
imgtype = self.params['type']
|
||||||
|
|
||||||
if force:
|
cmd = '{0} sources'.format(self.cmd)
|
||||||
cmd += ' -f'
|
|
||||||
|
|
||||||
if present:
|
if force:
|
||||||
cmd = '{0} -a {1} -t {2}'.format(cmd, source, imgtype)
|
cmd += ' -f'
|
||||||
(rc, stdout, stderr) = module.run_command(cmd)
|
|
||||||
|
|
||||||
# Check the various responses.
|
if self.present:
|
||||||
# Note that trying to add a source with the wrong type is handled
|
cmd = '{0} -a {1} -t {2}'.format(cmd, source, imgtype)
|
||||||
# above as it results in a non-zero status.
|
(rc, stdout, stderr) = self.module.run_command(cmd)
|
||||||
changed = True
|
|
||||||
|
|
||||||
regex = 'Already have "{0}" image source "{1}", no change'.format(imgtype, source)
|
if rc != 0:
|
||||||
if re.match(regex, stdout):
|
self.module.fail_json(msg='Failed to add source: {0}'.format(self.errmsg(stderr)))
|
||||||
changed = False
|
|
||||||
|
|
||||||
regex = 'Added "%s" image source "%s"' % (imgtype, source)
|
# Check the various responses.
|
||||||
if re.match(regex, stdout):
|
# Note that trying to add a source with the wrong type is handled
|
||||||
changed = True
|
# above as it results in a non-zero status.
|
||||||
|
|
||||||
# Fallthrough, assume changes
|
regex = 'Already have "{0}" image source "{1}", no change'.format(imgtype, source)
|
||||||
return (rc, stdout, errmsg(stderr), changed)
|
if re.match(regex, stdout):
|
||||||
else:
|
self.changed = False
|
||||||
# Type is ignored by imgadm(1M) here
|
|
||||||
cmd += ' -d %s' % (source)
|
|
||||||
(rc, stdout, stderr) = module.run_command(cmd)
|
|
||||||
|
|
||||||
changed = True
|
regex = 'Added "%s" image source "%s"' % (imgtype, source)
|
||||||
|
if re.match(regex, stdout):
|
||||||
|
self.changed = True
|
||||||
|
else:
|
||||||
|
# Type is ignored by imgadm(1M) here
|
||||||
|
cmd += ' -d %s' % (source)
|
||||||
|
(rc, stdout, stderr) = self.module.run_command(cmd)
|
||||||
|
|
||||||
regex = 'Do not have image source "%s", no change' % (source)
|
if rc != 0:
|
||||||
if re.match(regex, stdout):
|
self.module.fail_json(msg='Failed to remove source: {0}'.format(self.errmsg(stderr)))
|
||||||
changed = False
|
|
||||||
|
|
||||||
regex = 'Deleted ".*" image source "%s"' % (source)
|
regex = 'Do not have image source "%s", no change' % (source)
|
||||||
if re.match(regex, stdout):
|
if re.match(regex, stdout):
|
||||||
changed = True
|
self.changed = False
|
||||||
|
|
||||||
return (rc, stdout, errmsg(stderr), changed)
|
regex = 'Deleted ".*" image source "%s"' % (source)
|
||||||
|
if re.match(regex, stdout):
|
||||||
|
self.changed = True
|
||||||
|
|
||||||
|
def manage_images(self):
|
||||||
|
pool = self.params['pool']
|
||||||
|
state = self.params['state']
|
||||||
|
|
||||||
def manage_images(module, present):
|
if state == 'vacuumed':
|
||||||
uuid = module.params['uuid']
|
# Unconditionally pass '--force', otherwise we're prompted with 'y/N'
|
||||||
pool = module.params['pool']
|
cmd = '{0} vacuum -f'.format(self.cmd)
|
||||||
state = module.params['state']
|
|
||||||
|
|
||||||
if state == 'vacuumed':
|
(rc, stdout, stderr) = self.module.run_command(cmd)
|
||||||
# Unconditionally pass '--force', otherwise we're prompted with 'y/N'
|
|
||||||
cmd = '{0} vacuum -f'.format(IMGADM)
|
|
||||||
|
|
||||||
(rc, stdout, stderr) = module.run_command(cmd)
|
if rc != 0:
|
||||||
|
self.module.fail_json(msg='Failed to vacuum images: {0}'.format(self.errmsg(stderr)))
|
||||||
if rc == 0:
|
|
||||||
if stdout == '':
|
|
||||||
changed = False
|
|
||||||
else:
|
else:
|
||||||
changed = True
|
if stdout == '':
|
||||||
|
self.changed = False
|
||||||
|
else:
|
||||||
|
self.changed = True
|
||||||
|
if self.present:
|
||||||
|
cmd = '{0} import -P {1} -q {2}'.format(self.cmd, pool, self.uuid)
|
||||||
|
|
||||||
return (rc, stdout, errmsg(stderr), changed)
|
(rc, stdout, stderr) = self.module.run_command(cmd)
|
||||||
|
|
||||||
if present:
|
if rc != 0:
|
||||||
cmd = '{0} import -P {1} -q {2}'.format(IMGADM, pool, uuid)
|
self.module.fail_json(msg='Failed to import image: {0}'.format(self.errmsg(stderr)))
|
||||||
|
|
||||||
changed = False
|
regex = 'Image {0} \(.*\) is already installed, skipping'.format(self.uuid)
|
||||||
(rc, stdout, stderr) = module.run_command(cmd)
|
if re.match(regex, stdout):
|
||||||
|
self.changed = False
|
||||||
|
|
||||||
regex = 'Image {0} \(.*\) is already installed, skipping'.format(uuid)
|
regex = '.*ActiveImageNotFound.*'
|
||||||
if re.match(regex, stdout):
|
if re.match(regex, stderr):
|
||||||
changed = False
|
self.changed = False
|
||||||
|
|
||||||
regex = '.*ActiveImageNotFound.*'
|
regex = 'Imported image {0}.*'.format(self.uuid)
|
||||||
if re.match(regex, stderr):
|
if re.match(regex, stdout.splitlines()[-1]):
|
||||||
changed = False
|
self.changed = True
|
||||||
|
else:
|
||||||
|
cmd = '{0} delete -P {1} {2}'.format(self.cmd, pool, self.uuid)
|
||||||
|
|
||||||
regex = 'Imported image {0}'.format(uuid)
|
(rc, stdout, stderr) = self.module.run_command(cmd)
|
||||||
if re.match(regex, stdout):
|
|
||||||
changed = True
|
|
||||||
else:
|
|
||||||
cmd = '{0} delete -P {1} {2}'.format(IMGADM, pool, uuid)
|
|
||||||
|
|
||||||
changed = False
|
regex = '.*ImageNotInstalled.*'
|
||||||
(rc, stdout, stderr) = module.run_command(cmd)
|
if re.match(regex, stderr):
|
||||||
|
# Even if the 'rc' was non-zero (3), we handled the situation
|
||||||
|
# in order to determine if there was a change.
|
||||||
|
self.changed = False
|
||||||
|
|
||||||
regex = '.*ImageNotInstalled.*'
|
regex = 'Deleted image {0}'.format(self.uuid)
|
||||||
if re.match(regex, stderr):
|
if re.match(regex, stdout):
|
||||||
# Even if the 'rc' was non-zero (3), we handled the situation
|
self.changed = True
|
||||||
# in order to determine if there was a change, so set rc to success.
|
|
||||||
rc = 0
|
|
||||||
changed = False
|
|
||||||
|
|
||||||
regex = 'Deleted image {0}'.format(uuid)
|
|
||||||
if re.match(regex, stdout):
|
|
||||||
changed = True
|
|
||||||
|
|
||||||
return (rc, stdout, errmsg(stderr), changed)
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
@ -275,51 +287,30 @@ def main():
|
||||||
supports_check_mode=False,
|
supports_check_mode=False,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
imgadm = Imgadm(module)
|
||||||
|
|
||||||
uuid = module.params['uuid']
|
uuid = module.params['uuid']
|
||||||
source = module.params['source']
|
source = module.params['source']
|
||||||
state = module.params['state']
|
state = module.params['state']
|
||||||
|
|
||||||
# Since there are a number of (natural) aliases, prevent having to look
|
|
||||||
# them up everytime we operate on `state`.
|
|
||||||
if state in ['present', 'imported', 'updated']:
|
|
||||||
present = True
|
|
||||||
else:
|
|
||||||
present = False
|
|
||||||
|
|
||||||
stderr = stdout = ''
|
|
||||||
rc = 0
|
|
||||||
result = {'state': state}
|
result = {'state': state}
|
||||||
changed = False
|
|
||||||
|
|
||||||
# Perform basic UUID validation upfront.
|
|
||||||
if uuid and uuid != '*':
|
|
||||||
if not re.match('^[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$', uuid, re.IGNORECASE):
|
|
||||||
module.fail_json(msg='Provided value for uuid option is not a valid UUID.')
|
|
||||||
|
|
||||||
# Either manage sources or images.
|
# Either manage sources or images.
|
||||||
if module.params['source']:
|
if source:
|
||||||
(rc, stdout, stderr, changed) = manage_sources(module, present)
|
|
||||||
result['source'] = source
|
result['source'] = source
|
||||||
|
imgadm.manage_sources()
|
||||||
else:
|
else:
|
||||||
result['uuid'] = uuid
|
result['uuid'] = uuid
|
||||||
|
|
||||||
if state == 'updated':
|
if state == 'updated':
|
||||||
(rc, stdout, stderr, changed) = update_images(module)
|
imgadm.update_images()
|
||||||
else:
|
else:
|
||||||
# Make sure operate on a single image for the following actions
|
# Make sure operate on a single image for the following actions
|
||||||
if (uuid == '*') and (state != 'vacuumed'):
|
if (uuid == '*') and (state != 'vacuumed'):
|
||||||
module.fail_json(msg='Can only specify uuid as "*" when updating image(s)')
|
module.fail_json(msg='Can only specify uuid as "*" when updating image(s)')
|
||||||
|
imgadm.manage_images()
|
||||||
|
|
||||||
(rc, stdout, stderr, changed) = manage_images(module, present)
|
result['changed'] = imgadm.changed
|
||||||
|
|
||||||
if rc != 0:
|
|
||||||
if stderr:
|
|
||||||
module.fail_json(msg=stderr)
|
|
||||||
else:
|
|
||||||
module.fail_json(msg=stdout)
|
|
||||||
|
|
||||||
result['changed'] = changed
|
|
||||||
|
|
||||||
module.exit_json(**result)
|
module.exit_json(**result)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue