stages/systemd.unit: multiple Environment options

Update the org.osbuild.systemd.unit stage to also support multiple
Environment options where each is an object with {key: value}.  Enable
the allow_no_value option in configparser so we can add the multiple
entries.
This commit is contained in:
Achilleas Koutsou 2024-03-25 14:43:04 +01:00 committed by Michael Vogt
parent e3fd572b94
commit 0dc816c2f9
2 changed files with 34 additions and 4 deletions

View file

@ -22,7 +22,9 @@ def main(tree, options):
unit_dropins_dir = f"{tree}/usr/lib/systemd/user/{unit}.d"
os.makedirs(unit_dropins_dir, exist_ok=True)
config = configparser.ConfigParser()
# 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)
# prevent conversion of the option name to lowercase
config.optionxform = lambda option: option
@ -30,6 +32,14 @@ def main(tree, options):
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":
# Environment arrays are objects
# 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))
with open(f"{unit_dropins_dir}/{dropin_file}", "w", encoding="utf8") as f:

View file

@ -51,8 +51,28 @@
"description": "'Service' configuration section of a unit file.",
"properties": {
"Environment": {
"description": "Sets environment variables for executed process.",
"oneOf": [
{
"type": "string"
},
{
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"properties": {
"key": {
"type": "string",
"description": "Sets environment variables for executed process."
"pattern": "^[A-Za-z_][A-Za-z0-9_]*"
},
"value": {
"type": "string"
}
}
}
}
]
}
}
}