summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDevaev Maxim <[email protected]>2019-09-19 06:47:54 +0300
committerDevaev Maxim <[email protected]>2019-09-19 06:47:54 +0300
commit300d4603ca725dfb8749e466eabe0561ad54cb6b (patch)
tree2f53653dba9c95a233f404dafe1da6aff71011bf
parent313e2c77fd94b1384ebb9057b43115ef14a642ba (diff)
fixed config loading
-rw-r--r--configs/kvmd/override.yaml2
-rw-r--r--kvmd/apps/__init__.py69
-rw-r--r--kvmd/apps/cleanup/__init__.py7
-rw-r--r--kvmd/apps/htpasswd/__init__.py3
-rw-r--r--kvmd/apps/ipmi/__init__.py1
-rw-r--r--kvmd/apps/kvmd/__init__.py9
-rw-r--r--kvmd/yamlconf/__init__.py2
7 files changed, 44 insertions, 49 deletions
diff --git a/configs/kvmd/override.yaml b/configs/kvmd/override.yaml
index c3190c07..0e572b4f 100644
--- a/configs/kvmd/override.yaml
+++ b/configs/kvmd/override.yaml
@@ -5,3 +5,5 @@
# heartbeat: 5.0
#
# It will be applied AFTER other configs and "!include" directives and BEFORE validation.
+
+{}
diff --git a/kvmd/apps/__init__.py b/kvmd/apps/__init__.py
index 5121615b..9ec8050d 100644
--- a/kvmd/apps/__init__.py
+++ b/kvmd/apps/__init__.py
@@ -75,8 +75,7 @@ def init(
description: Optional[str]=None,
add_help: bool=True,
argv: Optional[List[str]]=None,
- sections: Optional[List[str]]=None,
- **plugins: bool,
+ **load: bool,
) -> Tuple[argparse.ArgumentParser, List[str], Section]:
argv = (argv or sys.argv)
@@ -91,10 +90,17 @@ def init(
help="View current configuration (include all overrides)")
(options, remaining) = args_parser.parse_known_args(argv)
- config = _init_config(options.config_path, options.set_options, (sections or []), **plugins)
if options.dump_config:
- _dump_config(config)
+ _dump_config(_init_config(
+ config_path=options.config_path,
+ override_options=options.set_options,
+ load_auth=True,
+ load_hid=True,
+ load_atx=True,
+ load_msd=True,
+ ))
raise SystemExit()
+ config = _init_config(options.config_path, options.set_options, **load)
logging.captureWarnings(True)
logging.config.dictConfig(config.logging)
@@ -105,37 +111,39 @@ def init(
def _init_config(
config_path: str,
override_options: List[str],
- sections: List[str],
- with_auth: bool=False,
- with_hid: bool=False,
- with_atx: bool=False,
- with_msd: bool=False,
+ load_auth: bool=False,
+ load_hid: bool=False,
+ load_atx: bool=False,
+ load_msd: bool=False,
) -> Section:
config_path = os.path.expanduser(config_path)
raw_config: Dict = load_yaml_file(config_path)
- scheme = _get_config_scheme(sections)
+ scheme = _get_config_scheme()
try:
_merge_dicts(raw_config, build_raw_from_options(override_options))
- _merge_dicts(raw_config, (raw_config.get("override") or {}))
+ _merge_dicts(raw_config, (raw_config.pop("override", {}) or {}))
config = make_config(raw_config, scheme)
- if "kvmd" in sections:
- if with_auth:
- scheme["kvmd"]["auth"]["internal"].update(get_auth_service_class(config.kvmd.auth.internal.type).get_plugin_options())
- if config.kvmd.auth.external.type:
- scheme["kvmd"]["auth"]["external"].update(get_auth_service_class(config.kvmd.auth.external.type).get_plugin_options())
+ rebuild = False
- if with_hid:
- scheme["kvmd"]["hid"].update(get_hid_class(config.kvmd.hid.type).get_plugin_options())
+ if load_auth:
+ scheme["kvmd"]["auth"]["internal"].update(get_auth_service_class(config.kvmd.auth.internal.type).get_plugin_options())
+ if config.kvmd.auth.external.type:
+ scheme["kvmd"]["auth"]["external"].update(get_auth_service_class(config.kvmd.auth.external.type).get_plugin_options())
+ rebuild = True
- if with_atx:
- scheme["kvmd"]["atx"].update(get_atx_class(config.kvmd.atx.type).get_plugin_options())
-
- if with_msd:
- scheme["kvmd"]["msd"].update(get_msd_class(config.kvmd.msd.type).get_plugin_options())
+ for (load, section, get_class) in [
+ (load_hid, "hid", get_hid_class),
+ (load_atx, "atx", get_atx_class),
+ (load_msd, "msd", get_msd_class),
+ ]:
+ if load:
+ scheme["kvmd"][section].update(get_class(getattr(config.kvmd, section).type).get_plugin_options())
+ rebuild = True
+ if rebuild:
config = make_config(raw_config, scheme)
return config
@@ -163,10 +171,8 @@ def _merge_dicts(dest: Dict, src: Dict) -> None:
dest[key] = src[key]
-def _get_config_scheme(sections: List[str]) -> Dict:
- scheme = {
- "override": Option({}),
-
+def _get_config_scheme() -> Dict:
+ return {
"logging": Option({}),
"kvmd": {
@@ -254,12 +260,3 @@ def _get_config_scheme(sections: List[str]) -> Dict:
},
},
}
-
- if sections:
- return {
- section: sub
- for (section, sub) in scheme.items()
- if section in sections
- }
- else:
- return scheme
diff --git a/kvmd/apps/cleanup/__init__.py b/kvmd/apps/cleanup/__init__.py
index 063c8eca..e97c1329 100644
--- a/kvmd/apps/cleanup/__init__.py
+++ b/kvmd/apps/cleanup/__init__.py
@@ -40,10 +40,9 @@ def main(argv: Optional[List[str]]=None) -> None:
prog="kvmd-cleanup",
description="Kill KVMD and clear resources",
argv=argv,
- sections=["logging", "kvmd"],
- with_hid=True,
- with_atx=True,
- with_msd=True,
+ load_hid=True,
+ load_atx=True,
+ load_msd=True,
)[2].kvmd
logger = get_logger(0)
diff --git a/kvmd/apps/htpasswd/__init__.py b/kvmd/apps/htpasswd/__init__.py
index aa42b429..b4d1cc2c 100644
--- a/kvmd/apps/htpasswd/__init__.py
+++ b/kvmd/apps/htpasswd/__init__.py
@@ -102,8 +102,7 @@ def main(argv: Optional[List[str]]=None) -> None:
(parent_parser, argv, config) = init(
add_help=False,
argv=argv,
- sections=["logging", "kvmd"],
- with_auth=True,
+ load_auth=True,
)
parser = argparse.ArgumentParser(
prog="kvmd-htpasswd",
diff --git a/kvmd/apps/ipmi/__init__.py b/kvmd/apps/ipmi/__init__.py
index 17988977..03235f10 100644
--- a/kvmd/apps/ipmi/__init__.py
+++ b/kvmd/apps/ipmi/__init__.py
@@ -35,7 +35,6 @@ def main(argv: Optional[List[str]]=None) -> None:
prog="kvmd-ipmi",
description="IPMI to KVMD proxy",
argv=argv,
- sections=["logging", "ipmi"],
)[2].ipmi
# pylint: disable=protected-access
diff --git a/kvmd/apps/kvmd/__init__.py b/kvmd/apps/kvmd/__init__.py
index 4f6806a2..d2c662a0 100644
--- a/kvmd/apps/kvmd/__init__.py
+++ b/kvmd/apps/kvmd/__init__.py
@@ -46,11 +46,10 @@ def main(argv: Optional[List[str]]=None) -> None:
prog="kvmd",
description="The main Pi-KVM daemon",
argv=argv,
- sections=["logging", "kvmd"],
- with_auth=True,
- with_hid=True,
- with_atx=True,
- with_msd=True,
+ load_auth=True,
+ load_hid=True,
+ load_atx=True,
+ load_msd=True,
)[2].kvmd
with gpio.bcm():
diff --git a/kvmd/yamlconf/__init__.py b/kvmd/yamlconf/__init__.py
index b0bc356f..a253d449 100644
--- a/kvmd/yamlconf/__init__.py
+++ b/kvmd/yamlconf/__init__.py
@@ -162,7 +162,7 @@ def make_config(raw: Dict[str, Any], scheme: Dict[str, Any], _keys: Tuple[str, .
value = raw.get(key, option.default)
try:
value = option.type(value)
- except ValueError as err:
+ except (TypeError, ValueError) as err:
raise ConfigError(f"Invalid value {value!r} for key {make_full_name(key)!r}: {err}")
config[key] = value