mirror of
https://github.com/mofeng-git/One-KVM.git
synced 2026-01-29 00:51:53 +08:00
fixed config loading
This commit is contained in:
@@ -5,3 +5,5 @@
|
|||||||
# heartbeat: 5.0
|
# heartbeat: 5.0
|
||||||
#
|
#
|
||||||
# It will be applied AFTER other configs and "!include" directives and BEFORE validation.
|
# It will be applied AFTER other configs and "!include" directives and BEFORE validation.
|
||||||
|
|
||||||
|
{}
|
||||||
|
|||||||
@@ -75,8 +75,7 @@ def init(
|
|||||||
description: Optional[str]=None,
|
description: Optional[str]=None,
|
||||||
add_help: bool=True,
|
add_help: bool=True,
|
||||||
argv: Optional[List[str]]=None,
|
argv: Optional[List[str]]=None,
|
||||||
sections: Optional[List[str]]=None,
|
**load: bool,
|
||||||
**plugins: bool,
|
|
||||||
) -> Tuple[argparse.ArgumentParser, List[str], Section]:
|
) -> Tuple[argparse.ArgumentParser, List[str], Section]:
|
||||||
|
|
||||||
argv = (argv or sys.argv)
|
argv = (argv or sys.argv)
|
||||||
@@ -91,10 +90,17 @@ def init(
|
|||||||
help="View current configuration (include all overrides)")
|
help="View current configuration (include all overrides)")
|
||||||
(options, remaining) = args_parser.parse_known_args(argv)
|
(options, remaining) = args_parser.parse_known_args(argv)
|
||||||
|
|
||||||
config = _init_config(options.config_path, options.set_options, (sections or []), **plugins)
|
|
||||||
if options.dump_config:
|
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()
|
raise SystemExit()
|
||||||
|
config = _init_config(options.config_path, options.set_options, **load)
|
||||||
|
|
||||||
logging.captureWarnings(True)
|
logging.captureWarnings(True)
|
||||||
logging.config.dictConfig(config.logging)
|
logging.config.dictConfig(config.logging)
|
||||||
@@ -105,37 +111,39 @@ def init(
|
|||||||
def _init_config(
|
def _init_config(
|
||||||
config_path: str,
|
config_path: str,
|
||||||
override_options: List[str],
|
override_options: List[str],
|
||||||
sections: List[str],
|
load_auth: bool=False,
|
||||||
with_auth: bool=False,
|
load_hid: bool=False,
|
||||||
with_hid: bool=False,
|
load_atx: bool=False,
|
||||||
with_atx: bool=False,
|
load_msd: bool=False,
|
||||||
with_msd: bool=False,
|
|
||||||
) -> Section:
|
) -> Section:
|
||||||
|
|
||||||
config_path = os.path.expanduser(config_path)
|
config_path = os.path.expanduser(config_path)
|
||||||
raw_config: Dict = load_yaml_file(config_path)
|
raw_config: Dict = load_yaml_file(config_path)
|
||||||
|
|
||||||
scheme = _get_config_scheme(sections)
|
scheme = _get_config_scheme()
|
||||||
try:
|
try:
|
||||||
_merge_dicts(raw_config, build_raw_from_options(override_options))
|
_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)
|
config = make_config(raw_config, scheme)
|
||||||
|
|
||||||
if "kvmd" in sections:
|
rebuild = False
|
||||||
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())
|
|
||||||
|
|
||||||
if with_hid:
|
if load_auth:
|
||||||
scheme["kvmd"]["hid"].update(get_hid_class(config.kvmd.hid.type).get_plugin_options())
|
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:
|
for (load, section, get_class) in [
|
||||||
scheme["kvmd"]["atx"].update(get_atx_class(config.kvmd.atx.type).get_plugin_options())
|
(load_hid, "hid", get_hid_class),
|
||||||
|
(load_atx, "atx", get_atx_class),
|
||||||
if with_msd:
|
(load_msd, "msd", get_msd_class),
|
||||||
scheme["kvmd"]["msd"].update(get_msd_class(config.kvmd.msd.type).get_plugin_options())
|
]:
|
||||||
|
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)
|
config = make_config(raw_config, scheme)
|
||||||
|
|
||||||
return config
|
return config
|
||||||
@@ -163,10 +171,8 @@ def _merge_dicts(dest: Dict, src: Dict) -> None:
|
|||||||
dest[key] = src[key]
|
dest[key] = src[key]
|
||||||
|
|
||||||
|
|
||||||
def _get_config_scheme(sections: List[str]) -> Dict:
|
def _get_config_scheme() -> Dict:
|
||||||
scheme = {
|
return {
|
||||||
"override": Option({}),
|
|
||||||
|
|
||||||
"logging": Option({}),
|
"logging": Option({}),
|
||||||
|
|
||||||
"kvmd": {
|
"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
|
|
||||||
|
|||||||
@@ -40,10 +40,9 @@ def main(argv: Optional[List[str]]=None) -> None:
|
|||||||
prog="kvmd-cleanup",
|
prog="kvmd-cleanup",
|
||||||
description="Kill KVMD and clear resources",
|
description="Kill KVMD and clear resources",
|
||||||
argv=argv,
|
argv=argv,
|
||||||
sections=["logging", "kvmd"],
|
load_hid=True,
|
||||||
with_hid=True,
|
load_atx=True,
|
||||||
with_atx=True,
|
load_msd=True,
|
||||||
with_msd=True,
|
|
||||||
)[2].kvmd
|
)[2].kvmd
|
||||||
|
|
||||||
logger = get_logger(0)
|
logger = get_logger(0)
|
||||||
|
|||||||
@@ -102,8 +102,7 @@ def main(argv: Optional[List[str]]=None) -> None:
|
|||||||
(parent_parser, argv, config) = init(
|
(parent_parser, argv, config) = init(
|
||||||
add_help=False,
|
add_help=False,
|
||||||
argv=argv,
|
argv=argv,
|
||||||
sections=["logging", "kvmd"],
|
load_auth=True,
|
||||||
with_auth=True,
|
|
||||||
)
|
)
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
prog="kvmd-htpasswd",
|
prog="kvmd-htpasswd",
|
||||||
|
|||||||
@@ -35,7 +35,6 @@ def main(argv: Optional[List[str]]=None) -> None:
|
|||||||
prog="kvmd-ipmi",
|
prog="kvmd-ipmi",
|
||||||
description="IPMI to KVMD proxy",
|
description="IPMI to KVMD proxy",
|
||||||
argv=argv,
|
argv=argv,
|
||||||
sections=["logging", "ipmi"],
|
|
||||||
)[2].ipmi
|
)[2].ipmi
|
||||||
|
|
||||||
# pylint: disable=protected-access
|
# pylint: disable=protected-access
|
||||||
|
|||||||
@@ -46,11 +46,10 @@ def main(argv: Optional[List[str]]=None) -> None:
|
|||||||
prog="kvmd",
|
prog="kvmd",
|
||||||
description="The main Pi-KVM daemon",
|
description="The main Pi-KVM daemon",
|
||||||
argv=argv,
|
argv=argv,
|
||||||
sections=["logging", "kvmd"],
|
load_auth=True,
|
||||||
with_auth=True,
|
load_hid=True,
|
||||||
with_hid=True,
|
load_atx=True,
|
||||||
with_atx=True,
|
load_msd=True,
|
||||||
with_msd=True,
|
|
||||||
)[2].kvmd
|
)[2].kvmd
|
||||||
|
|
||||||
with gpio.bcm():
|
with gpio.bcm():
|
||||||
|
|||||||
@@ -162,7 +162,7 @@ def make_config(raw: Dict[str, Any], scheme: Dict[str, Any], _keys: Tuple[str, .
|
|||||||
value = raw.get(key, option.default)
|
value = raw.get(key, option.default)
|
||||||
try:
|
try:
|
||||||
value = option.type(value)
|
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}")
|
raise ConfigError(f"Invalid value {value!r} for key {make_full_name(key)!r}: {err}")
|
||||||
|
|
||||||
config[key] = value
|
config[key] = value
|
||||||
|
|||||||
Reference in New Issue
Block a user