source: genesis/tools/getrange.py@ 13525

Last change on this file since 13525 was 13321, checked in by rick, 9 years ago

Cosmetics move cache generation to new function

  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 3.1 KB
Line 
1#!/usr/bin/env python
2#
3# Zoek de eerste vrije (== niet in een wleiden.conf gedefinieerde) interlink
4# range.
5#
6# Maybe this should use the py-iplib module?
7#
8# Orignal: lodewijk@wirelessleiden.nl
9# 07/March/2012 - Rick van der Zwet <info@rickvanderzwet.nl>
10#
11import re
12import sys
13import glob
14import gformat
15
16def parseaddr(s):
17 f = s.split('.')
18 return (long(f[0]) << 24L) + \
19 (long(f[1]) << 16L) + \
20 (long(f[2]) << 8L) + \
21 long(f[3])
22
23
24def showaddr(a):
25 return "%d.%d.%d.%d" % ((a >> 24) & 0xff, (a >> 16) & 0xff, (a >> 8) & 0xff, a & 0xff)
26
27
28bezet_cache = None
29def make_cache():
30 global bezet_cache
31 bezet = {}
32 substs = {}
33 def add_ip(address):
34 if '/' not in address:
35 address += "/32"
36
37 addr, mask = address.split('/')
38 if addr in substs:
39 addr = substs[addr]
40 try:
41 addr = parseaddr(addr)
42 mask = int(mask)
43 addr = addr & ~((1 << (32 - mask)) - 1)
44 for i in range(0, (1 << (32 - mask))):
45 bezet[addr + i] = 1
46 except ValueError, e:
47 print "[FOUT] in address %s node %s" % (address, node)
48
49 for node in gformat.get_hostlist():
50 datadump = gformat.get_yaml(node)
51
52 add_ip(datadump['masterip'])
53 for key in datadump['autogen_iface_keys']:
54 add_ip(datadump[key]['ip'])
55
56 # Fetching smallest in use IP
57 # XXX: Currently not used for anything
58 i = parseaddr("255.255.255.255")
59 for k in bezet.keys():
60 if k < i:
61 i = k
62 bezet_cache = bezet
63 return bezet
64
65
66def get_ranges(interlink, size, aantal, use_history=False):
67 global bezet_cache
68 numaddrs = 1 << (32 - size)
69
70 if not use_history or (use_history and not bezet_cache):
71 bezet = make_cache()
72 else:
73 bezet = bezet_cache
74
75
76 # Interlinks are living in special ranges usually
77 if interlink:
78 i = parseaddr("172.16.3.0")
79 else:
80 i = parseaddr("172.17.0.0")
81
82 ranges = []
83 for n in xrange(aantal):
84 okay = False
85 while not okay:
86 while bezet.has_key(i):
87 i = i + numaddrs
88
89 # Assume Ok, till we find a already used IP
90 okay = True
91 for j in range(numaddrs):
92 if bezet.has_key(i + j):
93 i = i + numaddrs
94 okay = False
95 break
96 ranges.append(i)
97 bezet[i] = 1
98
99 bezet_cache = bezet
100 return ranges
101
102
103
104def main():
105 if len(sys.argv) < 2:
106 print "Gebruik: %s <interlink|subnet> <grootte> [aantal]" % sys.argv[0]
107 print "Voorbeelden:"
108 print "\tVoor drie /29 interlinks - '%s interlink 29 3'" % sys.argv[0]
109 print "\tVoor een (1) /24 subnet - '%s subnet 24'" % sys.argv[0]
110 exit(1)
111
112 # Argument parsing
113 interlink = sys.argv[1] == 'interlink'
114 size = int(sys.argv[2])
115 try:
116 aantal = int(sys.argv[3])
117 except (KeyError,IndexError):
118 aantal = 1
119
120 ranges = get_ranges(interlink, size, aantal)
121 numaddrs = 1 << (32 - size)
122
123 for i in ranges:
124 print "%s/%d:" % (showaddr(i), size),
125 if size > 28:
126 print " en ".join([showaddr(i) for i in range(i + 1, i + numaddrs - 1)])
127 else:
128 print " en ".join([showaddr(i) for i in range(i + 1, i + 4)]),
129 print "...",
130 print " en ".join([showaddr(i) for i in range(i + numaddrs - 7, i + numaddrs - 4)])
131
132if __name__ == "__main__":
133 main()
Note: See TracBrowser for help on using the repository browser.