]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
manager: loading initial configuration on startup and default path changes
authorVasek Sraier <git@vakabus.cz>
Wed, 2 Jun 2021 14:31:37 +0000 (16:31 +0200)
committerAleš Mrázek <ales.mrazek@nic.cz>
Fri, 8 Apr 2022 14:17:52 +0000 (16:17 +0200)
manager/integration/tests/basic_crash/run
manager/integration/tests/basic_crash/run_test.py
manager/integration/tests/basic_startup/run
manager/integration/tests/basic_startup/send_request.py
manager/integration/tests/worker_count/run
manager/integration/tests/worker_count/run_test.py
manager/knot_resolver_manager/__main__.py
manager/knot_resolver_manager/constants.py [new file with mode: 0644]
manager/knot_resolver_manager/kres_manager.py
manager/scripts/container.py

index 4e48e036bf8ef7c9d39a110a781a180d74e1a8eb..5e3dbbd34ff52d6e8e99686bd2077a7f36ef7110 100755 (executable)
@@ -8,18 +8,18 @@ echo "Starting manager..."
 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
index ca590beb1a929585ec2879c83ae598530d099c05..4a3fc24414c1a814b56d36c680090ddd587fb76c 100644 (file)
@@ -19,7 +19,7 @@ PAYLOAD_F = lambda num: PAYLOAD % num
 
 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:
index 8162acae334445a0f2a219c5162c4730d2808564..b0469ccef5369a279a57c0c54a563eb96396f950 100755 (executable)
@@ -9,7 +9,7 @@ cmd=$(grep ExecStart /etc/systemd/system/knot-resolver-manager.service | sed 's/
 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
 
index 75dc3b36915bcb73cdd0c4d9d03fac63b40682b6..8cf864c0864ef6d4b8272355e9e95c390478a90d 100644 (file)
@@ -12,5 +12,5 @@ with open(PAYLOAD_PATH, "r") as file:
        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()
index 11ed0102f4293ba41abaca647bbcc3af91aeda7c..95f5c019ac55aab5cdb590a4ea6d9ed9fe1a9521 100755 (executable)
@@ -8,6 +8,6 @@ echo "Starting manager..."
 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
index 94e819a8e32a20ec8f106618a763a6fdcf3579bc..990307ad7ba8011df39d5c3b00518927bc180ae2 100644 (file)
@@ -19,12 +19,12 @@ PAYLOAD_F = lambda num: PAYLOAD % num
 
 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:
index a7f585da27063a043a5c3b5a2cbcadc7f7f33218..58ce4deac0b57807576e181695b0508e2b5bd09c 100644 (file)
@@ -7,12 +7,13 @@ from typing import Optional
 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"
 
 
@@ -39,12 +40,22 @@ async def apply_config(request: web.Request) -> web.Response:
 
 @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()
@@ -53,11 +64,26 @@ def main(listen: Optional[str], config: Optional[str]):
     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")
 
@@ -69,7 +95,7 @@ def main(listen: Optional[str], config: Optional[str]):
     # 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():
diff --git a/manager/knot_resolver_manager/constants.py b/manager/knot_resolver_manager/constants.py
new file mode 100644 (file)
index 0000000..bdefaaa
--- /dev/null
@@ -0,0 +1,8 @@
+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"
index 7e013a4bcb5b38cc173502d7b7ad772ee3d90387..67e4a469dce90ff419638547872fb813494980c7 100644 (file)
@@ -1,7 +1,9 @@
 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
@@ -65,10 +67,8 @@ class KresManager:
             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:
index 15f7f57ebe8278e0ddb4d2f57d925dd3449200bb..8ed4f1c44c5f0774ba7ba3dfe2e651745ad7cfad 100755 (executable)
@@ -1,15 +1,16 @@
 #!/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
@@ -265,4 +266,4 @@ def run(
 
 
 if __name__ == "__main__":
-    main()
\ No newline at end of file
+    main()