]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
constants: replaced static executable paths with $PATH lookup
authorVasek Sraier <git@vakabus.cz>
Fri, 5 Nov 2021 12:00:13 +0000 (13:00 +0100)
committerAleš Mrázek <ales.mrazek@nic.cz>
Fri, 8 Apr 2022 14:17:53 +0000 (16:17 +0200)
manager/knot_resolver_manager/constants.py
manager/knot_resolver_manager/kresd_controller/supervisord/__init__.py
manager/knot_resolver_manager/kresd_controller/systemd/dbus_api.py
manager/knot_resolver_manager/utils/which.py [new file with mode: 0644]

index 1da92a5086ed395f8a23c5d0836a06c4ff55f860..8f4dfb200cbdeb235936d96c2f1358245c0fab59 100644 (file)
@@ -3,11 +3,18 @@ from pathlib import Path
 
 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:
index 04ca84eea7d41b8173d3ed4229080dabe35f0e13..ed15daee076f9abb24b877b584ed722b40c7bc60 100644 (file)
@@ -13,10 +13,10 @@ from jinja2 import Template
 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,
@@ -59,9 +59,9 @@ class _Instance:
 
 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")
 
index 2e46fbae23fc8fa05439578236a6ff3e0d406413..d51ed883e946a775382695ffa8313c1cc9db9113 100644 (file)
@@ -13,7 +13,7 @@ from pydbus.bus import SessionBus
 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
@@ -141,8 +141,8 @@ def _kresd_unit_properties(config: KresConfig, kres_id: KresID) -> List[Tuple[st
                 "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,
                     )
                 ],
@@ -171,8 +171,8 @@ def _gc_unit_properties(config: KresConfig) -> Any:
                 "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,
                     )
                 ],
diff --git a/manager/knot_resolver_manager/utils/which.py b/manager/knot_resolver_manager/utils/which.py
new file mode 100644 (file)
index 0000000..450102f
--- /dev/null
@@ -0,0 +1,22 @@
+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")