]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
manager: controller: policy-loader program added
authorAleš Mrázek <ales.mrazek@nic.cz>
Tue, 12 Mar 2024 14:50:10 +0000 (15:50 +0100)
committerAleš Mrázek <ales.mrazek@nic.cz>
Tue, 2 Jul 2024 12:07:48 +0000 (14:07 +0200)
manager/knot_resolver_manager/constants.py
manager/knot_resolver_manager/kresd_controller/interface.py
manager/knot_resolver_manager/kresd_controller/supervisord/config_file.py
manager/knot_resolver_manager/kresd_controller/supervisord/supervisord.conf.j2

index 2d9ebd23d68b89340bc5212047dad9eee4e9a332..90ceed9f85f7df1eae49b7ad9f6c660fb2c49688 100644 (file)
@@ -39,6 +39,10 @@ def kresd_cache_dir(config: "KresConfig") -> Path:
     return config.cache.storage.to_path()
 
 
+def policy_loader_config_file(_config: "KresConfig") -> Path:
+    return Path("policy-loader.conf")
+
+
 def kresd_config_file(_config: "KresConfig", kres_id: "KresID") -> Path:
     return Path(f"kresd{int(kres_id)}.conf")
 
index bc7708721f208d71edd1810fd6c1ca4633f32ac5..25bf00ac39f8d5f45c9ae815a0cc650931a85451 100644 (file)
@@ -10,7 +10,7 @@ from pathlib import Path
 from typing import Dict, Iterable, Optional, Type, TypeVar
 from weakref import WeakValueDictionary
 
-from knot_resolver_manager.constants import kresd_config_file
+from knot_resolver_manager.constants import kresd_config_file, policy_loader_config_file
 from knot_resolver_manager.datamodel.config_schema import KresConfig
 from knot_resolver_manager.exceptions import SubprocessControllerException
 from knot_resolver_manager.kresd_controller.registered_workers import register_worker, unregister_worker
@@ -115,7 +115,7 @@ class Subprocess(ABC):
             await writefile(config_file, config_lua)
         elif self.type is SubprocessType.POLICY_LOADER:
             config_lua = self._config.render_lua_policy()
-            config_file = Path("policy-loader.conf")
+            config_file = policy_loader_config_file(self._config)
             await writefile(config_file, config_lua)
 
         try:
@@ -141,7 +141,7 @@ class Subprocess(ABC):
             await writefile(config_file, config_lua)
         elif self.type is SubprocessType.POLICY_LOADER:
             config_lua = self._config.render_lua_policy()
-            config_file = Path("policy-loader.conf")
+            config_file = policy_loader_config_file(self._config)
             await writefile(config_file, config_lua)
 
         # update runtime status
@@ -164,7 +164,7 @@ class Subprocess(ABC):
             config_file = kresd_config_file(self._config, self.id)
             config_file.unlink()
         elif self.type is SubprocessType.POLICY_LOADER:
-            config_file = Path("policy-loader.conf")
+            config_file = policy_loader_config_file(self._config)
             config_file.unlink()
 
     def __eq__(self, o: object) -> bool:
index fab0a1430039bfa6c14563cf2d73f9c63a65c731..15a81ba7e700a556883d2c60832d294e05b51946 100644 (file)
@@ -10,6 +10,7 @@ from knot_resolver_manager.constants import (
     kresd_cache_dir,
     kresd_config_file_supervisord_pattern,
     kresd_executable,
+    policy_loader_config_file,
     supervisord_config_file,
     supervisord_config_file_tmp,
     supervisord_pid_file,
@@ -32,9 +33,11 @@ class SupervisordKresID(KresID):
 
     @staticmethod
     def from_string(val: str) -> "SupervisordKresID":
+        # the double name is checked because thats how we read it from supervisord
         if val in ("cache-gc", "cache-gc:cache-gc"):
-            # the double name is checked because thats how we read it from supervisord
             return SupervisordKresID.new(SubprocessType.GC, 0)
+        elif val in ("policy-loader", "policy-loader:policy-loader"):
+            return SupervisordKresID.new(SubprocessType.POLICY_LOADER, 0)
         else:
             val = val.replace("kresd:kresd", "")
             return SupervisordKresID.new(SubprocessType.KRESD, int(val))
@@ -42,6 +45,8 @@ class SupervisordKresID(KresID):
     def __str__(self) -> str:
         if self.subprocess_type is SubprocessType.GC:
             return "cache-gc"
+        elif self.subprocess_type is SubprocessType.POLICY_LOADER:
+            return "policy-loader"
         elif self.subprocess_type is SubprocessType.KRESD:
             return f"kresd:kresd{self._id}"
         else:
@@ -94,6 +99,16 @@ class ProcessTypeConfig:
             environment="",
         )
 
+    @staticmethod
+    def create_policy_loader_config(config: KresConfig) -> "ProcessTypeConfig":
+        cwd = str(os.getcwd())
+        return ProcessTypeConfig(  # type: ignore[call-arg]
+            logfile=supervisord_subprocess_log_dir(config) / "policy-loader.log",
+            workdir=cwd,
+            command=f"{kresd_executable()} -c {(policy_loader_config_file(config))} -c - -n",
+            environment="X-SUPERVISORD-TYPE=notify",
+        )
+
     @staticmethod
     def create_kresd_config(config: KresConfig) -> "ProcessTypeConfig":
         cwd = str(os.getcwd())
@@ -172,11 +187,11 @@ async def write_config_file(config: KresConfig) -> None:
     template = template.decode("utf8")
     config_string = Template(template).render(
         gc=ProcessTypeConfig.create_gc_config(config),
+        loader=ProcessTypeConfig.create_policy_loader_config(config),
         kresd=ProcessTypeConfig.create_kresd_config(config),
         manager=ProcessTypeConfig.create_manager_config(config),
         config=SupervisordConfig.create(config),
     )
-    print(config_string)
     await writefile(supervisord_config_file_tmp(config), config_string)
     # atomically replace (we don't technically need this right now, but better safe then sorry)
     os.rename(supervisord_config_file_tmp(config), supervisord_config_file(config))
index dcbb645d9e8e9ce1ff3e933ca9bd0e91e831a5fc..b1fed1aa152a089fc59574acd1c2c32274b2d9cb 100644 (file)
@@ -12,7 +12,6 @@ silent = true
 loglevel = {{ config.loglevel }}
 {# there are more options in the plugin section #}
 
-
 [unix_http_server]
 file = {{ config.unix_http_server }}
 
@@ -37,8 +36,6 @@ supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
 [rpcinterface:fast]
 supervisor.rpcinterface_factory = knot_resolver_manager.kresd_controller.supervisord.plugin.fast_rpcinterface:make_main_rpcinterface
 
-
-
 [program:manager]
 redirect_stderr=false
 directory={{ manager.workdir }}
@@ -71,6 +68,17 @@ environment={{ kresd.environment }}
 stdout_logfile=NONE
 stderr_logfile=NONE
 
+[program:policy-loader]
+directory={{ loader.workdir }}
+command={{ loader.command }}
+autostart=false
+stopsignal=TERM
+killasgroup=true
+startsecs=300
+environment={{ loader.environment }}
+stdout_logfile=NONE
+stderr_logfile=NONE
+
 [program:cache-gc]
 redirect_stderr=false
 directory={{ gc.workdir }}
@@ -82,4 +90,4 @@ killasgroup=true
 startsecs=0
 environment={{ gc.environment }}
 stdout_logfile=NONE
-stderr_logfile=NONE
\ No newline at end of file
+stderr_logfile=NONE