From: Aleš Mrázek Date: Fri, 4 Aug 2023 13:42:15 +0000 (+0200) Subject: manager: better error handling for rundir X-Git-Tag: v6.0.2~8^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b1cb0e8f23e5be762b10ff684e3cd7be73eb18b4;p=thirdparty%2Fknot-resolver.git manager: better error handling for rundir --- diff --git a/manager/knot_resolver_manager/datamodel/config_schema.py b/manager/knot_resolver_manager/datamodel/config_schema.py index 6046f7a83..da68525cd 100644 --- a/manager/knot_resolver_manager/datamodel/config_schema.py +++ b/manager/knot_resolver_manager/datamodel/config_schema.py @@ -25,6 +25,8 @@ from knot_resolver_manager.datamodel.webmgmt_schema import WebmgmtSchema from knot_resolver_manager.utils.modeling import ConfigSchema from knot_resolver_manager.utils.modeling.base_schema import lazy_default +_DEFAULT_RUNDIR = "/var/run/knot-resolver" + logger = logging.getLogger(__name__) @@ -105,7 +107,7 @@ class KresConfig(ConfigSchema): version: int = 1 nsid: Optional[EscapedStr] = None hostname: Optional[EscapedStr] = None - rundir: Dir = lazy_default(Dir, "/var/run/knot-resolver") + rundir: Dir = lazy_default(Dir, _DEFAULT_RUNDIR) workers: Union[Literal["auto"], IntPositive] = IntPositive(1) max_workers: IntPositive = IntPositive(_default_max_worker_count()) management: ManagementSchema = lazy_default(ManagementSchema, {"unix-socket": "./manager.sock"}) @@ -190,12 +192,10 @@ class KresConfig(ConfigSchema): def get_rundir_without_validation(data: Dict[str, Any]) -> Dir: """ - Without fully parsing, try to get a rundir from a raw config data. When it fails, - attempts a full validation to produce a good error message. + Without fully parsing, try to get a rundir from a raw config data, otherwise use default. + Attempts a dir validation to produce a good error message. Used for initial manager startup. """ - if "rundir" in data: - return Dir(data["rundir"], object_path="/rundir") - return KresConfig(data).rundir # this should throw a descriptive error + return Dir(data["rundir"] if "rundir" in data else _DEFAULT_RUNDIR, object_path="/rundir") diff --git a/manager/knot_resolver_manager/server.py b/manager/knot_resolver_manager/server.py index 345a3f8a9..3aba44092 100644 --- a/manager/knot_resolver_manager/server.py +++ b/manager/knot_resolver_manager/server.py @@ -26,7 +26,6 @@ from knot_resolver_manager.constants import DEFAULT_MANAGER_CONFIG_FILE, PID_FIL from knot_resolver_manager.datamodel.config_schema import KresConfig, get_rundir_without_validation from knot_resolver_manager.datamodel.globals import ( Context, - reset_global_validation_context, set_global_validation_context, ) from knot_resolver_manager.datamodel.management_schema import ManagementSchema @@ -406,7 +405,10 @@ async def _deny_working_directory_changes(config_old: KresConfig, config_new: Kr def _set_working_directory(config_raw: Dict[str, Any]) -> None: - rundir = get_rundir_without_validation(config_raw) + try: + rundir = get_rundir_without_validation(config_raw) + except ValueError as e: + raise DataValidationError(str(e), "/rundir") logger.debug(f"Changing working directory to '{rundir.to_path().absolute()}'.") os.chdir(rundir.to_path()) @@ -482,21 +484,21 @@ async def start_server(config: Path = DEFAULT_MANAGER_CONFIG_FILE) -> int: # Preprocess config - load from file or in general take it to the last step before validation. config_raw = await _load_raw_config(config) + # before processing any configuration, set validation context + # - resolve_root = root against which all relative paths will be resolved + set_global_validation_context(Context(config.parent, True)) + # We want to change cwd as soon as possible. Some parts of the codebase are using os.getcwd() to get the # working directory. # # If we fail to read rundir from unparsed config, the first config validation error comes from here - set_global_validation_context(Context(config.parent, False)) # Strict validation for Paths is off. _set_working_directory(config_raw) - reset_global_validation_context() # We don't want more than one manager in a single working directory. So we lock it with a PID file. # Warning - this does not prevent multiple managers with the same naming of kresd service. _lock_working_directory() - # before processing any configuration, set validation context - # - resolve_root = root against which all relative paths will be resolved - set_global_validation_context(Context(config.parent)) + # set_global_validation_context(Context(config.parent)) # After the working directory is set, we can initialize proper config store with a newly parsed configuration. config_store = await _init_config_store(config_raw)