Add new org.osbuild.yum.config stage

Add a new stage for modifying YUM global configuration.

Add a unit test case for the newly added stage.

Because we test stages on Fedora, where there is no YUM, and this stage
is mostly intended for being used with RHEL-7 images, the stage does not
produce error in case the `/etc/yum.conf` file does not exist. It rather
produces a warning and creates the file. Ideally the stage would produce
an error in case the configuration file does not exist, but that would
be impossible to test on recent Fedora.

Signed-off-by: Tomas Hozza <thozza@redhat.com>
This commit is contained in:
Tomas Hozza 2021-11-04 17:08:54 +01:00 committed by Tomáš Hozza
parent 66a1fbad9d
commit 58ec1c9a83
6 changed files with 1649 additions and 0 deletions

62
stages/org.osbuild.yum.config Executable file
View file

@ -0,0 +1,62 @@
#!/usr/bin/python3
"""
Change YUM configuration.
The stage changes persistent YUM configuration on the filesystem.
"""
import sys
import iniparse
import osbuild.api
SCHEMA = r"""
"additionalProperties": false,
"description": "YUM configuration.",
"properties": {
"config": {
"type": "object",
"additionalProperties": false,
"description": "YUM global configuration.",
"properties": {
"http_caching": {
"type": "string",
"enum": ["all", "packages", "lazy:packages", "none"],
"description": "Determines how upstream HTTP caches are instructed to handle any HTTP downloads that YUM does."
}
}
}
}
"""
def main(tree, options):
config_options = options.get("config")
yum_config_path = f"{tree}/etc/yum.conf"
yum_config = iniparse.SafeConfigParser()
if config_options:
try:
with open(yum_config_path, "r") as f:
yum_config.readfp(f)
except FileNotFoundError:
print(f"Warning: YUM configuration file '{yum_config_path}' does not exist, will create it.")
# only global options from the [main] section are supported
if not yum_config.has_section("main"):
yum_config.add_section("main")
for option, value in config_options.items():
yum_config.set("main", option, value)
with open(yum_config_path, "w") as f:
yum_config.write(f)
return 0
if __name__ == '__main__':
args = osbuild.api.arguments()
r = main(args["tree"], args["options"])
sys.exit(r)