forked from osbuild/osbuild
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorg.osbuild.containers.unit.create
executable file
·61 lines (50 loc) · 2.32 KB
/
org.osbuild.containers.unit.create
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/python3
import configparser
import sys
import osbuild.api
def validate(filename, cfg):
# ensure the service name does not exceed maximum filename length
if len(filename) > 255:
raise ValueError(f"Error: the {filename} unit exceeds the maximum filename length.")
# Filename extension must match the config:
# .service requires a Service section
# .mount requires a Mount section
if filename.endswith(".container") and "Container" not in cfg:
raise ValueError(f"Error: {filename} unit requires Container section")
if filename.endswith(".volume") and "Volume" not in cfg:
raise ValueError(f"Error: {filename} unit requires Volume section")
if filename.endswith(".network") and "Network" not in cfg:
raise ValueError(f"Error: {filename} unit requires Network section")
def main(tree, options):
filename = options["filename"]
cfg = options["config"]
validate(filename, cfg)
# We trick configparser into letting us write multiple instances of the same option by writing them as keys with no
# value, so we enable allow_no_value
config = configparser.ConfigParser(allow_no_value=True, interpolation=None)
# prevent conversion of the option name to lowercase
config.optionxform = lambda option: option
for section, opts in cfg.items():
if not config.has_section(section):
config.add_section(section)
for option, value in opts.items():
if isinstance(value, list):
for v in value:
if option == "Environment":
# Option value becomes "KEY=VALUE" (quoted)
v = '"' + v["key"] + "=" + str(v["value"]) + '"'
config.set(section, str(option) + "=" + str(v))
else:
config.set(section, option, str(value))
persistent = options.get("unit-path", "usr")
systemd_dir = str()
if persistent == "usr":
systemd_dir = f"{tree}/usr/share/containers/systemd"
elif persistent == "etc":
systemd_dir = f"{tree}/etc/containers/systemd"
with open(f"{systemd_dir}/{filename}", "w", encoding="utf8") as f:
config.write(f, space_around_delimiters=False)
if __name__ == '__main__':
args = osbuild.api.arguments()
r = main(args["tree"], args["options"])
sys.exit(r)