60 lines
1.7 KiB
Python
60 lines
1.7 KiB
Python
"""Show configured service roles separately from health observations."""
|
|
|
|
from ipaddress import ip_address
|
|
|
|
ROLE_LABELS = {
|
|
"dns": "DNS primary",
|
|
"dns-secondary": "DNS secondary",
|
|
"mail": "Mail",
|
|
}
|
|
|
|
|
|
def host_key(host):
|
|
try:
|
|
return str(ip_address(host))
|
|
except ValueError:
|
|
return host.removesuffix(".").lower()
|
|
|
|
|
|
def inventory_rows(nodes, replication_addresses):
|
|
rows = {}
|
|
secondaries = []
|
|
observed_primary = False
|
|
|
|
def add(host, role, release=None, state="not checked", *, local=False):
|
|
key = host_key(host)
|
|
row = rows.setdefault(
|
|
key, {"host": key, "local": False, "services": {}}
|
|
)
|
|
row["local"] |= local
|
|
row["services"].setdefault(
|
|
role,
|
|
{"role": ROLE_LABELS[role], "release": release, "state": state},
|
|
)
|
|
|
|
for node in nodes:
|
|
role = node["role"]
|
|
host = node["host"]
|
|
add(
|
|
host,
|
|
role,
|
|
node.get("release"),
|
|
node.get("state") or "unavailable",
|
|
local=node.get("local", False),
|
|
)
|
|
if role == "dns":
|
|
observed_primary = True
|
|
peer = node.get("dns_secondary")
|
|
if peer and host_key(peer) != host_key(host):
|
|
secondaries.append(peer)
|
|
if not secondaries and replication_addresses:
|
|
primary, secondary = map(str, replication_addresses)
|
|
if not observed_primary:
|
|
add(primary, "dns")
|
|
secondaries.append(secondary)
|
|
for host in secondaries:
|
|
row = rows.get(host_key(host))
|
|
if row is None or "dns" not in row["services"]:
|
|
add(host, "dns-secondary")
|
|
return rows.values()
|