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
class SubprocessType(Enum):
KRESD = auto()
+ POLICY_LOADER = auto()
GC = auto()
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()
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
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)}")