diff --git a/stages/org.osbuild.dnf.config b/stages/org.osbuild.dnf.config index 4aa5a894..a9a160e0 100755 --- a/stages/org.osbuild.dnf.config +++ b/stages/org.osbuild.dnf.config @@ -6,8 +6,9 @@ The stage changes persistent DNF configuration on the filesystem. Currently, only DNF variables can be defined. """ - +import os import sys +import iniparse import osbuild.api @@ -30,6 +31,13 @@ SCHEMA = r""" "description": "Value of the variable." } } + }, + "config_main": { + "ip_resolve": { + "type": "string", + "enum": ["4", "IPv4", "6", "IPv6"], + "description": "Determines how DNF resolves host names." + } } }, "additionalProperties": false, @@ -41,6 +49,16 @@ SCHEMA = r""" "items": { "$ref": "#/definitions/variable" } + }, + "config": { + "additionalProperties": false, + "type": "object", + "description": "DNF global configuration.", + "properties": { + "main": { + "$ref": "#/definitions/config_main" + } + } } } """ @@ -61,12 +79,53 @@ def configure_variable(tree, name, value): f.write(value + "\n") +def make_value(value): + val = str(value) + return val + + +def make_section(cfg, name, settings): + if not cfg.has_section(name): + cfg.add_section(name) + + for key, value in settings.items(): + val = make_value(value) + cfg.set(name, key, val) + + +def make_dnf_config(tree, config_options): + """ + Merges the given config object into /etc/dnf/dnf.conf, overwriting existing + values. + """ + dnf_config_path = f"{tree}/etc/dnf/dnf.conf" + dnf_config = iniparse.SafeConfigParser() + + try: + with open(dnf_config_path, "r") as f: + dnf_config.readfp(f) + except FileNotFoundError: + print(f"Warning: DNF configuration file '{dnf_config_path}' does not exist, will create it.") + os.makedirs(f"{tree}/etc/dnf", exist_ok=True) + + for section, items in config_options.items(): + make_section(dnf_config, section, items) + + with open(dnf_config_path, "w") as f: + os.fchmod(f.fileno(), 0o644) + dnf_config.write(f) + + def main(tree, options): variables = options.get("variables", []) for variable in variables: configure_variable(tree, variable["name"], variable["value"]) + config_options = options.get("config") + if config_options: + make_dnf_config(tree, config_options) + return 0 diff --git a/test/data/stages/dnf.config/b.json b/test/data/stages/dnf.config/b.json index a519c161..2f937567 100644 --- a/test/data/stages/dnf.config/b.json +++ b/test/data/stages/dnf.config/b.json @@ -500,7 +500,12 @@ "name": "releasever", "value": "8.4" } - ] + ], + "config": { + "main": { + "ip_resolve": "4" + } + } } } ] diff --git a/test/data/stages/dnf.config/b.mpp.json b/test/data/stages/dnf.config/b.mpp.json index e8995894..2cf99836 100644 --- a/test/data/stages/dnf.config/b.mpp.json +++ b/test/data/stages/dnf.config/b.mpp.json @@ -37,7 +37,12 @@ "name": "releasever", "value": "8.4" } - ] + ], + "config": { + "main": { + "ip_resolve": "4" + } + } } } ] diff --git a/test/data/stages/dnf.config/diff.json b/test/data/stages/dnf.config/diff.json index d7892acf..52dfb64c 100644 --- a/test/data/stages/dnf.config/diff.json +++ b/test/data/stages/dnf.config/diff.json @@ -3,5 +3,12 @@ "/etc/dnf/vars/releasever" ], "deleted_files": [], - "differences": {} + "differences": { + "/etc/dnf/dnf.conf": { + "content": [ + "sha256:c517559154f0de26716dd6464fbf524aeebf965cb8cf89ebde69e51355633878", + "sha256:86795d352e3107dfb5257a9a4466fdc93668ec0776def8f62e211560f6fabeac" + ] + } + } }