Skip to content

Commit dc564c8

Browse files
avanthakkarobnoxxx
authored andcommitted
ctdb: improve hostname lookup for ctdb nodes
Fixes: #136 Signed-off-by: Avan Thakkar <[email protected]>
1 parent 7b361bf commit dc564c8

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

sambacc/commands/ctdb.py

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -261,12 +261,39 @@ def ctdb_migrate(ctx: Context) -> None:
261261
ctdb.archive_tdb(ctx.instance_config, ctx.cli.archive)
262262

263263

264-
def _lookup_hostname(hostname):
265-
# XXX this is a nasty little hack.
266-
ips = socket.gethostbyname_ex(hostname)[2]
267-
addr = [ip for ip in ips if ip != "127.0.0.1"][0]
268-
_logger.info(f"Determined address for {hostname}: {addr}")
269-
return addr
264+
def _lookup_hostname(hostname: str) -> str:
265+
try:
266+
addrinfo = socket.getaddrinfo(
267+
hostname,
268+
None,
269+
family=socket.AF_UNSPEC,
270+
type=socket.SOCK_STREAM,
271+
)
272+
ipv6_address = None
273+
274+
for entry in addrinfo:
275+
family, _, _, _, sockaddr = entry
276+
ip_address = sockaddr[0]
277+
278+
if ip_address.startswith("127.") or ip_address == "::1":
279+
continue
280+
281+
if family == socket.AF_INET:
282+
return ip_address
283+
284+
if family == socket.AF_INET6 and ipv6_address is None:
285+
ipv6_address = ip_address
286+
287+
if ipv6_address:
288+
return ipv6_address
289+
290+
raise RuntimeError(
291+
f"No valid IP address found for hostname '{hostname}'."
292+
)
293+
294+
except socket.gaierror as e:
295+
_logger.error(f"Failed to resolve hostname '{hostname}': {e}")
296+
raise
270297

271298

272299
@commands.command(name="ctdb-set-node", arg_func=_ctdb_set_node_args)

0 commit comments

Comments
 (0)