From 2f9ffff37b63f920556acabbbee21e8dc7f0a5da Mon Sep 17 00:00:00 2001 From: Vasek Sraier Date: Sun, 3 Oct 2021 15:02:50 +0200 Subject: [PATCH] cli client: added static hints + bug fix of id assignment --- .../knot_resolver_manager/client/__init__.py | 26 ++++++++++++++++++- .../knot_resolver_manager/client/__main__.py | 17 ++++++++++++ manager/knot_resolver_manager/kres_id.py | 8 +++--- .../kresd_controller/systemd/__init__.py | 2 +- manager/knot_resolver_manager/server.py | 4 ++- manager/scripts/run-debug | 2 +- 6 files changed, 52 insertions(+), 7 deletions(-) diff --git a/manager/knot_resolver_manager/client/__init__.py b/manager/knot_resolver_manager/client/__init__.py index 5c0d53a89..289fe3e76 100644 --- a/manager/knot_resolver_manager/client/__init__.py +++ b/manager/knot_resolver_manager/client/__init__.py @@ -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() diff --git a/manager/knot_resolver_manager/client/__main__.py b/manager/knot_resolver_manager/client/__main__.py index 77fbb9891..53e8e69a8 100644 --- a/manager/knot_resolver_manager/client/__main__.py +++ b/manager/knot_resolver_manager/client/__main__.py @@ -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 diff --git a/manager/knot_resolver_manager/kres_id.py b/manager/knot_resolver_manager/kres_id.py index ce072ed37..bbe01f4bd 100644 --- a/manager/knot_resolver_manager/kres_id.py +++ b/manager/knot_resolver_manager/kres_id.py @@ -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 diff --git a/manager/knot_resolver_manager/kresd_controller/systemd/__init__.py b/manager/knot_resolver_manager/kresd_controller/systemd/__init__.py index 507df2055..e5d1171bd 100644 --- a/manager/knot_resolver_manager/kresd_controller/systemd/__init__.py +++ b/manager/knot_resolver_manager/kresd_controller/systemd/__init__.py @@ -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: diff --git a/manager/knot_resolver_manager/server.py b/manager/knot_resolver_manager/server.py index e1e2e45ce..0245be42a 100644 --- a/manager/knot_resolver_manager/server.py +++ b/manager/knot_resolver_manager/server.py @@ -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!") diff --git a/manager/scripts/run-debug b/manager/scripts/run-debug index 7f7746b56..25f59e7dc 100755 --- a/manager/scripts/run-debug +++ b/manager/scripts/run-debug @@ -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 -- 2.47.3