Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ctdb: improve hostname lookup for ctdb nodes #137

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 33 additions & 6 deletions sambacc/commands/ctdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,39 @@ def ctdb_migrate(ctx: Context) -> None:
ctdb.archive_tdb(ctx.instance_config, ctx.cli.archive)


def _lookup_hostname(hostname):
# XXX this is a nasty little hack.
ips = socket.gethostbyname_ex(hostname)[2]
addr = [ip for ip in ips if ip != "127.0.0.1"][0]
_logger.info(f"Determined address for {hostname}: {addr}")
return addr
def _lookup_hostname(hostname: str) -> str:
try:
addrinfo = socket.getaddrinfo(
hostname,
None,
family=socket.AF_UNSPEC,
type=socket.SOCK_STREAM,
)
ipv6_address = None

for entry in addrinfo:
family, _, _, _, sockaddr = entry
ip_address = sockaddr[0]

if ip_address.startswith("127.") or ip_address == "::1":
continue

if family == socket.AF_INET:
return ip_address

if family == socket.AF_INET6 and ipv6_address is None:
ipv6_address = ip_address

if ipv6_address:
return ipv6_address

raise RuntimeError(
f"No valid IP address found for hostname '{hostname}'."
)

except socket.gaierror as e:
_logger.error(f"Failed to resolve hostname '{hostname}': {e}")
raise


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