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

@ -159,146 +159,148 @@ class Zone:
self.msg = []
self.module = module
self.path = self.module.params['path']
self.name = self.module.params['name']
self.sparse = self.module.params['sparse']
self.root_password = self.module.params['root_password']
self.timeout = self.module.params['timeout']
self.config = self.module.params['config']
self.create_options = self.module.params['create_options']
self.install_options = self.module.params['install_options']
self.attach_options = self.module.params['attach_options']
self.path = self.module.params["path"]
self.name = self.module.params["name"]
self.sparse = self.module.params["sparse"]
self.root_password = self.module.params["root_password"]
self.timeout = self.module.params["timeout"]
self.config = self.module.params["config"]
self.create_options = self.module.params["create_options"]
self.install_options = self.module.params["install_options"]
self.attach_options = self.module.params["attach_options"]
self.zoneadm_cmd = self.module.get_bin_path('zoneadm', True)
self.zonecfg_cmd = self.module.get_bin_path('zonecfg', True)
self.ssh_keygen_cmd = self.module.get_bin_path('ssh-keygen', True)
self.zoneadm_cmd = self.module.get_bin_path("zoneadm", True)
self.zonecfg_cmd = self.module.get_bin_path("zonecfg", True)
self.ssh_keygen_cmd = self.module.get_bin_path("ssh-keygen", True)
if self.module.check_mode:
self.msg.append('Running in check mode')
self.msg.append("Running in check mode")
if platform.system() != 'SunOS':
self.module.fail_json(msg='This module requires Solaris')
if platform.system() != "SunOS":
self.module.fail_json(msg="This module requires Solaris")
(self.os_major, self.os_minor) = platform.release().split('.')
(self.os_major, self.os_minor) = platform.release().split(".")
if int(self.os_minor) < 10:
self.module.fail_json(msg='This module requires Solaris 10 or later')
self.module.fail_json(msg="This module requires Solaris 10 or later")
match = re.match('^[a-zA-Z0-9][-_.a-zA-Z0-9]{0,62}$', self.name)
match = re.match("^[a-zA-Z0-9][-_.a-zA-Z0-9]{0,62}$", self.name)
if not match:
self.module.fail_json(msg="Provided zone name is not a valid zone name. "
"Please refer documentation for correct zone name specifications.")
self.module.fail_json(
msg="Provided zone name is not a valid zone name. "
"Please refer documentation for correct zone name specifications."
)
def configure(self):
if not self.path:
self.module.fail_json(msg='Missing required argument: path')
self.module.fail_json(msg="Missing required argument: path")
if not self.module.check_mode:
t = tempfile.NamedTemporaryFile(delete=False, mode='wt')
t = tempfile.NamedTemporaryFile(delete=False, mode="wt")
if self.sparse:
t.write(f'create {self.create_options}\n')
self.msg.append('creating sparse-root zone')
t.write(f"create {self.create_options}\n")
self.msg.append("creating sparse-root zone")
else:
t.write(f'create -b {self.create_options}\n')
self.msg.append('creating whole-root zone')
t.write(f"create -b {self.create_options}\n")
self.msg.append("creating whole-root zone")
t.write(f'set zonepath={self.path}\n')
t.write(f'{self.config}\n')
t.write(f"set zonepath={self.path}\n")
t.write(f"{self.config}\n")
t.close()
cmd = [self.zonecfg_cmd, '-z', self.name, '-f', t.name]
cmd = [self.zonecfg_cmd, "-z", self.name, "-f", t.name]
(rc, out, err) = self.module.run_command(cmd)
if rc != 0:
self.module.fail_json(msg=f'Failed to create zone. {out + err}')
self.module.fail_json(msg=f"Failed to create zone. {out + err}")
os.unlink(t.name)
self.changed = True
self.msg.append('zone configured')
self.msg.append("zone configured")
def install(self):
if not self.module.check_mode:
cmd = [self.zoneadm_cmd, '-z', self.name, 'install', self.install_options]
cmd = [self.zoneadm_cmd, "-z", self.name, "install", self.install_options]
(rc, out, err) = self.module.run_command(cmd)
if rc != 0:
self.module.fail_json(msg=f'Failed to install zone. {out + err}')
self.module.fail_json(msg=f"Failed to install zone. {out + err}")
if int(self.os_minor) == 10:
self.configure_sysid()
self.configure_password()
self.configure_ssh_keys()
self.changed = True
self.msg.append('zone installed')
self.msg.append("zone installed")
def uninstall(self):
if self.is_installed():
if not self.module.check_mode:
cmd = [self.zoneadm_cmd, '-z', self.name, 'uninstall', '-F']
cmd = [self.zoneadm_cmd, "-z", self.name, "uninstall", "-F"]
(rc, out, err) = self.module.run_command(cmd)
if rc != 0:
self.module.fail_json(msg=f'Failed to uninstall zone. {out + err}')
self.module.fail_json(msg=f"Failed to uninstall zone. {out + err}")
self.changed = True
self.msg.append('zone uninstalled')
self.msg.append("zone uninstalled")
def configure_sysid(self):
if os.path.isfile(f'{self.path}/root/etc/.UNCONFIGURED'):
os.unlink(f'{self.path}/root/etc/.UNCONFIGURED')
if os.path.isfile(f"{self.path}/root/etc/.UNCONFIGURED"):
os.unlink(f"{self.path}/root/etc/.UNCONFIGURED")
open(f'{self.path}/root/noautoshutdown', 'w').close()
open(f"{self.path}/root/noautoshutdown", "w").close()
with open(f'{self.path}/root/etc/nodename', 'w') as node:
with open(f"{self.path}/root/etc/nodename", "w") as node:
node.write(self.name)
with open(f'{self.path}/root/etc/.sysIDtool.state', 'w') as id:
id.write('1 # System previously configured?\n')
id.write('1 # Bootparams succeeded?\n')
id.write('1 # System is on a network?\n')
id.write('1 # Extended network information gathered?\n')
id.write('0 # Autobinder succeeded?\n')
id.write('1 # Network has subnets?\n')
id.write('1 # root password prompted for?\n')
id.write('1 # locale and term prompted for?\n')
id.write('1 # security policy in place\n')
id.write('1 # NFSv4 domain configured\n')
id.write('0 # Auto Registration Configured\n')
id.write('vt100')
with open(f"{self.path}/root/etc/.sysIDtool.state", "w") as id:
id.write("1 # System previously configured?\n")
id.write("1 # Bootparams succeeded?\n")
id.write("1 # System is on a network?\n")
id.write("1 # Extended network information gathered?\n")
id.write("0 # Autobinder succeeded?\n")
id.write("1 # Network has subnets?\n")
id.write("1 # root password prompted for?\n")
id.write("1 # locale and term prompted for?\n")
id.write("1 # security policy in place\n")
id.write("1 # NFSv4 domain configured\n")
id.write("0 # Auto Registration Configured\n")
id.write("vt100")
def configure_ssh_keys(self):
rsa_key_file = f'{self.path}/root/etc/ssh/ssh_host_rsa_key'
dsa_key_file = f'{self.path}/root/etc/ssh/ssh_host_dsa_key'
rsa_key_file = f"{self.path}/root/etc/ssh/ssh_host_rsa_key"
dsa_key_file = f"{self.path}/root/etc/ssh/ssh_host_dsa_key"
if not os.path.isfile(rsa_key_file):
cmd = [self.ssh_keygen_cmd, '-f', rsa_key_file, '-t', 'rsa', '-N', '']
cmd = [self.ssh_keygen_cmd, "-f", rsa_key_file, "-t", "rsa", "-N", ""]
(rc, out, err) = self.module.run_command(cmd)
if rc != 0:
self.module.fail_json(msg=f'Failed to create rsa key. {out + err}')
self.module.fail_json(msg=f"Failed to create rsa key. {out + err}")
if not os.path.isfile(dsa_key_file):
cmd = [self.ssh_keygen_cmd, '-f', dsa_key_file, '-t', 'dsa', '-N', '']
cmd = [self.ssh_keygen_cmd, "-f", dsa_key_file, "-t", "dsa", "-N", ""]
(rc, out, err) = self.module.run_command(cmd)
if rc != 0:
self.module.fail_json(msg=f'Failed to create dsa key. {out + err}')
self.module.fail_json(msg=f"Failed to create dsa key. {out + err}")
def configure_password(self):
shadow = f'{self.path}/root/etc/shadow'
shadow = f"{self.path}/root/etc/shadow"
if self.root_password:
with open(shadow, 'r') as f:
with open(shadow, "r") as f:
lines = f.readlines()
for i in range(0, len(lines)):
fields = lines[i].split(':')
if fields[0] == 'root':
fields = lines[i].split(":")
if fields[0] == "root":
fields[1] = self.root_password
lines[i] = ':'.join(fields)
lines[i] = ":".join(fields)
with open(shadow, 'w') as f:
with open(shadow, "w") as f:
for line in lines:
f.write(line)
def boot(self):
if not self.module.check_mode:
cmd = [self.zoneadm_cmd, '-z', self.name, 'boot']
cmd = [self.zoneadm_cmd, "-z", self.name, "boot"]
(rc, out, err) = self.module.run_command(cmd)
if rc != 0:
self.module.fail_json(msg=f'Failed to boot zone. {out + err}')
self.module.fail_json(msg=f"Failed to boot zone. {out + err}")
"""
The boot command can return before the zone has fully booted. This is especially
@ -310,14 +312,14 @@ class Zone:
elapsed = 0
while True:
if elapsed > self.timeout:
self.module.fail_json(msg='timed out waiting for zone to boot')
self.module.fail_json(msg="timed out waiting for zone to boot")
rc = os.system(f'ps -z {self.name} -o args|grep "ttymon.*-d /dev/console" > /dev/null 2>/dev/null')
if rc == 0:
break
time.sleep(10)
elapsed += 10
self.changed = True
self.msg.append('zone booted')
self.msg.append("zone booted")
def destroy(self):
if self.is_running():
@ -325,42 +327,42 @@ class Zone:
if self.is_installed():
self.uninstall()
if not self.module.check_mode:
cmd = [self.zonecfg_cmd, '-z', self.name, 'delete', '-F']
cmd = [self.zonecfg_cmd, "-z", self.name, "delete", "-F"]
(rc, out, err) = self.module.run_command(cmd)
if rc != 0:
self.module.fail_json(msg=f'Failed to delete zone. {out + err}')
self.module.fail_json(msg=f"Failed to delete zone. {out + err}")
self.changed = True
self.msg.append('zone deleted')
self.msg.append("zone deleted")
def stop(self):
if not self.module.check_mode:
cmd = [self.zoneadm_cmd, '-z', self.name, 'halt']
cmd = [self.zoneadm_cmd, "-z", self.name, "halt"]
(rc, out, err) = self.module.run_command(cmd)
if rc != 0:
self.module.fail_json(msg=f'Failed to stop zone. {out + err}')
self.module.fail_json(msg=f"Failed to stop zone. {out + err}")
self.changed = True
self.msg.append('zone stopped')
self.msg.append("zone stopped")
def detach(self):
if not self.module.check_mode:
cmd = [self.zoneadm_cmd, '-z', self.name, 'detach']
cmd = [self.zoneadm_cmd, "-z", self.name, "detach"]
(rc, out, err) = self.module.run_command(cmd)
if rc != 0:
self.module.fail_json(msg=f'Failed to detach zone. {out + err}')
self.module.fail_json(msg=f"Failed to detach zone. {out + err}")
self.changed = True
self.msg.append('zone detached')
self.msg.append("zone detached")
def attach(self):
if not self.module.check_mode:
cmd = [self.zoneadm_cmd, '-z', self.name, 'attach', self.attach_options]
cmd = [self.zoneadm_cmd, "-z", self.name, "attach", self.attach_options]
(rc, out, err) = self.module.run_command(cmd)
if rc != 0:
self.module.fail_json(msg=f'Failed to attach zone. {out + err}')
self.module.fail_json(msg=f"Failed to attach zone. {out + err}")
self.changed = True
self.msg.append('zone attached')
self.msg.append("zone attached")
def exists(self):
cmd = [self.zoneadm_cmd, '-z', self.name, 'list']
cmd = [self.zoneadm_cmd, "-z", self.name, "list"]
(rc, out, err) = self.module.run_command(cmd)
if rc == 0:
return True
@ -368,25 +370,25 @@ class Zone:
return False
def is_running(self):
return self.status() == 'running'
return self.status() == "running"
def is_installed(self):
return self.status() == 'installed'
return self.status() == "installed"
def is_configured(self):
return self.status() == 'configured'
return self.status() == "configured"
def status(self):
cmd = [self.zoneadm_cmd, '-z', self.name, 'list', '-p']
cmd = [self.zoneadm_cmd, "-z", self.name, "list", "-p"]
(rc, out, err) = self.module.run_command(cmd)
if rc == 0:
return out.split(':')[2]
return out.split(":")[2]
else:
return 'undefined'
return "undefined"
def state_present(self):
if self.exists():
self.msg.append('zone already exists')
self.msg.append("zone already exists")
else:
self.configure()
self.install()
@ -394,7 +396,7 @@ class Zone:
def state_running(self):
self.state_present()
if self.is_running():
self.msg.append('zone already running')
self.msg.append("zone already running")
else:
self.boot()
@ -402,7 +404,7 @@ class Zone:
if self.exists():
self.stop()
else:
self.module.fail_json(msg='zone does not exist')
self.module.fail_json(msg="zone does not exist")
def state_absent(self):
if self.exists():
@ -410,73 +412,86 @@ class Zone:
self.stop()
self.destroy()
else:
self.msg.append('zone does not exist')
self.msg.append("zone does not exist")
def state_configured(self):
if self.exists():
self.msg.append('zone already exists')
self.msg.append("zone already exists")
else:
self.configure()
def state_detached(self):
if not self.exists():
self.module.fail_json(msg='zone does not exist')
self.module.fail_json(msg="zone does not exist")
if self.is_configured():
self.msg.append('zone already detached')
self.msg.append("zone already detached")
else:
self.stop()
self.detach()
def state_attached(self):
if not self.exists():
self.msg.append('zone does not exist')
self.msg.append("zone does not exist")
if self.is_configured():
self.attach()
else:
self.msg.append('zone already attached')
self.msg.append("zone already attached")
def main():
module = AnsibleModule(
argument_spec=dict(
name=dict(type='str', required=True),
state=dict(type='str', default='present',
choices=['absent', 'attached', 'configured', 'detached', 'installed', 'present', 'running', 'started', 'stopped']),
path=dict(type='str'),
sparse=dict(type='bool', default=False),
root_password=dict(type='str', no_log=True),
timeout=dict(type='int', default=600),
config=dict(type='str', default=''),
create_options=dict(type='str', default=''),
install_options=dict(type='str', default=''),
attach_options=dict(type='str', default=''),
name=dict(type="str", required=True),
state=dict(
type="str",
default="present",
choices=[
"absent",
"attached",
"configured",
"detached",
"installed",
"present",
"running",
"started",
"stopped",
],
),
path=dict(type="str"),
sparse=dict(type="bool", default=False),
root_password=dict(type="str", no_log=True),
timeout=dict(type="int", default=600),
config=dict(type="str", default=""),
create_options=dict(type="str", default=""),
install_options=dict(type="str", default=""),
attach_options=dict(type="str", default=""),
),
supports_check_mode=True,
)
zone = Zone(module)
state = module.params['state']
state = module.params["state"]
if state == 'running' or state == 'started':
if state == "running" or state == "started":
zone.state_running()
elif state == 'present' or state == 'installed':
elif state == "present" or state == "installed":
zone.state_present()
elif state == 'stopped':
elif state == "stopped":
zone.state_stopped()
elif state == 'absent':
elif state == "absent":
zone.state_absent()
elif state == 'configured':
elif state == "configured":
zone.state_configured()
elif state == 'detached':
elif state == "detached":
zone.state_detached()
elif state == 'attached':
elif state == "attached":
zone.state_attached()
else:
module.fail_json(msg=f'Invalid state: {state}')
module.fail_json(msg=f"Invalid state: {state}")
module.exit_json(changed=zone.changed, msg=', '.join(zone.msg))
module.exit_json(changed=zone.changed, msg=", ".join(zone.msg))
if __name__ == '__main__':
if __name__ == "__main__":
main()