from knot_resolver_manager.datamodel.config_schema import KresConfig
from knot_resolver_manager.kres_id import KresID
+from knot_resolver_manager.utils import which
STARTUP_LOG_LEVEL = logging.DEBUG
DEFAULT_MANAGER_CONFIG_FILE = Path("/etc/knot-resolver/config.yml")
-KRESD_EXECUTABLE = Path("/usr/sbin/kresd")
-GC_EXECUTABLE = Path("/usr/sbin/kres-cache-gc")
+
+
+def kresd_executable() -> Path:
+ return which.which("kresd")
+
+
+def kres_gc_executable() -> Path:
+ return which.which("kres-cache-gc")
def kresd_cache_dir(config: KresConfig) -> Path:
from knot_resolver_manager.compat.asyncio import to_thread
from knot_resolver_manager.compat.dataclasses import dataclass
from knot_resolver_manager.constants import (
- GC_EXECUTABLE,
- KRESD_EXECUTABLE,
+ kres_gc_executable,
kresd_cache_dir,
kresd_config_file,
+ kresd_executable,
supervisord_config_file,
supervisord_config_file_tmp,
supervisord_log_file,
def _get_command_based_on_type(config: KresConfig, i: "SupervisordSubprocess") -> str:
if i.type is SubprocessType.KRESD:
- return f"{KRESD_EXECUTABLE} -c {kresd_config_file(config, i.id)} -n"
+ return f"{kresd_executable()} -c {kresd_config_file(config, i.id)} -n"
elif i.type is SubprocessType.GC:
- return f"{GC_EXECUTABLE} -c {kresd_cache_dir(config)} -d 1000"
+ return f"{kres_gc_executable()} -c {kresd_cache_dir(config)} -d 1000"
else:
raise NotImplementedError("This subprocess type is not supported")
from typing_extensions import Literal
from knot_resolver_manager.compat.dataclasses import dataclass
-from knot_resolver_manager.constants import GC_EXECUTABLE, KRESD_EXECUTABLE, kresd_cache_dir, kresd_config_file
+from knot_resolver_manager.constants import kres_gc_executable, kresd_cache_dir, kresd_config_file, kresd_executable
from knot_resolver_manager.datamodel.config_schema import KresConfig
from knot_resolver_manager.exceptions import SubprocessControllerException
from knot_resolver_manager.kres_id import KresID
"a(sasb)",
[
(
- str(KRESD_EXECUTABLE),
- [str(KRESD_EXECUTABLE), "-c", str(kresd_config_file(config, kres_id)), "-n"],
+ str(kresd_executable()),
+ [str(kresd_executable()), "-c", str(kresd_config_file(config, kres_id)), "-n"],
False,
)
],
"a(sasb)",
[
(
- str(GC_EXECUTABLE),
- [str(GC_EXECUTABLE), "-c", str(kresd_cache_dir(config)), "-d", "1000"],
+ str(kres_gc_executable()),
+ [str(kres_gc_executable()), "-c", str(kresd_cache_dir(config)), "-d", "1000"],
True,
)
],
--- /dev/null
+import functools
+import os
+from pathlib import Path
+
+
+@functools.lru_cache(maxsize=16)
+def which(binary_name: str) -> Path:
+ """
+ Given a name of an executable, search $PATH and return
+ the absolute path of that executable. The results of this function
+ are LRU cached.
+
+ If not found, throws an RuntimeError.
+ """
+
+ possible_directories = os.get_exec_path()
+ for dr in possible_directories:
+ p = Path(dr, binary_name)
+ if p.exists():
+ return p.absolute()
+
+ raise RuntimeError(f"Executable {binary_name} was not found in $PATH")