1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-04-03 10:47:08 +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

@ -91,76 +91,71 @@ from ansible.module_utils.common.text.converters import to_bytes
def main():
module = AnsibleModule(
argument_spec=dict(
name=dict(type='str', required=True),
state=dict(type='str', required=True, choices=['absent', 'opts_absent', 'opts_present', 'present']),
backing_device=dict(type='str'),
password=dict(type='path'),
opts=dict(type='str'),
path=dict(type='path', default='/etc/crypttab')
name=dict(type="str", required=True),
state=dict(type="str", required=True, choices=["absent", "opts_absent", "opts_present", "present"]),
backing_device=dict(type="str"),
password=dict(type="path"),
opts=dict(type="str"),
path=dict(type="path", default="/etc/crypttab"),
),
supports_check_mode=True,
)
backing_device = module.params['backing_device']
password = module.params['password']
opts = module.params['opts']
state = module.params['state']
path = module.params['path']
name = module.params['name']
if name.startswith('/dev/mapper/'):
name = name[len('/dev/mapper/'):]
backing_device = module.params["backing_device"]
password = module.params["password"]
opts = module.params["opts"]
state = module.params["state"]
path = module.params["path"]
name = module.params["name"]
if name.startswith("/dev/mapper/"):
name = name[len("/dev/mapper/") :]
if state != 'absent' and backing_device is None and password is None and opts is None:
module.fail_json(msg="expected one or more of 'backing_device', 'password' or 'opts'",
**module.params)
if state != "absent" and backing_device is None and password is None and opts is None:
module.fail_json(msg="expected one or more of 'backing_device', 'password' or 'opts'", **module.params)
if 'opts' in state and (backing_device is not None or password is not None):
module.fail_json(msg=f"cannot update 'backing_device' or 'password' when state={state}",
**module.params)
if "opts" in state and (backing_device is not None or password is not None):
module.fail_json(msg=f"cannot update 'backing_device' or 'password' when state={state}", **module.params)
for arg_name, arg in (('name', name),
('backing_device', backing_device),
('password', password),
('opts', opts)):
if arg is not None and (' ' in arg or '\t' in arg or arg == ''):
module.fail_json(msg=f"invalid '{arg_name}': contains white space or is empty",
**module.params)
for arg_name, arg in (("name", name), ("backing_device", backing_device), ("password", password), ("opts", opts)):
if arg is not None and (" " in arg or "\t" in arg or arg == ""):
module.fail_json(msg=f"invalid '{arg_name}': contains white space or is empty", **module.params)
try:
crypttab = Crypttab(path)
existing_line = crypttab.match(name)
except Exception as e:
module.fail_json(msg=f"failed to open and parse crypttab file: {e}", exception=traceback.format_exc(), **module.params)
module.fail_json(
msg=f"failed to open and parse crypttab file: {e}", exception=traceback.format_exc(), **module.params
)
if 'present' in state and existing_line is None and backing_device is None:
module.fail_json(msg="'backing_device' required to add a new entry",
**module.params)
if "present" in state and existing_line is None and backing_device is None:
module.fail_json(msg="'backing_device' required to add a new entry", **module.params)
changed, reason = False, '?'
changed, reason = False, "?"
if state == 'absent':
if state == "absent":
if existing_line is not None:
changed, reason = existing_line.remove()
elif state == 'present':
elif state == "present":
if existing_line is not None:
changed, reason = existing_line.set(backing_device, password, opts)
else:
changed, reason = crypttab.add(Line(None, name, backing_device, password, opts))
elif state == 'opts_present':
elif state == "opts_present":
if existing_line is not None:
changed, reason = existing_line.opts.add(opts)
else:
changed, reason = crypttab.add(Line(None, name, backing_device, password, opts))
elif state == 'opts_absent':
elif state == "opts_absent":
if existing_line is not None:
changed, reason = existing_line.opts.remove(opts)
if changed and not module.check_mode:
with open(path, 'wb') as f:
f.write(to_bytes(crypttab, errors='surrogate_or_strict'))
with open(path, "wb") as f:
f.write(to_bytes(crypttab, errors="surrogate_or_strict"))
module.exit_json(changed=changed, msg=reason, **module.params)
@ -174,15 +169,15 @@ class Crypttab:
if not os.path.exists(path):
if not os.path.exists(os.path.dirname(path)):
os.makedirs(os.path.dirname(path))
open(path, 'a').close()
open(path, "a").close()
with open(path, 'r') as f:
with open(path, "r") as f:
for line in f.readlines():
self._lines.append(Line(line))
def add(self, line):
self._lines.append(line)
return True, 'added line'
return True, "added line"
def lines(self):
for line in self._lines:
@ -199,11 +194,11 @@ class Crypttab:
lines = []
for line in self._lines:
lines.append(str(line))
crypttab = '\n'.join(lines)
crypttab = "\n".join(lines)
if len(crypttab) == 0:
crypttab += '\n'
if crypttab[-1] != '\n':
crypttab += '\n'
crypttab += "\n"
if crypttab[-1] != "\n":
crypttab += "\n"
return crypttab
@ -216,7 +211,7 @@ class Line:
self.opts = Options(opts)
if line is not None:
self.line = self.line.rstrip('\n')
self.line = self.line.rstrip("\n")
if self._line_valid(line):
self.name, backing_device, password, opts = self._split_line(line)
@ -239,10 +234,10 @@ class Line:
self.opts = opts
changed = True
return changed, 'updated line'
return changed, "updated line"
def _line_valid(self, line):
if not line.strip() or line.startswith('#') or len(line.split()) not in (2, 3, 4):
if not line.strip() or line.startswith("#") or len(line.split()) not in (2, 3, 4):
return False
return True
@ -257,14 +252,11 @@ class Line:
except IndexError:
field3 = None
return (fields[0],
fields[1],
field2,
field3)
return (fields[0], fields[1], field2, field3)
def remove(self):
self.line, self.name, self.backing_device = '', None, None
return True, 'removed line'
self.line, self.name, self.backing_device = "", None, None
return True, "removed line"
def valid(self):
if self.name is not None and self.backing_device is not None:
@ -278,22 +270,22 @@ class Line:
if self.password is not None:
fields.append(self.password)
else:
fields.append('none')
fields.append("none")
if self.opts:
fields.append(str(self.opts))
return ' '.join(fields)
return " ".join(fields)
return self.line
class Options(dict):
"""opts_string looks like: 'discard,foo=bar,baz=greeble' """
"""opts_string looks like: 'discard,foo=bar,baz=greeble'"""
def __init__(self, opts_string):
super().__init__()
self.itemlist = []
if opts_string is not None:
for opt in opts_string.split(','):
kv = opt.split('=')
for opt in opts_string.split(","):
kv = opt.split("=")
if len(kv) > 1:
k, v = (kv[0], kv[1])
else:
@ -309,7 +301,7 @@ class Options(dict):
else:
changed = True
self[k] = v
return changed, 'updated options'
return changed, "updated options"
def remove(self, opts_string):
changed = False
@ -317,7 +309,7 @@ class Options(dict):
if k in self:
del self[k]
changed = True
return changed, 'removed options'
return changed, "removed options"
def keys(self):
return self.itemlist
@ -349,9 +341,9 @@ class Options(dict):
if v is None:
ret.append(k)
else:
ret.append(f'{k}={v}')
return ','.join(ret)
ret.append(f"{k}={v}")
return ",".join(ret)
if __name__ == '__main__':
if __name__ == "__main__":
main()