1
0
Fork 0

added map_param to ease the generation simple parameters for systemd.

it reduces the boilerplate for simple key-value parameters
Dieser Commit ist enthalten in:
Sebastian Tobie 2023-04-23 09:23:15 +02:00
Ursprung f2f3580337
Commit f7880850dc
1 geänderte Dateien mit 23 neuen und 14 gelöschten Zeilen

Datei anzeigen

@ -230,22 +230,31 @@ class SystemdUnitModule(AnsibleModule):
def header(self) -> str:
header = "[Unit]\n"
if self.get("description", False):
header += "Description={}\n".format(self.get("description"))
if self.get("documentation", False):
header += "Documentation={}\n".format(" ".join(self.get("documentation")))
if self.get("requires", False):
header += "Requires={}\n".format(" ".join(self.get("requires")))
if self.get("wants", False):
header += "Wants={}\n".format(" ".join(self.get("wants")))
if self.get("partof", False):
header += "PartOf={}\n".format(" ".join(self.get("partof")))
if self.get("before", False):
header += "Before={}\n".format(" ".join(self.get("before")))
if self.get("after", False):
header += "After={}\n".format(" ".join(self.get("after")))
header += "\n".join(
self.map_param(
description="Description",
documentation="Documentation",
requires="Requires",
wants="Wants",
partof="PartOf",
before="Before",
after="After",
)
)
return header
def map_param(self, **parammap: str):
"""maps an dict with keys for an section with given params. The key of the dictionary is the parameter and the value is the key in the unitfile. If an parameter has multiple values it adds multiple entries"""
output: list[str] = []
for param, key in parammap:
if self.params[param] is not None:
params = self.params[param]
if isinstance(params, list):
output.extend(("{}={}".format(key, p) for p in params))
else:
output.append("{}={}".format(key, self.params[param]))
return output
def unitfile_gen(self):
path = self.tmpdir / "newunit"
with open(path, "w") as unit: