From: Aleš Mrázek Date: Mon, 11 Mar 2024 13:24:22 +0000 (+0100) Subject: manager: controller: new SubprocessType for loading policy added X-Git-Tag: v6.0.8~8^2~18 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=7815f24d0e45559838d4fe1b1ef5dc63e72a3139;p=thirdparty%2Fknot-resolver.git manager: controller: new SubprocessType for loading policy added --- diff --git a/manager/knot_resolver_manager/kresd_controller/interface.py b/manager/knot_resolver_manager/kresd_controller/interface.py index 1dc99505f..bc7708721 100644 --- a/manager/knot_resolver_manager/kresd_controller/interface.py +++ b/manager/knot_resolver_manager/kresd_controller/interface.py @@ -6,6 +6,7 @@ import struct import sys from abc import ABC, abstractmethod # pylint: disable=no-name-in-module from enum import Enum, auto +from pathlib import Path from typing import Dict, Iterable, Optional, Type, TypeVar from weakref import WeakValueDictionary @@ -20,6 +21,7 @@ logger = logging.getLogger(__name__) class SubprocessType(Enum): KRESD = auto() + POLICY_LOADER = auto() GC = auto() @@ -105,24 +107,43 @@ class Subprocess(ABC): self._registered_worker: bool = False async def start(self) -> None: - # create config file - lua_config = self._config.render_lua() - await writefile(kresd_config_file(self._config, self.id), lua_config) + + config_file: Optional[Path] = None + if self.type is SubprocessType.KRESD: + config_lua = self._config.render_lua() + config_file = kresd_config_file(self._config, self.id) + 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") + await writefile(config_file, config_lua) + try: await self._start() if self.type is SubprocessType.KRESD: register_worker(self) self._registered_worker = True except SubprocessControllerException as e: - kresd_config_file(self._config, self.id).unlink() + if config_file: + config_file.unlink() raise e async def apply_new_config(self, new_config: KresConfig) -> None: self._config = new_config + # update config file logger.debug(f"Writing config file for {self.id}") - lua_config = new_config.render_lua() - await writefile(kresd_config_file(new_config, self.id), lua_config) + + config_file: Optional[Path] = None + if self.type is SubprocessType.KRESD: + config_lua = self._config.render_lua() + config_file = kresd_config_file(self._config, self.id) + 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") + await writefile(config_file, config_lua) + # update runtime status logger.debug(f"Restarting {self.id}") await self._restart() @@ -138,7 +159,13 @@ class Subprocess(ABC): Remove temporary files and all traces of this instance running. It is NOT SAFE to call this while the kresd is running, because it will break automatic restarts (at the very least). """ - kresd_config_file(self._config, self.id).unlink() + + if self.type is SubprocessType.KRESD: + 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.unlink() def __eq__(self, o: object) -> bool: return isinstance(o, type(self)) and o.type == self.type and o.id == self.id @@ -167,8 +194,12 @@ class Subprocess(ABC): return self._id async def command(self, cmd: str) -> object: + if not self._registered_worker: + raise RuntimeError("the command cannot be sent to a process other than the kresd worker") + reader: asyncio.StreamReader writer: Optional[asyncio.StreamWriter] = None + try: reader, writer = await asyncio.open_unix_connection(f"./control/{int(self.id)}")