1 | #!/usr/bin/env python
|
---|
2 | #
|
---|
3 | # vim:ts=2:et:sw=2:ai
|
---|
4 | # Wireless Leiden configuration generator, based on yaml files'
|
---|
5 | # Rick van der Zwet <info@rickvanderzwet.nl>
|
---|
6 | import cgi
|
---|
7 | import cgitb
|
---|
8 | import copy
|
---|
9 | import glob
|
---|
10 | import os
|
---|
11 | import socket
|
---|
12 | import string
|
---|
13 | import subprocess
|
---|
14 | import sys
|
---|
15 | import time
|
---|
16 | from pprint import pprint
|
---|
17 | try:
|
---|
18 | import yaml
|
---|
19 | except ImportError, e:
|
---|
20 | print e
|
---|
21 | print "[ERROR] Please install the python-yaml or devel/py-yaml package"
|
---|
22 | exit(1)
|
---|
23 |
|
---|
24 |
|
---|
25 | NODE_DIR = os.path.dirname(os.path.realpath(__file__))
|
---|
26 | __version__ = '$Id: gformat.py 8585 2010-10-20 22:40:20Z rick $'
|
---|
27 |
|
---|
28 |
|
---|
29 | files = [
|
---|
30 | 'authorized_keys',
|
---|
31 | 'dnsmasq.conf',
|
---|
32 | 'rc.conf.local',
|
---|
33 | 'resolv.conf',
|
---|
34 | 'wleiden.yaml'
|
---|
35 | ]
|
---|
36 |
|
---|
37 | # Global variables uses
|
---|
38 | OK = 10
|
---|
39 | DOWN = 20
|
---|
40 | UNKNOWN = 90
|
---|
41 |
|
---|
42 |
|
---|
43 | def get_proxylist():
|
---|
44 | """Get all available proxies proxyX sorting based on X number"""
|
---|
45 | os.chdir(NODE_DIR)
|
---|
46 | proxylist = sorted(glob.glob("proxy*"),
|
---|
47 | key=lambda name: int(''.join([c for c in name if c in string.digits])),
|
---|
48 | cmp=lambda x,y: x - y)
|
---|
49 | return proxylist
|
---|
50 |
|
---|
51 |
|
---|
52 |
|
---|
53 | def valid_addr(addr):
|
---|
54 | """ Show which address is valid in which are not """
|
---|
55 | return str(addr).startswith('172.')
|
---|
56 |
|
---|
57 |
|
---|
58 | def get_nodelist():
|
---|
59 | """ Get all available nodes - sorted """
|
---|
60 | os.chdir(NODE_DIR)
|
---|
61 | nodelist = sorted(glob.glob("CNode*"))
|
---|
62 | return nodelist
|
---|
63 |
|
---|
64 | def get_hostlist():
|
---|
65 | """ Combined hosts and proxy list"""
|
---|
66 | return get_nodelist() + get_proxylist()
|
---|
67 |
|
---|
68 |
|
---|
69 | def generate_title(nodelist):
|
---|
70 | """ Main overview page """
|
---|
71 | items = {'root' : "." }
|
---|
72 | output = """
|
---|
73 | <html>
|
---|
74 | <head>
|
---|
75 | <title>Wireless leiden Configurator - GFormat</title>
|
---|
76 | <style type="text/css">
|
---|
77 | th {background-color: #999999}
|
---|
78 | tr:nth-child(odd) {background-color: #cccccc}
|
---|
79 | tr:nth-child(even) {background-color: #ffffff}
|
---|
80 | th, td {padding: 0.1em 1em}
|
---|
81 | </style>
|
---|
82 | </head>
|
---|
83 | <body>
|
---|
84 | <center>
|
---|
85 | <form type="GET" action="%(root)s">
|
---|
86 | <input type="hidden" name="action" value="update">
|
---|
87 | <input type="submit" value="Update Configuration Database (SVN)">
|
---|
88 | </form>
|
---|
89 | <table>
|
---|
90 | <caption><h3>Wireless Leiden Configurator</h3></caption>
|
---|
91 | """ % items
|
---|
92 |
|
---|
93 | for node in nodelist:
|
---|
94 | items['node'] = node
|
---|
95 | output += '<tr><td><a href="%(root)s/%(node)s">%(node)s</a></td>' % items
|
---|
96 | for config in files:
|
---|
97 | items['config'] = config
|
---|
98 | output += '<td><a href="%(root)s/%(node)s/%(config)s">%(config)s</a></td>' % items
|
---|
99 | output += "</tr>"
|
---|
100 | output += """
|
---|
101 | </table>
|
---|
102 | <hr />
|
---|
103 | <em>%s</em>
|
---|
104 | </center>
|
---|
105 | </body>
|
---|
106 | </html>
|
---|
107 | """ % __version__
|
---|
108 |
|
---|
109 | return output
|
---|
110 |
|
---|
111 |
|
---|
112 |
|
---|
113 | def generate_node(node):
|
---|
114 | """ Print overview of all files available for node """
|
---|
115 | return "\n".join(files)
|
---|
116 |
|
---|
117 |
|
---|
118 |
|
---|
119 | def generate_header(ctag="#"):
|
---|
120 | return """\
|
---|
121 | %(ctag)s
|
---|
122 | %(ctag)s DO NOT EDIT - Automatically generated by 'gformat'
|
---|
123 | %(ctag)s Generated at %(date)s by %(host)s
|
---|
124 | %(ctag)s
|
---|
125 | """ % { 'ctag' : ctag, 'date' : time.ctime(), 'host' : socket.gethostname() }
|
---|
126 |
|
---|
127 |
|
---|
128 |
|
---|
129 | def parseaddr(s):
|
---|
130 | """ Process IPv4 CIDR notation addr to a (binary) number """
|
---|
131 | f = s.split('.')
|
---|
132 | return (long(f[0]) << 24L) + \
|
---|
133 | (long(f[1]) << 16L) + \
|
---|
134 | (long(f[2]) << 8L) + \
|
---|
135 | long(f[3])
|
---|
136 |
|
---|
137 |
|
---|
138 |
|
---|
139 | def showaddr(a):
|
---|
140 | """ Display IPv4 addr in (dotted) CIDR notation """
|
---|
141 | return "%d.%d.%d.%d" % ((a >> 24) & 0xff, (a >> 16) & 0xff, (a >> 8) & 0xff, a & 0xff)
|
---|
142 |
|
---|
143 |
|
---|
144 | def is_member(ip, mask, canidate):
|
---|
145 | """ Return True if canidate is part of ip/mask block"""
|
---|
146 | ip_addr = gformat.parseaddr(ip)
|
---|
147 | ip_canidate = gformat.parseaddr(canidate)
|
---|
148 | mask = int(mask)
|
---|
149 | ip_addr = ip_addr & ~((1 << (32 - mask)) - 1)
|
---|
150 | ip_canidate = ip_canidate & ~((1 << (32 - mask)) - 1)
|
---|
151 | return ip_addr == ip_canidate
|
---|
152 |
|
---|
153 |
|
---|
154 |
|
---|
155 |
|
---|
156 | def netmask2subnet(netmask):
|
---|
157 | """ Given a 'netmask' return corresponding CIDR """
|
---|
158 | return showaddr(0xffffffff & (0xffffffff << (32 - int(netmask))))
|
---|
159 |
|
---|
160 |
|
---|
161 |
|
---|
162 | def generate_dnsmasq_conf(datadump):
|
---|
163 | """ Generate configuration file '/usr/local/etc/dnsmasq.conf' """
|
---|
164 | output = generate_header()
|
---|
165 | output += """\
|
---|
166 | # DHCP server options
|
---|
167 | dhcp-authoritative
|
---|
168 | dhcp-fqdn
|
---|
169 | domain=dhcp.%(nodename_lower)s.%(domain)s
|
---|
170 | domain-needed
|
---|
171 | expand-hosts
|
---|
172 |
|
---|
173 | # Low memory footprint
|
---|
174 | cache-size=10000
|
---|
175 | \n""" % datadump
|
---|
176 |
|
---|
177 | for iface_key in datadump['iface_keys']:
|
---|
178 | if not datadump[iface_key].has_key('comment'):
|
---|
179 | datadump[iface_key]['comment'] = None
|
---|
180 | output += "## %(interface)s - %(desc)s - %(comment)s\n" % datadump[iface_key]
|
---|
181 |
|
---|
182 | try:
|
---|
183 | (dhcp_start, dhcp_stop) = datadump[iface_key]['dhcp'].split('-')
|
---|
184 | (ip, netmask) = datadump[iface_key]['ip'].split('/')
|
---|
185 | datadump[iface_key]['subnet'] = netmask2subnet(netmask)
|
---|
186 | except (AttributeError, ValueError):
|
---|
187 | output += "# not autoritive\n\n"
|
---|
188 | continue
|
---|
189 |
|
---|
190 | dhcp_part = ".".join(ip.split('.')[0:3])
|
---|
191 | datadump[iface_key]['dhcp_start'] = dhcp_part + "." + dhcp_start
|
---|
192 | datadump[iface_key]['dhcp_stop'] = dhcp_part + "." + dhcp_stop
|
---|
193 | output += "dhcp-range=%(interface)s,%(dhcp_start)s,%(dhcp_stop)s,%(subnet)s,24h\n\n" % datadump[iface_key]
|
---|
194 |
|
---|
195 | return output
|
---|
196 |
|
---|
197 |
|
---|
198 |
|
---|
199 | def generate_rc_conf_local(datadump):
|
---|
200 | """ Generate configuration file '/etc/rc.conf.local' """
|
---|
201 | output = generate_header("#");
|
---|
202 | output += """\
|
---|
203 | hostname='%(nodetype)s%(nodename)s.%(domain)s'
|
---|
204 | location='%(location)s'
|
---|
205 | """ % datadump
|
---|
206 |
|
---|
207 | # TProxy configuration
|
---|
208 | output += "\n"
|
---|
209 | try:
|
---|
210 | if datadump['tproxy']:
|
---|
211 | output += """\
|
---|
212 | tproxy_enable='YES'
|
---|
213 | tproxy_range='%(tproxy)s'
|
---|
214 | """ % datadump
|
---|
215 | except KeyError:
|
---|
216 | output += "tproxy_enable='NO'\n"
|
---|
217 |
|
---|
218 | output += '\n'
|
---|
219 | # lo0 configuration:
|
---|
220 | # - 172.32.255.1/32 is the proxy.wleiden.net deflector
|
---|
221 | # - masterip is special as it needs to be assigned to at
|
---|
222 | # least one interface, so if not used assign to lo0
|
---|
223 | addrs_list = { 'lo0' : ["127.0.0.1/8", "172.31.255.1/32"] }
|
---|
224 | iface_map = {'lo0' : 'lo0'}
|
---|
225 |
|
---|
226 | masterip_used = False
|
---|
227 | for iface_key in datadump['iface_keys']:
|
---|
228 | if datadump[iface_key]['ip'].startswith(datadump['masterip']):
|
---|
229 | masterip_used = True
|
---|
230 | break
|
---|
231 | if not masterip_used:
|
---|
232 | addrs_list['lo0'].append(datadump['masterip'] + "/32")
|
---|
233 |
|
---|
234 | wlan_count = 0
|
---|
235 | for iface_key in datadump['iface_keys']:
|
---|
236 | ifacedump = datadump[iface_key]
|
---|
237 | interface = ifacedump['interface']
|
---|
238 | # By default no special interface mapping
|
---|
239 | iface_map[interface] = interface
|
---|
240 |
|
---|
241 | # Add interface IP to list
|
---|
242 | if addrs_list.has_key(interface):
|
---|
243 | addrs_list[interface].append(ifacedump['ip'])
|
---|
244 | else:
|
---|
245 | addrs_list[interface] = [ifacedump['ip']]
|
---|
246 |
|
---|
247 | # Alias only needs IP assignment for now, this might change if we
|
---|
248 | # are going to use virtual accesspoints
|
---|
249 | if "alias" in iface_key:
|
---|
250 | continue
|
---|
251 |
|
---|
252 | # XXX: Might want to deduct type directly from interface name
|
---|
253 | if ifacedump['type'] in ['11a', '11b', '11g', 'wireless']:
|
---|
254 | # Create wlanX interface
|
---|
255 | ifacedump['wlanif'] ="wlan%i" % wlan_count
|
---|
256 | iface_map[interface] = ifacedump['wlanif']
|
---|
257 | wlan_count += 1
|
---|
258 |
|
---|
259 | # Default to station (client) mode
|
---|
260 | ifacedump['wlanmode'] = "sta"
|
---|
261 | if ifacedump['mode'] in ['master', 'master-wds']:
|
---|
262 | ifacedump['wlanmode'] = "ap"
|
---|
263 | # Default to 802.11b mode
|
---|
264 | ifacedump['mode'] = '11b'
|
---|
265 | if ifacedump['type'] in ['11a', '11b' '11g']:
|
---|
266 | ifacedump['mode'] = ifacedump['type']
|
---|
267 |
|
---|
268 | if not ifacedump.has_key('channel'):
|
---|
269 | if ifacedump['type'] == '11a':
|
---|
270 | ifacedump['channel'] = 36
|
---|
271 | else:
|
---|
272 | ifacedump['channel'] = 1
|
---|
273 |
|
---|
274 | # Allow special hacks at the back like wds and stuff
|
---|
275 | if not ifacedump.has_key('extra'):
|
---|
276 | ifacedump['extra'] = 'regdomain ETSI country NL'
|
---|
277 |
|
---|
278 | output += "wlans_%(interface)s='%(wlanif)s'\n" % ifacedump
|
---|
279 | output += ("create_args_%(wlanif)s='wlanmode %(wlanmode)s mode " +\
|
---|
280 | "%(mode)s ssid %(ssid)s %(extra)s channel %(channel)s'\n") % ifacedump
|
---|
281 |
|
---|
282 | elif ifacedump['type'] in ['ethernet', 'eth']:
|
---|
283 | # No special config needed besides IP
|
---|
284 | pass
|
---|
285 | else:
|
---|
286 | assert False, "Unknown type " + ifacedump['type']
|
---|
287 |
|
---|
288 | # Print IP address which needs to be assigned over here
|
---|
289 | output += "\n"
|
---|
290 | for iface,addrs in sorted(addrs_list.iteritems()):
|
---|
291 | output += "ipv4_addrs_%s='%s'\n" % (iface_map[iface], " ".join(addrs))
|
---|
292 |
|
---|
293 | return output
|
---|
294 |
|
---|
295 |
|
---|
296 |
|
---|
297 | def get_yaml(item):
|
---|
298 | """ Get configuration yaml for 'item'"""
|
---|
299 | gfile = NODE_DIR + '/%s/wleiden.yaml' % item
|
---|
300 |
|
---|
301 | f = open(gfile, 'r')
|
---|
302 | datadump = yaml.load(f)
|
---|
303 | f.close()
|
---|
304 |
|
---|
305 | return datadump
|
---|
306 |
|
---|
307 |
|
---|
308 |
|
---|
309 | def get_all_configs():
|
---|
310 | """ Get dict with key 'host' with all configs present """
|
---|
311 | configs = dict()
|
---|
312 | for host in get_hostlist():
|
---|
313 | datadump = get_yaml(host)
|
---|
314 | configs[host] = datadump
|
---|
315 | return configs
|
---|
316 |
|
---|
317 |
|
---|
318 | def get_interface_keys(config):
|
---|
319 | """ Quick hack to get all interface keys, later stage convert this to a iterator """
|
---|
320 | return [elem for elem in config.keys() if (elem.startswith('iface_') and not "lo0" in elem)]
|
---|
321 |
|
---|
322 |
|
---|
323 | def get_used_ips(configs):
|
---|
324 | """ Return array of all IPs used in config files"""
|
---|
325 | ip_list = []
|
---|
326 | for config in configs:
|
---|
327 | ip_list.append(config['masterip'])
|
---|
328 | for iface_key in get_interface_keys(config):
|
---|
329 | l = config[iface_key]['ip']
|
---|
330 | addr, mask = l.split('/')
|
---|
331 | # Special case do not process
|
---|
332 | if valid_addr(addr):
|
---|
333 | ip_list.append(addr)
|
---|
334 | else:
|
---|
335 | print "## IP '%s' in '%s' not valid" % (addr, config['nodename'])
|
---|
336 | return sorted(ip_list)
|
---|
337 |
|
---|
338 |
|
---|
339 |
|
---|
340 | def write_yaml(item, datadump):
|
---|
341 | """ Write configuration yaml for 'item'"""
|
---|
342 | gfile = NODE_DIR + '/%s/wleiden.yaml' % item
|
---|
343 |
|
---|
344 | f = open(gfile, 'w')
|
---|
345 | f.write(format_wleiden_yaml(datadump))
|
---|
346 | f.close()
|
---|
347 |
|
---|
348 |
|
---|
349 |
|
---|
350 | def generate_resolv_conf(datadump):
|
---|
351 | """ Generate configuration file '/etc/resolv.conf' """
|
---|
352 | output = generate_header("#");
|
---|
353 | output += """\
|
---|
354 | search wleiden.net
|
---|
355 | # Try local (cache) first
|
---|
356 | nameserver 127.0.0.1
|
---|
357 |
|
---|
358 | # Proxies are recursive nameservers
|
---|
359 | # needs to be in resolv.conf for dnsmasq as well
|
---|
360 | """ % datadump
|
---|
361 |
|
---|
362 | for proxy in get_proxylist():
|
---|
363 | proxy_ip = get_yaml(proxy)['masterip']
|
---|
364 | output += "nameserver %-15s # %s\n" % (proxy_ip, proxy)
|
---|
365 | return output
|
---|
366 |
|
---|
367 |
|
---|
368 |
|
---|
369 | def format_yaml_value(value):
|
---|
370 | """ Get yaml value in right syntax for outputting """
|
---|
371 | if isinstance(value,str):
|
---|
372 | output = "'%s'" % value
|
---|
373 | else:
|
---|
374 | output = value
|
---|
375 | return output
|
---|
376 |
|
---|
377 |
|
---|
378 |
|
---|
379 | def format_wleiden_yaml(datadump):
|
---|
380 | """ Special formatting to ensure it is editable"""
|
---|
381 | output = "# Genesis config yaml style\n"
|
---|
382 | output += "# vim:ts=2:et:sw=2:ai\n"
|
---|
383 | output += "#\n"
|
---|
384 | iface_keys = [elem for elem in datadump.keys() if elem.startswith('iface_')]
|
---|
385 | for key in sorted(set(datadump.keys()) - set(iface_keys)):
|
---|
386 | output += "%-10s: %s\n" % (key, format_yaml_value(datadump[key]))
|
---|
387 |
|
---|
388 | output += "\n\n"
|
---|
389 |
|
---|
390 | key_order = [ 'comment', 'interface', 'ip', 'desc', 'sdesc', 'mode', 'type',
|
---|
391 | 'extra_type', 'channel', 'ssid', 'dhcp' ]
|
---|
392 |
|
---|
393 | for iface_key in sorted(iface_keys):
|
---|
394 | output += "%s:\n" % iface_key
|
---|
395 | for key in key_order + list(sorted(set(datadump[iface_key].keys()) - set(key_order))):
|
---|
396 | if datadump[iface_key].has_key(key):
|
---|
397 | output += " %-11s: %s\n" % (key, format_yaml_value(datadump[iface_key][key]))
|
---|
398 | output += "\n\n"
|
---|
399 |
|
---|
400 | return output
|
---|
401 |
|
---|
402 |
|
---|
403 |
|
---|
404 | def generate_wleiden_yaml(datadump):
|
---|
405 | """ Generate (petty) version of wleiden.yaml"""
|
---|
406 | output = generate_header("#")
|
---|
407 | output += format_wleiden_yaml(datadump)
|
---|
408 | return output
|
---|
409 |
|
---|
410 |
|
---|
411 |
|
---|
412 | def generate_config(node, config, datadump=None):
|
---|
413 | """ Print configuration file 'config' of 'node' """
|
---|
414 | output = ""
|
---|
415 | try:
|
---|
416 | # Load config file
|
---|
417 | if datadump == None:
|
---|
418 | datadump = get_yaml(node)
|
---|
419 |
|
---|
420 | # Preformat certain needed variables for formatting and push those into special object
|
---|
421 | datadump_extra = copy.deepcopy(datadump)
|
---|
422 | if not datadump_extra.has_key('domain'):
|
---|
423 | datadump_extra['domain'] = 'wleiden.net'
|
---|
424 | datadump_extra['nodename_lower'] = datadump_extra['nodename'].lower()
|
---|
425 | datadump_extra['iface_keys'] = sorted([elem for elem in datadump.keys() if elem.startswith('iface_')])
|
---|
426 |
|
---|
427 | if config == 'wleiden.yaml':
|
---|
428 | output += generate_wleiden_yaml(datadump)
|
---|
429 | elif config == 'authorized_keys':
|
---|
430 | f = open("global_keys", 'r')
|
---|
431 | output += f.read()
|
---|
432 | f.close()
|
---|
433 | elif config == 'dnsmasq.conf':
|
---|
434 | output += generate_dnsmasq_conf(datadump_extra)
|
---|
435 | elif config == 'rc.conf.local':
|
---|
436 | output += generate_rc_conf_local(datadump_extra)
|
---|
437 | elif config == 'resolv.conf':
|
---|
438 | output += generate_resolv_conf(datadump_extra)
|
---|
439 | else:
|
---|
440 | assert False, "Config not found!"
|
---|
441 | except IOError, e:
|
---|
442 | output += "[ERROR] Config file not found"
|
---|
443 | return output
|
---|
444 |
|
---|
445 |
|
---|
446 |
|
---|
447 | def process_cgi_request():
|
---|
448 | """ When calling from CGI """
|
---|
449 | # Update repository if requested
|
---|
450 | form = cgi.FieldStorage()
|
---|
451 | if form.getvalue("action") == "update":
|
---|
452 | print "Refresh: 5; url=."
|
---|
453 | print "Content-type:text/plain\r\n\r\n",
|
---|
454 | print "[INFO] Updating subverion, please wait..."
|
---|
455 | print subprocess.Popen(['svn', 'up', NODE_DIR], stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0],
|
---|
456 | print "[INFO] All done, redirecting in 5 seconds"
|
---|
457 | sys.exit(0)
|
---|
458 |
|
---|
459 |
|
---|
460 | uri = os.environ['PATH_INFO'].strip('/').split('/')
|
---|
461 | output = ""
|
---|
462 | if not uri[0]:
|
---|
463 | output += "Content-type:text/html\r\n\r\n"
|
---|
464 | output += generate_title(get_hostlist())
|
---|
465 | elif len(uri) == 1:
|
---|
466 | output += "Content-type:text/plain\r\n\r\n"
|
---|
467 | output += generate_node(uri[0])
|
---|
468 | elif len(uri) == 2:
|
---|
469 | output += "Content-type:text/plain\r\n\r\n"
|
---|
470 | output += generate_config(uri[0], uri[1])
|
---|
471 | else:
|
---|
472 | assert False, "Invalid option"
|
---|
473 | print output
|
---|
474 |
|
---|
475 |
|
---|
476 | def usage():
|
---|
477 | print """Usage: %s <standalone [port] |test [test arguments]|static>
|
---|
478 | Examples:
|
---|
479 | \tstandalone = Run configurator webserver [default port=8000]
|
---|
480 | \tstatic = Generate all config files and store on disk
|
---|
481 | \t with format ./static/%%NODE%%/%%FILE%%
|
---|
482 | \ttest CNodeRick dnsmasq.conf = Receive output of CGI script
|
---|
483 | \t for arguments CNodeRick/dnsmasq.conf
|
---|
484 | """
|
---|
485 | exit(0)
|
---|
486 |
|
---|
487 |
|
---|
488 |
|
---|
489 | def main():
|
---|
490 | """Hard working sub"""
|
---|
491 | # Allow easy hacking using the CLI
|
---|
492 | if not os.environ.has_key('PATH_INFO'):
|
---|
493 | if len(sys.argv) < 2:
|
---|
494 | usage()
|
---|
495 |
|
---|
496 | if sys.argv[1] == "standalone":
|
---|
497 | import SocketServer
|
---|
498 | import CGIHTTPServer
|
---|
499 | try:
|
---|
500 | PORT = int(sys.argv[2])
|
---|
501 | except (IndexError,ValueError):
|
---|
502 | PORT = 8000
|
---|
503 |
|
---|
504 | class MyCGIHTTPRequestHandler(CGIHTTPServer.CGIHTTPRequestHandler):
|
---|
505 | """ Serve this CGI from the root of the webserver """
|
---|
506 | def is_cgi(self):
|
---|
507 | if "favicon" in self.path:
|
---|
508 | return False
|
---|
509 |
|
---|
510 | self.cgi_info = (__file__, self.path)
|
---|
511 | self.path = ''
|
---|
512 | return True
|
---|
513 | handler = MyCGIHTTPRequestHandler
|
---|
514 | httpd = SocketServer.TCPServer(("", PORT), handler)
|
---|
515 | httpd.server_name = 'localhost'
|
---|
516 | httpd.server_port = PORT
|
---|
517 |
|
---|
518 | print "serving at port", PORT
|
---|
519 | httpd.serve_forever()
|
---|
520 | elif sys.argv[1] == "test":
|
---|
521 | os.environ['PATH_INFO'] = "/".join(sys.argv[2:])
|
---|
522 | os.environ['SCRIPT_NAME'] = __file__
|
---|
523 | process_cgi_request()
|
---|
524 | elif sys.argv[1] == "static":
|
---|
525 | items = dict()
|
---|
526 | for node in get_hostlist():
|
---|
527 | items['node'] = node
|
---|
528 | items['wdir'] = "./static/%(node)s" % items
|
---|
529 | if not os.path.isdir(items['wdir']):
|
---|
530 | os.makedirs(items['wdir'])
|
---|
531 | datadump = get_yaml(node)
|
---|
532 | for config in files:
|
---|
533 | items['config'] = config
|
---|
534 | print "## Generating %(node)s %(config)s" % items
|
---|
535 | f = open("%(wdir)s/%(config)s" % items, "w")
|
---|
536 | f.write(generate_config(node, config, datadump))
|
---|
537 | f.close()
|
---|
538 | elif sys.argv[1] == "dns":
|
---|
539 | items = dict()
|
---|
540 | # hostname is key, IP is value
|
---|
541 | wleiden_zone = dict()
|
---|
542 | wleiden_cname = dict()
|
---|
543 | pool = dict()
|
---|
544 | for node in get_hostlist():
|
---|
545 | datadump = get_yaml(node)
|
---|
546 |
|
---|
547 | # Proxy naming convention is special
|
---|
548 | if datadump['nodetype'] == 'Proxy':
|
---|
549 | fqdn = datadump['nodename']
|
---|
550 | else:
|
---|
551 | # By default the full name is listed and also a shortname CNAME for easy use.
|
---|
552 | fqdn = datadump['nodetype'] + datadump['nodename']
|
---|
553 | wleiden_cname[datadump['nodename']] = fqdn
|
---|
554 | wleiden_zone[fqdn] = datadump['masterip']
|
---|
555 |
|
---|
556 | #items['node'] = node
|
---|
557 | #items['wdir'] = "./static/%(node)s" % items
|
---|
558 | #if not os.path.isdir(items['wdir']):
|
---|
559 | # os.makedirs(items['wdir'])
|
---|
560 | for iface_key in get_interface_keys(datadump):
|
---|
561 | iface_name = datadump[iface_key]['interface'].replace(':',"_alias_")
|
---|
562 | (ip, netmask) = datadump[iface_key]['ip'].split('/')
|
---|
563 | try:
|
---|
564 | (dhcp_start, dhcp_stop) = datadump[iface_key]['dhcp'].split('-')
|
---|
565 | datadump[iface_key]['subnet'] = netmask2subnet(netmask)
|
---|
566 | dhcp_part = ".".join(ip.split('.')[0:3])
|
---|
567 | if ip != datadump['masterip']:
|
---|
568 | wleiden_zone["dhcp-gateway-%s.%s" % (iface_name, fqdn)] = ip
|
---|
569 | for i in range(int(dhcp_start), int(dhcp_stop) + 1):
|
---|
570 | wleiden_zone["dhcp-%s-%s.%s" % (i, iface_name, fqdn)] = "%s.%s" % (dhcp_part, i)
|
---|
571 | except (AttributeError, ValueError):
|
---|
572 | # First push it into a pool, to indentify the counter-part later on
|
---|
573 | addr = parseaddr(ip)
|
---|
574 | netmask = int(netmask)
|
---|
575 | addr = addr & ~((1 << (32 - netmask)) - 1)
|
---|
576 | if pool.has_key(addr):
|
---|
577 | pool[addr] += [(iface_name, fqdn, ip)]
|
---|
578 | else:
|
---|
579 | pool[addr] = [(iface_name, fqdn, ip)]
|
---|
580 | continue
|
---|
581 |
|
---|
582 | # wleiden_zone["2%s-%s.%s" % ("unused", iface_name, fqdn)] = ip
|
---|
583 | # #XXX: automatic naming convention namely 2 + remote.lower()
|
---|
584 | for (key,value) in pool.iteritems():
|
---|
585 | if len(value) == 1:
|
---|
586 | (iface_name, fqdn, ip) = value[0]
|
---|
587 | wleiden_zone["2unused-%s.%s" % (iface_name, fqdn)] = ip
|
---|
588 | elif len(value) == 2:
|
---|
589 | (a_iface_name, a_fqdn, a_ip) = value[0]
|
---|
590 | (b_iface_name, b_fqdn, b_ip) = value[1]
|
---|
591 | wleiden_zone["2%s.%s" % (b_fqdn,a_fqdn)] = a_ip
|
---|
592 | wleiden_zone["2%s.%s" % (a_fqdn,b_fqdn)] = b_ip
|
---|
593 | else:
|
---|
594 | pool_members = [k[1] for k in value]
|
---|
595 | for item in value:
|
---|
596 | (iface_name, fqdn, ip) = item
|
---|
597 | pool_name = "2pool-" + showaddr(key).replace('.','-') + "-" + "_".join(sorted(list(set(pool_members) - set([fqdn]))))
|
---|
598 | wleiden_zone["%s.%s" % (pool_name, fqdn)] = ip
|
---|
599 |
|
---|
600 |
|
---|
601 | f = open("db.wleiden.net", "w")
|
---|
602 | for host,ip in wleiden_zone.iteritems():
|
---|
603 | f.write("%s.wleiden.net. A %s ~\n" % (host, ip))
|
---|
604 | for source,dest in wleiden_cname.iteritems():
|
---|
605 | f.write("%s.wleiden.net. CNAME %s.wleiden.net.\n" % (source, dest))
|
---|
606 | f.close()
|
---|
607 | f = open("db.172.in-addr.arpa", "w")
|
---|
608 | for host,ip in wleiden_zone.iteritems():
|
---|
609 | rev_ip = '.'.join(reversed(ip.split('.')))
|
---|
610 | f.write("%s.in-addr.arpa. PTR %s.wleiden.net. ~\n" % (rev_ip, host))
|
---|
611 | f.close()
|
---|
612 | #pprint(pool)
|
---|
613 | #pprint(wleiden_zone)
|
---|
614 | #for config in files:
|
---|
615 | # items['config'] = config
|
---|
616 | # print "## Generating %(node)s %(config)s" % items
|
---|
617 | # f = open("%(wdir)s/%(config)s" % items, "w")
|
---|
618 | # f.write(generate_config(node, config, datadump))
|
---|
619 | # f.close()
|
---|
620 | else:
|
---|
621 | usage()
|
---|
622 | else:
|
---|
623 | cgitb.enable()
|
---|
624 | process_cgi_request()
|
---|
625 |
|
---|
626 |
|
---|
627 | if __name__ == "__main__":
|
---|
628 | main()
|
---|