|
4 | 4 | from typing import Dict, List, Optional, Set, Tuple, Type, TypeVar
|
5 | 5 | from urllib.parse import quote
|
6 | 6 |
|
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 |
8 | 8 | from knot_resolver.datamodel.types import IPAddressPort
|
9 | 9 | from knot_resolver.utils.modeling import parsing
|
10 | 10 | from knot_resolver.utils.modeling.exceptions import DataValidationError
|
@@ -154,25 +154,38 @@ def get_socket_from_config(config: Path, optional_file: bool) -> Optional[Socket
|
154 | 154 | try:
|
155 | 155 | with open(config, "r", encoding="utf8") as f:
|
156 | 156 | data = parsing.try_to_parse(f.read())
|
| 157 | + |
| 158 | + rkey = "rundir" |
| 159 | + rundir = Path(data[rkey]) if rkey in data else RUN_DIR |
| 160 | + |
157 | 161 | mkey = "management"
|
158 | 162 | if mkey in data:
|
159 | 163 | 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}") |
171 | 168 | return SocketDesc(
|
172 | 169 | f"http://{ip.addr}:{ip.port}",
|
173 | 170 | f'Key "/management/interface" in "{config}" file',
|
174 | 171 | )
|
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 | + |
176 | 189 | except ValueError as e:
|
177 | 190 | raise DataValidationError(*e.args) from e # pylint: disable=no-value-for-parameter
|
178 | 191 | except OSError as e:
|
|
0 commit comments