systemctl start knot-resolver-manager.service
# give it time to start
-while [ ! -e /tmp/manager.sock ]; do sleep 0.5; done
+while [ ! -e /etc/knot-resolver/manager.sock ]; do sleep 0.5; done
# start kresd instances and verify, that it works
python3 run_test.py
# kill the manager and start it again
systemctl kill --signal=SIGKILL knot-resolver-manager.service
-rm /tmp/manager.sock
+rm /etc/knot-resolver/manager.sock
systemctl start knot-resolver-manager.service
# wait for proper startup
-while [ ! -e /tmp/manager.sock ]; do sleep 0.5; done
+while [ ! -e /etc/knot-resolver/manager.sock ]; do sleep 0.5; done
# run the same test again testing, that instances can start and that the count is correct
# because there should be kresd instances left after the first run, this tests that the initialization of the manager properly finds them
def set_workers(num: int):
# send the config
- r = requests.post('http+unix://%2Ftmp%2Fmanager.sock/config', data=PAYLOAD_F(num))
+ r = requests.post('http+unix://%2Fetc%2Fknot-resolver%2Fmanager.sock/config', data=PAYLOAD_F(num))
r.raise_for_status()
def count_running() -> int:
bash -c "cd /code; $cmd" &
# give it time to start
-while [ ! -e /tmp/manager.sock ]; do sleep 0.5; done
+while [ ! -e /etc/knot-resolver/manager.sock ]; do sleep 0.5; done
python3 send_request.py
PAYLOAD = file.read()
# send the config
-r = requests.post('http+unix://%2Ftmp%2Fmanager.sock/config', data=PAYLOAD)
+r = requests.post('http+unix://%2Fetc%2Fknot-resolver%2Fmanager.sock/config', data=PAYLOAD)
r.raise_for_status()
systemctl start knot-resolver-manager.service
# give it time to start
-while [ ! -e /tmp/manager.sock ]; do sleep 0.5; done
+while [ ! -e /etc/knot-resolver/manager.sock ]; do sleep 0.5; done
python3 run_test.py
def set_workers(num: int):
# send the config
- r = requests.post('http+unix://%2Ftmp%2Fmanager.sock/config', data=PAYLOAD_F(num))
+ r = requests.post('http+unix://%2Fetc%2Fknot-resolver%2Fmanager.sock/config', data=PAYLOAD_F(num))
r.raise_for_status()
def set_workers_auto():
# send the config
- r = requests.post('http+unix://%2Ftmp%2Fmanager.sock/config', data=PAYLOAD_F("\"auto\""))
+ r = requests.post('http+unix://%2Fetc%2Fknot-resolver%2Fmanager.sock/config', data=PAYLOAD_F("\"auto\""))
r.raise_for_status()
def count_running() -> int:
import click
from aiohttp import web
+from knot_resolver_manager.constants import LISTEN_SOCKET_PATH, MANAGER_CONFIG_FILE
+from knot_resolver_manager.utils.async_utils import readfile
+
from .datamodel import KresConfig
from .kres_manager import KresManager
from .utils import ignore_exceptions
-# when changing this, change the help message in main()
-_SOCKET_PATH = "/tmp/manager.sock"
_MANAGER = "kres_manager"
@click.command()
@click.argument("listen", type=str, nargs=1, required=False, default=None)
-@click.option("--config", "-c", type=str, nargs=1, required=False, default=None)
+@click.option(
+ "--config",
+ "-c",
+ type=str,
+ nargs=1,
+ required=False,
+ default=None,
+ help="Overrides default config location at '" + str(MANAGER_CONFIG_FILE) + "'",
+)
def main(listen: Optional[str], config: Optional[str]):
"""Knot Resolver Manager
[listen] ... numeric port or a path for a Unix domain socket, default is \"/tmp/manager.sock\"
"""
+ config_path = Path(config) if config is not None else MANAGER_CONFIG_FILE
+
start_time = time()
app = web.Application()
app[_MANAGER] = None
async def init_manager(app: web.Application):
+ """
+ Called asynchronously when the application initializes.
+ """
+ # Create KresManager. This will perform autodetection of available service managers and
+ # select the most appropriate to use
manager = await KresManager.create()
app[_MANAGER] = manager
- if config is not None:
- # TODO Use config loaded from the file system
- pass
+
+ # Initial static configuration of the manager
+ # optional step, could be skipped
+ if config_path is not None:
+ if not config_path.exists():
+ logger.warning(
+ "Manager is configured to load config file at %s on startup, but the file does not exist.",
+ config_path,
+ )
+ else:
+ initial_config = KresConfig.from_yaml(await readfile(config_path))
+ await manager.apply_config(initial_config)
+
end_time = time()
logger.info(f"Manager fully initialized after {end_time - start_time} seconds")
# run forever, listen at the appropriate place
maybe_port = ignore_exceptions(None, ValueError, TypeError)(int)(listen)
if listen is None:
- web.run_app(app, path=_SOCKET_PATH)
+ web.run_app(app, path=str(LISTEN_SOCKET_PATH))
elif maybe_port is not None:
web.run_app(app, port=maybe_port)
elif Path(listen).parent.exists():
--- /dev/null
+from pathlib import Path
+
+CONFIGURATION_DIR = Path("/etc/knot-resolver")
+
+KRESD_CONFIG_FILE = CONFIGURATION_DIR / "kresd.conf"
+MANAGER_CONFIG_FILE = CONFIGURATION_DIR / "config.yml"
+
+LISTEN_SOCKET_PATH = CONFIGURATION_DIR / "manager.sock"
import asyncio
from typing import Any, List, Type
+from knot_resolver_manager.constants import KRESD_CONFIG_FILE
from knot_resolver_manager.kresd_controller import BaseKresdController, get_best_controller_implementation
+from knot_resolver_manager.utils.async_utils import writefile
from . import configuration
from .datamodel import KresConfig
await self._spawn_new_child()
async def _write_config(self, config: KresConfig):
- # FIXME: this code is blocking!!!
lua_config = await configuration.render_lua(config)
- with open("/etc/knot-resolver/kresd.conf", "w") as f:
- f.write(lua_config)
+ await writefile(KRESD_CONFIG_FILE, lua_config)
async def apply_config(self, config: KresConfig):
async with self._children_lock:
#!/usr/bin/env python
+import atexit
import subprocess
import sys
import time
+from os import environ
from pathlib import Path
from typing import Dict, List, NoReturn, Optional
-from os import environ
-import atexit
import click
+
def _get_git_root() -> Path:
result = subprocess.run(
"git rev-parse --show-toplevel", shell=True, stdout=subprocess.PIPE
if __name__ == "__main__":
- main()
\ No newline at end of file
+ main()