source: genesis/tools/syntax-checker.py@ 14376

Last change on this file since 14376 was 14374, checked in by rick, 6 years ago

Update gformat to FreeBSD new default python3

  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 3.0 KB
Line 
1#!/usr/bin/env python3
2# vim:ts=2:et:sw=2:ai
3#
4# Build topological network graph
5# Rick van der Zwet <info@rickvanderzwet.nl>
6import gformat
7import sys
8import ipaddress
9
10from collections import defaultdict
11
12__version__ = '$Id: syntax-checker.py 14374 2019-06-11 10:41:16Z rick $'
13
14allowed_multi_use = list(map(lambda x: ipaddress.ip_network(x, strict=True), [
15 '192.168.0.0/22',
16 '192.168.0.0/16',
17 '192.168.0.0/24',
18 '192.168.1.0/24',
19 '192.168.178.0/24',
20 ]))
21
22
23def check_double_ip():
24 pool = defaultdict(list)
25 try:
26 for host in gformat.get_hostlist():
27 print("## Processing host %-25s: " % host, end='')
28 datadump = gformat.get_yaml(host,add_version_info=False)
29 masterip_addr = ipaddress.IPv4Interface(datadump['masterip'])
30 masterip_is_used = False
31
32 # Check syntax of defined variables
33 _ = gformat.generate_wleiden_yaml(datadump)
34 try:
35 # Process interfaces
36 iface_keys = [elem for elem in datadump.keys() if (elem.startswith('iface_') and not "lo0" in elem)]
37 for iface_key in iface_keys:
38 # Extra (descriptive entries) are ignored
39 if '_extra' in iface_key:
40 continue
41
42 # Process actual and virtual IP (avoiding clashes with nanostation IP)
43 for entry in ['ip', 'ns_ip']:
44 if entry in datadump[iface_key]:
45 addr = ipaddress.IPv4Interface(datadump[iface_key][entry])
46 if masterip_addr in addr.network:
47 masterip_is_used = True
48 pool[addr.network].append((host, iface_key, entry, addr))
49
50
51 # Add masterip to the list if IP has not been defined at interface
52 if not masterip_is_used:
53 pool[masterip_addr.network].append((host, 'masterip', '', masterip_addr))
54
55 print("OK")
56 except (KeyError, ValueError) as e:
57 print("[ERROR] in '%s' interface '%s' (%s)" % (host, iface_key, e))
58 raise
59 except Exception as e:
60 raise
61 sys.exit(1)
62
63 errors = 0
64 keys = sorted(pool.keys(),reverse=True)
65
66 for i,network in enumerate(keys):
67 if not network in allowed_multi_use:
68 for network2 in keys[i+1:]:
69 if not network2 in allowed_multi_use and network2.overlaps(network):
70 errors += 1
71 print("[ERROR#%i] network %s overlaps with %s:" % (errors, network, network2))
72 for (host, key, entry, addr) in sorted(pool[network] + pool[network2]):
73 print(" - %-20s - %-20s - %-5s - %s" % (host, key, entry, addr))
74
75 leden = sorted(pool[network])
76 for i,lid in enumerate(leden):
77 for lid2 in leden[i+1:]:
78 if lid[3] == lid2[3]:
79 errors += 1
80 print("[ERROR#%i] Multiple usages of IP %s:" % (errors, lid[3]))
81 print(" - %-20s - %-20s - %-5s" % (lid[0], lid[1], lid[2]))
82 print(" - %-20s - %-20s - %-5s" % (lid2[0], lid2[1], lid2[2]))
83
84 if errors > 0:
85 print("# %i Errors found" % errors)
86 return 1
87 else:
88 print("# No multiple usages of IPs found")
89 return 0
90
91
92if __name__ == "__main__":
93 sys.exit(check_double_ip())
94
Note: See TracBrowser for help on using the repository browser.