1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-04-30 07:28:52 +00:00

Reformat everything.

This commit is contained in:
Felix Fontein 2025-11-01 12:08:41 +01:00
parent 3f2213791a
commit 340ff8586d
1008 changed files with 61301 additions and 58309 deletions

View file

@ -130,40 +130,40 @@ class Imgadm:
def __init__(self, module):
self.module = module
self.params = module.params
self.cmd = module.get_bin_path('imgadm', required=True)
self.cmd = module.get_bin_path("imgadm", required=True)
self.changed = False
self.uuid = module.params['uuid']
self.uuid = module.params["uuid"]
# Since there are a number of (natural) aliases, prevent having to look
# them up every time we operate on `state`.
if self.params['state'] in ['present', 'imported', 'updated']:
if self.params["state"] in ["present", "imported", "updated"]:
self.present = True
else:
self.present = False
# Perform basic UUID validation upfront.
if self.uuid and self.uuid != '*':
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 self.uuid and self.uuid != "*":
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.")
# Helper method to massage stderr
def errmsg(self, stderr):
match = re.match(r'^imgadm .*?: error \(\w+\): (.*): .*', stderr)
match = re.match(r"^imgadm .*?: error \(\w+\): (.*): .*", stderr)
if match:
return match.groups()[0]
else:
return 'Unexpected failure'
return "Unexpected failure"
def update_images(self):
if self.uuid == '*':
cmd = [self.cmd, 'update']
if self.uuid == "*":
cmd = [self.cmd, "update"]
else:
cmd = [self.cmd, 'update', self.uuid]
cmd = [self.cmd, "update", self.uuid]
(rc, stdout, stderr) = self.module.run_command(cmd)
if rc != 0:
self.module.fail_json(msg=f'Failed to update images: {self.errmsg(stderr)}')
self.module.fail_json(msg=f"Failed to update images: {self.errmsg(stderr)}")
# There is no feedback from imgadm(1M) to determine if anything
# was actually changed. So treat this as an 'always-changes' operation.
@ -171,21 +171,21 @@ class Imgadm:
self.changed = True
def manage_sources(self):
force = self.params['force']
source = self.params['source']
imgtype = self.params['type']
force = self.params["force"]
source = self.params["source"]
imgtype = self.params["type"]
cmd = [self.cmd, 'sources']
cmd = [self.cmd, "sources"]
if force:
cmd = cmd + ['-f']
cmd = cmd + ["-f"]
if self.present:
cmd = cmd + ['-a', source, '-t', imgtype]
cmd = cmd + ["-a", source, "-t", imgtype]
(rc, stdout, stderr) = self.module.run_command(cmd)
if rc != 0:
self.module.fail_json(msg=f'Failed to add source: {self.errmsg(stderr)}')
self.module.fail_json(msg=f"Failed to add source: {self.errmsg(stderr)}")
# Check the various responses.
# Note that trying to add a source with the wrong type is handled
@ -200,11 +200,11 @@ class Imgadm:
self.changed = True
else:
# Type is ignored by imgadm(1M) here
cmd += f' -d {source}'
cmd += f" -d {source}"
(rc, stdout, stderr) = self.module.run_command(cmd)
if rc != 0:
self.module.fail_json(msg=f'Failed to remove source: {self.errmsg(stderr)}')
self.module.fail_json(msg=f"Failed to remove source: {self.errmsg(stderr)}")
regex = f'Do not have image source "{source}", no change'
if re.match(regex, stdout):
@ -215,51 +215,51 @@ class Imgadm:
self.changed = True
def manage_images(self):
pool = self.params['pool']
state = self.params['state']
pool = self.params["pool"]
state = self.params["state"]
if state == 'vacuumed':
if state == "vacuumed":
# Unconditionally pass '--force', otherwise we're prompted with 'y/N'
cmd = [self.cmd, 'vacuum', '-f']
cmd = [self.cmd, "vacuum", "-f"]
(rc, stdout, stderr) = self.module.run_command(cmd)
if rc != 0:
self.module.fail_json(msg=f'Failed to vacuum images: {self.errmsg(stderr)}')
self.module.fail_json(msg=f"Failed to vacuum images: {self.errmsg(stderr)}")
else:
if stdout == '':
if stdout == "":
self.changed = False
else:
self.changed = True
if self.present:
cmd = [self.cmd, 'import', '-P', pool, '-q'] + ([self.uuid] if self.uuid else [])
cmd = [self.cmd, "import", "-P", pool, "-q"] + ([self.uuid] if self.uuid else [])
(rc, stdout, stderr) = self.module.run_command(cmd)
if rc != 0:
self.module.fail_json(msg=f'Failed to import image: {self.errmsg(stderr)}')
self.module.fail_json(msg=f"Failed to import image: {self.errmsg(stderr)}")
regex = rf'Image {self.uuid} \(.*\) is already installed, skipping'
regex = rf"Image {self.uuid} \(.*\) is already installed, skipping"
if re.match(regex, stdout):
self.changed = False
regex = '.*ActiveImageNotFound.*'
regex = ".*ActiveImageNotFound.*"
if re.match(regex, stderr):
self.changed = False
regex = f'Imported image {self.uuid}.*'
regex = f"Imported image {self.uuid}.*"
if re.match(regex, stdout.splitlines()[-1]):
self.changed = True
else:
cmd = [self.cmd, 'delete', '-P', pool] + ([self.uuid] if self.uuid else [])
cmd = [self.cmd, "delete", "-P", pool] + ([self.uuid] if self.uuid else [])
(rc, stdout, stderr) = self.module.run_command(cmd)
regex = '.*ImageNotInstalled.*'
regex = ".*ImageNotInstalled.*"
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 = f'Deleted image {self.uuid}'
regex = f"Deleted image {self.uuid}"
if re.match(regex, stdout):
self.changed = True
@ -267,12 +267,12 @@ class Imgadm:
def main():
module = AnsibleModule(
argument_spec=dict(
force=dict(type='bool'),
pool=dict(default='zones'),
force=dict(type="bool"),
pool=dict(default="zones"),
source=dict(),
state=dict(required=True, choices=['present', 'absent', 'deleted', 'imported', 'updated', 'vacuumed']),
type=dict(default='imgapi', choices=['imgapi', 'docker', 'dsapi']),
uuid=dict()
state=dict(required=True, choices=["present", "absent", "deleted", "imported", "updated", "vacuumed"]),
type=dict(default="imgapi", choices=["imgapi", "docker", "dsapi"]),
uuid=dict(),
),
# This module relies largely on imgadm(1M) to enforce idempotency, which does not
# provide a "noop" (or equivalent) mode to do a dry-run.
@ -281,30 +281,30 @@ def main():
imgadm = Imgadm(module)
uuid = module.params['uuid']
source = module.params['source']
state = module.params['state']
uuid = module.params["uuid"]
source = module.params["source"]
state = module.params["state"]
result = {'state': state}
result = {"state": state}
# Either manage sources or images.
if source:
result['source'] = source
result["source"] = source
imgadm.manage_sources()
else:
result['uuid'] = uuid
result["uuid"] = uuid
if state == 'updated':
if state == "updated":
imgadm.update_images()
else:
# 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)')
imgadm.manage_images()
result['changed'] = imgadm.changed
result["changed"] = imgadm.changed
module.exit_json(**result)
if __name__ == '__main__':
if __name__ == "__main__":
main()