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__)
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"})
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")
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
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())
# 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)