from aiohttp import web
from knot_resolver_manager.kresd_manager import KresdManager
-from . import confmodel
+from . import configuration
_SOCKET_PATH = "/tmp/manager.sock"
async def apply_config(request: web.Request) -> web.Response:
- config = await confmodel.parse(await request.text())
+ config = await configuration.parse(await request.text())
manager: KresdManager = request.app["kresd_manager"]
await manager.apply_config(config)
return web.Response(text="OK")
from strictyaml.parser import load
from strictyaml.representation import YAML
+from .datamodel import ConfData
+
_CONFIG_SCHEMA = Map({"lua_config": Str(), "num_workers": Int()})
raise ConfigValidationException("Number of workers must be non-negative")
-async def parse(textual_config: str) -> YAML:
- schema = _get_config_schema()
- conf = load(textual_config, schema)
- await _validate_config(conf)
- return conf
+async def parse(yaml: str) -> ConfData:
+ conf = ConfData.from_yaml(yaml)
+ await conf.validate()
+ return conf
\ No newline at end of file
--- /dev/null
+from dataclasses import dataclass
+
+from .utils import dataclass_strictyaml
+
+
+class ConfDataValidationException(Exception):
+ pass
+
+
+@dataclass
+@dataclass_strictyaml
+class ConfData:
+ num_workers: int = 1
+ lua_config: str = None
+
+ async def validate(self) -> bool:
+ if self.num_workers < 0:
+ raise ConfDataValidationException("Number of workers must be non-negative")
from . import compat
from . import systemd
+from .datamodel import ConfData
class Kresd:
while len(self._children) < n:
await self._spawn_new_child()
- async def _write_config(self, config: YAML):
+ async def _write_config(self, config: ConfData):
# FIXME: this code is blocking!!!
with open("/etc/knot-resolver/kresd.conf", "w") as f:
- f.write(config["lua_config"].text)
+ f.write(config.lua_config)
- async def apply_config(self, config: YAML):
+ async def apply_config(self, config: ConfData):
async with self._children_lock:
await self._write_config(config)
- await self._ensure_number_of_children(config["num_workers"])
+ await self._ensure_number_of_children(config.num_workers)
await self._rolling_restart()