Skip to content

Commit aa07b35

Browse files
committed
fixup! datamodel: management socket default based on rundir
1 parent 38f9b26 commit aa07b35

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

python/knot_resolver/client/command.py

+26-13
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from typing import Dict, List, Optional, Set, Tuple, Type, TypeVar
55
from urllib.parse import quote
66

7-
from knot_resolver.constants import API_SOCK_FILE, CONFIG_FILE, RUN_DIR
7+
from knot_resolver.constants import API_SOCK_FILE, API_SOCK_NAME, CONFIG_FILE, RUN_DIR
88
from knot_resolver.datamodel.types import IPAddressPort
99
from knot_resolver.utils.modeling import parsing
1010
from knot_resolver.utils.modeling.exceptions import DataValidationError
@@ -154,25 +154,38 @@ def get_socket_from_config(config: Path, optional_file: bool) -> Optional[Socket
154154
try:
155155
with open(config, "r", encoding="utf8") as f:
156156
data = parsing.try_to_parse(f.read())
157+
158+
rkey = "rundir"
159+
rundir = Path(data[rkey]) if rkey in data else RUN_DIR
160+
157161
mkey = "management"
158162
if mkey in data:
159163
management = data[mkey]
160-
if "unix-socket" in management:
161-
soc = Path(management["unix-socket"])
162-
if not soc.is_absolute():
163-
rundir = data["rundir"] if "rundir" in data else RUN_DIR
164-
soc = rundir / soc
165-
return SocketDesc(
166-
f'http+unix://{quote(str(soc), safe="")}/',
167-
f'Key "/management/unix-socket" in "{config}" file',
168-
)
169-
if "interface" in management:
170-
ip = IPAddressPort(management["interface"], object_path=f"/{mkey}/interface")
164+
165+
ikey = "interface"
166+
if ikey in data[mkey]:
167+
ip = IPAddressPort(data[mkey][ikey], object_path=f"/{mkey}/{ikey}")
171168
return SocketDesc(
172169
f"http://{ip.addr}:{ip.port}",
173170
f'Key "/management/interface" in "{config}" file',
174171
)
175-
return None
172+
173+
skey = "unix-socket"
174+
if skey in management:
175+
socket = Path(management[skey])
176+
if not socket.is_absolute():
177+
socket = rundir / socket
178+
return SocketDesc(
179+
f'http+unix://{quote(str(socket), safe="")}/',
180+
f'Key "/management/unix-socket" in "{config}" file',
181+
)
182+
183+
socket = rundir / API_SOCK_NAME
184+
return SocketDesc(
185+
f'http+unix://{quote(str(socket), safe="")}/',
186+
f'Key "/rundir" in "{config}" file',
187+
)
188+
176189
except ValueError as e:
177190
raise DataValidationError(*e.args) from e # pylint: disable=no-value-for-parameter
178191
except OSError as e:

0 commit comments

Comments
 (0)