source: genesis/tools/gformat.py@ 10692

Last change on this file since 10692 was 10692, checked in by rick, 13 years ago

_only_ directories with a wleiden.yaml are valid node items.

  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 44.4 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
33import make_network_kml
34from pprint import pprint
35from collections import defaultdict
36try:
37 import yaml
38except ImportError, e:
39 print e
40 print "[ERROR] Please install the python-yaml or devel/py-yaml package"
41 exit(1)
42
43try:
44 from yaml import CLoader as Loader
45 from yaml import CDumper as Dumper
46except ImportError:
47 from yaml import Loader, Dumper
48
49from jinja2 import Environment, Template
50def yesorno(value):
51 return "YES" if bool(value) else "NO"
52env = Environment()
53env.filters['yesorno'] = yesorno
54def render_template(datadump, template):
55 result = env.from_string(template).render(datadump)
56 # Make it look pretty to the naked eye, as jinja templates are not so
57 # friendly when it comes to whitespace formatting
58 ## Remove extra whitespace at end of line lstrip() style.
59 result = re.sub(r'\n[\ ]+','\n', result)
60 ## Include only a single newline between an definition and a comment
61 result = re.sub(r'(["\'])\n+([a-z]|\n#\n)',r'\1\n\2', result)
62 ## Remove extra newlines after single comment
63 result = re.sub(r'(#\n)\n+([a-z])',r'\1\2', result)
64 return result
65
66import logging
67logging.basicConfig(format='# %(levelname)s: %(message)s' )
68logger = logging.getLogger()
69logger.setLevel(logging.DEBUG)
70
71
72if os.environ.has_key('CONFIGROOT'):
73 NODE_DIR = os.environ['CONFIGROOT']
74else:
75 NODE_DIR = os.path.abspath(os.path.dirname(__file__)) + '/../nodes'
76__version__ = '$Id: gformat.py 10692 2012-05-06 20:44:45Z rick $'
77
78
79files = [
80 'authorized_keys',
81 'dnsmasq.conf',
82 'dhcpd.conf',
83 'rc.conf.local',
84 'resolv.conf',
85 'motd',
86 'ntp.conf',
87 'wleiden.yaml',
88 ]
89
90# Global variables uses
91OK = 10
92DOWN = 20
93UNKNOWN = 90
94
95def get_yaml(item):
96 """ Get configuration yaml for 'item'"""
97 gfile = os.path.join(NODE_DIR,item,'wleiden.yaml')
98
99 # Use some boring defaults
100 datadump = { 'service_proxy_normal' : False, 'service_proxy_ileiden' : False, 'service_accesspoint' : True }
101 f = open(gfile, 'r')
102 datadump.update(yaml.load(f,Loader=Loader))
103 f.close()
104
105 # Preformat certain needed variables for formatting and push those into special object
106 datadump['autogen_iface_keys'] = get_interface_keys(datadump)
107
108 wlan_count=0
109 for key in datadump['autogen_iface_keys']:
110 if datadump[key]['type'] in ['11a', '11b', '11g', 'wireless']:
111 datadump[key]['autogen_ifname'] = 'wlan%i' % wlan_count
112 wlan_count += 1
113 else:
114 datadump[key]['autogen_ifname'] = datadump[key]['interface'].split(':')[0]
115
116 dhcp_interfaces = [datadump[key]['autogen_ifname'] for key in datadump['autogen_iface_keys'] if datadump[key]['dhcp']]
117 datadump['autogen_dhcp_interfaces'] = ','.join(dhcp_interfaces)
118 datadump['autogen_item'] = item
119
120 datadump['autogen_realname'] = get_realname(datadump)
121 datadump['autogen_domain'] = datadump['domain'] if datadump.has_key('domain') else 'wleiden.net.'
122 datadump['autogen_fqdn'] = datadump['autogen_realname'] + '.' + datadump['autogen_domain']
123 return datadump
124
125
126def store_yaml(datadump, header=False):
127 """ Store configuration yaml for 'item'"""
128 item = datadump['autogen_item']
129 gfile = os.path.join(NODE_DIR,item,'wleiden.yaml')
130
131 f = open(gfile, 'w')
132 f.write(generate_wleiden_yaml(datadump, header))
133 f.close()
134
135
136
137def make_relations():
138 """ Process _ALL_ yaml files to get connection relations """
139 errors = ""
140 poel = defaultdict(list)
141 for host in get_hostlist():
142 try:
143 datadump = get_yaml(host)
144 for iface_key in datadump['autogen_iface_keys']:
145 l = datadump[iface_key]['ip']
146 addr, mask = l.split('/')
147
148 # Not parsing of these folks please
149 if not valid_addr(addr):
150 continue
151
152 addr = parseaddr(addr)
153 mask = int(mask)
154 network = addr & ~((1 << (32 - mask)) - 1)
155 poel[network] += [(host,datadump[iface_key])]
156 except (KeyError, ValueError), e:
157 errors += "[FOUT] in '%s' interface '%s'" % (host,iface_key)
158 errors += e
159 continue
160 return (poel, errors)
161
162
163
164def valid_addr(addr):
165 """ Show which address is valid in which are not """
166 return str(addr).startswith('172.')
167
168def get_system_list(prefix):
169 return sorted([os.path.basename(os.path.dirname(x)) for x in glob.glob("%s/%s*/wleiden.yaml" % (NODE_DIR, prefix))])
170
171get_hybridlist = lambda: get_system_list("Hybrid")
172get_nodelist = lambda: get_system_list("CNode")
173get_proxylist = lambda: get_system_list("Proxy")
174
175def get_hostlist():
176 """ Combined hosts and proxy list"""
177 return get_nodelist() + get_proxylist() + get_hybridlist()
178
179def angle_between_points(lat1,lat2,long1,long2):
180 """
181 Return Angle in radians between two GPS coordinates
182 See: http://stackoverflow.com/questions/3809179/angle-between-2-gps-coordinates
183 """
184 dy = lat2 - lat1
185 dx = math.cos(math.pi/180*lat1)*(long2 - long1)
186 angle = math.atan2(dy,dx)
187 return angle
188
189def angle_to_cd(angle):
190 """ Return Dutch Cardinal Direction estimation in 'one digit' of radian angle """
191
192 # For easy conversion get positive degree
193 degrees = math.degrees(angle)
194 if degrees < 0:
195 360 - abs(degrees)
196
197 # Numbers can be confusing calculate from the 4 main directions
198 p = 22.5
199 if degrees < p:
200 return "n"
201 elif degrees < (90 - p):
202 return "no"
203 elif degrees < (90 + p):
204 return "o"
205 elif degrees < (180 - p):
206 return "zo"
207 elif degrees < (180 + p):
208 return "z"
209 elif degrees < (270 - p):
210 return "zw"
211 elif degrees < (270 + p):
212 return "w"
213 elif degrees < (360 - p):
214 return "nw"
215 else:
216 return "n"
217
218
219def generate_title(nodelist):
220 """ Main overview page """
221 items = {'root' : "." }
222 def fl(spaces, line):
223 return (' ' * spaces) + line + '\n'
224
225 output = """
226<html>
227 <head>
228 <title>Wireless leiden Configurator - GFormat</title>
229 <style type="text/css">
230 th {background-color: #999999}
231 tr:nth-child(odd) {background-color: #cccccc}
232 tr:nth-child(even) {background-color: #ffffff}
233 th, td {padding: 0.1em 1em}
234 </style>
235 </head>
236 <body>
237 <center>
238 <form type="GET" action="%(root)s">
239 <input type="hidden" name="action" value="update">
240 <input type="submit" value="Update Configuration Database (SVN)">
241 </form>
242 <table>
243 <caption><h3>Wireless Leiden Configurator</h3></caption>
244 """ % items
245
246 for node in nodelist:
247 items['node'] = node
248 output += fl(5, '<tr>') + fl(7,'<td><a href="%(root)s/%(node)s">%(node)s</a></td>' % items)
249 for config in files:
250 items['config'] = config
251 output += fl(7,'<td><a href="%(root)s/%(node)s/%(config)s">%(config)s</a></td>' % items)
252 output += fl(5, "</tr>")
253 output += """
254 </table>
255 <hr />
256 <em>%s</em>
257 </center>
258 </body>
259</html>
260 """ % __version__
261
262 return output
263
264
265
266def generate_node(node):
267 """ Print overview of all files available for node """
268 return "\n".join(files)
269
270def generate_node_overview(host):
271 """ Print overview of all files available for node """
272 datadump = get_yaml(host)
273 params = { 'host' : host }
274 output = "<em><a href='..'>Back to overview</a></em><hr />"
275 output += "<h2>Available files:</h2><ul>"
276 for cf in files:
277 params['cf'] = cf
278 output += '<li><a href="%(host)s/%(cf)s">%(cf)s</a></li>\n' % params
279 output += "</ul>"
280
281 # Generate and connection listing
282 output += "<h2>Connected To:</h2><ul>"
283 (poel, errors) = make_relations()
284 for network, hosts in poel.iteritems():
285 if host in [x[0] for x in hosts]:
286 if len(hosts) == 1:
287 # Single not connected interface
288 continue
289 for remote,ifacedump in hosts:
290 if remote == host:
291 # This side of the interface
292 continue
293 params = { 'remote': remote, 'remote_ip' : ifacedump['ip'] }
294 output += '<li><a href="%(remote)s">%(remote)s</a> -- %(remote_ip)s</li>\n' % params
295 output += "</ul>"
296 output += "<h2>MOTD details:</h2><pre>" + generate_motd(datadump) + "</pre>"
297
298 output += "<hr /><em><a href='..'>Back to overview</a></em>"
299 return output
300
301
302def generate_header(ctag="#"):
303 return """\
304%(ctag)s
305%(ctag)s DO NOT EDIT - Automatically generated by 'gformat'
306%(ctag)s Generated at %(date)s by %(host)s
307%(ctag)s
308""" % { 'ctag' : ctag, 'date' : time.ctime(), 'host' : socket.gethostname() }
309
310
311
312def parseaddr(s):
313 """ Process IPv4 CIDR notation addr to a (binary) number """
314 f = s.split('.')
315 return (long(f[0]) << 24L) + \
316 (long(f[1]) << 16L) + \
317 (long(f[2]) << 8L) + \
318 long(f[3])
319
320
321
322def showaddr(a):
323 """ Display IPv4 addr in (dotted) CIDR notation """
324 return "%d.%d.%d.%d" % ((a >> 24) & 0xff, (a >> 16) & 0xff, (a >> 8) & 0xff, a & 0xff)
325
326
327def is_member(ip, mask, canidate):
328 """ Return True if canidate is part of ip/mask block"""
329 ip_addr = gformat.parseaddr(ip)
330 ip_canidate = gformat.parseaddr(canidate)
331 mask = int(mask)
332 ip_addr = ip_addr & ~((1 << (32 - mask)) - 1)
333 ip_canidate = ip_canidate & ~((1 << (32 - mask)) - 1)
334 return ip_addr == ip_canidate
335
336
337
338def cidr2netmask(netmask):
339 """ Given a 'netmask' return corresponding CIDR """
340 return showaddr(0xffffffff & (0xffffffff << (32 - int(netmask))))
341
342def get_network(addr, mask):
343 return showaddr(parseaddr(addr) & ~((1 << (32 - int(mask))) - 1))
344
345
346def generate_dhcpd_conf(datadump):
347 """ Generate config file '/usr/local/etc/dhcpd.conf """
348 output = generate_header()
349 output += Template("""\
350# option definitions common to all supported networks...
351option domain-name "dhcp.{{ autogen_fqdn }}";
352
353default-lease-time 600;
354max-lease-time 7200;
355
356# Use this to enble / disable dynamic dns updates globally.
357#ddns-update-style none;
358
359# If this DHCP server is the official DHCP server for the local
360# network, the authoritative directive should be uncommented.
361authoritative;
362
363# Use this to send dhcp log messages to a different log file (you also
364# have to hack syslog.conf to complete the redirection).
365log-facility local7;
366
367#
368# Interface definitions
369#
370\n""").render(datadump)
371
372 for iface_key in datadump['autogen_iface_keys']:
373 if not datadump[iface_key].has_key('comment'):
374 datadump[iface_key]['comment'] = None
375 output += "## %(interface)s - %(desc)s - %(comment)s\n" % datadump[iface_key]
376
377 (addr, mask) = datadump[iface_key]['ip'].split('/')
378 datadump[iface_key]['addr'] = addr
379 datadump[iface_key]['netmask'] = cidr2netmask(mask)
380 datadump[iface_key]['subnet'] = get_network(addr, mask)
381 try:
382 (dhcp_start, dhcp_stop) = datadump[iface_key]['dhcp'].split('-')
383 except (AttributeError, ValueError):
384 output += "subnet %(subnet)s netmask %(netmask)s {\n ### not autoritive\n}\n\n" % datadump[iface_key]
385 continue
386
387 dhcp_part = ".".join(addr.split('.')[0:3])
388 datadump[iface_key]['dhcp_start'] = dhcp_part + "." + dhcp_start
389 datadump[iface_key]['dhcp_stop'] = dhcp_part + "." + dhcp_stop
390 output += """\
391subnet %(subnet)s netmask %(netmask)s {
392 range %(dhcp_start)s %(dhcp_stop)s;
393 option routers %(addr)s;
394 option domain-name-servers %(addr)s;
395}
396\n""" % datadump[iface_key]
397
398 return output
399
400
401
402def generate_dnsmasq_conf(datadump):
403 """ Generate configuration file '/usr/local/etc/dnsmasq.conf' """
404 output = generate_header()
405 output += Template("""\
406# DHCP server options
407dhcp-authoritative
408dhcp-fqdn
409domain=dhcp.{{ autogen_fqdn }}
410domain-needed
411expand-hosts
412log-async=100
413
414# Low memory footprint
415cache-size=10000
416
417\n""").render(datadump)
418
419 for iface_key in datadump['autogen_iface_keys']:
420 if not datadump[iface_key].has_key('comment'):
421 datadump[iface_key]['comment'] = None
422 output += "## %(interface)s - %(desc)s - %(comment)s\n" % datadump[iface_key]
423
424 try:
425 (dhcp_start, dhcp_stop) = datadump[iface_key]['dhcp'].split('-')
426 (ip, cidr) = datadump[iface_key]['ip'].split('/')
427 datadump[iface_key]['netmask'] = cidr2netmask(cidr)
428 except (AttributeError, ValueError):
429 output += "# not autoritive\n\n"
430 continue
431
432 dhcp_part = ".".join(ip.split('.')[0:3])
433 datadump[iface_key]['dhcp_start'] = dhcp_part + "." + dhcp_start
434 datadump[iface_key]['dhcp_stop'] = dhcp_part + "." + dhcp_stop
435 output += "dhcp-range=%(interface)s,%(dhcp_start)s,%(dhcp_stop)s,%(netmask)s,24h\n\n" % datadump[iface_key]
436
437 return output
438
439
440
441def generate_rc_conf_local(datadump):
442 """ Generate configuration file '/etc/rc.conf.local' """
443 if not datadump.has_key('ileiden'):
444 datadump['autogen_ileiden_enable'] = False
445 else:
446 datadump['autogen_ileiden_enable'] = datadump['ileiden']
447
448 datadump['autogen_ileiden_enable'] = switchFormat(datadump['autogen_ileiden_enable'])
449
450 ileiden_proxies = []
451 normal_proxies = []
452 for proxy in get_proxylist():
453 proxydump = get_yaml(proxy)
454 if proxydump['ileiden']:
455 ileiden_proxies.append(proxydump)
456 else:
457 normal_proxies.append(proxydump)
458 for host in get_hybridlist():
459 hostdump = get_yaml(host)
460 if hostdump['service_proxy_ileiden']:
461 ileiden_proxies.append(hostdump)
462 if hostdump['service_proxy_normal']:
463 normal_proxies.append(hostdump)
464
465 datadump['autogen_ileiden_proxies'] = ileiden_proxies
466 datadump['autogen_normal_proxies'] = normal_proxies
467 datadump['autogen_ileiden_proxies_ips'] = ','.join([x['masterip'] for x in ileiden_proxies])
468 datadump['autogen_ileiden_proxies_names'] = ','.join([x['autogen_item'] for x in ileiden_proxies])
469 datadump['autogen_normal_proxies_ips'] = ','.join([x['masterip'] for x in normal_proxies])
470 datadump['autogen_normal_proxies_names'] = ','.join([x['autogen_item'] for x in normal_proxies])
471
472 output = generate_header("#");
473 output += render_template(datadump, """\
474hostname='{{ autogen_fqdn }}'
475location='{{ location }}'
476nodetype="{{ nodetype }}"
477
478#
479# Configured listings
480#
481captive_portal_whitelist=""
482{% if nodetype == "Proxy" %}
483#
484# Proxy Configuration
485#
486{% if gateway -%}
487defaultrouter="{{ gateway }}"
488{% else -%}
489#defaultrouter="NOTSET"
490{% endif -%}
491internalif="{{ internalif }}"
492ileiden_enable="{{ autogen_ileiden_enable }}"
493gateway_enable="{{ autogen_ileiden_enable }}"
494pf_enable="yes"
495pf_rules="/etc/pf.conf"
496{% if autogen_ileiden_enable -%}
497pf_flags="-D ext_if={{ externalif }} -D int_if={{ internalif }} -D publicnat={80,443}"
498lvrouted_enable="{{ autogen_ileiden_enable }}"
499lvrouted_flags="-u -s s00p3rs3kr3t -m 28"
500{% else -%}
501pf_flags="-D ext_if={{ externalif }} -D int_if={{ internalif }} -D publicnat={0}"
502{% endif -%}
503{% if internalroute -%}
504static_routes="wleiden"
505route_wleiden="-net 172.16.0.0/12 {{ internalroute }}"
506{% endif -%}
507
508{% elif nodetype == "Hybrid" %}
509 #
510 # Hybrid Configuration
511 #
512 list_ileiden_proxies="
513 {% for item in autogen_ileiden_proxies -%}
514 {{ "%-16s"|format(item.masterip) }} # {{ item.autogen_realname }}
515 {% endfor -%}
516 "
517 list_normal_proxies="
518 {% for item in autogen_normal_proxies -%}
519 {{ "%-16s"|format(item.masterip) }} # {{ item.autogen_realname }}
520 {% endfor -%}
521 "
522
523 captive_portal_interfaces="{{ autogen_dhcp_interfaces|default('none', true) }}"
524 externalif="{{ externalif|default('vr0', true) }}"
525 masterip="{{ masterip }}"
526
527 # Defined services
528 service_proxy_ileiden="{{ service_proxy_ileiden|yesorno }}"
529 service_proxy_normal="{{ service_proxy_normal|yesorno }}"
530 service_accesspoint="{{ service_accesspoint|yesorno }}"
531 #
532
533 {% if service_proxy_ileiden %}
534 pf_rules="/etc/pf.hybrid.conf"
535 pf_flags="-D ext_if=$externalif -D ext_if_net=$externalif:network -D masterip=$masterip"
536 pf_flags="$pf_flags -D publicnat=80,443"
537 {% elif service_proxy_normal %}
538 pf_rules="/etc/pf.hybrid.conf"
539 pf_flags="-D ext_if=$externalif -D ext_if_net=$externalif:network -D masterip=$masterip"
540 pf_flags="$pf_flags -D publicnat=0"
541 lvrouted_flags="$lvrouted_flags -z `make_list "$list_ileiden_proxies" ","`"
542 named_setfib="1"
543 tinyproxy_setfib="1"
544 dnsmasq_setfib="1"
545 {% else %}
546 pf_rules="/etc/pf.node.conf"
547 pf_flags=""
548 {% endif %}
549
550 {% if service_proxy_normal %}
551 tinyproxy_enable="yes"
552 {% else %}
553 pen_wrapper_enable="yes"
554 {% endif %}
555
556 {% if service_accesspoint %}
557 pf_flags="$pf_flags -D captive_portal_interfaces=$captive_portal_interfaces"
558 {% endif %}
559
560 {% if board == "ALIX2" %}
561 #
562 # ''Fat'' configuration, board has 256MB RAM
563 #
564 dnsmasq_enable="NO"
565 named_enable="YES"
566 dhcpd_enable="YES"
567 {% endif -%}
568
569 {% if service_proxy_ileiden and gateway %}
570 defaultrouter="{{ gateway }}"
571 {% endif %}
572{% elif nodetype == "CNode" %}
573#
574# NODE iLeiden Configuration
575#
576
577# iLeiden Proxies {{ autogen_ileiden_proxies_names }}
578list_ileiden_proxies="{{ autogen_ileiden_proxies_ips }}"
579# normal Proxies {{ autogen_normal_proxies_names }}
580list_normal_proxies="{{ autogen_normal_proxies_ips }}"
581
582captive_portal_interfaces="{{ autogen_dhcp_interfaces }}"
583
584{% if tproxy -%}
585tproxy_enable='YES'
586tproxy_range='{{ tproxy }}'
587{% else -%}
588tproxy_enable='NO'
589{% endif -%}
590
591lvrouted_flags="-u -s s00p3rs3kr3t -m 28 -z $list_ileiden_proxies"
592{% endif %}
593
594#
595# Interface definitions
596#\n
597""")
598
599 # lo0 configuration:
600 # - 172.32.255.1/32 is the proxy.wleiden.net deflector
601 # - masterip is special as it needs to be assigned to at
602 # least one interface, so if not used assign to lo0
603 addrs_list = { 'lo0' : [("127.0.0.1/8", "LocalHost"), ("172.31.255.1/32","Proxy IP")] }
604 iface_map = {'lo0' : 'lo0'}
605 dhclient_if = {'lo0' : False}
606
607 masterip_used = False
608 for iface_key in datadump['autogen_iface_keys']:
609 if datadump[iface_key]['ip'].startswith(datadump['masterip']):
610 masterip_used = True
611 break
612 if not masterip_used:
613 addrs_list['lo0'].append((datadump['masterip'] + "/32", 'Master IP Not used in interface'))
614
615 for iface_key in datadump['autogen_iface_keys']:
616 ifacedump = datadump[iface_key]
617 ifname = ifacedump['autogen_ifname']
618
619 # Flag dhclient is possible
620 dhclient_if[ifname] = ifacedump.has_key('dhcpclient') and ifacedump['dhcpclient']
621
622 # Add interface IP to list
623 item = (ifacedump['ip'], ifacedump['desc'])
624 if addrs_list.has_key(ifname):
625 addrs_list[ifname].append(item)
626 else:
627 addrs_list[ifname] = [item]
628
629 # Alias only needs IP assignment for now, this might change if we
630 # are going to use virtual accesspoints
631 if "alias" in iface_key:
632 continue
633
634 # XXX: Might want to deduct type directly from interface name
635 if ifacedump['type'] in ['11a', '11b', '11g', 'wireless']:
636 # Default to station (client) mode
637 ifacedump['wlanmode'] = "sta"
638 if ifacedump['mode'] in ['master', 'master-wds', 'ap', 'ap-wds']:
639 ifacedump['wlanmode'] = "ap"
640 # Default to 802.11b mode
641 ifacedump['mode'] = '11b'
642 if ifacedump['type'] in ['11a', '11b' '11g']:
643 ifacedump['mode'] = ifacedump['type']
644
645 if not ifacedump.has_key('channel'):
646 if ifacedump['type'] == '11a':
647 ifacedump['channel'] = 36
648 else:
649 ifacedump['channel'] = 1
650
651 # Allow special hacks at the back like wds and stuff
652 if not ifacedump.has_key('extra'):
653 ifacedump['extra'] = 'regdomain ETSI country NL'
654
655 output += "wlans_%(interface)s='%(autogen_ifname)s'\n" % ifacedump
656 output += ("create_args_%(autogen_ifname)s='wlanmode %(wlanmode)s mode " +\
657 "%(mode)s ssid %(ssid)s %(extra)s channel %(channel)s'\n") % ifacedump
658
659 elif ifacedump['type'] in ['ethernet', 'eth']:
660 # No special config needed besides IP
661 pass
662 else:
663 assert False, "Unknown type " + ifacedump['type']
664
665 # Print IP address which needs to be assigned over here
666 output += "\n"
667 for iface,addrs in sorted(addrs_list.iteritems()):
668 for addr, comment in sorted(addrs,key=lambda x: parseaddr(x[0].split('/')[0])):
669 output += "# %s || %s || %s\n" % (iface, addr, comment)
670
671 # Write DHCLIENT entry
672 if dhclient_if[iface]:
673 output += "ifconfig_%s='SYNCDHCP'\n\n" % (iface)
674 else:
675 output += "ipv4_addrs_%s='%s'\n\n" % (iface, " ".join([x[0] for x in addrs]))
676
677 return output
678
679
680
681
682def get_all_configs():
683 """ Get dict with key 'host' with all configs present """
684 configs = dict()
685 for host in get_hostlist():
686 datadump = get_yaml(host)
687 configs[host] = datadump
688 return configs
689
690
691def get_interface_keys(config):
692 """ Quick hack to get all interface keys, later stage convert this to a iterator """
693 return sorted([elem for elem in config.keys() if (elem.startswith('iface_') and not "lo0" in elem)])
694
695
696def get_used_ips(configs):
697 """ Return array of all IPs used in config files"""
698 ip_list = []
699 for config in configs:
700 ip_list.append(config['masterip'])
701 for iface_key in get_interface_keys(config):
702 l = config[iface_key]['ip']
703 addr, mask = l.split('/')
704 # Special case do not process
705 if valid_addr(addr):
706 ip_list.append(addr)
707 else:
708 logger.error("## IP '%s' in '%s' not valid" % (addr, config['nodename']))
709 return sorted(ip_list)
710
711
712
713def generate_resolv_conf(datadump):
714 """ Generate configuration file '/etc/resolv.conf' """
715 # XXX: This should properly going to be an datastructure soon
716 datadump['autogen_header'] = generate_header("#")
717 datadump['autogen_edge_nameservers'] = ''
718 for host in get_proxylist():
719 hostdump = get_yaml(host)
720 datadump['autogen_edge_nameservers'] += "nameserver %(masterip)-15s # %(autogen_realname)s\n" % hostdump
721 for host in get_hybridlist():
722 hostdump = get_yaml(host)
723 if hostdump['service_proxy_ileiden'] or hostdump['service_proxy_normal']:
724 datadump['autogen_edge_nameservers'] += "nameserver %(masterip)-15s # %(autogen_realname)s\n" % hostdump
725
726 return Template("""\
727{{ autogen_header }}
728search wleiden.net
729
730# Try local (cache) first
731nameserver 127.0.0.1
732
733{% if service_proxy_normal or service_proxy_ileiden or nodetype == 'Proxy' -%}
734nameserver 8.8.8.8 # Google Public NameServer
735nameserver 8.8.4.4 # Google Public NameServer
736{% else -%}
737# START DYNAMIC LIST - updated by /tools/nameserver-shuffle
738{{ autogen_edge_nameservers }}
739{% endif -%}
740""").render(datadump)
741
742
743
744def generate_ntp_conf(datadump):
745 """ Generate configuration file '/etc/ntp.conf' """
746 # XXX: This should properly going to be an datastructure soon
747
748 datadump['autogen_header'] = generate_header("#")
749 datadump['autogen_ntp_servers'] = ''
750 for host in get_proxylist():
751 hostdump = get_yaml(host)
752 datadump['autogen_ntp_servers'] += "server %(masterip)-15s iburst maxpoll 9 # %(autogen_realname)s\n" % hostdump
753 for host in get_hybridlist():
754 hostdump = get_yaml(host)
755 if hostdump['service_proxy_ileiden'] or hostdump['service_proxy_normal']:
756 datadump['autogen_ntp_servers'] += "server %(masterip)-15s iburst maxpoll 9 # %(autogen_realname)s\n" % hostdump
757
758 return Template("""\
759{{ autogen_header }}
760
761{% if service_proxy_normal or service_proxy_ileiden or nodetype == 'Proxy' -%}
762# Machine hooked to internet.
763server 0.nl.pool.ntp.org iburst maxpoll 9
764server 1.nl.pool.ntp.org iburst maxpoll 9
765server 2.nl.pool.ntp.org iburst maxpoll 9
766server 3.nl.pool.ntp.org iburst maxpoll 9
767{% else -%}
768# Local Wireless Leiden NTP Servers.
769server 0.pool.ntp.wleiden.net iburst maxpoll 9
770server 1.pool.ntp.wleiden.net iburst maxpoll 9
771server 2.pool.ntp.wleiden.net iburst maxpoll 9
772server 3.pool.ntp.wleiden.net iburst maxpoll 9
773
774# All the configured NTP servers
775{{ autogen_ntp_servers }}
776{% endif %}
777
778# If a server loses sync with all upstream servers, NTP clients
779# no longer follow that server. The local clock can be configured
780# to provide a time source when this happens, but it should usually
781# be configured on just one server on a network. For more details see
782# http://support.ntp.org/bin/view/Support/UndisciplinedLocalClock
783# The use of Orphan Mode may be preferable.
784#
785server 127.127.1.0
786fudge 127.127.1.0 stratum 10
787""").render(datadump)
788
789
790
791def generate_motd(datadump):
792 """ Generate configuration file '/etc/motd' """
793 output = Template("""\
794FreeBSD run ``service motd onestart'' to make me look normal
795
796 WWW: {{ autogen_fqdn }} - http://www.wirelessleiden.nl
797 Loc: {{ location }}
798
799Services:
800{% if board == "ALIX2" -%}
801 - Core Node ({{ board }})
802{% else -%}
803 - Hulp Node ({{ board }})
804{% endif -%}
805{% if service_proxy_normal -%}
806 - Normal Proxy
807{% endif -%}
808{% if service_proxy_ileiden -%}
809 - iLeiden Proxy
810{% endif %}
811Interlinks:\n
812""").render(datadump)
813
814 # XXX: This is a hacky way to get the required data
815 for line in generate_rc_conf_local(datadump).split('\n'):
816 if '||' in line and not line[1:].split()[0] in ['lo0', 'ath0'] :
817 output += " - %s \n" % line[1:]
818 output += """\
819Attached bridges:
820"""
821 for iface_key in datadump['autogen_iface_keys']:
822 ifacedump = datadump[iface_key]
823 if ifacedump.has_key('ns_ip'):
824 output += " - %(interface)s || %(mode)s || %(ns_ip)s\n" % ifacedump
825
826 return output
827
828
829def format_yaml_value(value):
830 """ Get yaml value in right syntax for outputting """
831 if isinstance(value,str):
832 output = '"%s"' % value
833 else:
834 output = value
835 return output
836
837
838
839def format_wleiden_yaml(datadump):
840 """ Special formatting to ensure it is editable"""
841 output = "# Genesis config yaml style\n"
842 output += "# vim:ts=2:et:sw=2:ai\n"
843 output += "#\n"
844 iface_keys = [elem for elem in datadump.keys() if elem.startswith('iface_')]
845 for key in sorted(set(datadump.keys()) - set(iface_keys)):
846 output += "%-10s: %s\n" % (key, format_yaml_value(datadump[key]))
847
848 output += "\n\n"
849
850 key_order = [ 'comment', 'interface', 'ip', 'desc', 'sdesc', 'mode', 'type',
851 'extra_type', 'channel', 'ssid', 'dhcp' ]
852
853 for iface_key in sorted(iface_keys):
854 output += "%s:\n" % iface_key
855 for key in key_order + list(sorted(set(datadump[iface_key].keys()) - set(key_order))):
856 if datadump[iface_key].has_key(key):
857 output += " %-11s: %s\n" % (key, format_yaml_value(datadump[iface_key][key]))
858 output += "\n\n"
859
860 return output
861
862
863
864def generate_wleiden_yaml(datadump, header=True):
865 """ Generate (petty) version of wleiden.yaml"""
866 for key in datadump.keys():
867 if key.startswith('autogen_'):
868 del datadump[key]
869 # Interface autogen cleanups
870 elif type(datadump[key]) == dict:
871 for key2 in datadump[key].keys():
872 if key2.startswith('autogen_'):
873 del datadump[key][key2]
874
875 output = generate_header("#") if header else ''
876 output += format_wleiden_yaml(datadump)
877 return output
878
879
880def generate_yaml(datadump):
881 return generate_config(datadump['nodename'], "wleiden.yaml", datadump)
882
883
884
885def generate_config(node, config, datadump=None):
886 """ Print configuration file 'config' of 'node' """
887 output = ""
888 try:
889 # Load config file
890 if datadump == None:
891 datadump = get_yaml(node)
892
893 if config == 'wleiden.yaml':
894 output += generate_wleiden_yaml(datadump)
895 elif config == 'authorized_keys':
896 f = open(os.path.join(NODE_DIR,"global_keys"), 'r')
897 output += f.read()
898 f.close()
899 elif config == 'dnsmasq.conf':
900 output += generate_dnsmasq_conf(datadump)
901 elif config == 'dhcpd.conf':
902 output += generate_dhcpd_conf(datadump)
903 elif config == 'rc.conf.local':
904 output += generate_rc_conf_local(datadump)
905 elif config == 'resolv.conf':
906 output += generate_resolv_conf(datadump)
907 elif config == 'ntp.conf':
908 output += generate_ntp_conf(datadump)
909 elif config == 'motd':
910 output += generate_motd(datadump)
911 else:
912 assert False, "Config not found!"
913 except IOError, e:
914 output += "[ERROR] Config file not found"
915 return output
916
917
918
919def process_cgi_request():
920 """ When calling from CGI """
921 # Update repository if requested
922 form = cgi.FieldStorage()
923 if form.getvalue("action") == "update":
924 print "Refresh: 5; url=."
925 print "Content-type:text/plain\r\n\r\n",
926 print "[INFO] Updating subverion, please wait..."
927 print subprocess.Popen(['svn', 'cleanup', "%s/.." % NODE_DIR], stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0],
928 print subprocess.Popen(['svn', 'up', "%s/.." % NODE_DIR], stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0],
929 print "[INFO] All done, redirecting in 5 seconds"
930 sys.exit(0)
931
932
933 base_uri = os.environ['PATH_INFO']
934 uri = base_uri.strip('/').split('/')
935
936 output = "Template Holder"
937 content_type='text/plain'
938 if base_uri.endswith('/create/network.kml'):
939 content_type='application/vnd.google-earth.kml+xml'
940 output = make_network_kml.make_graph()
941 elif not uri[0]:
942 if is_text_request():
943 content_type = 'text/plain'
944 output = '\n'.join(get_hostlist())
945 else:
946 content_type = 'text/html'
947 output = generate_title(get_hostlist())
948 elif len(uri) == 1:
949 if is_text_request():
950 content_type = 'text/plain'
951 output = generate_node(uri[0])
952 else:
953 content_type = 'text/html'
954 output = generate_node_overview(uri[0])
955 elif len(uri) == 2:
956 content_type = 'text/plain'
957 output = generate_config(uri[0], uri[1])
958 else:
959 assert False, "Invalid option"
960
961 print "Content-Type: %s" % content_type
962 print "Content-Length: %s" % len(output)
963 print ""
964 print output
965
966def get_realname(datadump):
967 # Proxy naming convention is special, as the proxy name is also included in
968 # the nodename, when it comes to the numbered proxies.
969 if datadump['nodetype'] == 'Proxy':
970 realname = datadump['nodetype'] + datadump['nodename'].replace('proxy','')
971 else:
972 # By default the full name is listed and also a shortname CNAME for easy use.
973 realname = datadump['nodetype'] + datadump['nodename']
974 return(realname)
975
976
977
978def make_dns(output_dir = 'dns', external = False):
979 items = dict()
980
981 # hostname is key, IP is value
982 wleiden_zone = defaultdict(list)
983 wleiden_cname = dict()
984
985 pool = dict()
986 for node in get_hostlist():
987 datadump = get_yaml(node)
988
989 # Proxy naming convention is special
990 fqdn = datadump['autogen_realname']
991 if datadump['nodetype'] in ['CNode', 'Hybrid']:
992 wleiden_cname[datadump['nodename']] = fqdn
993
994 wleiden_zone[fqdn].append((datadump['masterip'], True))
995
996 # Hacking to get proper DHCP IPs and hostnames
997 for iface_key in get_interface_keys(datadump):
998 iface_name = datadump[iface_key]['interface'].replace(':',"-alias-")
999 (ip, cidr) = datadump[iface_key]['ip'].split('/')
1000 try:
1001 (dhcp_start, dhcp_stop) = datadump[iface_key]['dhcp'].split('-')
1002 datadump[iface_key]['netmask'] = cidr2netmask(cidr)
1003 dhcp_part = ".".join(ip.split('.')[0:3])
1004 if ip != datadump['masterip']:
1005 wleiden_zone["dhcp-gateway-%s.%s" % (iface_name, fqdn)].append((ip, False))
1006 for i in range(int(dhcp_start), int(dhcp_stop) + 1):
1007 wleiden_zone["dhcp-%s-%s.%s" % (i, iface_name, fqdn)].append(("%s.%s" % (dhcp_part, i), True))
1008 except (AttributeError, ValueError):
1009 # First push it into a pool, to indentify the counter-part later on
1010 addr = parseaddr(ip)
1011 cidr = int(cidr)
1012 addr = addr & ~((1 << (32 - cidr)) - 1)
1013 if pool.has_key(addr):
1014 pool[addr] += [(iface_name, fqdn, ip)]
1015 else:
1016 pool[addr] = [(iface_name, fqdn, ip)]
1017 continue
1018
1019
1020 def pool_to_name(fqdn, pool_members):
1021 """Convert the joined name to a usable pool name"""
1022
1023 def isplit(item):
1024 (prefix, name, number) = re.match('^(cnode|hybrid|proxy)([a-z]+)([0-9]*)$',item.lower()).group(1,2,3)
1025 return (prefix, name, number)
1026
1027 my_name = isplit(fqdn.split('.')[0])[1]
1028
1029 short_names = defaultdict(list)
1030 for node in sorted(pool_members):
1031 (prefix, name, number) = isplit(node)
1032 short_names[name].append((prefix,number))
1033
1034 return '-'.join(sorted(short_names.keys()))
1035
1036
1037 # WL uses an /29 to configure an interface. IP's are ordered like this:
1038 # MasterA (.1) -- DeviceA (.2) <<>> DeviceB (.3) --- SlaveB (.4)
1039
1040 sn = lambda x: re.sub(r'(?i)^cnode','',x)
1041
1042 # Automatic naming convention of interlinks namely 2 + remote.lower()
1043 for (key,value) in pool.iteritems():
1044 # Make sure they are sorted from low-ip to high-ip
1045 value = sorted(value, key=lambda x: parseaddr(x[2]))
1046
1047 if len(value) == 1:
1048 (iface_name, fqdn, ip) = value[0]
1049 wleiden_zone["2unused-%s.%s" % (iface_name, fqdn)].append((ip, True))
1050
1051 # Device DNS names
1052 if 'cnode' in fqdn.lower():
1053 wleiden_zone["d-at-%s.%s" % (iface_name, fqdn)].append((showaddr(parseaddr(ip) + 1), False))
1054 wleiden_cname["d-at-%s.%s" % (iface_name,sn(fqdn))] = "d-at-%s.%s" % ((iface_name, fqdn))
1055
1056 elif len(value) == 2:
1057 (a_iface_name, a_fqdn, a_ip) = value[0]
1058 (b_iface_name, b_fqdn, b_ip) = value[1]
1059 wleiden_zone["2%s.%s" % (b_fqdn,a_fqdn)].append((a_ip, True))
1060 wleiden_zone["2%s.%s" % (a_fqdn,b_fqdn)].append((b_ip, True))
1061
1062 # Device DNS names
1063 if 'cnode' in a_fqdn.lower() and 'cnode' in b_fqdn.lower():
1064 wleiden_zone["d-at-%s.%s" % (a_iface_name, a_fqdn)].append((showaddr(parseaddr(a_ip) + 1), False))
1065 wleiden_zone["d-at-%s.%s" % (b_iface_name, b_fqdn)].append((showaddr(parseaddr(b_ip) - 1), False))
1066 wleiden_cname["d-at-%s.%s" % (a_iface_name,sn(a_fqdn))] = "d-at-%s.%s" % (a_iface_name, a_fqdn)
1067 wleiden_cname["d-at-%s.%s" % (b_iface_name,sn(b_fqdn))] = "d-at-%s.%s" % (b_iface_name, b_fqdn)
1068 wleiden_cname["d2%s.%s" % (sn(b_fqdn),sn(a_fqdn))] = "d-at-%s.%s" % (a_iface_name, a_fqdn)
1069 wleiden_cname["d2%s.%s" % (sn(a_fqdn),sn(b_fqdn))] = "d-at-%s.%s" % (b_iface_name, b_fqdn)
1070
1071 else:
1072 pool_members = [k[1] for k in value]
1073 for item in value:
1074 (iface_name, fqdn, ip) = item
1075 pool_name = "2pool-" + pool_to_name(fqdn,pool_members)
1076 wleiden_zone["%s.%s" % (pool_name, fqdn)].append((ip, True))
1077
1078 # Include static DNS entries
1079 # XXX: Should they override the autogenerated results?
1080 # XXX: Convert input to yaml more useable.
1081 # Format:
1082 ##; this is a comment
1083 ## roomburgh=CNodeRoomburgh1
1084 ## apkerk1.CNodeVosko=172.17.176.8 ;this as well
1085 dns_list = yaml.load(open(os.path.join(NODE_DIR,'../dns/staticDNS.yaml'),'r'))
1086
1087 # Hack to allow special entries, for development
1088 wleiden_raw = {}
1089
1090 for line in dns_list:
1091 reverse = False
1092 k, items = line.items()[0]
1093 if type(items) == dict:
1094 if items.has_key('reverse'):
1095 reverse = items['reverse']
1096 items = items['a']
1097 else:
1098 items = items['cname']
1099 items = [items] if type(items) != list else items
1100 for item in items:
1101 if item.startswith('IN '):
1102 wleiden_raw[k] = item
1103 elif valid_addr(item):
1104 wleiden_zone[k].append((item, reverse))
1105 else:
1106 wleiden_cname[k] = item
1107
1108 details = dict()
1109 # 24 updates a day allowed
1110 details['serial'] = time.strftime('%Y%m%d%H')
1111
1112 if external:
1113 dns_masters = ['siteview.wirelessleiden.nl', 'ns1.vanderzwet.net']
1114 else:
1115 dns_masters = ['sunny.wleiden.net']
1116
1117 details['master'] = dns_masters[0]
1118 details['ns_servers'] = '\n'.join(['\tNS\t%s.' % x for x in dns_masters])
1119
1120 dns_header = '''
1121$TTL 3h
1122%(zone)s. SOA %(master)s. beheer.lijst.wirelessleiden.nl. ( %(serial)s 1d 12h 1w 60s )
1123 ; Serial, Refresh, Retry, Expire, Neg. cache TTL
1124
1125%(ns_servers)s
1126 \n'''
1127
1128
1129 if not os.path.isdir(output_dir):
1130 os.makedirs(output_dir)
1131 details['zone'] = 'wleiden.net'
1132 f = open(os.path.join(output_dir,"db." + details['zone']), "w")
1133 f.write(dns_header % details)
1134
1135 for host,items in wleiden_zone.iteritems():
1136 for ip,reverse in items:
1137 if valid_addr(ip):
1138 f.write("%s.wleiden.net. IN A %s \n" % (host.lower(), ip))
1139 for source,dest in wleiden_cname.iteritems():
1140 f.write("%s.wleiden.net. IN CNAME %s.wleiden.net.\n" % (source.lower(), dest.lower()))
1141 for source, dest in wleiden_raw.iteritems():
1142 f.write("%s.wleiden.net. %s\n" % (source, dest))
1143 f.close()
1144
1145 # Create whole bunch of specific sub arpa zones. To keep it compliant
1146 for s in range(16,32):
1147 details['zone'] = '%i.172.in-addr.arpa' % s
1148 f = open(os.path.join(output_dir,"db." + details['zone']), "w")
1149 f.write(dns_header % details)
1150
1151 #XXX: Not effient, fix to proper data structure and do checks at other
1152 # stages
1153 for host,items in wleiden_zone.iteritems():
1154 for ip,reverse in items:
1155 if not reverse:
1156 continue
1157 if valid_addr(ip):
1158 if valid_addr(ip):
1159 if int(ip.split('.')[1]) == s:
1160 rev_ip = '.'.join(reversed(ip.split('.')))
1161 f.write("%s.in-addr.arpa. IN PTR %s.wleiden.net.\n" % (rev_ip.lower(), host.lower()))
1162 f.close()
1163
1164
1165def usage():
1166 print """Usage: %(prog)s <argument>
1167Argument:
1168\tstandalone [port] = Run configurator webserver [8000]
1169\tdns [outputdir] = Generate BIND compliant zone files in dns [./dns]
1170\tfull-export = Generate yaml export script for heatmap.
1171\tstatic [outputdir] = Generate all config files and store on disk
1172\t with format ./<outputdir>/%%NODE%%/%%FILE%% [./static]
1173\ttest <node> <file> = Receive output of CGI script.
1174\tlist <status> <items> = List systems which have certain status
1175
1176Arguments:
1177\t<node> = NodeName (example: HybridRick)
1178\t<file> = %(files)s
1179\t<status> = all|up|down|planned
1180\t<items> = systems|nodes|proxies
1181
1182NOTE FOR DEVELOPERS; you can test your changes like this:
1183 BEFORE any changes in this code:
1184 $ ./gformat.py static /tmp/pre
1185 AFTER the changes:
1186 $ ./gformat.py static /tmp/post
1187 VIEW differences and VERIFY all are OK:
1188 $ diff -urI 'Generated' -r /tmp/pre /tmp/post
1189""" % { 'prog' : sys.argv[0], 'files' : '|'.join(files) }
1190 exit(0)
1191
1192
1193def is_text_request():
1194 """ Find out whether we are calling from the CLI or any text based CLI utility """
1195 try:
1196 return os.environ['HTTP_USER_AGENT'].split()[0] in ['curl', 'fetch', 'wget']
1197 except KeyError:
1198 return True
1199
1200def switchFormat(setting):
1201 if setting:
1202 return "YES"
1203 else:
1204 return "NO"
1205
1206def main():
1207 """Hard working sub"""
1208 # Allow easy hacking using the CLI
1209 if not os.environ.has_key('PATH_INFO'):
1210 if len(sys.argv) < 2:
1211 usage()
1212
1213 if sys.argv[1] == "standalone":
1214 import SocketServer
1215 import CGIHTTPServer
1216 # Hop to the right working directory.
1217 os.chdir(os.path.dirname(__file__))
1218 try:
1219 PORT = int(sys.argv[2])
1220 except (IndexError,ValueError):
1221 PORT = 8000
1222
1223 class MyCGIHTTPRequestHandler(CGIHTTPServer.CGIHTTPRequestHandler):
1224 """ Serve this CGI from the root of the webserver """
1225 def is_cgi(self):
1226 if "favicon" in self.path:
1227 return False
1228
1229 self.cgi_info = (os.path.basename(__file__), self.path)
1230 self.path = ''
1231 return True
1232 handler = MyCGIHTTPRequestHandler
1233 SocketServer.TCPServer.allow_reuse_address = True
1234 httpd = SocketServer.TCPServer(("", PORT), handler)
1235 httpd.server_name = 'localhost'
1236 httpd.server_port = PORT
1237
1238 logger.info("serving at port %s", PORT)
1239 try:
1240 httpd.serve_forever()
1241 except KeyboardInterrupt:
1242 httpd.shutdown()
1243 logger.info("All done goodbye")
1244 elif sys.argv[1] == "test":
1245 os.environ['PATH_INFO'] = "/".join(sys.argv[2:])
1246 os.environ['SCRIPT_NAME'] = __file__
1247 process_cgi_request()
1248 elif sys.argv[1] == "static":
1249 items = dict()
1250 items['output_dir'] = sys.argv[2] if len(sys.argv) > 2 else "./static"
1251 for node in get_hostlist():
1252 items['node'] = node
1253 items['wdir'] = "%(output_dir)s/%(node)s" % items
1254 if not os.path.isdir(items['wdir']):
1255 os.makedirs(items['wdir'])
1256 datadump = get_yaml(node)
1257 for config in files:
1258 items['config'] = config
1259 logger.info("## Generating %(node)s %(config)s" % items)
1260 f = open("%(wdir)s/%(config)s" % items, "w")
1261 f.write(generate_config(node, config, datadump))
1262 f.close()
1263 elif sys.argv[1] == "wind-export":
1264 items = dict()
1265 for node in get_hostlist():
1266 datadump = get_yaml(node)
1267 sql = """INSERT IGNORE INTO nodes (name, name_ns, longitude, latitude)
1268 VALUES ('%(nodename)s', '%(nodename)s', %(latitude)s, %(longitude)s);""" % datadump;
1269 sql = """INSERT IGNORE INTO users_nodes (user_id, node_id, owner)
1270 VALUES (
1271 (SELECT id FROM users WHERE username = 'rvdzwet'),
1272 (SELECT id FROM nodes WHERE name = '%(nodename)s'),
1273 'Y');""" % datadump
1274 #for config in files:
1275 # items['config'] = config
1276 # print "## Generating %(node)s %(config)s" % items
1277 # f = open("%(wdir)s/%(config)s" % items, "w")
1278 # f.write(generate_config(node, config, datadump))
1279 # f.close()
1280 for node in get_hostlist():
1281 datadump = get_yaml(node)
1282 for iface_key in sorted([elem for elem in datadump.keys() if elem.startswith('iface_')]):
1283 ifacedump = datadump[iface_key]
1284 if ifacedump.has_key('mode') and ifacedump['mode'] == 'ap-wds':
1285 ifacedump['nodename'] = datadump['nodename']
1286 if not ifacedump.has_key('channel') or not ifacedump['channel']:
1287 ifacedump['channel'] = 0
1288 sql = """INSERT INTO links (node_id, type, ssid, protocol, channel, status)
1289 VALUES ((SELECT id FROM nodes WHERE name = '%(nodename)s'), 'ap',
1290 '%(ssid)s', 'IEEE 802.11b', %(channel)s, 'active');""" % ifacedump
1291 elif sys.argv[1] == "full-export":
1292 hosts = {}
1293 for node in get_hostlist():
1294 datadump = get_yaml(node)
1295 hosts[datadump['nodename']] = datadump
1296 print yaml.dump(hosts)
1297
1298 elif sys.argv[1] == "dns":
1299 make_dns(sys.argv[2] if len(sys.argv) > 2 else 'dns', 'external' in sys.argv)
1300 elif sys.argv[1] == "cleanup":
1301 # First generate all datadumps
1302 datadumps = dict()
1303 for host in get_hostlist():
1304 logger.info("# Processing: %s", host)
1305 # Set some boring default values
1306 datadump = { 'board' : 'UNKNOWN' }
1307 datadump.update(get_yaml(host))
1308 datadumps[datadump['autogen_realname']] = datadump
1309
1310
1311 for host,datadump in datadumps.iteritems():
1312 # Convert all yes and no to boolean values
1313 def fix_boolean(dump):
1314 for key in dump.keys():
1315 if type(dump[key]) == dict:
1316 dump[key] = fix_boolean(dump[key])
1317 elif str(dump[key]).lower() in ["yes", "true"]:
1318 dump[key] = True
1319 elif str(dump[key]).lower() in ["no", "false"]:
1320 # Compass richting no (Noord Oost) is valid input
1321 if key != "compass": dump[key] = False
1322 return dump
1323 datadump = fix_boolean(datadump)
1324
1325 if datadump['rdnap_x'] and datadump['rdnap_y']:
1326 datadump['latitude'], datadump['longitude'] = rdnap.rd2etrs(datadump['rdnap_x'], datadump['rdnap_y'])
1327 elif datadump['latitude'] and datadump['longitude']:
1328 datadump['rdnap_x'], datadump['rdnap_y'] = rdnap.etrs2rd(datadump['latitude'], datadump['longitude'])
1329
1330 if datadump['nodename'].startswith('Proxy'):
1331 datadump['nodename'] = datadump['nodename'].lower()
1332
1333 for iface_key in datadump['autogen_iface_keys']:
1334 # Wireless Leiden SSID have an consistent lowercase/uppercase
1335 if datadump[iface_key].has_key('ssid'):
1336 ssid = datadump[iface_key]['ssid']
1337 prefix = 'ap-WirelessLeiden-'
1338 if ssid.lower().startswith(prefix.lower()):
1339 datadump[iface_key]['ssid'] = prefix + ssid[len(prefix)].upper() + ssid[len(prefix) + 1:]
1340 if datadump[iface_key].has_key('ns_ip') and not datadump[iface_key].has_key('mode'):
1341 datadump[iface_key]['mode'] = 'autogen-FIXME'
1342 if not datadump[iface_key].has_key('desc'):
1343 datadump[iface_key]['desc'] = 'autogen-FIXME'
1344 store_yaml(datadump)
1345 elif sys.argv[1] == "list":
1346 use_fqdn = False
1347 if len(sys.argv) < 4 or not sys.argv[2] in ["up", "down", "planned", "all"]:
1348 usage()
1349 if sys.argv[3] == "nodes":
1350 systems = get_nodelist()
1351 elif sys.argv[3] == "proxies":
1352 systems = get_proxylist()
1353 elif sys.argv[3] == "systems":
1354 systems = get_hostlist()
1355 else:
1356 usage()
1357 if len(sys.argv) > 4:
1358 if sys.argv[4] == "fqdn":
1359 use_fqdn = True
1360 else:
1361 usage()
1362
1363 for system in systems:
1364 datadump = get_yaml(system)
1365
1366 output = datadump['autogen_fqdn'] if use_fqdn else system
1367 if sys.argv[2] == "all":
1368 print output
1369 elif datadump['status'] == sys.argv[2]:
1370 print output
1371 elif sys.argv[1] == "create":
1372 if sys.argv[2] == "network.kml":
1373 print make_network_kml.make_graph()
1374 else:
1375 usage()
1376 usage()
1377 else:
1378 # Do not enable debugging for config requests as it highly clutters the output
1379 if not is_text_request():
1380 cgitb.enable()
1381 process_cgi_request()
1382
1383
1384if __name__ == "__main__":
1385 main()
Note: See TracBrowser for help on using the repository browser.