-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_inventory.py
More file actions
123 lines (87 loc) Β· 2.99 KB
/
validate_inventory.py
File metadata and controls
123 lines (87 loc) Β· 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env python3
import sys
import tomllib
from ipaddress import IPv4Address
from pathlib import Path
INVENTORY_FILE = Path("inventory.toml")
def validate_homes(homes: dict) -> bool:
has_errors = False
for name, home in homes.items():
user = home.get("user")
if not user:
print(f"β missing 'user' for home: {name}")
has_errors = True
continue
return has_errors
def validate_clients(clients: dict) -> bool:
has_errors = False
for name, client in clients.items():
disk_id = client.get("disk_id")
if not disk_id:
print(f"β missing 'disk_id' for client: {name}")
has_errors = True
continue
return has_errors
def validate_services(hosts: dict, clusters: dict, services: dict) -> bool:
has_errors = False
host_names = hosts.keys()
cluster_names = clusters.keys()
for svc_name, svc in services.items():
host = svc.get("host")
cluster = svc.get("cluster")
if not host and not cluster:
print(f"β service {svc_name} needs either a host or a cluster")
has_errors = True
if host and host not in host_names:
print(f"β service {svc_name} references unknown host {host}")
has_errors = True
if cluster and cluster not in cluster_names:
print(f"β service {svc_name} references unknown cluster {cluster}")
has_errors = True
return has_errors
def validate_hosts(hosts: dict) -> bool:
has_errors = False
seen = {}
for name, host in hosts.items():
addr = host.get("address")
if not addr:
print(f"β missing 'address' for host: {name}")
has_errors = True
continue
try:
IPv4Address(addr)
except ValueError:
print(f"β invalid IPv4 address on host {name}: {addr}")
has_errors = True
continue
if addr in seen:
print(f"β duplicate IPv4 address {addr} (hosts: {seen[addr]} and {name})")
has_errors = True
else:
seen[addr] = name
return has_errors
def load_inventory(path: Path) -> dict:
try:
with open(path, "rb") as f:
return tomllib.load(f)
except FileNotFoundError:
print(f"β inventory file not found: {path}")
sys.exit(1)
except tomllib.TOMLDecodeError as e:
print(f"β invalid TOML in {path}: {e}")
sys.exit(1)
def main():
data = load_inventory(INVENTORY_FILE)
errors = False
hosts = data.get("hosts", {})
errors |= validate_hosts(hosts)
clusters = data.get("clusters", {})
services = data.get("services", {})
errors |= validate_services(hosts, clusters, services)
clients = data.get("clients", {})
errors |= validate_clients(clients)
homes = data.get("homes", {})
errors |= validate_homes(homes)
sys.exit(1 if errors else 0)
if __name__ == "__main__":
main()