source: genesis/tools/gformat.py@ 10696

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

tproxy is no longer used, so get rid of the old config variables.

Fixes: nodefactory#141

  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 44.3 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 10696 2012-05-06 23:01:35Z 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
584lvrouted_flags="-u -s s00p3rs3kr3t -m 28 -z $list_ileiden_proxies"
585{% endif %}
586
587#
588# Interface definitions
589#\n
590""")
591
592 # lo0 configuration:
593 # - 172.32.255.1/32 is the proxy.wleiden.net deflector
594 # - masterip is special as it needs to be assigned to at
595 # least one interface, so if not used assign to lo0
596 addrs_list = { 'lo0' : [("127.0.0.1/8", "LocalHost"), ("172.31.255.1/32","Proxy IP")] }
597 iface_map = {'lo0' : 'lo0'}
598 dhclient_if = {'lo0' : False}
599
600 masterip_used = False
601 for iface_key in datadump['autogen_iface_keys']:
602 if datadump[iface_key]['ip'].startswith(datadump['masterip']):
603 masterip_used = True
604 break
605 if not masterip_used:
606 addrs_list['lo0'].append((datadump['masterip'] + "/32", 'Master IP Not used in interface'))
607
608 for iface_key in datadump['autogen_iface_keys']:
609 ifacedump = datadump[iface_key]
610 ifname = ifacedump['autogen_ifname']
611
612 # Flag dhclient is possible
613 dhclient_if[ifname] = ifacedump.has_key('dhcpclient') and ifacedump['dhcpclient']
614
615 # Add interface IP to list
616 item = (ifacedump['ip'], ifacedump['desc'])
617 if addrs_list.has_key(ifname):
618 addrs_list[ifname].append(item)
619 else:
620 addrs_list[ifname] = [item]
621
622 # Alias only needs IP assignment for now, this might change if we
623 # are going to use virtual accesspoints
624 if "alias" in iface_key:
625 continue
626
627 # XXX: Might want to deduct type directly from interface name
628 if ifacedump['type'] in ['11a', '11b', '11g', 'wireless']:
629 # Default to station (client) mode
630 ifacedump['wlanmode'] = "sta"
631 if ifacedump['mode'] in ['master', 'master-wds', 'ap', 'ap-wds']:
632 ifacedump['wlanmode'] = "ap"
633 # Default to 802.11b mode
634 ifacedump['mode'] = '11b'
635 if ifacedump['type'] in ['11a', '11b' '11g']:
636 ifacedump['mode'] = ifacedump['type']
637
638 if not ifacedump.has_key('channel'):
639 if ifacedump['type'] == '11a':
640 ifacedump['channel'] = 36
641 else:
642 ifacedump['channel'] = 1
643
644 # Allow special hacks at the back like wds and stuff
645 if not ifacedump.has_key('extra'):
646 ifacedump['extra'] = 'regdomain ETSI country NL'
647
648 output += "wlans_%(interface)s='%(autogen_ifname)s'\n" % ifacedump
649 output += ("create_args_%(autogen_ifname)s='wlanmode %(wlanmode)s mode " +\
650 "%(mode)s ssid %(ssid)s %(extra)s channel %(channel)s'\n") % ifacedump
651
652 elif ifacedump['type'] in ['ethernet', 'eth']:
653 # No special config needed besides IP
654 pass
655 else:
656 assert False, "Unknown type " + ifacedump['type']
657
658 # Print IP address which needs to be assigned over here
659 output += "\n"
660 for iface,addrs in sorted(addrs_list.iteritems()):
661 for addr, comment in sorted(addrs,key=lambda x: parseaddr(x[0].split('/')[0])):
662 output += "# %s || %s || %s\n" % (iface, addr, comment)
663
664 # Write DHCLIENT entry
665 if dhclient_if[iface]:
666 output += "ifconfig_%s='SYNCDHCP'\n\n" % (iface)
667 else:
668 output += "ipv4_addrs_%s='%s'\n\n" % (iface, " ".join([x[0] for x in addrs]))
669
670 return output
671
672
673
674
675def get_all_configs():
676 """ Get dict with key 'host' with all configs present """
677 configs = dict()
678 for host in get_hostlist():
679 datadump = get_yaml(host)
680 configs[host] = datadump
681 return configs
682
683
684def get_interface_keys(config):
685 """ Quick hack to get all interface keys, later stage convert this to a iterator """
686 return sorted([elem for elem in config.keys() if (elem.startswith('iface_') and not "lo0" in elem)])
687
688
689def get_used_ips(configs):
690 """ Return array of all IPs used in config files"""
691 ip_list = []
692 for config in configs:
693 ip_list.append(config['masterip'])
694 for iface_key in get_interface_keys(config):
695 l = config[iface_key]['ip']
696 addr, mask = l.split('/')
697 # Special case do not process
698 if valid_addr(addr):
699 ip_list.append(addr)
700 else:
701 logger.error("## IP '%s' in '%s' not valid" % (addr, config['nodename']))
702 return sorted(ip_list)
703
704
705
706def generate_resolv_conf(datadump):
707 """ Generate configuration file '/etc/resolv.conf' """
708 # XXX: This should properly going to be an datastructure soon
709 datadump['autogen_header'] = generate_header("#")
710 datadump['autogen_edge_nameservers'] = ''
711 for host in get_proxylist():
712 hostdump = get_yaml(host)
713 datadump['autogen_edge_nameservers'] += "nameserver %(masterip)-15s # %(autogen_realname)s\n" % hostdump
714 for host in get_hybridlist():
715 hostdump = get_yaml(host)
716 if hostdump['service_proxy_ileiden'] or hostdump['service_proxy_normal']:
717 datadump['autogen_edge_nameservers'] += "nameserver %(masterip)-15s # %(autogen_realname)s\n" % hostdump
718
719 return Template("""\
720{{ autogen_header }}
721search wleiden.net
722
723# Try local (cache) first
724nameserver 127.0.0.1
725
726{% if service_proxy_normal or service_proxy_ileiden or nodetype == 'Proxy' -%}
727nameserver 8.8.8.8 # Google Public NameServer
728nameserver 8.8.4.4 # Google Public NameServer
729{% else -%}
730# START DYNAMIC LIST - updated by /tools/nameserver-shuffle
731{{ autogen_edge_nameservers }}
732{% endif -%}
733""").render(datadump)
734
735
736
737def generate_ntp_conf(datadump):
738 """ Generate configuration file '/etc/ntp.conf' """
739 # XXX: This should properly going to be an datastructure soon
740
741 datadump['autogen_header'] = generate_header("#")
742 datadump['autogen_ntp_servers'] = ''
743 for host in get_proxylist():
744 hostdump = get_yaml(host)
745 datadump['autogen_ntp_servers'] += "server %(masterip)-15s iburst maxpoll 9 # %(autogen_realname)s\n" % hostdump
746 for host in get_hybridlist():
747 hostdump = get_yaml(host)
748 if hostdump['service_proxy_ileiden'] or hostdump['service_proxy_normal']:
749 datadump['autogen_ntp_servers'] += "server %(masterip)-15s iburst maxpoll 9 # %(autogen_realname)s\n" % hostdump
750
751 return Template("""\
752{{ autogen_header }}
753
754{% if service_proxy_normal or service_proxy_ileiden or nodetype == 'Proxy' -%}
755# Machine hooked to internet.
756server 0.nl.pool.ntp.org iburst maxpoll 9
757server 1.nl.pool.ntp.org iburst maxpoll 9
758server 2.nl.pool.ntp.org iburst maxpoll 9
759server 3.nl.pool.ntp.org iburst maxpoll 9
760{% else -%}
761# Local Wireless Leiden NTP Servers.
762server 0.pool.ntp.wleiden.net iburst maxpoll 9
763server 1.pool.ntp.wleiden.net iburst maxpoll 9
764server 2.pool.ntp.wleiden.net iburst maxpoll 9
765server 3.pool.ntp.wleiden.net iburst maxpoll 9
766
767# All the configured NTP servers
768{{ autogen_ntp_servers }}
769{% endif %}
770
771# If a server loses sync with all upstream servers, NTP clients
772# no longer follow that server. The local clock can be configured
773# to provide a time source when this happens, but it should usually
774# be configured on just one server on a network. For more details see
775# http://support.ntp.org/bin/view/Support/UndisciplinedLocalClock
776# The use of Orphan Mode may be preferable.
777#
778server 127.127.1.0
779fudge 127.127.1.0 stratum 10
780""").render(datadump)
781
782
783
784def generate_motd(datadump):
785 """ Generate configuration file '/etc/motd' """
786 output = Template("""\
787FreeBSD run ``service motd onestart'' to make me look normal
788
789 WWW: {{ autogen_fqdn }} - http://www.wirelessleiden.nl
790 Loc: {{ location }}
791
792Services:
793{% if board == "ALIX2" -%}
794 - Core Node ({{ board }})
795{% else -%}
796 - Hulp Node ({{ board }})
797{% endif -%}
798{% if service_proxy_normal -%}
799 - Normal Proxy
800{% endif -%}
801{% if service_proxy_ileiden -%}
802 - iLeiden Proxy
803{% endif %}
804Interlinks:\n
805""").render(datadump)
806
807 # XXX: This is a hacky way to get the required data
808 for line in generate_rc_conf_local(datadump).split('\n'):
809 if '||' in line and not line[1:].split()[0] in ['lo0', 'ath0'] :
810 output += " - %s \n" % line[1:]
811 output += """\
812Attached bridges:
813"""
814 for iface_key in datadump['autogen_iface_keys']:
815 ifacedump = datadump[iface_key]
816 if ifacedump.has_key('ns_ip'):
817 output += " - %(interface)s || %(mode)s || %(ns_ip)s\n" % ifacedump
818
819 return output
820
821
822def format_yaml_value(value):
823 """ Get yaml value in right syntax for outputting """
824 if isinstance(value,str):
825 output = '"%s"' % value
826 else:
827 output = value
828 return output
829
830
831
832def format_wleiden_yaml(datadump):
833 """ Special formatting to ensure it is editable"""
834 output = "# Genesis config yaml style\n"
835 output += "# vim:ts=2:et:sw=2:ai\n"
836 output += "#\n"
837 iface_keys = [elem for elem in datadump.keys() if elem.startswith('iface_')]
838 for key in sorted(set(datadump.keys()) - set(iface_keys)):
839 output += "%-10s: %s\n" % (key, format_yaml_value(datadump[key]))
840
841 output += "\n\n"
842
843 key_order = [ 'comment', 'interface', 'ip', 'desc', 'sdesc', 'mode', 'type',
844 'extra_type', 'channel', 'ssid', 'dhcp' ]
845
846 for iface_key in sorted(iface_keys):
847 output += "%s:\n" % iface_key
848 for key in key_order + list(sorted(set(datadump[iface_key].keys()) - set(key_order))):
849 if datadump[iface_key].has_key(key):
850 output += " %-11s: %s\n" % (key, format_yaml_value(datadump[iface_key][key]))
851 output += "\n\n"
852
853 return output
854
855
856
857def generate_wleiden_yaml(datadump, header=True):
858 """ Generate (petty) version of wleiden.yaml"""
859 for key in datadump.keys():
860 if key.startswith('autogen_'):
861 del datadump[key]
862 # Interface autogen cleanups
863 elif type(datadump[key]) == dict:
864 for key2 in datadump[key].keys():
865 if key2.startswith('autogen_'):
866 del datadump[key][key2]
867
868 output = generate_header("#") if header else ''
869 output += format_wleiden_yaml(datadump)
870 return output
871
872
873def generate_yaml(datadump):
874 return generate_config(datadump['nodename'], "wleiden.yaml", datadump)
875
876
877
878def generate_config(node, config, datadump=None):
879 """ Print configuration file 'config' of 'node' """
880 output = ""
881 try:
882 # Load config file
883 if datadump == None:
884 datadump = get_yaml(node)
885
886 if config == 'wleiden.yaml':
887 output += generate_wleiden_yaml(datadump)
888 elif config == 'authorized_keys':
889 f = open(os.path.join(NODE_DIR,"global_keys"), 'r')
890 output += f.read()
891 f.close()
892 elif config == 'dnsmasq.conf':
893 output += generate_dnsmasq_conf(datadump)
894 elif config == 'dhcpd.conf':
895 output += generate_dhcpd_conf(datadump)
896 elif config == 'rc.conf.local':
897 output += generate_rc_conf_local(datadump)
898 elif config == 'resolv.conf':
899 output += generate_resolv_conf(datadump)
900 elif config == 'ntp.conf':
901 output += generate_ntp_conf(datadump)
902 elif config == 'motd':
903 output += generate_motd(datadump)
904 else:
905 assert False, "Config not found!"
906 except IOError, e:
907 output += "[ERROR] Config file not found"
908 return output
909
910
911
912def process_cgi_request():
913 """ When calling from CGI """
914 # Update repository if requested
915 form = cgi.FieldStorage()
916 if form.getvalue("action") == "update":
917 print "Refresh: 5; url=."
918 print "Content-type:text/plain\r\n\r\n",
919 print "[INFO] Updating subverion, please wait..."
920 print subprocess.Popen(['svn', 'cleanup', "%s/.." % NODE_DIR], stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0],
921 print subprocess.Popen(['svn', 'up', "%s/.." % NODE_DIR], stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0],
922 print "[INFO] All done, redirecting in 5 seconds"
923 sys.exit(0)
924
925
926 base_uri = os.environ['PATH_INFO']
927 uri = base_uri.strip('/').split('/')
928
929 output = "Template Holder"
930 content_type='text/plain'
931 if base_uri.endswith('/create/network.kml'):
932 content_type='application/vnd.google-earth.kml+xml'
933 output = make_network_kml.make_graph()
934 elif not uri[0]:
935 if is_text_request():
936 content_type = 'text/plain'
937 output = '\n'.join(get_hostlist())
938 else:
939 content_type = 'text/html'
940 output = generate_title(get_hostlist())
941 elif len(uri) == 1:
942 if is_text_request():
943 content_type = 'text/plain'
944 output = generate_node(uri[0])
945 else:
946 content_type = 'text/html'
947 output = generate_node_overview(uri[0])
948 elif len(uri) == 2:
949 content_type = 'text/plain'
950 output = generate_config(uri[0], uri[1])
951 else:
952 assert False, "Invalid option"
953
954 print "Content-Type: %s" % content_type
955 print "Content-Length: %s" % len(output)
956 print ""
957 print output
958
959def get_realname(datadump):
960 # Proxy naming convention is special, as the proxy name is also included in
961 # the nodename, when it comes to the numbered proxies.
962 if datadump['nodetype'] == 'Proxy':
963 realname = datadump['nodetype'] + datadump['nodename'].replace('proxy','')
964 else:
965 # By default the full name is listed and also a shortname CNAME for easy use.
966 realname = datadump['nodetype'] + datadump['nodename']
967 return(realname)
968
969
970
971def make_dns(output_dir = 'dns', external = False):
972 items = dict()
973
974 # hostname is key, IP is value
975 wleiden_zone = defaultdict(list)
976 wleiden_cname = dict()
977
978 pool = dict()
979 for node in get_hostlist():
980 datadump = get_yaml(node)
981
982 # Proxy naming convention is special
983 fqdn = datadump['autogen_realname']
984 if datadump['nodetype'] in ['CNode', 'Hybrid']:
985 wleiden_cname[datadump['nodename']] = fqdn
986
987 wleiden_zone[fqdn].append((datadump['masterip'], True))
988
989 # Hacking to get proper DHCP IPs and hostnames
990 for iface_key in get_interface_keys(datadump):
991 iface_name = datadump[iface_key]['interface'].replace(':',"-alias-")
992 (ip, cidr) = datadump[iface_key]['ip'].split('/')
993 try:
994 (dhcp_start, dhcp_stop) = datadump[iface_key]['dhcp'].split('-')
995 datadump[iface_key]['netmask'] = cidr2netmask(cidr)
996 dhcp_part = ".".join(ip.split('.')[0:3])
997 if ip != datadump['masterip']:
998 wleiden_zone["dhcp-gateway-%s.%s" % (iface_name, fqdn)].append((ip, False))
999 for i in range(int(dhcp_start), int(dhcp_stop) + 1):
1000 wleiden_zone["dhcp-%s-%s.%s" % (i, iface_name, fqdn)].append(("%s.%s" % (dhcp_part, i), True))
1001 except (AttributeError, ValueError):
1002 # First push it into a pool, to indentify the counter-part later on
1003 addr = parseaddr(ip)
1004 cidr = int(cidr)
1005 addr = addr & ~((1 << (32 - cidr)) - 1)
1006 if pool.has_key(addr):
1007 pool[addr] += [(iface_name, fqdn, ip)]
1008 else:
1009 pool[addr] = [(iface_name, fqdn, ip)]
1010 continue
1011
1012
1013 def pool_to_name(fqdn, pool_members):
1014 """Convert the joined name to a usable pool name"""
1015
1016 def isplit(item):
1017 (prefix, name, number) = re.match('^(cnode|hybrid|proxy)([a-z]+)([0-9]*)$',item.lower()).group(1,2,3)
1018 return (prefix, name, number)
1019
1020 my_name = isplit(fqdn.split('.')[0])[1]
1021
1022 short_names = defaultdict(list)
1023 for node in sorted(pool_members):
1024 (prefix, name, number) = isplit(node)
1025 short_names[name].append((prefix,number))
1026
1027 return '-'.join(sorted(short_names.keys()))
1028
1029
1030 # WL uses an /29 to configure an interface. IP's are ordered like this:
1031 # MasterA (.1) -- DeviceA (.2) <<>> DeviceB (.3) --- SlaveB (.4)
1032
1033 sn = lambda x: re.sub(r'(?i)^cnode','',x)
1034
1035 # Automatic naming convention of interlinks namely 2 + remote.lower()
1036 for (key,value) in pool.iteritems():
1037 # Make sure they are sorted from low-ip to high-ip
1038 value = sorted(value, key=lambda x: parseaddr(x[2]))
1039
1040 if len(value) == 1:
1041 (iface_name, fqdn, ip) = value[0]
1042 wleiden_zone["2unused-%s.%s" % (iface_name, fqdn)].append((ip, True))
1043
1044 # Device DNS names
1045 if 'cnode' in fqdn.lower():
1046 wleiden_zone["d-at-%s.%s" % (iface_name, fqdn)].append((showaddr(parseaddr(ip) + 1), False))
1047 wleiden_cname["d-at-%s.%s" % (iface_name,sn(fqdn))] = "d-at-%s.%s" % ((iface_name, fqdn))
1048
1049 elif len(value) == 2:
1050 (a_iface_name, a_fqdn, a_ip) = value[0]
1051 (b_iface_name, b_fqdn, b_ip) = value[1]
1052 wleiden_zone["2%s.%s" % (b_fqdn,a_fqdn)].append((a_ip, True))
1053 wleiden_zone["2%s.%s" % (a_fqdn,b_fqdn)].append((b_ip, True))
1054
1055 # Device DNS names
1056 if 'cnode' in a_fqdn.lower() and 'cnode' in b_fqdn.lower():
1057 wleiden_zone["d-at-%s.%s" % (a_iface_name, a_fqdn)].append((showaddr(parseaddr(a_ip) + 1), False))
1058 wleiden_zone["d-at-%s.%s" % (b_iface_name, b_fqdn)].append((showaddr(parseaddr(b_ip) - 1), False))
1059 wleiden_cname["d-at-%s.%s" % (a_iface_name,sn(a_fqdn))] = "d-at-%s.%s" % (a_iface_name, a_fqdn)
1060 wleiden_cname["d-at-%s.%s" % (b_iface_name,sn(b_fqdn))] = "d-at-%s.%s" % (b_iface_name, b_fqdn)
1061 wleiden_cname["d2%s.%s" % (sn(b_fqdn),sn(a_fqdn))] = "d-at-%s.%s" % (a_iface_name, a_fqdn)
1062 wleiden_cname["d2%s.%s" % (sn(a_fqdn),sn(b_fqdn))] = "d-at-%s.%s" % (b_iface_name, b_fqdn)
1063
1064 else:
1065 pool_members = [k[1] for k in value]
1066 for item in value:
1067 (iface_name, fqdn, ip) = item
1068 pool_name = "2pool-" + pool_to_name(fqdn,pool_members)
1069 wleiden_zone["%s.%s" % (pool_name, fqdn)].append((ip, True))
1070
1071 # Include static DNS entries
1072 # XXX: Should they override the autogenerated results?
1073 # XXX: Convert input to yaml more useable.
1074 # Format:
1075 ##; this is a comment
1076 ## roomburgh=CNodeRoomburgh1
1077 ## apkerk1.CNodeVosko=172.17.176.8 ;this as well
1078 dns_list = yaml.load(open(os.path.join(NODE_DIR,'../dns/staticDNS.yaml'),'r'))
1079
1080 # Hack to allow special entries, for development
1081 wleiden_raw = {}
1082
1083 for line in dns_list:
1084 reverse = False
1085 k, items = line.items()[0]
1086 if type(items) == dict:
1087 if items.has_key('reverse'):
1088 reverse = items['reverse']
1089 items = items['a']
1090 else:
1091 items = items['cname']
1092 items = [items] if type(items) != list else items
1093 for item in items:
1094 if item.startswith('IN '):
1095 wleiden_raw[k] = item
1096 elif valid_addr(item):
1097 wleiden_zone[k].append((item, reverse))
1098 else:
1099 wleiden_cname[k] = item
1100
1101 details = dict()
1102 # 24 updates a day allowed
1103 details['serial'] = time.strftime('%Y%m%d%H')
1104
1105 if external:
1106 dns_masters = ['siteview.wirelessleiden.nl', 'ns1.vanderzwet.net']
1107 else:
1108 dns_masters = ['sunny.wleiden.net']
1109
1110 details['master'] = dns_masters[0]
1111 details['ns_servers'] = '\n'.join(['\tNS\t%s.' % x for x in dns_masters])
1112
1113 dns_header = '''
1114$TTL 3h
1115%(zone)s. SOA %(master)s. beheer.lijst.wirelessleiden.nl. ( %(serial)s 1d 12h 1w 60s )
1116 ; Serial, Refresh, Retry, Expire, Neg. cache TTL
1117
1118%(ns_servers)s
1119 \n'''
1120
1121
1122 if not os.path.isdir(output_dir):
1123 os.makedirs(output_dir)
1124 details['zone'] = 'wleiden.net'
1125 f = open(os.path.join(output_dir,"db." + details['zone']), "w")
1126 f.write(dns_header % details)
1127
1128 for host,items in wleiden_zone.iteritems():
1129 for ip,reverse in items:
1130 if valid_addr(ip):
1131 f.write("%s.wleiden.net. IN A %s \n" % (host.lower(), ip))
1132 for source,dest in wleiden_cname.iteritems():
1133 f.write("%s.wleiden.net. IN CNAME %s.wleiden.net.\n" % (source.lower(), dest.lower()))
1134 for source, dest in wleiden_raw.iteritems():
1135 f.write("%s.wleiden.net. %s\n" % (source, dest))
1136 f.close()
1137
1138 # Create whole bunch of specific sub arpa zones. To keep it compliant
1139 for s in range(16,32):
1140 details['zone'] = '%i.172.in-addr.arpa' % s
1141 f = open(os.path.join(output_dir,"db." + details['zone']), "w")
1142 f.write(dns_header % details)
1143
1144 #XXX: Not effient, fix to proper data structure and do checks at other
1145 # stages
1146 for host,items in wleiden_zone.iteritems():
1147 for ip,reverse in items:
1148 if not reverse:
1149 continue
1150 if valid_addr(ip):
1151 if valid_addr(ip):
1152 if int(ip.split('.')[1]) == s:
1153 rev_ip = '.'.join(reversed(ip.split('.')))
1154 f.write("%s.in-addr.arpa. IN PTR %s.wleiden.net.\n" % (rev_ip.lower(), host.lower()))
1155 f.close()
1156
1157
1158def usage():
1159 print """Usage: %(prog)s <argument>
1160Argument:
1161\tstandalone [port] = Run configurator webserver [8000]
1162\tdns [outputdir] = Generate BIND compliant zone files in dns [./dns]
1163\tfull-export = Generate yaml export script for heatmap.
1164\tstatic [outputdir] = Generate all config files and store on disk
1165\t with format ./<outputdir>/%%NODE%%/%%FILE%% [./static]
1166\ttest <node> <file> = Receive output of CGI script.
1167\tlist <status> <items> = List systems which have certain status
1168
1169Arguments:
1170\t<node> = NodeName (example: HybridRick)
1171\t<file> = %(files)s
1172\t<status> = all|up|down|planned
1173\t<items> = systems|nodes|proxies
1174
1175NOTE FOR DEVELOPERS; you can test your changes like this:
1176 BEFORE any changes in this code:
1177 $ ./gformat.py static /tmp/pre
1178 AFTER the changes:
1179 $ ./gformat.py static /tmp/post
1180 VIEW differences and VERIFY all are OK:
1181 $ diff -urI 'Generated' -r /tmp/pre /tmp/post
1182""" % { 'prog' : sys.argv[0], 'files' : '|'.join(files) }
1183 exit(0)
1184
1185
1186def is_text_request():
1187 """ Find out whether we are calling from the CLI or any text based CLI utility """
1188 try:
1189 return os.environ['HTTP_USER_AGENT'].split()[0] in ['curl', 'fetch', 'wget']
1190 except KeyError:
1191 return True
1192
1193def switchFormat(setting):
1194 if setting:
1195 return "YES"
1196 else:
1197 return "NO"
1198
1199def main():
1200 """Hard working sub"""
1201 # Allow easy hacking using the CLI
1202 if not os.environ.has_key('PATH_INFO'):
1203 if len(sys.argv) < 2:
1204 usage()
1205
1206 if sys.argv[1] == "standalone":
1207 import SocketServer
1208 import CGIHTTPServer
1209 # Hop to the right working directory.
1210 os.chdir(os.path.dirname(__file__))
1211 try:
1212 PORT = int(sys.argv[2])
1213 except (IndexError,ValueError):
1214 PORT = 8000
1215
1216 class MyCGIHTTPRequestHandler(CGIHTTPServer.CGIHTTPRequestHandler):
1217 """ Serve this CGI from the root of the webserver """
1218 def is_cgi(self):
1219 if "favicon" in self.path:
1220 return False
1221
1222 self.cgi_info = (os.path.basename(__file__), self.path)
1223 self.path = ''
1224 return True
1225 handler = MyCGIHTTPRequestHandler
1226 SocketServer.TCPServer.allow_reuse_address = True
1227 httpd = SocketServer.TCPServer(("", PORT), handler)
1228 httpd.server_name = 'localhost'
1229 httpd.server_port = PORT
1230
1231 logger.info("serving at port %s", PORT)
1232 try:
1233 httpd.serve_forever()
1234 except KeyboardInterrupt:
1235 httpd.shutdown()
1236 logger.info("All done goodbye")
1237 elif sys.argv[1] == "test":
1238 os.environ['PATH_INFO'] = "/".join(sys.argv[2:])
1239 os.environ['SCRIPT_NAME'] = __file__
1240 process_cgi_request()
1241 elif sys.argv[1] == "static":
1242 items = dict()
1243 items['output_dir'] = sys.argv[2] if len(sys.argv) > 2 else "./static"
1244 for node in get_hostlist():
1245 items['node'] = node
1246 items['wdir'] = "%(output_dir)s/%(node)s" % items
1247 if not os.path.isdir(items['wdir']):
1248 os.makedirs(items['wdir'])
1249 datadump = get_yaml(node)
1250 for config in files:
1251 items['config'] = config
1252 logger.info("## Generating %(node)s %(config)s" % items)
1253 f = open("%(wdir)s/%(config)s" % items, "w")
1254 f.write(generate_config(node, config, datadump))
1255 f.close()
1256 elif sys.argv[1] == "wind-export":
1257 items = dict()
1258 for node in get_hostlist():
1259 datadump = get_yaml(node)
1260 sql = """INSERT IGNORE INTO nodes (name, name_ns, longitude, latitude)
1261 VALUES ('%(nodename)s', '%(nodename)s', %(latitude)s, %(longitude)s);""" % datadump;
1262 sql = """INSERT IGNORE INTO users_nodes (user_id, node_id, owner)
1263 VALUES (
1264 (SELECT id FROM users WHERE username = 'rvdzwet'),
1265 (SELECT id FROM nodes WHERE name = '%(nodename)s'),
1266 'Y');""" % datadump
1267 #for config in files:
1268 # items['config'] = config
1269 # print "## Generating %(node)s %(config)s" % items
1270 # f = open("%(wdir)s/%(config)s" % items, "w")
1271 # f.write(generate_config(node, config, datadump))
1272 # f.close()
1273 for node in get_hostlist():
1274 datadump = get_yaml(node)
1275 for iface_key in sorted([elem for elem in datadump.keys() if elem.startswith('iface_')]):
1276 ifacedump = datadump[iface_key]
1277 if ifacedump.has_key('mode') and ifacedump['mode'] == 'ap-wds':
1278 ifacedump['nodename'] = datadump['nodename']
1279 if not ifacedump.has_key('channel') or not ifacedump['channel']:
1280 ifacedump['channel'] = 0
1281 sql = """INSERT INTO links (node_id, type, ssid, protocol, channel, status)
1282 VALUES ((SELECT id FROM nodes WHERE name = '%(nodename)s'), 'ap',
1283 '%(ssid)s', 'IEEE 802.11b', %(channel)s, 'active');""" % ifacedump
1284 elif sys.argv[1] == "full-export":
1285 hosts = {}
1286 for node in get_hostlist():
1287 datadump = get_yaml(node)
1288 hosts[datadump['nodename']] = datadump
1289 print yaml.dump(hosts)
1290
1291 elif sys.argv[1] == "dns":
1292 make_dns(sys.argv[2] if len(sys.argv) > 2 else 'dns', 'external' in sys.argv)
1293 elif sys.argv[1] == "cleanup":
1294 # First generate all datadumps
1295 datadumps = dict()
1296 for host in get_hostlist():
1297 logger.info("# Processing: %s", host)
1298 # Set some boring default values
1299 datadump = { 'board' : 'UNKNOWN' }
1300 datadump.update(get_yaml(host))
1301 datadumps[datadump['autogen_realname']] = datadump
1302
1303
1304 for host,datadump in datadumps.iteritems():
1305 # Convert all yes and no to boolean values
1306 def fix_boolean(dump):
1307 for key in dump.keys():
1308 if type(dump[key]) == dict:
1309 dump[key] = fix_boolean(dump[key])
1310 elif str(dump[key]).lower() in ["yes", "true"]:
1311 dump[key] = True
1312 elif str(dump[key]).lower() in ["no", "false"]:
1313 # Compass richting no (Noord Oost) is valid input
1314 if key != "compass": dump[key] = False
1315 return dump
1316 datadump = fix_boolean(datadump)
1317
1318 if datadump['rdnap_x'] and datadump['rdnap_y']:
1319 datadump['latitude'], datadump['longitude'] = rdnap.rd2etrs(datadump['rdnap_x'], datadump['rdnap_y'])
1320 elif datadump['latitude'] and datadump['longitude']:
1321 datadump['rdnap_x'], datadump['rdnap_y'] = rdnap.etrs2rd(datadump['latitude'], datadump['longitude'])
1322
1323 if datadump['nodename'].startswith('Proxy'):
1324 datadump['nodename'] = datadump['nodename'].lower()
1325
1326 for iface_key in datadump['autogen_iface_keys']:
1327 # Wireless Leiden SSID have an consistent lowercase/uppercase
1328 if datadump[iface_key].has_key('ssid'):
1329 ssid = datadump[iface_key]['ssid']
1330 prefix = 'ap-WirelessLeiden-'
1331 if ssid.lower().startswith(prefix.lower()):
1332 datadump[iface_key]['ssid'] = prefix + ssid[len(prefix)].upper() + ssid[len(prefix) + 1:]
1333 if datadump[iface_key].has_key('ns_ip') and not datadump[iface_key].has_key('mode'):
1334 datadump[iface_key]['mode'] = 'autogen-FIXME'
1335 if not datadump[iface_key].has_key('desc'):
1336 datadump[iface_key]['desc'] = 'autogen-FIXME'
1337 store_yaml(datadump)
1338 elif sys.argv[1] == "list":
1339 use_fqdn = False
1340 if len(sys.argv) < 4 or not sys.argv[2] in ["up", "down", "planned", "all"]:
1341 usage()
1342 if sys.argv[3] == "nodes":
1343 systems = get_nodelist()
1344 elif sys.argv[3] == "proxies":
1345 systems = get_proxylist()
1346 elif sys.argv[3] == "systems":
1347 systems = get_hostlist()
1348 else:
1349 usage()
1350 if len(sys.argv) > 4:
1351 if sys.argv[4] == "fqdn":
1352 use_fqdn = True
1353 else:
1354 usage()
1355
1356 for system in systems:
1357 datadump = get_yaml(system)
1358
1359 output = datadump['autogen_fqdn'] if use_fqdn else system
1360 if sys.argv[2] == "all":
1361 print output
1362 elif datadump['status'] == sys.argv[2]:
1363 print output
1364 elif sys.argv[1] == "create":
1365 if sys.argv[2] == "network.kml":
1366 print make_network_kml.make_graph()
1367 else:
1368 usage()
1369 usage()
1370 else:
1371 # Do not enable debugging for config requests as it highly clutters the output
1372 if not is_text_request():
1373 cgitb.enable()
1374 process_cgi_request()
1375
1376
1377if __name__ == "__main__":
1378 main()
Note: See TracBrowser for help on using the repository browser.