From 0dc816c2f91e8bbb03b00240ee199af23b009b75 Mon Sep 17 00:00:00 2001 From: Achilleas Koutsou Date: Mon, 25 Mar 2024 14:43:04 +0100 Subject: [PATCH] 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. --- stages/org.osbuild.systemd.unit | 14 +++++++++++-- stages/org.osbuild.systemd.unit.meta.json | 24 +++++++++++++++++++++-- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/stages/org.osbuild.systemd.unit b/stages/org.osbuild.systemd.unit index 7f278f84..354c664e 100755 --- a/stages/org.osbuild.systemd.unit +++ b/stages/org.osbuild.systemd.unit @@ -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,7 +32,15 @@ def main(tree, options): if not config.has_section(section): config.add_section(section) for option, value in opts.items(): - config.set(section, option, str(value)) + 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: config.write(f, space_around_delimiters=False) diff --git a/stages/org.osbuild.systemd.unit.meta.json b/stages/org.osbuild.systemd.unit.meta.json index 5b534069..4b39df52 100644 --- a/stages/org.osbuild.systemd.unit.meta.json +++ b/stages/org.osbuild.systemd.unit.meta.json @@ -51,8 +51,28 @@ "description": "'Service' configuration section of a unit file.", "properties": { "Environment": { - "type": "string", - "description": "Sets environment variables for executed process." + "description": "Sets environment variables for executed process.", + "oneOf": [ + { + "type": "string" + }, + { + "type": "array", + "items": { + "type": "object", + "additionalProperties": false, + "properties": { + "key": { + "type": "string", + "pattern": "^[A-Za-z_][A-Za-z0-9_]*" + }, + "value": { + "type": "string" + } + } + } + } + ] } } }