]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
cli client: added static hints + bug fix of id assignment
authorVasek Sraier <git@vakabus.cz>
Sun, 3 Oct 2021 13:02:50 +0000 (15:02 +0200)
committerAleš Mrázek <ales.mrazek@nic.cz>
Fri, 8 Apr 2022 14:17:53 +0000 (16:17 +0200)
manager/knot_resolver_manager/client/__init__.py
manager/knot_resolver_manager/client/__main__.py
manager/knot_resolver_manager/kres_id.py
manager/knot_resolver_manager/kresd_controller/systemd/__init__.py
manager/knot_resolver_manager/server.py
manager/scripts/run-debug

index 5c0d53a89c21552a16600bba7c9fd1f0ec1b3510..289fe3e76a702563466a76aaa05699d1257e94d7 100644 (file)
@@ -4,7 +4,8 @@ import subprocess
 import time
 import urllib.parse
 from pathlib import Path
-from typing import Union
+from typing import Dict, List, Union
+import ipaddress
 
 import requests
 
@@ -27,6 +28,29 @@ class KnotManagerClient:
     def set_num_workers(self, n: int):
         response = requests.post(self._create_url("/config/server/workers"), data=str(n))
         print(response.text)
+    
+    def set_static_hints(self, hints: Dict[str, List[Union[ipaddress.IPv4Address, ipaddress.IPv6Address]]]):
+        payload = [
+            {
+                "name": name,
+                "addresses": [str(a) for a in addrs]
+            }
+            for name, addrs in hints.items()
+        ]
+        response = requests.post(self._create_url("/config/static-hints/hints"), json=payload)
+        print(response.text)
+    
+    def set_listen_ip_address(self, ip: Union[ipaddress.IPv4Address, ipaddress.IPv6Address], port: int):
+        payload = [
+            {
+                "listen": {
+                    "ip": str(ip),
+                    "port": port
+                }
+            }
+        ]
+        response = requests.post(self._create_url("/config/network/interfaces"), json=payload)
+        print(response)
 
     def wait_for_initialization(self, timeout_sec: float = 5, time_step: float = 0.4):
         started = time.time()
index 77fbb98918660019c82835ddcf2ef85a5a1d6235..53e8e69a87d32e5ba3a0ca920d1bfbef469d5973 100644 (file)
@@ -1,3 +1,4 @@
+import ipaddress
 import sys
 
 import click
@@ -36,6 +37,22 @@ def workers(ctx: click.Context, instances: int):
     client = KnotManagerClient(ctx.obj[BASE_URL])
     client.set_num_workers(instances)
 
+@main.command("one-static-hint", help="Set one inline static-hint hints (replaces old static hints)")
+@click.argument("name", type=str, nargs=1)
+@click.argument("ip", type=str, nargs=1)
+@click.pass_context
+def one_static_hint(ctx: click.Context, name: str, ip: str):
+    client = KnotManagerClient(ctx.obj[BASE_URL])
+    client.set_static_hints({name: [ipaddress.ip_address(ip)]})
+
+@main.command("listen-ip", help="Configure where the resolver should listen (replaces all previous locations)")
+@click.argument("ip", type=str, nargs=1)
+@click.argument("port", type=int, nargs=1)
+@click.pass_context
+def listen_ip(ctx: click.Context, ip: str, port: int):
+    client = KnotManagerClient(ctx.obj[BASE_URL])
+    client.set_listen_ip_address(ipaddress.ip_address(ip), port)
+
 
 @main.command(help="Wait for manager initialization")
 @click.pass_context
index ce072ed377d55b4a5e45a5b5de9cb5ab776e3da8..bbe01f4bddaf84d4530e78bf2386338284f3a345 100644 (file)
@@ -34,9 +34,9 @@ class KresID:
 _used: "weakref.WeakSet[KresID]" = weakref.WeakSet()
 
 
-def alloc() -> KresID:
+def alloc(_custom_name_id: bool=False) -> KresID:
     for i in itertools.count(start=1):
-        val = KresID(i)
+        val = KresID(i if not _custom_name_id else -i)
         if val not in _used:
             _used.add(val)
             return val
@@ -52,6 +52,8 @@ def alloc_from_string(val: str) -> KresID:
         _used.add(res)
         return res
     else:
-        res = alloc()
+        # this would be for example 'gc'
+        # we want a special value, so that they do not clash with normal numerical values
+        res = alloc(_custom_name_id=True)
         res.set_custom_str_representation(val)
         return res
index 507df20551dad736454820812b7beb3a567ca6b1..e5d1171bdc4d640eba7355a036c141586bb53285 100644 (file)
@@ -155,7 +155,7 @@ class SystemdSubprocessController(SubprocessController):
                 # we can't easily check, if the unit is transient or not without additional systemd call
                 # we ignore it for now and assume the default persistency state. It shouldn't cause any
                 # problems, because interactions with the process are done the same way in all cases
-                res.append(SystemdSubprocess(SubprocessType.GC, alloc(), self._systemd_type))
+                res.append(SystemdSubprocess(SubprocessType.GC, alloc_from_string("gc"), self._systemd_type))
         return res
 
     async def initialize_controller(self) -> None:
index e1e2e45cee0fc5ba51e65110d6142e369f241c68..0245be42a91ab18a20dda5d1c9c6dee12697045e 100644 (file)
@@ -242,7 +242,9 @@ async def start_server(config: Union[Path, ParsedTree, _DefaultSentinel] = _DEFA
 
     await server.wait_for_shutdown()
 
-    logger.info("Gracefull shutdown triggered. Cleaning up...")
+    logger.info("Gracefull shutdown triggered.")
+    logger.info("Stopping API service...")
     await server.shutdown()
+    logger.info("Stopping kresd manager...")
     await manager.stop()
     logger.info(f"The manager run for {round(time() - start_time)} seconds... Hope it served well. Bye!")
index 7f7746b5613764dca485834684e8905b11de810a..25f59e7dc0b31ff2de4fe0e19b107d5c179bd28d 100755 (executable)
@@ -10,4 +10,4 @@ echo The manager will start after you connect
 echo API will be running on port 5000
 echo ----------------------------------------
 
-poetry run python -m debugpy --listen 0.0.0.0:5678 --wait-for-client -m knot_resolver_manager 5000 $@
\ No newline at end of file
+poetry run python -m debugpy --listen 0.0.0.0:5678 --wait-for-client -m knot_resolver_manager $@
\ No newline at end of file