From 03b467da7281985a407b0ca394cc5dc279e1362d Mon Sep 17 00:00:00 2001 From: Achilleas Koutsou Date: Tue, 7 Feb 2023 20:24:28 +0100 Subject: [PATCH] stages: add shell.init stage New stage for writing shell init files in /etc/profile.d. Currently only supports writing environment variables as key-value pairs. --- stages/org.osbuild.shell.init | 60 +++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100755 stages/org.osbuild.shell.init diff --git a/stages/org.osbuild.shell.init b/stages/org.osbuild.shell.init new file mode 100755 index 00000000..626de098 --- /dev/null +++ b/stages/org.osbuild.shell.init @@ -0,0 +1,60 @@ +#!/usr/bin/python3 +""" +Write systemwide initialization files, executed for login shells. + +Write files to /etc/profile.d with key-value pairs to be set in the shell environment. +Each file written is sourced by the login shell on initialization. +""" +import os +import sys + +import osbuild.api + +SCHEMA_2 = r""" +"options": { + "properties": { + "patternProperties": { + "^[\\w.-_]{1,250}$": { + "type": "object", + "additionalProperties": false, + "properties": { + "env": { + "type": "array", + "description": "Environment variables to set and export on shell init.", + "minItems": 1, + "items": { + "type": "object", + "additionalProperties": false, + "required": ["key", "value"], + "properties": { + "key": "string", + "value": "string" + } + } + } + } + } + } + } +} +""" + + +def main(tree, options): + for filename, opts in options.items(): + lines = [] + for item in opts["env"]: + key = item["key"] + value = item["value"] + lines.append(f'export {key}="{value}"\n') + + path = os.path.join(tree, "etc/profile.d", filename) + with open(path, "w", encoding="utf8") as f: + f.writelines(lines) + + return 0 + + +if __name__ == '__main__': + args = osbuild.api.arguments() + sys.exit(main(args["tree"], args["options"]))