source: genesis/tools/gformat.py@ 10213

Last change on this file since 10213 was 10209, checked in by richardvm, 13 years ago

adding proxies anywat

  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 32.6 KB
Line 
1#!/usr/bin/env python
2#
3# vim:ts=2:et:sw=2:ai
4# Wireless Leiden configuration generator, based on yaml files'
5#
6# XXX: This should be rewritten to make use of the ipaddr.py library.
7#
8# Sample apache configuration (mind the AcceptPathInfo!)
9# ScriptAlias /wleiden/config /usr/local/www/genesis/tools/gformat.py
10# <Directory /usr/local/www/genesis>
11# Allow from all
12# AcceptPathInfo On
13# </Directory>
14#
15# Rick van der Zwet <info@rickvanderzwet.nl>
16#
17
18# Hack to make the script directory is also threated as a module search path.
19import sys
20import os
21import re
22sys.path.append(os.path.dirname(__file__))
23
24import cgi
25import cgitb
26import copy
27import glob
28import socket
29import string
30import subprocess
31import time
32import rdnap
33from pprint import pprint
34try:
35 import yaml
36except ImportError, e:
37 print e
38 print "[ERROR] Please install the python-yaml or devel/py-yaml package"
39 exit(1)
40
41try:
42 from yaml import CLoader as Loader
43 from yaml import CDumper as Dumper
44except ImportError:
45 from yaml import Loader, Dumper
46
47from jinja2 import Template
48
49import logging
50logging.basicConfig(format='# %(levelname)s: %(message)s' )
51logger = logging.getLogger()
52logger.setLevel(logging.DEBUG)
53
54
55if os.environ.has_key('CONFIGROOT'):
56 NODE_DIR = os.environ['CONFIGROOT']
57else:
58 NODE_DIR = os.path.abspath(os.path.dirname(__file__)) + '/../nodes'
59__version__ = '$Id: gformat.py 10209 2012-03-17 19:24:37Z richardvm $'
60
61
62files = [
63 'authorized_keys',
64 'dnsmasq.conf',
65 'rc.conf.local',
66 'resolv.conf',
67 'motd',
68 'wleiden.yaml',
69 ]
70
71# Global variables uses
72OK = 10
73DOWN = 20
74UNKNOWN = 90
75
76
77def get_proxylist():
78 """Get all available proxies proxyX sorting based on X number"""
79 proxylist = sorted([os.path.basename(x) for x in glob.glob("%s/proxy*" % NODE_DIR)],
80 key=lambda name: int(''.join([c for c in name if c in string.digits])),
81 cmp=lambda x,y: x - y)
82 return proxylist
83
84def get_hybridlist():
85 """Get all available proxies hybridX sorting based on X number"""
86 hybridlist = sorted([os.path.basename(x) for x in glob.glob("%s/hybrid*" % NODE_DIR)],
87 key=lambda name: int(''.join([c for c in name if c in string.digits])),
88 cmp=lambda x,y: x - y)
89 return hybridlist
90
91
92def valid_addr(addr):
93 """ Show which address is valid in which are not """
94 return str(addr).startswith('172.')
95
96
97def get_nodelist():
98 """ Get all available nodes - sorted """
99 nodelist = sorted([os.path.basename(x) for x in glob.glob("%s/CNode*" % NODE_DIR)])
100 return nodelist
101
102def get_hostlist():
103 """ Combined hosts and proxy list"""
104 return get_nodelist() + get_proxylist() + get_hybridlist()
105
106def angle_between_points(lat1,lat2,long1,long2):
107 """
108 Return Angle in radians between two GPS coordinates
109 See: http://stackoverflow.com/questions/3809179/angle-between-2-gps-coordinates
110 """
111 dy = lat2 - lat1
112 dx = math.cos(math.pi/180*lat1)*(long2 - long1)
113 angle = math.atan2(dy,dx)
114 return angle
115
116def angle_to_cd(angle):
117 """ Return Dutch Cardinal Direction estimation in 'one digit' of radian angle """
118
119 # For easy conversion get positive degree
120 degrees = math.degrees(angle)
121 if degrees < 0:
122 360 - abs(degrees)
123
124 # Numbers can be confusing calculate from the 4 main directions
125 p = 22.5
126 if degrees < p:
127 return "n"
128 elif degrees < (90 - p):
129 return "no"
130 elif degrees < (90 + p):
131 return "o"
132 elif degrees < (180 - p):
133 return "zo"
134 elif degrees < (180 + p):
135 return "z"
136 elif degrees < (270 - p):
137 return "zw"
138 elif degrees < (270 + p):
139 return "w"
140 elif degrees < (360 - p):
141 return "nw"
142 else:
143 return "n"
144
145
146def generate_title(nodelist):
147 """ Main overview page """
148 items = {'root' : "." }
149 output = """
150<html>
151 <head>
152 <title>Wireless leiden Configurator - GFormat</title>
153 <style type="text/css">
154 th {background-color: #999999}
155 tr:nth-child(odd) {background-color: #cccccc}
156 tr:nth-child(even) {background-color: #ffffff}
157 th, td {padding: 0.1em 1em}
158 </style>
159 </head>
160 <body>
161 <center>
162 <form type="GET" action="%(root)s">
163 <input type="hidden" name="action" value="update">
164 <input type="submit" value="Update Configuration Database (SVN)">
165 </form>
166 <table>
167 <caption><h3>Wireless Leiden Configurator</h3></caption>
168 """ % items
169
170 for node in nodelist:
171 items['node'] = node
172 output += '<tr><td><a href="%(root)s/%(node)s">%(node)s</a></td>' % items
173 for config in files:
174 items['config'] = config
175 output += '<td><a href="%(root)s/%(node)s/%(config)s">%(config)s</a></td>' % items
176 output += "</tr>"
177 output += """
178 </table>
179 <hr />
180 <em>%s</em>
181 </center>
182 </body>
183</html>
184 """ % __version__
185
186 return output
187
188
189
190def generate_node(node):
191 """ Print overview of all files available for node """
192 return "\n".join(files)
193
194
195
196def generate_header(ctag="#"):
197 return """\
198%(ctag)s
199%(ctag)s DO NOT EDIT - Automatically generated by 'gformat'
200%(ctag)s Generated at %(date)s by %(host)s
201%(ctag)s
202""" % { 'ctag' : ctag, 'date' : time.ctime(), 'host' : socket.gethostname() }
203
204
205
206def parseaddr(s):
207 """ Process IPv4 CIDR notation addr to a (binary) number """
208 f = s.split('.')
209 return (long(f[0]) << 24L) + \
210 (long(f[1]) << 16L) + \
211 (long(f[2]) << 8L) + \
212 long(f[3])
213
214
215
216def showaddr(a):
217 """ Display IPv4 addr in (dotted) CIDR notation """
218 return "%d.%d.%d.%d" % ((a >> 24) & 0xff, (a >> 16) & 0xff, (a >> 8) & 0xff, a & 0xff)
219
220
221def is_member(ip, mask, canidate):
222 """ Return True if canidate is part of ip/mask block"""
223 ip_addr = gformat.parseaddr(ip)
224 ip_canidate = gformat.parseaddr(canidate)
225 mask = int(mask)
226 ip_addr = ip_addr & ~((1 << (32 - mask)) - 1)
227 ip_canidate = ip_canidate & ~((1 << (32 - mask)) - 1)
228 return ip_addr == ip_canidate
229
230
231
232
233def netmask2subnet(netmask):
234 """ Given a 'netmask' return corresponding CIDR """
235 return showaddr(0xffffffff & (0xffffffff << (32 - int(netmask))))
236
237
238
239def generate_dnsmasq_conf(datadump):
240 """ Generate configuration file '/usr/local/etc/dnsmasq.conf' """
241 output = generate_header()
242 output += """\
243# DHCP server options
244dhcp-authoritative
245dhcp-fqdn
246domain=dhcp.%(nodename_lower)s.%(domain)s
247domain-needed
248expand-hosts
249log-async=100
250
251# Low memory footprint
252cache-size=10000
253 \n""" % datadump
254
255 for iface_key in datadump['iface_keys']:
256 if not datadump[iface_key].has_key('comment'):
257 datadump[iface_key]['comment'] = None
258 output += "## %(interface)s - %(desc)s - %(comment)s\n" % datadump[iface_key]
259
260 try:
261 (dhcp_start, dhcp_stop) = datadump[iface_key]['dhcp'].split('-')
262 (ip, netmask) = datadump[iface_key]['ip'].split('/')
263 datadump[iface_key]['subnet'] = netmask2subnet(netmask)
264 except (AttributeError, ValueError):
265 output += "# not autoritive\n\n"
266 continue
267
268 dhcp_part = ".".join(ip.split('.')[0:3])
269 datadump[iface_key]['dhcp_start'] = dhcp_part + "." + dhcp_start
270 datadump[iface_key]['dhcp_stop'] = dhcp_part + "." + dhcp_stop
271 output += "dhcp-range=%(interface)s,%(dhcp_start)s,%(dhcp_stop)s,%(subnet)s,24h\n\n" % datadump[iface_key]
272
273 return output
274
275
276
277def generate_rc_conf_local(datadump):
278 """ Generate configuration file '/etc/rc.conf.local' """
279 datadump['autogen_ileiden_enable'] = 'yes' if datadump['ileiden'] else 'no'
280
281 ileiden_proxies = []
282 for proxy in get_proxylist():
283 proxydump = get_yaml(proxy)
284 if proxydump['ileiden']:
285 ileiden_proxies.append(proxydump)
286 datadump['autogen_ileiden_proxies'] = ','.join([x['masterip'] for x in ileiden_proxies])
287 datadump['autogen_ileiden_proxies_names'] = ','.join([x['autogen_item'] for x in ileiden_proxies])
288
289 output = generate_header("#");
290 output += Template("""\
291hostname='{{ autogen_fqdn }}.{{ domain }}'
292location='{{ location }}'
293nodetype="{{ nodetype }}"
294
295{% if tproxy -%}
296tproxy_enable='YES'
297tproxy_range='{{ tproxy }}'
298{% else -%}
299tproxy_enable='NO'
300{% endif -%}
301
302{% if nodetype == "Proxy" %}
303#
304# PROXY Configuration
305#
306{% if internalroute -%}
307static_routes="wleiden"
308route_wleiden="-net 172.16.0.0/12 {{ internalroute }}"
309{% endif -%}
310
311{% if gateway -%}
312defaultrouter="{{ gateway }}"
313{% else -%}
314#defaultrouter="NOTSET"
315{% endif -%}
316
317internalif="{{ internalif }}"
318
319# PROXY iLeiden Configuration
320lvrouted_enable="{{ autogen_ileiden_enable }}"
321lvrouted_flags="-u -s s00p3rs3kr3t -m 28"
322ileiden_enable="{{ autogen_ileiden_enable }}"
323gateway_enable="{{ autogen_ileiden_enable }}"
324firewall_enable="{{ autogen_ileiden_enable }}"
325firewall_script="/etc/ipfw.sh"
326firewall_nat_enable="{{ autogen_ileiden_enable }}"
327{% endif -%}
328
329{% if nodetype == "CNode" %}
330#
331# NODE iLeiden Configuration
332#
333# iLeiden Proxies {{ autogen_ileiden_proxies_names }}
334lvrouted_flags="-u -s s00p3rs3kr3t -m 28 -z {{ autogen_ileiden_proxies }}"
335
336captive_portal_whitelist=""
337captive_portal_interfaces="{{ autogen_dhcp_interfaces }}"
338{% endif %}
339
340{% if nodetype == "Hybrid" %}
341#
342# Hybrid Node
343#
344{% if internalroute -%}
345static_routes="wleiden"
346route_wleiden="-net 172.16.0.0/12 {{ internalroute }}"
347{% endif -%}
348
349{% if gateway -%}
350defaultrouter="{{ gateway }}"
351{% else -%}
352#defaultrouter="NOTSET"
353{% endif -%}
354
355internalif="{{ internalif }}"
356{% if vpnif -%}
357 vpnif="{{ vpnif }}"\n
358{% endif -%}
359
360lvrouted_enable="{{ autogen_ileiden_enable }}"
361lvrouted_flags="-u -s s00p3rs3kr3t -m 28"
362
363gateway_enable="{{ autogen_ileiden_enable }}"
364
365ileiden_enable="{{ autogen_ileiden_enable }}"
366firewall_enable="{{ autogen_ileiden_enable }}"
367firewall_script="/etc/ipfw.sh"
368
369captive_portal_whitelist=""
370captive_portal_interfaces="{{ autogen_dhcp_interfaces }}"
371{% endif %}
372
373""").render(datadump)
374
375 # lo0 configuration:
376 # - 172.32.255.1/32 is the proxy.wleiden.net deflector
377 # - masterip is special as it needs to be assigned to at
378 # least one interface, so if not used assign to lo0
379 addrs_list = { 'lo0' : [("127.0.0.1/8", "LocalHost"), ("172.31.255.1/32","Proxy IP")] }
380 iface_map = {'lo0' : 'lo0'}
381
382 masterip_used = False
383 for iface_key in datadump['iface_keys']:
384 if datadump[iface_key]['ip'].startswith(datadump['masterip']):
385 masterip_used = True
386 break
387 if not masterip_used:
388 addrs_list['lo0'].append((datadump['masterip'] + "/32", 'Master IP Not used in interface'))
389
390 for iface_key in datadump['iface_keys']:
391 ifacedump = datadump[iface_key]
392 ifname = ifacedump['autogen_ifname']
393
394 # Add interface IP to list
395 item = (ifacedump['ip'], ifacedump['desc'])
396 if addrs_list.has_key(ifname):
397 addrs_list[ifname].append(item)
398 else:
399 addrs_list[ifname] = [item]
400
401 # Alias only needs IP assignment for now, this might change if we
402 # are going to use virtual accesspoints
403 if "alias" in iface_key:
404 continue
405
406 # XXX: Might want to deduct type directly from interface name
407 if ifacedump['type'] in ['11a', '11b', '11g', 'wireless']:
408 # Default to station (client) mode
409 ifacedump['wlanmode'] = "sta"
410 if ifacedump['mode'] in ['master', 'master-wds', 'ap', 'ap-wds']:
411 ifacedump['wlanmode'] = "ap"
412 # Default to 802.11b mode
413 ifacedump['mode'] = '11b'
414 if ifacedump['type'] in ['11a', '11b' '11g']:
415 ifacedump['mode'] = ifacedump['type']
416
417 if not ifacedump.has_key('channel'):
418 if ifacedump['type'] == '11a':
419 ifacedump['channel'] = 36
420 else:
421 ifacedump['channel'] = 1
422
423 # Allow special hacks at the back like wds and stuff
424 if not ifacedump.has_key('extra'):
425 ifacedump['extra'] = 'regdomain ETSI country NL'
426
427 output += "wlans_%(interface)s='%(autogen_ifname)s'\n" % ifacedump
428 output += ("create_args_%(autogen_ifname)s='wlanmode %(wlanmode)s mode " +\
429 "%(mode)s ssid %(ssid)s %(extra)s channel %(channel)s'\n") % ifacedump
430
431 elif ifacedump['type'] in ['ethernet', 'eth']:
432 # No special config needed besides IP
433 pass
434 else:
435 assert False, "Unknown type " + ifacedump['type']
436
437 # Print IP address which needs to be assigned over here
438 output += "\n"
439 for iface,addrs in sorted(addrs_list.iteritems()):
440 for addr, comment in sorted(addrs,key=lambda x: parseaddr(x[0].split('/')[0])):
441 output += "# %s || %s || %s\n" % (iface, addr, comment)
442 output += "ipv4_addrs_%s='%s'\n\n" % (iface, " ".join([x[0] for x in addrs]))
443
444 return output
445
446
447
448def get_yaml(item):
449 """ Get configuration yaml for 'item'"""
450 gfile = os.path.join(NODE_DIR,item,'wleiden.yaml')
451
452 f = open(gfile, 'r')
453 datadump = yaml.load(f,Loader=Loader)
454 datadump['autogen_iface_keys'] = get_interface_keys(datadump)
455
456 wlan_count=0
457 for key in datadump['autogen_iface_keys']:
458 if datadump[key]['type'] in ['11a', '11b', '11g', 'wireless']:
459 datadump[key]['autogen_ifname'] = 'wlan%i' % wlan_count
460 wlan_count += 1
461 else:
462 datadump[key]['autogen_ifname'] = datadump[key]['interface'].split(':')[0]
463
464
465 dhcp_interfaces = [datadump[key]['autogen_ifname'] for key in datadump['autogen_iface_keys'] if datadump[key]['dhcp'] != 'no']
466 datadump['autogen_dhcp_interfaces'] = ' '.join(dhcp_interfaces)
467 datadump['autogen_item'] = item
468 datadump['autogen_fqdn'] = get_fqdn(datadump)
469 f.close()
470
471 return datadump
472
473def store_yaml(datadump, header=False):
474 """ Store configuration yaml for 'item'"""
475 item = datadump['autogen_item']
476 gfile = os.path.join(NODE_DIR,item,'wleiden.yaml')
477
478 f = open(gfile, 'w')
479 f.write(generate_wleiden_yaml(datadump, header))
480 f.close()
481
482
483
484def get_all_configs():
485 """ Get dict with key 'host' with all configs present """
486 configs = dict()
487 for host in get_hostlist():
488 datadump = get_yaml(host)
489 configs[host] = datadump
490 return configs
491
492
493def get_interface_keys(config):
494 """ Quick hack to get all interface keys, later stage convert this to a iterator """
495 return sorted([elem for elem in config.keys() if (elem.startswith('iface_') and not "lo0" in elem)])
496
497
498def get_used_ips(configs):
499 """ Return array of all IPs used in config files"""
500 ip_list = []
501 for config in configs:
502 ip_list.append(config['masterip'])
503 for iface_key in get_interface_keys(config):
504 l = config[iface_key]['ip']
505 addr, mask = l.split('/')
506 # Special case do not process
507 if valid_addr(addr):
508 ip_list.append(addr)
509 else:
510 logger.error("## IP '%s' in '%s' not valid" % (addr, config['nodename']))
511 return sorted(ip_list)
512
513
514
515def generate_resolv_conf(datadump):
516 """ Generate configuration file '/etc/resolv.conf' """
517 output = generate_header("#");
518 output += """\
519search wleiden.net
520"""
521 if datadump['nodetype'] == 'Proxy':
522 output += """\
523# Try local (cache) first
524nameserver 127.0.0.1
525nameserver 8.8.8.8 # Google Public NameServer
526nameserver 8.8.4.4 # Google Public NameServer
527"""
528 elif datadump['nodetype'] == 'Hybrid':
529 for proxy in get_proxylist():
530 proxy_ip = get_yaml(proxy)['masterip']
531 output += "nameserver %-15s # %s\n" % (proxy_ip, proxy)
532 output += """\
533nameserver 8.8.8.8 # Google Public NameServer
534nameserver 8.8.4.4 # Google Public NameServer
535"""
536 else:
537 output += """\
538# Try local (cache) first
539nameserver 127.0.0.1
540
541# Proxies are recursive nameservers
542# needs to be in resolv.conf for dnsmasq as well
543""" % datadump
544 for proxy in get_proxylist():
545 proxy_ip = get_yaml(proxy)['masterip']
546 output += "nameserver %-15s # %s\n" % (proxy_ip, proxy)
547
548 return output
549
550def generate_motd(datadump):
551 """ Generate configuration file '/etc/motd' """
552 output = """\
553FreeBSD 9.0-RELEASE (kernel.wleiden) #0 r230587: Sun Jan 29 17:09:57 CET 2012
554
555 WWW: %(autogen_fqdn)s.wleiden.net - http://www.wirelessleiden.nl
556 Loc: %(location)s
557
558Interlinks:
559""" % datadump
560
561 # XXX: This is a hacky way to get the required data
562 for line in generate_rc_conf_local(datadump).split('\n'):
563 if '||' in line and not line[1:].split()[0] in ['lo0', 'ath0'] :
564 output += " - %s \n" % line[1:]
565 output += """\
566Attached bridges:
567"""
568 for iface_key in datadump['autogen_iface_keys']:
569 ifacedump = datadump[iface_key]
570 if ifacedump.has_key('ns_ip'):
571 output += " - %(interface)s || %(mode)s || %(ns_ip)s\n" % ifacedump
572
573 return output
574
575
576def format_yaml_value(value):
577 """ Get yaml value in right syntax for outputting """
578 if isinstance(value,str):
579 output = '"%s"' % value
580 else:
581 output = value
582 return output
583
584
585
586def format_wleiden_yaml(datadump):
587 """ Special formatting to ensure it is editable"""
588 output = "# Genesis config yaml style\n"
589 output += "# vim:ts=2:et:sw=2:ai\n"
590 output += "#\n"
591 iface_keys = [elem for elem in datadump.keys() if elem.startswith('iface_')]
592 for key in sorted(set(datadump.keys()) - set(iface_keys)):
593 output += "%-10s: %s\n" % (key, format_yaml_value(datadump[key]))
594
595 output += "\n\n"
596
597 key_order = [ 'comment', 'interface', 'ip', 'desc', 'sdesc', 'mode', 'type',
598 'extra_type', 'channel', 'ssid', 'dhcp' ]
599
600 for iface_key in sorted(iface_keys):
601 output += "%s:\n" % iface_key
602 for key in key_order + list(sorted(set(datadump[iface_key].keys()) - set(key_order))):
603 if datadump[iface_key].has_key(key):
604 output += " %-11s: %s\n" % (key, format_yaml_value(datadump[iface_key][key]))
605 output += "\n\n"
606
607 return output
608
609
610
611def generate_wleiden_yaml(datadump, header=True):
612 """ Generate (petty) version of wleiden.yaml"""
613 for key in datadump.keys():
614 if key.startswith('autogen_'):
615 del datadump[key]
616 # Interface autogen cleanups
617 elif type(datadump[key]) == dict:
618 for key2 in datadump[key].keys():
619 if key2.startswith('autogen_'):
620 del datadump[key][key2]
621
622 output = generate_header("#") if header else ''
623 output += format_wleiden_yaml(datadump)
624 return output
625
626
627def generate_yaml(datadump):
628 return generate_config(datadump['nodename'], "wleiden.yaml", datadump)
629
630
631
632def generate_config(node, config, datadump=None):
633 """ Print configuration file 'config' of 'node' """
634 output = ""
635 try:
636 # Load config file
637 if datadump == None:
638 datadump = get_yaml(node)
639
640 # Preformat certain needed variables for formatting and push those into special object
641 datadump_extra = copy.deepcopy(datadump)
642 if not datadump_extra.has_key('domain'):
643 datadump_extra['domain'] = 'wleiden.net'
644 datadump_extra['nodename_lower'] = datadump_extra['nodename'].lower()
645 datadump_extra['iface_keys'] = sorted([elem for elem in datadump.keys() if elem.startswith('iface_')])
646
647 if config == 'wleiden.yaml':
648 output += generate_wleiden_yaml(datadump)
649 elif config == 'authorized_keys':
650 f = open(os.path.join(NODE_DIR,"global_keys"), 'r')
651 output += f.read()
652 f.close()
653 elif config == 'dnsmasq.conf':
654 output += generate_dnsmasq_conf(datadump_extra)
655 elif config == 'rc.conf.local':
656 output += generate_rc_conf_local(datadump_extra)
657 elif config == 'resolv.conf':
658 output += generate_resolv_conf(datadump_extra)
659 elif config == 'motd':
660 output += generate_motd(datadump_extra)
661 else:
662 assert False, "Config not found!"
663 except IOError, e:
664 output += "[ERROR] Config file not found"
665 return output
666
667
668
669def process_cgi_request():
670 """ When calling from CGI """
671 # Update repository if requested
672 form = cgi.FieldStorage()
673 if form.getvalue("action") == "update":
674 print "Refresh: 5; url=."
675 print "Content-type:text/plain\r\n\r\n",
676 print "[INFO] Updating subverion, please wait..."
677 print subprocess.Popen(['svn', 'cleanup', "%s/.." % NODE_DIR], stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0],
678 print subprocess.Popen(['svn', 'up', "%s/.." % NODE_DIR], stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0],
679 print "[INFO] All done, redirecting in 5 seconds"
680 sys.exit(0)
681
682
683 uri = os.environ['PATH_INFO'].strip('/').split('/')
684 output = ""
685 if not uri[0]:
686 if is_text_request():
687 output += "Content-type:text/plain\r\n\r\n"
688 output += '\n'.join(get_hostlist())
689 else:
690 output += "Content-type:text/html\r\n\r\n"
691 output += generate_title(get_hostlist())
692 elif len(uri) == 1:
693 output += "Content-type:text/plain\r\n\r\n"
694 output += generate_node(uri[0])
695 elif len(uri) == 2:
696 output += "Content-type:text/plain\r\n\r\n"
697 output += generate_config(uri[0], uri[1])
698 else:
699 assert False, "Invalid option"
700 print output
701
702def get_fqdn(datadump):
703 # Proxy naming convention is special
704 if datadump['nodetype'] == 'Proxy':
705 fqdn = datadump['nodename']
706 else:
707 # By default the full name is listed and also a shortname CNAME for easy use.
708 fqdn = datadump['nodetype'] + datadump['nodename']
709 return(fqdn)
710
711
712
713def make_dns(output_dir = 'dns'):
714 items = dict()
715
716 # hostname is key, IP is value
717 wleiden_zone = dict()
718 wleiden_cname = dict()
719
720 pool = dict()
721 for node in get_hostlist():
722 logger.info("Processing host %s", node)
723 datadump = get_yaml(node)
724
725 # Proxy naming convention is special
726 fqdn = get_fqdn(datadump)
727 if datadump['nodetype'] == 'CNode':
728 wleiden_cname[datadump['nodename']] = fqdn
729
730 wleiden_zone[fqdn] = datadump['masterip']
731
732 # Hacking to get proper DHCP IPs and hostnames
733 for iface_key in get_interface_keys(datadump):
734 iface_name = datadump[iface_key]['interface'].replace(':',"-alias-")
735 (ip, netmask) = datadump[iface_key]['ip'].split('/')
736 try:
737 (dhcp_start, dhcp_stop) = datadump[iface_key]['dhcp'].split('-')
738 datadump[iface_key]['subnet'] = netmask2subnet(netmask)
739 dhcp_part = ".".join(ip.split('.')[0:3])
740 if ip != datadump['masterip']:
741 wleiden_zone["dhcp-gateway-%s.%s" % (iface_name, fqdn)] = ip
742 for i in range(int(dhcp_start), int(dhcp_stop) + 1):
743 wleiden_zone["dhcp-%s-%s.%s" % (i, iface_name, fqdn)] = "%s.%s" % (dhcp_part, i)
744 except (AttributeError, ValueError):
745 # First push it into a pool, to indentify the counter-part later on
746 addr = parseaddr(ip)
747 netmask = int(netmask)
748 addr = addr & ~((1 << (32 - netmask)) - 1)
749 if pool.has_key(addr):
750 pool[addr] += [(iface_name, fqdn, ip)]
751 else:
752 pool[addr] = [(iface_name, fqdn, ip)]
753 continue
754
755
756 def pool_to_name(node, pool_members):
757 """Convert the joined name to a usable pool name"""
758
759 # Get rid of the own entry
760 pool_members = list(set(pool_members) - set([fqdn]))
761
762 target = oldname = ''
763 for node in sorted(pool_members):
764 (name, number) = re.match('^([A-Za-z]+)([0-9]*)$',node).group(1,2)
765 target += "-" + number if name == oldname else "-" + node if target else node
766 oldname = name
767
768 return target
769
770
771 # WL uses an /29 to configure an interface. IP's are ordered like this:
772 # MasterA (.1) -- DeviceA (.2) <<>> DeviceB (.3) --- SlaveB (.4)
773
774 sn = lambda x: re.sub(r'(?i)^cnode','',x)
775
776 # Automatic naming convention of interlinks namely 2 + remote.lower()
777 for (key,value) in pool.iteritems():
778 # Make sure they are sorted from low-ip to high-ip
779 value = sorted(value, key=lambda x: parseaddr(x[2]))
780
781 if len(value) == 1:
782 (iface_name, fqdn, ip) = value[0]
783 wleiden_zone["2unused-%s.%s" % (iface_name, fqdn)] = ip
784
785 # Device DNS names
786 if 'cnode' in fqdn.lower():
787 wleiden_zone["d-at-%s.%s" % (iface_name, fqdn)] = showaddr(parseaddr(ip) + 1)
788 wleiden_cname["d-at-%s.%s" % (iface_name,sn(fqdn))] = "d-at-%s.%s" % (iface_name, fqdn)
789
790 elif len(value) == 2:
791 (a_iface_name, a_fqdn, a_ip) = value[0]
792 (b_iface_name, b_fqdn, b_ip) = value[1]
793 wleiden_zone["2%s.%s" % (b_fqdn,a_fqdn)] = a_ip
794 wleiden_zone["2%s.%s" % (a_fqdn,b_fqdn)] = b_ip
795
796 # Device DNS names
797 if 'cnode' in a_fqdn.lower() and 'cnode' in b_fqdn.lower():
798 wleiden_zone["d-at-%s.%s" % (a_iface_name, a_fqdn)] = showaddr(parseaddr(a_ip) + 1)
799 wleiden_zone["d-at-%s.%s" % (b_iface_name, b_fqdn)] = showaddr(parseaddr(b_ip) - 1)
800 wleiden_cname["d-at-%s.%s" % (a_iface_name,sn(a_fqdn))] = "d-at-%s.%s" % (a_iface_name, a_fqdn)
801 wleiden_cname["d-at-%s.%s" % (b_iface_name,sn(b_fqdn))] = "d-at-%s.%s" % (b_iface_name, b_fqdn)
802 wleiden_cname["d2%s.%s" % (sn(b_fqdn),sn(a_fqdn))] = "d-at-%s.%s" % (a_iface_name, a_fqdn)
803 wleiden_cname["d2%s.%s" % (sn(a_fqdn),sn(b_fqdn))] = "d-at-%s.%s" % (b_iface_name, b_fqdn)
804
805 else:
806 pool_members = [k[1] for k in value]
807 for item in value:
808 (iface_name, fqdn, ip) = item
809 pool_name = "2pool-" + showaddr(key).replace('.','-') + "-" + pool_to_name(fqdn,pool_members)
810 wleiden_zone["%s.%s" % (pool_name, fqdn)] = ip
811
812 # Include static DNS entries
813 # XXX: Should they override the autogenerated results?
814 # XXX: Convert input to yaml more useable.
815 # Format:
816 ##; this is a comment
817 ## roomburgh=CNodeRoomburgh1
818 ## apkerk1.CNodeVosko=172.17.176.8 ;this as well
819 dns = yaml.load(open(os.path.join(NODE_DIR,'../dns/staticDNS.yaml'),'r'))
820
821 # Hack to allow special entries, for development
822 wleiden_raw = dns['raw']
823 del dns['raw']
824
825 for comment, block in dns.iteritems():
826 for k,v in block.iteritems():
827 if valid_addr(v):
828 wleiden_zone[k] = v
829 else:
830 wleiden_cname[k] = v
831
832 details = dict()
833 # 24 updates a day allowed
834 details['serial'] = time.strftime('%Y%m%d%H')
835
836 dns_header = '''
837$TTL 3h
838%(zone)s. SOA sunny.wleiden.net. beheer.lijst.wirelessleiden.nl. ( %(serial)s 1d 12h 1w 3h )
839 ; Serial, Refresh, Retry, Expire, Neg. cache TTL
840
841 NS sunny.wleiden.net.
842 \n'''
843
844
845 if not os.path.isdir('dns'):
846 os.makedirs('dns')
847 details['zone'] = 'wleiden.net'
848 f = open(os.path.join(output_dir,"db." + details['zone']), "w")
849 f.write(dns_header % details)
850
851 for host,ip in wleiden_zone.iteritems():
852 if valid_addr(ip):
853 f.write("%s.wleiden.net. IN A %s \n" % (host.lower(), ip))
854 for source,dest in wleiden_cname.iteritems():
855 f.write("%s.wleiden.net. IN CNAME %s.wleiden.net.\n" % (source.lower(), dest.lower()))
856 for source, dest in wleiden_raw.iteritems():
857 f.write("%s.wleiden.net. %s\n" % (source, dest))
858 f.close()
859
860 # Create whole bunch of specific sub arpa zones. To keep it compliant
861 for s in range(16,32):
862 details['zone'] = '%i.172.in-addr.arpa' % s
863 f = open(os.path.join(output_dir,"db." + details['zone']), "w")
864 f.write(dns_header % details)
865
866 #XXX: Not effient, fix to proper data structure and do checks at other
867 # stages
868 for host,ip in wleiden_zone.iteritems():
869 if valid_addr(ip):
870 if int(ip.split('.')[1]) == s:
871 rev_ip = '.'.join(reversed(ip.split('.')))
872 f.write("%s.in-addr.arpa. IN PTR %s.wleiden.net.\n" % (rev_ip.lower(), host.lower()))
873 f.close()
874
875
876def usage():
877 print """Usage: %s <standalone [port] |test [test arguments]|static|dns>
878Examples:
879\tdns [outputdir] = Generate BIND compliant zone files in dns.
880\tstandalone = Run configurator webserver [default port=8000]
881\twind-export = Generate SQL import scripts for WIND database
882\tfull-export = Generate yaml export script for heatmap.
883\tstatic = Generate all config files and store on disk
884\t with format ./static/%%NODE%%/%%FILE%%
885\ttest CNodeRick dnsmasq.conf = Receive output of CGI script
886\t for arguments CNodeRick/dnsmasq.conf
887\tlist <nodes|proxies> = List systems which marked up.
888"""
889 exit(0)
890
891
892def is_text_request():
893 """ Find out whether we are calling from the CLI or any text based CLI utility """
894 try:
895 return os.environ['HTTP_USER_AGENT'].split()[0] in ['curl', 'fetch', 'wget']
896 except KeyError:
897 return True
898
899def main():
900 """Hard working sub"""
901 # Allow easy hacking using the CLI
902 if not os.environ.has_key('PATH_INFO'):
903 if len(sys.argv) < 2:
904 usage()
905
906 if sys.argv[1] == "standalone":
907 import SocketServer
908 import CGIHTTPServer
909 # Hop to the right working directory.
910 os.chdir(os.path.dirname(__file__))
911 try:
912 PORT = int(sys.argv[2])
913 except (IndexError,ValueError):
914 PORT = 8000
915
916 class MyCGIHTTPRequestHandler(CGIHTTPServer.CGIHTTPRequestHandler):
917 """ Serve this CGI from the root of the webserver """
918 def is_cgi(self):
919 if "favicon" in self.path:
920 return False
921
922 self.cgi_info = (__file__, self.path)
923 self.path = ''
924 return True
925 handler = MyCGIHTTPRequestHandler
926 SocketServer.TCPServer.allow_reuse_address = True
927 httpd = SocketServer.TCPServer(("", PORT), handler)
928 httpd.server_name = 'localhost'
929 httpd.server_port = PORT
930
931 logger.info("serving at port %s", PORT)
932 try:
933 httpd.serve_forever()
934 except KeyboardInterrupt:
935 httpd.shutdown()
936 logger.info("All done goodbye")
937 elif sys.argv[1] == "test":
938 os.environ['PATH_INFO'] = "/".join(sys.argv[2:])
939 os.environ['SCRIPT_NAME'] = __file__
940 process_cgi_request()
941 elif sys.argv[1] == "unit-test":
942 os.environ['SCRIPT_NAME'] = __file__
943 for host in get_hostlist():
944 for outfile in files:
945 os.environ['PATH_INFO'] = "/".join([host,outfile])
946 try:
947 process_cgi_request()
948 except Exception:
949 print "# ERROR: %s" % os.environ['PATH_INFO']
950 raise
951
952
953 elif sys.argv[1] == "static":
954 items = dict()
955 for node in get_hostlist():
956 items['node'] = node
957 items['wdir'] = "./static/%(node)s" % items
958 if not os.path.isdir(items['wdir']):
959 os.makedirs(items['wdir'])
960 datadump = get_yaml(node)
961 for config in files:
962 items['config'] = config
963 logger.info("## Generating %(node)s %(config)s" % items)
964 f = open("%(wdir)s/%(config)s" % items, "w")
965 f.write(generate_config(node, config, datadump))
966 f.close()
967 elif sys.argv[1] == "wind-export":
968 items = dict()
969 for node in get_hostlist():
970 datadump = get_yaml(node)
971 sql = """INSERT IGNORE INTO nodes (name, name_ns, longitude, latitude)
972 VALUES ('%(nodename)s', '%(nodename)s', %(latitude)s, %(longitude)s);""" % datadump;
973 sql = """INSERT IGNORE INTO users_nodes (user_id, node_id, owner)
974 VALUES (
975 (SELECT id FROM users WHERE username = 'rvdzwet'),
976 (SELECT id FROM nodes WHERE name = '%(nodename)s'),
977 'Y');""" % datadump
978 #for config in files:
979 # items['config'] = config
980 # print "## Generating %(node)s %(config)s" % items
981 # f = open("%(wdir)s/%(config)s" % items, "w")
982 # f.write(generate_config(node, config, datadump))
983 # f.close()
984 for node in get_hostlist():
985 datadump = get_yaml(node)
986 for iface_key in sorted([elem for elem in datadump.keys() if elem.startswith('iface_')]):
987 ifacedump = datadump[iface_key]
988 if ifacedump.has_key('mode') and ifacedump['mode'] == 'ap-wds':
989 ifacedump['nodename'] = datadump['nodename']
990 if not ifacedump.has_key('channel') or not ifacedump['channel']:
991 ifacedump['channel'] = 0
992 sql = """INSERT INTO links (node_id, type, ssid, protocol, channel, status)
993 VALUES ((SELECT id FROM nodes WHERE name = '%(nodename)s'), 'ap',
994 '%(ssid)s', 'IEEE 802.11b', %(channel)s, 'active');""" % ifacedump
995 elif sys.argv[1] == "full-export":
996 hosts = {}
997 for node in get_hostlist():
998 datadump = get_yaml(node)
999 hosts[datadump['nodename']] = datadump
1000 print yaml.dump(hosts)
1001
1002 elif sys.argv[1] == "dns":
1003 make_dns(sys.argv[2] if len(sys.argv) > 2 else 'dns')
1004 elif sys.argv[1] == "cleanup":
1005 # First generate all datadumps
1006 datadumps = dict()
1007 for host in get_hostlist():
1008 logger.info("# Processing: %s", host)
1009 datadump = get_yaml(host)
1010 datadumps[get_fqdn(datadump)] = datadump
1011
1012 for host,datadump in datadumps.iteritems():
1013 datadump['latitude'], datadump['longitude'] = rdnap.rd2etrs(datadump['rdnap_x'], datadump['rdnap_y'])
1014 for iface_key in datadump['autogen_iface_keys']:
1015 # Wireless Leiden SSID have an consistent lowercase/uppercase
1016 if datadump[iface_key].has_key('ssid'):
1017 ssid = datadump[iface_key]['ssid']
1018 prefix = 'ap-WirelessLeiden-'
1019 if ssid.lower().startswith(prefix.lower()):
1020 datadump[iface_key]['ssid'] = prefix + ssid[len(prefix)].upper() + ssid[len(prefix) + 1:]
1021 if datadump[iface_key].has_key('ns_ip') and not datadump[iface_key].has_key('mode'):
1022 datadump[iface_key]['mode'] = 'autogen-FIXME'
1023 if not datadump[iface_key].has_key('desc'):
1024 datadump[iface_key]['desc'] = 'autogen-FIXME'
1025 store_yaml(datadump)
1026 elif sys.argv[1] == "list":
1027 if sys.argv[2] == "nodes":
1028 systems = get_nodelist()
1029 elif sys.argv[2] == "proxies":
1030 systems = get_proxylist()
1031 else:
1032 usage()
1033 for system in systems:
1034 datadump = get_yaml(system)
1035 if datadump['status'] == "up":
1036 print system
1037 else:
1038 usage()
1039 else:
1040 # Do not enable debugging for config requests as it highly clutters the output
1041 if not is_text_request():
1042 cgitb.enable()
1043 process_cgi_request()
1044
1045
1046if __name__ == "__main__":
1047 main()
Note: See TracBrowser for help on using the repository browser.