stages: add insights-client config stage
This commit is contained in:
parent
e93cd75e5b
commit
a62276c7dc
3 changed files with 143 additions and 0 deletions
50
stages/org.osbuild.insights-client.config
Executable file
50
stages/org.osbuild.insights-client.config
Executable file
|
|
@ -0,0 +1,50 @@
|
|||
#!/usr/bin/python3
|
||||
|
||||
import os
|
||||
import sys
|
||||
|
||||
import osbuild.api
|
||||
|
||||
|
||||
def main(tree, options):
|
||||
insights_client_config = options.get("config", {})
|
||||
proxy = insights_client_config.get("proxy")
|
||||
path = options.get("path", "/etc/insights-client/insights-client.conf")
|
||||
path = os.path.join(tree, path.lstrip("/"))
|
||||
|
||||
changes = {}
|
||||
if proxy is not None:
|
||||
changes["proxy"] = {"key": "proxy", "value": proxy}
|
||||
|
||||
if not os.path.exists(path):
|
||||
with open(path, 'w', encoding="utf8"):
|
||||
pass
|
||||
|
||||
# For each of the configured options, find the first non-commented out instance
|
||||
# of the option and replace it (if necessary). If it does not already exist, append
|
||||
# the option to the end of the file.
|
||||
# Keys are case insensitive, values are not. Try to preserve the key and default to
|
||||
# camel-case.
|
||||
with open(path, 'r', encoding='utf8') as f:
|
||||
lines = f.readlines()
|
||||
|
||||
with open(path, 'w', encoding='utf8') as f:
|
||||
for line in lines:
|
||||
line_list = line.split('=')
|
||||
if len(line_list) == 2:
|
||||
key, current_value = line_list
|
||||
entry = changes.pop(key, None)
|
||||
if entry is not None and current_value != entry['value']:
|
||||
f.write(f"{key}={entry['value']}\n")
|
||||
continue
|
||||
f.write(line)
|
||||
for entry in changes.values():
|
||||
f.write(f"{entry['key']}={entry['value']}\n")
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
args = osbuild.api.arguments()
|
||||
r = main(args["tree"], args["options"])
|
||||
sys.exit(r)
|
||||
31
stages/org.osbuild.insights-client.config.meta.json
Normal file
31
stages/org.osbuild.insights-client.config.meta.json
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
{
|
||||
"summary": "Configure insights-client.",
|
||||
"description": [
|
||||
"Configures insights-client.",
|
||||
"Creates a new config file if one does not exist,",
|
||||
"or modifies the existing one."
|
||||
],
|
||||
"schema": {
|
||||
"additionalProperties": false,
|
||||
"required": [
|
||||
"config"
|
||||
],
|
||||
"properties": {
|
||||
"config": {
|
||||
"additionalProperties": false,
|
||||
"description": "insights-client config options",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"proxy": {
|
||||
"description": "URL for the proxy.",
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
},
|
||||
"path": {
|
||||
"description": "path to the insights-client config file",
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
62
stages/test/test_insights_client_conf.py
Normal file
62
stages/test/test_insights_client_conf.py
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
#!/usr/bin/python3
|
||||
|
||||
STAGE_NAME = "org.osbuild.insights-client.config"
|
||||
|
||||
CONF_FILE = """[insights-client]
|
||||
# Example options in this file are the defaults
|
||||
|
||||
# Change log level, valid options DEBUG, INFO, WARNING, ERROR, CRITICAL. Default DEBUG
|
||||
#loglevel=DEBUG
|
||||
|
||||
# Attempt to auto configure with Satellite server
|
||||
#auto_config=True
|
||||
|
||||
# URL for your proxy. Example: http://user:pass@192.168.100.50:8080
|
||||
#proxy=
|
||||
proxy=https://localhost/
|
||||
"""
|
||||
|
||||
|
||||
def test_insights_client_conf(tmp_path, stage_module):
|
||||
tree = tmp_path / "tree"
|
||||
path = tree / "etc/insights-client/"
|
||||
path.mkdir(parents=True)
|
||||
|
||||
options = {
|
||||
"config": {
|
||||
"proxy": "test-proxy"
|
||||
}
|
||||
}
|
||||
stage_module.main(tree, options)
|
||||
|
||||
with open(path / "insights-client.conf", 'r', encoding="utf8") as f:
|
||||
proxy = f.readline()
|
||||
expected_proxy = options["config"]["proxy"]
|
||||
expected = f"proxy={expected_proxy}\n"
|
||||
assert proxy == expected
|
||||
|
||||
|
||||
def test_insights_client_conf_already_exists(tmp_path, stage_module):
|
||||
tree = tmp_path / "tree"
|
||||
path = tree / "etc/insights-client/"
|
||||
path.mkdir(parents=True)
|
||||
|
||||
with open(path / "insights-client.conf", "w", encoding="utf8") as f:
|
||||
f.write(CONF_FILE)
|
||||
|
||||
options = {
|
||||
"config": {
|
||||
"proxy": "test-proxy"
|
||||
}
|
||||
}
|
||||
stage_module.main(tree, options)
|
||||
|
||||
with open(path / "insights-client.conf", 'r', encoding="utf8") as f:
|
||||
expected_proxy = options["config"]["proxy"]
|
||||
expected = f"proxy={expected_proxy}\n"
|
||||
original_proxy = "proxy=https://localhost/"
|
||||
commented_proxy = f"#proxy={expected_proxy}\n"
|
||||
lines = f.readlines()
|
||||
assert expected in lines
|
||||
assert original_proxy not in lines
|
||||
assert commented_proxy not in lines
|
||||
Loading…
Add table
Add a link
Reference in a new issue