stages/containers.storage: ability to specify a base file

In newer version of the container storage package the config file
moved from `/etc/containers` to `/usr/containers/`. The later is
not marked as config, so we don't want to change it.
The current containers code[1] will read _either_ a file in `usr`
or in `etc` depending on the existence of the latter. This we can
not just write the keys we want into a file in `/etc/containers`
without losing all other defaults set in the config file.
A new option `filebase` is therefore added, that when given will
be read and form the bases of the configuration data. Then data
from the target file (given via `filename`) will be merged into
and finally the actual configuration will be applied on top.

[1] 232bf398bd/types/options.go (L85)
This commit is contained in:
Christian Kellner 2022-11-09 19:19:57 +01:00
parent b0ffe6c2b3
commit a2e212bb26

View file

@ -87,6 +87,10 @@ SCHEMA = r"""
"/usr/share/containers/storage.conf"
]
},
"filebase": {
"type": "string",
"description": "Read the base configuration from this file."
},
"comment": {
"type": "array",
"items": {
@ -166,14 +170,24 @@ def main(tree, options):
location = options.get("filename", DEFAULT_LOCATION)
config = options["config"]
comment = options.get("comment", [])
filebase = options.get("filebase")
path = os.path.join(tree, location.lstrip("/"))
data = {}
with contextlib.suppress(FileNotFoundError):
with open(path, "r", encoding="utf8") as f:
# if a filebase was specified, we use it as base
if filebase:
with open(filebase, "r", encoding="utf8") as f:
data = toml.load(f)
# if the target exists, we merge it
with contextlib.suppress(FileNotFoundError):
with open(path, "r", encoding="utf8") as f:
have = toml.load(f)
merge_config("storage", data, have)
# now merge our configuration into data
merge_config("storage", data, config)
with open(path, "w", encoding="utf8") as f: