source: genesis/tools/gformat.py@ 13960

Last change on this file since 13960 was 13940, checked in by rick, 7 years ago

Update nameservers, backup moved to TransIP

  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 78.6 KB
RevLine 
[13935]1#!/usr/local/bin/python
[8242]2#
3# vim:ts=2:et:sw=2:ai
4# Wireless Leiden configuration generator, based on yaml files'
[9957]5#
6# XXX: This should be rewritten to make use of the ipaddr.py library.
7#
[10058]8# Sample apache configuration (mind the AcceptPathInfo!)
[13935]9# Alias /config /usr/local/www/config
10# <Directory /usr/local/www/config>
11# AddHandler cgi-script .py
12# Require all granted
[11426]13#
[13935]14# RewriteEngine on
15# RewriteCond %{REQUEST_FILENAME} !-f
16# RewriteRule ^(.*)$ gformat.py/$1 [L,QSA]
17# Options +FollowSymlinks +ExecCGI
[11426]18# </Directory>
19#
[12473]20# Package dependencies list:
[13842]21# FreeBSD:
22# pkg install devel/py-Jinja2 graphics/py-pyproj devel/py-yaml lang/python
23# Fedora:
24# yum install python-yaml pyproj proj-epsg python-jinja2
[11426]25#
[8242]26# Rick van der Zwet <info@rickvanderzwet.nl>
[9957]27#
[8622]28
29# Hack to make the script directory is also threated as a module search path.
30import sys
31import os
32sys.path.append(os.path.dirname(__file__))
33
[12246]34SVN = filter(os.path.isfile, ('/usr/local/bin/svn', '/usr/bin/svn'))[0]
[12245]35
[12570]36import argparse
[8242]37import cgi
[8267]38import cgitb
39import copy
[8242]40import glob
[11426]41import make_network_kml
42import math
[12570]43import pyproj
[11738]44import random
45import re
[8242]46import socket
47import string
48import subprocess
[12570]49import textwrap
[8242]50import time
[11426]51import urlparse
[11738]52
[8584]53from pprint import pprint
[13680]54from collections import defaultdict, OrderedDict
[13276]55from sys import stderr
[8575]56try:
57 import yaml
58except ImportError, e:
59 print e
60 print "[ERROR] Please install the python-yaml or devel/py-yaml package"
61 exit(1)
[8588]62
63try:
64 from yaml import CLoader as Loader
65 from yaml import CDumper as Dumper
66except ImportError:
67 from yaml import Loader, Dumper
68
[10584]69from jinja2 import Environment, Template
70def yesorno(value):
71 return "YES" if bool(value) else "NO"
72env = Environment()
73env.filters['yesorno'] = yesorno
74def render_template(datadump, template):
75 result = env.from_string(template).render(datadump)
76 # Make it look pretty to the naked eye, as jinja templates are not so
77 # friendly when it comes to whitespace formatting
78 ## Remove extra whitespace at end of line lstrip() style.
79 result = re.sub(r'\n[\ ]+','\n', result)
80 ## Include only a single newline between an definition and a comment
81 result = re.sub(r'(["\'])\n+([a-z]|\n#\n)',r'\1\n\2', result)
82 ## Remove extra newlines after single comment
83 result = re.sub(r'(#\n)\n+([a-z])',r'\1\2', result)
84 return result
[10110]85
[9697]86import logging
87logging.basicConfig(format='# %(levelname)s: %(message)s' )
88logger = logging.getLogger()
89logger.setLevel(logging.DEBUG)
[8242]90
[9283]91
[8948]92if os.environ.has_key('CONFIGROOT'):
93 NODE_DIR = os.environ['CONFIGROOT']
94else:
[13935]95 NODE_DIR = os.path.abspath(os.path.dirname(os.path.realpath(__file__))) + '/../nodes'
[8242]96__version__ = '$Id: gformat.py 13940 2017-07-18 09:36:02Z rick $'
97
[13935]98CACHE_DIR = os.path.abspath(os.path.dirname(__file__))
99
[9283]100files = [
[8242]101 'authorized_keys',
102 'dnsmasq.conf',
[10410]103 'dhcpd.conf',
[8242]104 'rc.conf.local',
105 'resolv.conf',
[10069]106 'motd',
[10654]107 'ntp.conf',
[10705]108 'pf.hybrid.conf.local',
[13696]109 'unbound.wleiden.conf',
[10054]110 'wleiden.yaml',
[8242]111 ]
112
[8319]113# Global variables uses
[8323]114OK = 10
115DOWN = 20
116UNKNOWN = 90
[8257]117
[11426]118
[13680]119ileiden_proxies = OrderedDict()
[11503]120normal_proxies = []
[10860]121datadump_cache = {}
[11426]122interface_list_cache = {}
123rc_conf_local_cache = {}
[11503]124nameservers_cache = []
[13324]125relations_cache = None
[11533]126
[10887]127NO_DHCP = 0
128DHCP_CLIENT = 10
129DHCP_SERVER = 20
130def dhcp_type(item):
131 if not item.has_key('dhcp'):
132 return NO_DHCP
133 elif not item['dhcp']:
134 return NO_DHCP
135 elif item['dhcp'].lower() == 'client':
136 return DHCP_CLIENT
137 else:
[10889]138 # Validation Checks
139 begin,end = map(int,item['dhcp'].split('-'))
140 if begin >= end:
141 raise ValueError("DHCP Start >= DHCP End")
[10887]142 return DHCP_SERVER
143
[12473]144def etrs2rd(lat, lon):
145 p1 = pyproj.Proj(proj='latlon',datum='WGS84')
146 p2 = pyproj.Proj(init='EPSG:28992')
147 RDx, RDy = pyproj.transform(p1,p2,lon, lat)
148 return (RDx, RDy)
[10904]149
[12473]150def rd2etrs(RDx, RDy):
151 p1 = pyproj.Proj(init='EPSG:28992')
152 p2 = pyproj.Proj(proj='latlon',datum='WGS84')
153 lon, lat = pyproj.transform(p1,p2, RDx, RDy)
154 return (lat, lon)
[10904]155
156def get_yaml(item,add_version_info=True):
[10872]157 try:
158 """ Get configuration yaml for 'item'"""
159 if datadump_cache.has_key(item):
160 return datadump_cache[item].copy()
[10860]161
[10872]162 gfile = os.path.join(NODE_DIR,item,'wleiden.yaml')
[13861]163 global_rdr_file = os.path.join(NODE_DIR,'global_rdr_rules.yaml')
164 d = yaml.load(open(global_rdr_file, 'r'), Loader=Loader)
[8257]165
[10904]166 # Default values
167 datadump = {
168 'autogen_revision' : 'NOTFOUND',
169 'autogen_gfile' : gfile,
[13279]170 'service_proxy_ileiden' : False,
[10904]171 }
[10872]172 f = open(gfile, 'r')
173 datadump.update(yaml.load(f,Loader=Loader))
[13861]174 datadump['autogen_global_rdr_rules'] = d['global_rdr_rules']
[10872]175 if datadump['nodetype'] == 'Hybrid':
176 # Some values are defined implicitly
[13861]177 if datadump.has_key('rdr_host') and datadump['rdr_host'] and not datadump.has_key('service_incoming_rdr'):
[10872]178 datadump['service_incoming_rdr'] = True
179 # Use some boring defaults
180 defaults = {
181 'service_proxy_normal' : False,
182 'service_accesspoint' : True,
[11326]183 'service_incoming_rdr' : False,
[11538]184 'service_concentrator' : False,
[11326]185 'monitoring_group' : 'wleiden',
[10872]186 }
187 for (key,value) in defaults.iteritems():
188 if not datadump.has_key(key):
189 datadump[key] = value
190 f.close()
[10391]191
[10904]192 # Sometimes getting version information is useless or harmfull, like in the pre-commit hooks
193 if add_version_info:
[12245]194 p = subprocess.Popen([SVN, 'info', datadump['autogen_gfile']], stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
[11624]195 lines = p.communicate()[0].split('\n')
196 if p.returncode == 0:
197 for line in lines:
198 if line:
199 (key, value) = line.split(': ')
200 datadump["autogen_" + key.lower().replace(' ','_')] = value
[10904]201
[10872]202 # Preformat certain needed variables for formatting and push those into special object
203 datadump['autogen_iface_keys'] = get_interface_keys(datadump)
[10391]204
[10872]205 wlan_count=0
206 try:
[13328]207 for key in get_interface_keys(datadump, True):
[10890]208 datadump[key]['autogen_ifbase'] = key.split('_')[1]
[13403]209 datadump[key]['autogen_vlan'] = False
210
[13618]211 datadump[key]['autogen_bridge_member'] = datadump[key].has_key('parent')
212 datadump[key]['autogen_bridge'] = datadump[key]['autogen_ifbase'].startswith('bridge')
[13930]213 datadump[key]['autogen_bridge_alias'] = datadump[key]['autogen_ifbase'].startswith('bridge') and '_alias' in key
[13618]214
[13705]215 if datadump[key].has_key('parent'):
216 if datadump[key].has_key('ip'):
217 raise ValueError("Interface bridge member cannot have IP assigned")
218 if datadump[key].has_key('dhcp') and datadump[key]['dhcp'] != False:
219 raise ValueError("Interface bridge member cannot have DHCP set")
220
[13618]221 if datadump[key].has_key('ip'):
222 datadump[key]['autogen_gateway'] = datadump[key]['ip'].split('/')[0]
223
[10872]224 if datadump[key]['type'] in ['11a', '11b', '11g', 'wireless']:
225 datadump[key]['autogen_ifname'] = 'wlan%i' % wlan_count
[13618]226 datadump[key]['autogen_iface'] = 'wlan%i' % wlan_count
[13929]227 datadump[key]['autogen_if_dhcp'] = 'wlan%i' % wlan_count
[10872]228 wlan_count += 1
229 else:
[13929]230 datadump[key]['autogen_if_dhcp'] = datadump[key]['autogen_ifbase']
[13403]231 datadump[key]['autogen_ifname'] = '_'.join(key.split('_')[1:])
232 if len(key.split('_')) > 2 and key.split('_')[2].isdigit():
233 datadump[key]['autogen_vlan'] = key.split('_')[2]
[13618]234 datadump[key]['autogen_iface'] = '.'.join(key.split('_')[1:])
235 else:
236 datadump[key]['autogen_iface'] = '_'.join(key.split('_')[1:])
[13403]237
238 except Exception:
[10872]239 print "# Error while processing interface %s" % key
240 raise
[10391]241
[13929]242 dhcp_interfaces = [datadump[key]['autogen_if_dhcp'] for key in datadump['autogen_iface_keys'] \
[10887]243 if dhcp_type(datadump[key]) == DHCP_SERVER]
244
[13929]245 datadump['autogen_dhcp_interfaces'] = [x.replace('_','.') for x in set(dhcp_interfaces)]
[10872]246 datadump['autogen_item'] = item
[10391]247
[10872]248 datadump['autogen_domain'] = datadump['domain'] if datadump.has_key('domain') else 'wleiden.net.'
[13405]249 datadump['autogen_fqdn'] = datadump['nodename'] + '.' + datadump['autogen_domain']
[10872]250 datadump_cache[item] = datadump.copy()
[13403]251 except Exception:
[10872]252 print "# Error while processing %s" % item
253 raise
[10391]254 return datadump
255
256
257def store_yaml(datadump, header=False):
258 """ Store configuration yaml for 'item'"""
259 item = datadump['autogen_item']
260 gfile = os.path.join(NODE_DIR,item,'wleiden.yaml')
261
[10881]262 output = generate_wleiden_yaml(datadump, header)
263
[10391]264 f = open(gfile, 'w')
[10881]265 f.write(output)
[10391]266 f.close()
267
268
[10729]269def network(ip):
270 addr, mask = ip.split('/')
271 # Not parsing of these folks please
272 addr = parseaddr(addr)
273 mask = int(mask)
274 network = addr & ~((1 << (32 - mask)) - 1)
275 return network
276
[10391]277
[10729]278
[13324]279def make_relations():
[10270]280 """ Process _ALL_ yaml files to get connection relations """
[13324]281 global relations_cache
282
283 if relations_cache:
284 return relations_cache
285
[10729]286 errors = []
[10281]287 poel = defaultdict(list)
[10729]288
[13324]289 for host in get_hostlist():
290 datadump = get_yaml(host)
[10270]291 try:
[13328]292 for iface_key in get_interface_keys(datadump):
[13843]293 # Bridge members has no IP assigned
294 if 'parent' in datadump[iface_key] and not 'ip' in datadump[iface_key]:
295 continue
[10729]296 net_addr = network(datadump[iface_key]['ip'])
[13324]297 poel[net_addr] += [(host,datadump[iface_key].copy())]
[13843]298 except (KeyError, ValueError) as e:
299 errors.append("[FOUT] in '%s' interface '%s' (%s)" % (host,iface_key, type(e).__name__ + ': ' + str(e)))
[10270]300 continue
301
[13324]302 relations_cache = (poel, errors)
303 return relations_cache
[10270]304
[8267]305
[13324]306
[8321]307def valid_addr(addr):
308 """ Show which address is valid in which are not """
309 return str(addr).startswith('172.')
310
[8296]311def get_hostlist():
312 """ Combined hosts and proxy list"""
[13404]313 return sorted([os.path.basename(os.path.dirname(x)) for x in glob.glob("%s/*/wleiden.yaml" % (NODE_DIR))])
[8267]314
[8588]315def angle_between_points(lat1,lat2,long1,long2):
[9283]316 """
[8588]317 Return Angle in radians between two GPS coordinates
318 See: http://stackoverflow.com/questions/3809179/angle-between-2-gps-coordinates
319 """
320 dy = lat2 - lat1
[10729]321 dx = math.cos(lat1)*(long2 - long1)
[8588]322 angle = math.atan2(dy,dx)
323 return angle
[8267]324
[10729]325
326
[8588]327def angle_to_cd(angle):
328 """ Return Dutch Cardinal Direction estimation in 'one digit' of radian angle """
329
330 # For easy conversion get positive degree
331 degrees = math.degrees(angle)
[10729]332 abs_degrees = 360 + degrees if degrees < 0 else degrees
[8588]333
334 # Numbers can be confusing calculate from the 4 main directions
335 p = 22.5
[10729]336 if abs_degrees < p:
337 cd = "n"
338 elif abs_degrees < (90 - p):
339 cd = "no"
340 elif abs_degrees < (90 + p):
341 cd = "o"
342 elif abs_degrees < (180 - p):
343 cd = "zo"
344 elif abs_degrees < (180 + p):
345 cd = "z"
346 elif abs_degrees < (270 - p):
347 cd = "zw"
348 elif abs_degrees < (270 + p):
349 cd = "w"
350 elif abs_degrees < (360 - p):
351 cd = "nw"
[8588]352 else:
[10729]353 cd = "n"
354 return cd
[8588]355
356
[10729]357
358def cd_between_hosts(hostA, hostB, datadumps):
359 # Using RDNAP coordinates
360 dx = float(int(datadumps[hostA]['rdnap_x']) - int(datadumps[hostB]['rdnap_x'])) * -1
361 dy = float(int(datadumps[hostA]['rdnap_y']) - int(datadumps[hostB]['rdnap_y'])) * -1
362 return angle_to_cd(math.atan2(dx,dy))
363
364 # GPS coordinates seems to fail somehow
365 #latA = float(datadumps[hostA]['latitude'])
366 #latB = float(datadumps[hostB]['latitude'])
367 #lonA = float(datadumps[hostA]['longitude'])
368 #lonB = float(datadumps[hostB]['longitude'])
369 #return angle_to_cd(angle_between_points(latA, latB, lonA, lonB))
370
371
[8267]372def generate_title(nodelist):
[8257]373 """ Main overview page """
[9283]374 items = {'root' : "." }
[10682]375 def fl(spaces, line):
376 return (' ' * spaces) + line + '\n'
377
[8267]378 output = """
[8257]379<html>
380 <head>
381 <title>Wireless leiden Configurator - GFormat</title>
382 <style type="text/css">
383 th {background-color: #999999}
384 tr:nth-child(odd) {background-color: #cccccc}
385 tr:nth-child(even) {background-color: #ffffff}
386 th, td {padding: 0.1em 1em}
387 </style>
388 </head>
389 <body>
390 <center>
[8259]391 <form type="GET" action="%(root)s">
[8257]392 <input type="hidden" name="action" value="update">
393 <input type="submit" value="Update Configuration Database (SVN)">
394 </form>
395 <table>
[10682]396 <caption><h3>Wireless Leiden Configurator</h3></caption>
[8257]397 """ % items
[8242]398
[8296]399 for node in nodelist:
[8257]400 items['node'] = node
[10682]401 output += fl(5, '<tr>') + fl(7,'<td><a href="%(root)s/%(node)s">%(node)s</a></td>' % items)
[8257]402 for config in files:
403 items['config'] = config
[10682]404 output += fl(7,'<td><a href="%(root)s/%(node)s/%(config)s">%(config)s</a></td>' % items)
405 output += fl(5, "</tr>")
[8267]406 output += """
[8257]407 </table>
408 <hr />
409 <em>%s</em>
410 </center>
411 </body>
412</html>
413 """ % __version__
[8242]414
[8267]415 return output
[8257]416
417
[8267]418
419def generate_node(node):
[8257]420 """ Print overview of all files available for node """
[8267]421 return "\n".join(files)
[8242]422
[13935]423def generate_node_overview(host, datadump=False):
[10270]424 """ Print overview of all files available for node """
[13935]425 if not datadump:
426 datadump = get_yaml(host)
[10270]427 params = { 'host' : host }
428 output = "<em><a href='..'>Back to overview</a></em><hr />"
429 output += "<h2>Available files:</h2><ul>"
430 for cf in files:
431 params['cf'] = cf
[13935]432 output += '<li><a href="%(cf)s">%(cf)s</a></li>\n' % params
[10270]433 output += "</ul>"
[8257]434
[10270]435 # Generate and connection listing
436 output += "<h2>Connected To:</h2><ul>"
[10281]437 (poel, errors) = make_relations()
438 for network, hosts in poel.iteritems():
439 if host in [x[0] for x in hosts]:
440 if len(hosts) == 1:
441 # Single not connected interface
442 continue
443 for remote,ifacedump in hosts:
444 if remote == host:
445 # This side of the interface
446 continue
447 params = { 'remote': remote, 'remote_ip' : ifacedump['ip'] }
[13935]448 output += '<li><a href="../%(remote)s">%(remote)s</a> -- %(remote_ip)s</li>\n' % params
[10270]449 output += "</ul>"
[10281]450 output += "<h2>MOTD details:</h2><pre>" + generate_motd(datadump) + "</pre>"
[8257]451
[10270]452 output += "<hr /><em><a href='..'>Back to overview</a></em>"
453 return output
454
455
[10904]456def generate_header(datadump, ctag="#"):
[8242]457 return """\
[9283]458%(ctag)s
[8242]459%(ctag)s DO NOT EDIT - Automatically generated by 'gformat'
[9283]460%(ctag)s
[10904]461""" % { 'ctag' : ctag, 'date' : time.ctime(), 'host' : socket.gethostname(), 'revision' : datadump['autogen_revision'] }
[8242]462
[8257]463
464
[8242]465def parseaddr(s):
[8257]466 """ Process IPv4 CIDR notation addr to a (binary) number """
[8242]467 f = s.split('.')
468 return (long(f[0]) << 24L) + \
469 (long(f[1]) << 16L) + \
470 (long(f[2]) << 8L) + \
471 long(f[3])
472
[8257]473
474
[8242]475def showaddr(a):
[8257]476 """ Display IPv4 addr in (dotted) CIDR notation """
[8242]477 return "%d.%d.%d.%d" % ((a >> 24) & 0xff, (a >> 16) & 0xff, (a >> 8) & 0xff, a & 0xff)
478
[8257]479
[8584]480def is_member(ip, mask, canidate):
481 """ Return True if canidate is part of ip/mask block"""
[10729]482 ip_addr = parseaddr(ip)
483 ip_canidate = parseaddr(canidate)
[8584]484 mask = int(mask)
485 ip_addr = ip_addr & ~((1 << (32 - mask)) - 1)
486 ip_canidate = ip_canidate & ~((1 << (32 - mask)) - 1)
487 return ip_addr == ip_canidate
[8257]488
[8584]489
490
[10410]491def cidr2netmask(netmask):
[8257]492 """ Given a 'netmask' return corresponding CIDR """
[8242]493 return showaddr(0xffffffff & (0xffffffff << (32 - int(netmask))))
494
[10410]495def get_network(addr, mask):
496 return showaddr(parseaddr(addr) & ~((1 << (32 - int(mask))) - 1))
[8257]497
498
[10410]499def generate_dhcpd_conf(datadump):
500 """ Generate config file '/usr/local/etc/dhcpd.conf """
[13761]501 # Redundency support, in cause local DNS server is not running/responding.
[13861]502 datadump['autogen_backup_dns_servers'] = [x[1] for x in get_neighbours(datadump)]
[10904]503 output = generate_header(datadump)
[10410]504 output += Template("""\
505# option definitions common to all supported networks...
506option domain-name "dhcp.{{ autogen_fqdn }}";
507
508default-lease-time 600;
509max-lease-time 7200;
510
511# Use this to enble / disable dynamic dns updates globally.
512#ddns-update-style none;
513
514# If this DHCP server is the official DHCP server for the local
515# network, the authoritative directive should be uncommented.
516authoritative;
517
518# Use this to send dhcp log messages to a different log file (you also
519# have to hack syslog.conf to complete the redirection).
520log-facility local7;
521
[13906]522# UniFi Discovery Support
523option space ubnt;
524option ubnt.unifi-address code 1 = ip-address;
[10410]525#
[13906]526class "ubnt" {
527 match if substring (option vendor-class-identifier, 0, 4) = "ubnt";
528 option vendor-class-identifier "ubnt";
529 vendor-option-space ubnt;
530}
531
532#
[10410]533# Interface definitions
534#
[13524]535\n\n""").render(datadump)
[10410]536
[13524]537
538 # TODO: Use textwrap.fill instead
539 def indent(text, count):
540 return '\n'.join(map(lambda x: ' ' * count + x, text.split('\n')))
541
[13734]542 # Process DHCP blocks
[10734]543 dhcp_out = defaultdict(list)
[13328]544 for iface_key in get_interface_keys(datadump):
[13435]545 ifname = datadump[iface_key]['autogen_ifbase']
[13735]546 groupif = ifname if not datadump[iface_key]['autogen_vlan'] else iface_key
[10410]547 if not datadump[iface_key].has_key('comment'):
[10455]548 datadump[iface_key]['comment'] = None
[10410]549
[13618]550 if not datadump[iface_key].has_key('ip'):
551 continue
[13503]552
[13735]553 dhcp_out[groupif].append("## %(autogen_iface)s - %(comment)s\n" % datadump[iface_key])
[13618]554
[10410]555 (addr, mask) = datadump[iface_key]['ip'].split('/')
[10882]556 datadump[iface_key]['autogen_addr'] = addr
557 datadump[iface_key]['autogen_netmask'] = cidr2netmask(mask)
558 datadump[iface_key]['autogen_subnet'] = get_network(addr, mask)
[13524]559
[10889]560 if dhcp_type(datadump[iface_key]) != DHCP_SERVER:
[13735]561 dhcp_out[groupif].append(textwrap.dedent("""\
[13524]562 subnet %(autogen_subnet)s netmask %(autogen_netmask)s {
563 ### not autoritive
564 }
565 """ % datadump[iface_key]))
[10410]566 continue
567
[10889]568 (dhcp_start, dhcp_stop) = datadump[iface_key]['dhcp'].split('-')
[10410]569 dhcp_part = ".".join(addr.split('.')[0:3])
[10882]570 datadump[iface_key]['autogen_dhcp_start'] = dhcp_part + "." + dhcp_start
571 datadump[iface_key]['autogen_dhcp_stop'] = dhcp_part + "." + dhcp_stop
[13861]572 datadump[iface_key]['autogen_dns_servers'] = ','.join([datadump[iface_key]['autogen_addr']] + datadump['autogen_backup_dns_servers'])
[12480]573
574 # Assume the first 10 IPs could be used for static entries
575 if 'no_portal' in datadump:
576 fixed = 5
577 for mac in datadump['no_portal']:
[13735]578 dhcp_out[groupif].append(textwrap.dedent("""\
[13618]579 host fixed-%(ifname)s-%(fixed)s {
580 hardware ethernet %(mac)s;
581 fixed-address %(prefix)s.%(fixed)s;
582 }
583 """ % { 'ifname' : ifname, 'mac' : mac, 'prefix': dhcp_part, 'fixed' : fixed }))
[12480]584 fixed += 1
585
[13883]586 if 'dhcp_fixed' in datadump[iface_key]:
587 for (mac,addr,host) in datadump[iface_key]['dhcp_fixed']:
588 dhcp_out[groupif].append(textwrap.dedent("""\
589 host fixed-%(host)s {
590 hardware ethernet %(mac)s;
591 fixed-address %(addr)s;
592 }
593 """ % { 'host' : host, 'mac' : mac, 'addr' : addr}))
594
595
[13735]596 dhcp_out[groupif].append(textwrap.dedent("""\
[13618]597 subnet %(autogen_subnet)s netmask %(autogen_netmask)s {
598 range %(autogen_dhcp_start)s %(autogen_dhcp_stop)s;
599 option routers %(autogen_addr)s;
[13861]600 option domain-name-servers %(autogen_dns_servers)s;
[13906]601 option ubnt.unifi-address 172.17.107.10;
[13618]602 }
603 """ % datadump[iface_key]))
[13524]604
[13734]605 # Output the blocks in groups
[13733]606 for ifname,value in sorted(dhcp_out.iteritems()):
[13618]607 if len(value) > 2:
[13568]608 output += ("shared-network %s {\n" % ifname) + indent(''.join(value), 2) + '\n}\n\n'
[13524]609 else:
610 output += ''.join(value) + "\n\n"
[10410]611 return output
612
613
614
[8242]615def generate_dnsmasq_conf(datadump):
[8257]616 """ Generate configuration file '/usr/local/etc/dnsmasq.conf' """
[10904]617 output = generate_header(datadump)
[10368]618 output += Template("""\
[9283]619# DHCP server options
[8242]620dhcp-authoritative
621dhcp-fqdn
[10391]622domain=dhcp.{{ autogen_fqdn }}
[8242]623domain-needed
624expand-hosts
[10120]625log-async=100
[8242]626
627# Low memory footprint
628cache-size=10000
629
[10368]630\n""").render(datadump)
631
[13328]632 for iface_key in get_interface_keys(datadump):
[8262]633 if not datadump[iface_key].has_key('comment'):
[10455]634 datadump[iface_key]['comment'] = None
[10890]635 output += "## %(autogen_ifname)s - %(comment)s\n" % datadump[iface_key]
[8242]636
[10889]637 if dhcp_type(datadump[iface_key]) != DHCP_SERVER:
[8242]638 output += "# not autoritive\n\n"
639 continue
640
[10889]641 (dhcp_start, dhcp_stop) = datadump[iface_key]['dhcp'].split('-')
642 (ip, cidr) = datadump[iface_key]['ip'].split('/')
643 datadump[iface_key]['autogen_netmask'] = cidr2netmask(cidr)
644
[8242]645 dhcp_part = ".".join(ip.split('.')[0:3])
[10882]646 datadump[iface_key]['autogen_dhcp_start'] = dhcp_part + "." + dhcp_start
647 datadump[iface_key]['autogen_dhcp_stop'] = dhcp_part + "." + dhcp_stop
[13618]648 output += "dhcp-range=%(autogen_iface)s,%(autogen_dhcp_start)s,%(autogen_dhcp_stop)s,%(autogen_netmask)s,24h\n\n" % datadump[iface_key]
[9283]649
[8242]650 return output
651
[8257]652
[13598]653class AutoVivification(dict):
654 """Implementation of perl's autovivification feature."""
655 def __getitem__(self, item):
656 try:
657 return dict.__getitem__(self, item)
658 except KeyError:
659 value = self[item] = type(self)()
660 return value
661
[10907]662def make_interface_list(datadump):
663 if interface_list_cache.has_key(datadump['autogen_item']):
664 return (interface_list_cache[datadump['autogen_item']])
665 # lo0 configuration:
666 # - 172.32.255.1/32 is the proxy.wleiden.net deflector
667 # - masterip is special as it needs to be assigned to at
668 # least one interface, so if not used assign to lo0
669 addrs_list = { 'lo0' : [("127.0.0.1/8", "LocalHost"), ("172.31.255.1/32","Proxy IP")] }
[13403]670 vlan_list = defaultdict(list)
[13618]671 bridge_list = defaultdict(list)
[13598]672 flags_if = AutoVivification()
[10907]673 dhclient_if = {'lo0' : False}
674
675 # XXX: Find some way of send this output nicely
676 output = ''
677
678 masterip_used = False
[13328]679 for iface_key in get_interface_keys(datadump):
[13618]680 if datadump[iface_key].has_key('ip') and datadump[iface_key]['ip'].startswith(datadump['masterip']):
[10907]681 masterip_used = True
682 break
683 if not masterip_used:
684 addrs_list['lo0'].append((datadump['masterip'] + "/32", 'Master IP Not used in interface'))
685
[13680]686 if 'serviceid' in datadump:
687 addrs_list['lo0'].append((datadump['serviceid'] + "/32", 'Lvrouted GW IP'))
688
[13328]689 for iface_key in get_interface_keys(datadump):
[10907]690 ifacedump = datadump[iface_key]
[13930]691 if ifacedump['autogen_bridge_alias']:
692 ifname = ifacedump['autogen_ifbase']
693 else:
694 ifname = ifacedump['autogen_ifname']
[10907]695
[13403]696 # If defined as vlan interface
697 if ifacedump['autogen_vlan']:
698 vlan_list[ifacedump['autogen_ifbase']].append(ifacedump['autogen_vlan'])
699
[13618]700 # If defined as bridge interface
701 if ifacedump['autogen_bridge_member']:
702 bridge_list[ifacedump['parent']].append(ifacedump['autogen_iface'])
703
[13930]704
[10907]705 # Flag dhclient is possible
[11739]706 if not dhclient_if.has_key(ifname) or dhclient_if[ifname] == False:
[11736]707 dhclient_if[ifname] = dhcp_type(ifacedump) == DHCP_CLIENT
[10907]708
[13598]709 # Ethernet address
710 if ifacedump.has_key('ether'):
711 flags_if[ifname]['ether'] = ifacedump['ether']
712
[10907]713 # Add interface IP to list
[13618]714 if ifacedump.has_key('ip'):
715 item = (ifacedump['ip'], ifacedump['comment'])
716 if addrs_list.has_key(ifname):
717 addrs_list[ifname].append(item)
718 else:
719 addrs_list[ifname] = [item]
[10907]720
721 # Alias only needs IP assignment for now, this might change if we
722 # are going to use virtual accesspoints
723 if "alias" in iface_key:
724 continue
725
726 # XXX: Might want to deduct type directly from interface name
727 if ifacedump['type'] in ['11a', '11b', '11g', 'wireless']:
728 # Default to station (client) mode
729 ifacedump['autogen_wlanmode'] = "sta"
730 if ifacedump['mode'] in ['master', 'master-wds', 'ap', 'ap-wds']:
731 ifacedump['autogen_wlanmode'] = "ap"
732
733 if not ifacedump.has_key('channel'):
734 if ifacedump['type'] == '11a':
735 ifacedump['channel'] = 36
736 else:
737 ifacedump['channel'] = 1
738
739 # Allow special hacks at the back like wds and stuff
740 if not ifacedump.has_key('extra'):
741 ifacedump['autogen_extra'] = 'regdomain ETSI country NL'
742 else:
743 ifacedump['autogen_extra'] = ifacedump['extra']
744
[13564]745 ifacedump['autogen_ssid_hex'] = '0x' + ''.join(x.encode('hex') for x in ifacedump['ssid'])
746
[10907]747 output += "wlans_%(autogen_ifbase)s='%(autogen_ifname)s'\n" % ifacedump
[13564]748 output += "# SSID is encoded in Hexadecimal to support spaces, plain text value is '%(ssid)s'\n" % ifacedump
[13525]749 output += ("create_args_%(autogen_ifname)s=\"wlanmode %(autogen_wlanmode)s mode " +\
[13564]750 "%(type)s ssid %(autogen_ssid_hex)s %(autogen_extra)s channel %(channel)s\"\n") % ifacedump
[13169]751 output += "\n"
[10907]752
753 elif ifacedump['type'] in ['ethernet', 'eth']:
754 # No special config needed besides IP
[13618]755 pass
756 elif ifacedump['type'] in ['vlan']:
757 # VLAN member has no special configuration
758 pass
[10907]759 else:
760 assert False, "Unknown type " + ifacedump['type']
761
[13618]762 store = (addrs_list, vlan_list, bridge_list, dhclient_if, flags_if, output)
[10907]763 interface_list_cache[datadump['autogen_item']] = store
764 return(store)
765
766
767
[13696]768def create_proxies_list():
769 if not ileiden_proxies or not normal_proxies:
[13762]770 # Placeholder for to-be-installed proxies, this will avoid updating the all
771 # nodes to include this new machine, yet due to an unbound issue, this list
772 # has to be kept small.
773
774 for i in range(1,20):
[13696]775 ileiden_proxies['172.31.254.%i' % i] = {'nodename' : 'unused'}
776
777 for host in get_hostlist():
778 hostdump = get_yaml(host)
779 if hostdump['status'] == 'up':
780 if hostdump['service_proxy_ileiden']:
781 ileiden_proxies[hostdump['serviceid']] = hostdump
782 if hostdump['service_proxy_normal']:
783 normal_proxies.append(hostdump)
784
785
786
[8242]787def generate_rc_conf_local(datadump):
[8257]788 """ Generate configuration file '/etc/rc.conf.local' """
[10860]789 item = datadump['autogen_item']
790 if rc_conf_local_cache.has_key(item):
791 return rc_conf_local_cache[item]
792
[10455]793 if not datadump.has_key('ileiden'):
794 datadump['autogen_ileiden_enable'] = False
795 else:
796 datadump['autogen_ileiden_enable'] = datadump['ileiden']
[10110]797
[10547]798 datadump['autogen_ileiden_enable'] = switchFormat(datadump['autogen_ileiden_enable'])
799
[13696]800 create_proxies_list()
[10585]801 datadump['autogen_ileiden_proxies'] = ileiden_proxies
802 datadump['autogen_normal_proxies'] = normal_proxies
803 datadump['autogen_normal_proxies_ips'] = ','.join([x['masterip'] for x in normal_proxies])
[10367]804 datadump['autogen_normal_proxies_names'] = ','.join([x['autogen_item'] for x in normal_proxies])
[13336]805 datadump['autogen_attached_devices'] = [x[2] for x in get_attached_devices(datadump)]
806 datadump['autogen_neighbours'] = [x[1] for x in get_neighbours(datadump)]
[10112]807
[10904]808 output = generate_header(datadump, "#");
[10584]809 output += render_template(datadump, """\
[10391]810hostname='{{ autogen_fqdn }}'
[10110]811location='{{ location }}'
812nodetype="{{ nodetype }}"
[9283]813
[10459]814#
815# Configured listings
816#
817captive_portal_whitelist=""
818{% if nodetype == "Proxy" %}
[10054]819#
[10459]820# Proxy Configuration
[10054]821#
[13358]822{% if gateway and service_proxy_ileiden -%}
[10110]823defaultrouter="{{ gateway }}"
824{% else -%}
825#defaultrouter="NOTSET"
826{% endif -%}
827internalif="{{ internalif }}"
[10112]828ileiden_enable="{{ autogen_ileiden_enable }}"
829gateway_enable="{{ autogen_ileiden_enable }}"
[10238]830pf_enable="yes"
[10302]831pf_rules="/etc/pf.conf"
[10455]832{% if autogen_ileiden_enable -%}
[10234]833pf_flags="-D ext_if={{ externalif }} -D int_if={{ internalif }} -D publicnat={80,443}"
[10238]834lvrouted_enable="{{ autogen_ileiden_enable }}"
835lvrouted_flags="-u -s s00p3rs3kr3t -m 28"
836{% else -%}
837pf_flags="-D ext_if={{ externalif }} -D int_if={{ internalif }} -D publicnat={0}"
[10310]838{% endif -%}
[10238]839{% if internalroute -%}
840static_routes="wleiden"
841route_wleiden="-net 172.16.0.0/12 {{ internalroute }}"
[10110]842{% endif -%}
[10054]843
[10584]844{% elif nodetype == "Hybrid" %}
845 #
846 # Hybrid Configuration
847 #
[13305]848 list_ileiden_proxies="
[13680]849 {% for serviceid,item in autogen_ileiden_proxies.iteritems() -%}
850 {{ "%-16s"|format(serviceid) }} # {{ item.nodename }}
[13305]851 {% endfor -%}
852 "
853 list_normal_proxies="
854 {% for item in autogen_normal_proxies -%}
[13680]855 {{ "%-16s"|format(item.serviceid) }} # {{ item.nodename }}
[13305]856 {% endfor -%}
857 "
858
[13896]859 {% if autogen_dhcp_interfaces -%}
[13618]860 captive_portal_interfaces="{{ autogen_dhcp_interfaces|join(',') }}"
[13896]861 {% else %}
862 captive_portal_interfaces="dummy"
863 {% endif %}
[10584]864 externalif="{{ externalif|default('vr0', true) }}"
865 masterip="{{ masterip }}"
[13398]866
867 {% if gateway and service_proxy_ileiden %}
868 defaultrouter="{{ gateway }}"
869 {% else %}
870 #defaultrouter="NOTSET"
871 {% endif %}
[10584]872
[13398]873 #
[10584]874 # Defined services
[13398]875 #
[10584]876 service_proxy_ileiden="{{ service_proxy_ileiden|yesorno }}"
877 service_proxy_normal="{{ service_proxy_normal|yesorno }}"
878 service_accesspoint="{{ service_accesspoint|yesorno }}"
[10748]879 service_incoming_rdr="{{ service_incoming_rdr|yesorno }}"
[11538]880 service_concentrator="{{ service_concentrator|yesorno }}"
[10459]881
[11540]882 {% if service_proxy_ileiden %}
[10584]883 pf_rules="/etc/pf.hybrid.conf"
[11540]884 {% if service_concentrator %}
[11541]885 pf_flags="-D ext_if=$externalif -D ext_if_net=$externalif:network -D inet_if=tun0 -D inet_ip='(tun0)' -D masterip=$masterip"
[11540]886 {% else %}
887 pf_flags="-D ext_if=$externalif -D ext_if_net=$externalif:network -D inet_if=$externalif -D inet_ip='($externalif:0)' -D masterip=$masterip"
888 {% endif %}
[10587]889 pf_flags="$pf_flags -D publicnat=80,443"
[12247]890 lvrouted_flags="$lvrouted_flags -g"
[10748]891 {% elif service_proxy_normal or service_incoming_rdr %}
[10649]892 pf_rules="/etc/pf.hybrid.conf"
[10587]893 pf_flags="-D ext_if=$externalif -D ext_if_net=$externalif:network -D masterip=$masterip"
[10649]894 pf_flags="$pf_flags -D publicnat=0"
[13305]895 lvrouted_flags="$lvrouted_flags -z `make_list "$list_ileiden_proxies" ","`"
[10649]896 named_setfib="1"
897 tinyproxy_setfib="1"
898 dnsmasq_setfib="1"
[10698]899 sshd_setfib="1"
[10584]900 {% else %}
[10983]901 named_auto_forward_only="YES"
[10584]902 pf_rules="/etc/pf.node.conf"
[10587]903 pf_flags=""
[13305]904 lvrouted_flags="$lvrouted_flags -z `make_list "$list_ileiden_proxies" ","`"
[10584]905 {% endif %}
[11539]906 {% if service_concentrator %}
907 # Do mind installing certificates is NOT done automatically for security reasons
908 openvpn_enable="YES"
909 openvpn_configfile="/usr/local/etc/openvpn/client.conf"
910 {% endif %}
[10459]911
[10584]912 {% if service_proxy_normal %}
913 tinyproxy_enable="yes"
914 {% else %}
915 pen_wrapper_enable="yes"
916 {% endif %}
[10460]917
[10584]918 {% if service_accesspoint %}
919 pf_flags="$pf_flags -D captive_portal_interfaces=$captive_portal_interfaces"
920 {% endif %}
[10459]921
[13927]922 {% if board == "ALIX2" or board == "net4801" %}
[10584]923 #
924 # ''Fat'' configuration, board has 256MB RAM
925 #
926 dnsmasq_enable="NO"
927 named_enable="YES"
[13696]928 unbound_enable="YES"
[10732]929 {% if autogen_dhcp_interfaces -%}
[10584]930 dhcpd_enable="YES"
[13618]931 dhcpd_flags="$dhcpd_flags {{ autogen_dhcp_interfaces|join(' ') }}"
[10732]932 {% endif -%}
[13419]933 {% elif board == "apu1d" %}
934 #
935 # ''Fat'' configuration, board has 1024MB RAM
936 #
937 dnsmasq_enable="NO"
[13912]938 unbound_enable="YES"
[13419]939 {% if autogen_dhcp_interfaces -%}
940 dhcpd_enable="YES"
[13618]941 dhcpd_flags="$dhcpd_flags {{ autogen_dhcp_interfaces|join(' ') }}"
[13419]942 {% endif -%}
[10584]943 {% endif -%}
[10110]944{% endif %}
945
[10584]946#
[13336]947# Script variables
948#
949attached_devices="{{ autogen_attached_devices|join(' ') }}"
950neighbours="{{ autogen_neighbours|join(' ') }}"
951
952
953#
[10584]954# Interface definitions
955#\n
956""")
957
[13618]958 (addrs_list, vlan_list, bridge_list, dhclient_if, flags_if, extra_ouput) = make_interface_list(datadump)
[13673]959 for iface, vlans in sorted(vlan_list.items()):
960 output += 'vlans_%s="%s"\n' % (iface, ' '.join(sorted(set(vlans))))
[8242]961
[13420]962 # VLAN Parent interfaces not containing a configuration should be marked active explcitly.
[13673]963 for iface in sorted(vlan_list.keys()):
[13420]964 if not iface in addrs_list.keys():
965 output += "ifconfig_%s='up'\n" % iface
966
[13503]967 output += "\n"
968
[13618]969 # Bridge configuration:
970 if bridge_list.keys():
971 output += "cloned_interfaces='%s'\n" % ' '.join(bridge_list.keys())
972
973 for iface in bridge_list.keys():
[13930]974 output += "ifconfig_%s='%s up'\n" % (iface, ' '.join(['addm %(iface)s private %(iface)s' % {'iface': x} for x in bridge_list[iface]]))
[13618]975
976 # Bridge member interfaces not containing a configuration should be marked active explcitly.
977 for _,members in bridge_list.items():
978 for iface in members:
979 if not iface in addrs_list.keys():
[13676]980 output += "ifconfig_%s='up'\n" % iface.replace('.','_')
[13618]981
982 output += "\n"
983
[13403]984 # Details like SSID
985 if extra_ouput:
986 output += extra_ouput.strip() + "\n"
987
[9283]988 # Print IP address which needs to be assigned over here
[8242]989 output += "\n"
990 for iface,addrs in sorted(addrs_list.iteritems()):
[10079]991 for addr, comment in sorted(addrs,key=lambda x: parseaddr(x[0].split('/')[0])):
[9808]992 output += "# %s || %s || %s\n" % (iface, addr, comment)
[8242]993
[10366]994 # Write DHCLIENT entry
[13503]995 if iface in dhclient_if and dhclient_if[iface]:
[10366]996 output += "ifconfig_%s='SYNCDHCP'\n\n" % (iface)
[13565]997 continue
[11739]998
999 # Make sure the external address is always first as this is needed in the
1000 # firewall setup
1001 addrs = sorted(
1002 [x for x in addrs if not '0.0.0.0' in x[0]],
1003 key=lambda x: x[0].split('.')[0],
1004 cmp=lambda x,y: cmp(1 if x == '172' else 0, 1 if y == '172' else 0)
1005 )
[13504]1006
[13599]1007 idx_offset = 0
[13618]1008 # Set MAC is required
1009 if flags_if[iface].has_key('ether'):
1010 output += "ifconfig_%s='link %s'\n" % (iface, flags_if[iface]['ether'])
1011 output += "ifconfig_%s_alias0='inet %s'\n" % (iface, addrs[0][0])
1012 idx_offset += 1
[13930]1013 elif iface in bridge_list:
1014 output += "ifconfig_%s_alias0='inet %s'\n" % (iface, addrs[0][0])
1015 idx_offset += 1
[13618]1016 else:
1017 output += "ifconfig_%s='inet %s'\n" % (iface, addrs[0][0])
[13599]1018
[13403]1019 for idx, addr in enumerate(addrs[1:]):
[13599]1020 output += "ifconfig_%s_alias%s='inet %s'\n" % (iface, idx + idx_offset, addr[0])
[13618]1021
[13403]1022 output += "\n"
[10366]1023
[10860]1024 rc_conf_local_cache[datadump['autogen_item']] = output
[8242]1025 return output
1026
[8257]1027
1028
[8317]1029def get_all_configs():
1030 """ Get dict with key 'host' with all configs present """
1031 configs = dict()
1032 for host in get_hostlist():
1033 datadump = get_yaml(host)
1034 configs[host] = datadump
1035 return configs
1036
1037
[13328]1038def get_interface_keys(config, extra=False):
[8319]1039 """ Quick hack to get all interface keys, later stage convert this to a iterator """
[13328]1040 elems = sorted([elem for elem in config.keys() if (elem.startswith('iface_') and not "lo0" in elem)])
1041 if extra == False:
1042 return filter(lambda x: not "extra" in x, elems)
1043 else:
1044 return elems
[8317]1045
[8319]1046
[8317]1047def get_used_ips(configs):
1048 """ Return array of all IPs used in config files"""
1049 ip_list = []
[8319]1050 for config in configs:
[8317]1051 ip_list.append(config['masterip'])
[13680]1052 if 'serviceid' in config:
1053 ip_list.append(config['serviceid'])
[13328]1054 for iface_key in get_interface_keys(config, True):
[8317]1055 l = config[iface_key]['ip']
1056 addr, mask = l.split('/')
1057 # Special case do not process
[8332]1058 if valid_addr(addr):
1059 ip_list.append(addr)
1060 else:
[9728]1061 logger.error("## IP '%s' in '%s' not valid" % (addr, config['nodename']))
[8317]1062 return sorted(ip_list)
1063
1064
1065
[10980]1066def get_nameservers(max_servers=None):
[10934]1067 if nameservers_cache:
[10980]1068 return nameservers_cache[0:max_servers]
[10934]1069
[13404]1070 for host in get_hostlist():
[10935]1071 hostdump = get_yaml(host)
[10937]1072 if hostdump['status'] == 'up' and (hostdump['service_proxy_ileiden'] or hostdump['service_proxy_normal']):
[13916]1073 nameservers_cache.append((hostdump['serviceid'], hostdump['nodename']))
[10934]1074
[10980]1075 return nameservers_cache[0:max_servers]
[10934]1076
1077
[13336]1078def get_neighbours(datadump):
[13618]1079 (addrs_list, _, _, dhclient_if, _, extra_ouput) = make_interface_list(datadump)
[13336]1080
1081 (poel, errors) = make_relations()
1082 table = []
1083 for iface,addrs in sorted(addrs_list.iteritems()):
1084 if iface in ['lo0']:
1085 continue
1086
1087 for addr, comment in sorted(addrs,key=lambda x: parseaddr(x[0].split('/')[0])):
1088 if not addr.startswith('172.'):
1089 # Avoid listing internet connections as pool
1090 continue
1091 for neighbour in poel[network(addr)]:
1092 if neighbour[0] != datadump['autogen_item']:
[13618]1093 table.append((iface, neighbour[1]['ip'].split('/')[0], neighbour[0] + " (" + neighbour[1]['autogen_iface'] + ")", neighbour[1]['comment']))
[13336]1094 return table
1095
1096
1097def get_attached_devices(datadump, url=False):
1098 table = []
1099 for iface_key in get_interface_keys(datadump, True):
1100 ifacedump = datadump[iface_key]
1101
[13618]1102 if not ifacedump.has_key('ns_ip'):
1103 continue
1104
1105 x_ip = ifacedump['ns_ip'].split('/')[0]
1106
[13336]1107 if 'mode' in ifacedump:
1108 x_mode = ifacedump['mode']
1109 else:
1110 x_mode = 'unknown'
1111
1112 if 'bridge_type' in ifacedump:
1113 device_type = ifacedump['bridge_type']
1114 else:
1115 device_type = 'Unknown'
1116
[13618]1117 table.append((ifacedump['autogen_iface'], x_mode, 'http://%s' % x_ip if url else x_ip, device_type))
[13336]1118 return table
1119
1120
[8242]1121def generate_resolv_conf(datadump):
[8257]1122 """ Generate configuration file '/etc/resolv.conf' """
[10468]1123 # XXX: This should properly going to be an datastructure soon
[10904]1124 datadump['autogen_header'] = generate_header(datadump, "#")
[10468]1125 datadump['autogen_edge_nameservers'] = ''
1126
[10934]1127
[10936]1128 for masterip,realname in get_nameservers():
[10934]1129 datadump['autogen_edge_nameservers'] += "nameserver %-15s # %s\n" % (masterip, realname)
1130
[10468]1131 return Template("""\
1132{{ autogen_header }}
[8242]1133search wleiden.net
[10468]1134
1135# Try local (cache) first
[10209]1136nameserver 127.0.0.1
[10468]1137
[10584]1138{% if service_proxy_normal or service_proxy_ileiden or nodetype == 'Proxy' -%}
[10053]1139nameserver 8.8.8.8 # Google Public NameServer
[13726]1140nameserver 64.6.64.6 # Verisign Public NameServer
[10468]1141{% else -%}
[10646]1142# START DYNAMIC LIST - updated by /tools/nameserver-shuffle
[10468]1143{{ autogen_edge_nameservers }}
1144{% endif -%}
1145""").render(datadump)
[10209]1146
[9283]1147
[8242]1148
[10654]1149def generate_ntp_conf(datadump):
1150 """ Generate configuration file '/etc/ntp.conf' """
1151 # XXX: This should properly going to be an datastructure soon
1152
[10904]1153 datadump['autogen_header'] = generate_header(datadump, "#")
[10654]1154 datadump['autogen_ntp_servers'] = ''
[13404]1155 for host in get_hostlist():
[10654]1156 hostdump = get_yaml(host)
1157 if hostdump['service_proxy_ileiden'] or hostdump['service_proxy_normal']:
[13405]1158 datadump['autogen_ntp_servers'] += "server %(masterip)-15s iburst maxpoll 9 # %(nodename)s\n" % hostdump
[10654]1159
1160 return Template("""\
1161{{ autogen_header }}
1162
1163{% if service_proxy_normal or service_proxy_ileiden or nodetype == 'Proxy' -%}
1164# Machine hooked to internet.
1165server 0.nl.pool.ntp.org iburst maxpoll 9
1166server 1.nl.pool.ntp.org iburst maxpoll 9
1167server 2.nl.pool.ntp.org iburst maxpoll 9
1168server 3.nl.pool.ntp.org iburst maxpoll 9
1169{% else -%}
1170# Local Wireless Leiden NTP Servers.
1171server 0.pool.ntp.wleiden.net iburst maxpoll 9
1172server 1.pool.ntp.wleiden.net iburst maxpoll 9
1173server 2.pool.ntp.wleiden.net iburst maxpoll 9
1174server 3.pool.ntp.wleiden.net iburst maxpoll 9
1175
1176# All the configured NTP servers
1177{{ autogen_ntp_servers }}
1178{% endif %}
1179
1180# If a server loses sync with all upstream servers, NTP clients
1181# no longer follow that server. The local clock can be configured
1182# to provide a time source when this happens, but it should usually
1183# be configured on just one server on a network. For more details see
1184# http://support.ntp.org/bin/view/Support/UndisciplinedLocalClock
1185# The use of Orphan Mode may be preferable.
1186#
1187server 127.127.1.0
1188fudge 127.127.1.0 stratum 10
1189""").render(datadump)
1190
1191
[10705]1192def generate_pf_hybrid_conf_local(datadump):
1193 """ Generate configuration file '/etc/pf.hybrid.conf.local' """
[10904]1194 datadump['autogen_header'] = generate_header(datadump, "#")
[13861]1195 if datadump['service_incoming_rdr']:
1196 datadump['global_rdr_rules'] = datadump['autogen_global_rdr_rules']
[10705]1197 return Template("""\
1198{{ autogen_header }}
[10654]1199
[10705]1200# Redirect some internal facing services outside (7)
[13861]1201# INFO: {{ global_rdr_rules|count }} global_rdr_rules active on this node.
[13863]1202{% for protocol, src_port,dest_ip,dest_port,comment in global_rdr_rules -%}
[13886]1203rdr on $ext_if inet proto {{ protocol }} from any to $ext_if port {{ src_port }} tag SRV -> {{ "%-14s"|format(dest_ip) }} port {{ "%4s"|format(dest_port) }} # {{ comment }}
[13861]1204{% endfor -%}
1205# INFO: {{ rdr_rules|count }} node specific rdr_rules defined.
[13863]1206{% for protocol, src_port,dest_ip,dest_port,comment in rdr_rules -%}
[13886]1207rdr on $ext_if inet proto {{ protocol }} from any to $ext_if port {{ src_port }} tag SRV -> {{ "%-14s"|format(dest_ip) }} port {{ "%4s"|format(dest_port) }} # {{ comment }}
[10714]1208{% endfor -%}
[10705]1209""").render(datadump)
1210
[13696]1211def generate_unbound_wleiden_conf(datadump):
1212 """ Generate configuration file '/usr/local/etc/unbound.wleiden.conf' """
1213 datadump['autogen_header'] = generate_header(datadump, "#")
1214
1215 autogen_ips = []
1216 (addrs_list, _, _, dhclient_if, _, extra_ouput) = make_interface_list(datadump)
1217 for iface,addrs in sorted(addrs_list.iteritems()):
1218 for addr, comment in sorted(addrs,key=lambda x: parseaddr(x[0].split('/')[0])):
1219 if addr.startswith('172'):
1220 autogen_ips.append((addr.split('/')[0], comment))
1221 datadump['autogen_ips'] = autogen_ips
1222
1223 create_proxies_list()
1224 datadump['autogen_ileiden_proxies'] = ileiden_proxies
1225 return Template("""\
1226{{ autogen_header }}
1227
1228server:
[13911]1229 ## Static definitions fail to start on systems with broken ue(4) interfaces
[13696]1230{%- for ip,comment in autogen_ips %}
[13911]1231 # interface: {{ "%-16s"|format(ip) }} # {{ comment }}
[13696]1232{%- endfor %}
[13911]1233 ## Enabling wildcard matching as work-around
1234 interface: 0.0.0.0
1235 interface: ::0
[13696]1236
1237forward-zone:
1238 name: '.'
1239{%- if service_proxy_ileiden %}
1240 forward-addr: 8.8.8.8 # Google DNS A
1241 forward-addr: 8.8.4.4 # Google DNS B
1242 forward-addr: 208.67.222.222 # OpenDNS DNS A
1243 forward-addr: 208.67.220.220 # OpenDNS DNS B
1244{% else -%}
[13911]1245{%- for serviceid,item in autogen_ileiden_proxies.iteritems() %}
1246 {%- if loop.index <= 5 %}
[13696]1247 forward-addr: {{ "%-16s"|format(serviceid) }} # {{ item.nodename }}
[13911]1248 {%- endif %}
[13696]1249{%- endfor %}
[13911]1250{%- endif %}
[13696]1251""").render(datadump)
1252
[10069]1253def generate_motd(datadump):
1254 """ Generate configuration file '/etc/motd' """
[10568]1255 output = Template("""\
[10627]1256FreeBSD run ``service motd onestart'' to make me look normal
[8242]1257
[10568]1258 WWW: {{ autogen_fqdn }} - http://www.wirelessleiden.nl
1259 Loc: {{ location }}
[8257]1260
[10568]1261Services:
[13927]1262{% if board == "ALIX2" or board == "net4801" -%}
[10906]1263{{" -"}} Core Node ({{ board }})
[10568]1264{% else -%}
[10906]1265{{" -"}} Hulp Node ({{ board }})
[10568]1266{% endif -%}
[10584]1267{% if service_proxy_normal -%}
[10906]1268{{" -"}} Normal Proxy
[10568]1269{% endif -%}
[10584]1270{% if service_proxy_ileiden -%}
[10906]1271{{" -"}} iLeiden Proxy
[10748]1272{% endif -%}
1273{% if service_incoming_rdr -%}
[10906]1274{{" -"}} Incoming port redirects
[10568]1275{% endif %}
[10626]1276Interlinks:\n
[10568]1277""").render(datadump)
[10069]1278
[13327]1279
1280 def make_table(table):
1281 if not table:
1282 return " - none\n"
1283 else:
1284 lines = ""
1285 col_width = [max(len(x) for x in col) for col in zip(*table)]
1286 for row in table:
[13618]1287 # replace('_','.') is a hack to convert vlan interfaces to proper named interfaces
1288 lines += " - " + " || ".join("{:{}}".format(x.replace('_','.'), col_width[i]) for i, x in enumerate(row)) + "\n"
[13327]1289 return lines
1290
[13618]1291 (addrs_list, vlan_list, bridge_list, dhclient_if, flags_if, extra_ouput) = make_interface_list(datadump)
[13327]1292 table = []
[10907]1293 for iface,addrs in sorted(addrs_list.iteritems()):
1294 if iface in ['lo0']:
1295 continue
1296 for addr, comment in sorted(addrs,key=lambda x: parseaddr(x[0].split('/')[0])):
[13327]1297 table.append((iface, addr, comment))
[10907]1298
[13327]1299 output += make_table(table)
[10907]1300 output += '\n'
[10069]1301 output += """\
[13327]1302Attached devices:
[10069]1303"""
[13336]1304 output += make_table(get_attached_devices(datadump, url=True))
[13324]1305 output += '\n'
1306 output += """\
1307Available neighbours:
1308"""
[13336]1309 output += make_table(get_neighbours(datadump))
[13324]1310
[10069]1311 return output
1312
1313
[8267]1314def format_yaml_value(value):
1315 """ Get yaml value in right syntax for outputting """
1316 if isinstance(value,str):
[10049]1317 output = '"%s"' % value
[8267]1318 else:
1319 output = value
[9283]1320 return output
[8267]1321
1322
1323
1324def format_wleiden_yaml(datadump):
[8242]1325 """ Special formatting to ensure it is editable"""
[9283]1326 output = "# Genesis config yaml style\n"
[8262]1327 output += "# vim:ts=2:et:sw=2:ai\n"
[8242]1328 output += "#\n"
1329 iface_keys = [elem for elem in datadump.keys() if elem.startswith('iface_')]
1330 for key in sorted(set(datadump.keys()) - set(iface_keys)):
[10714]1331 if key == 'rdr_rules':
1332 output += '%-10s:\n' % 'rdr_rules'
1333 for rdr_rule in datadump[key]:
1334 output += '- %s\n' % rdr_rule
1335 else:
1336 output += "%-10s: %s\n" % (key, format_yaml_value(datadump[key]))
[9283]1337
[8242]1338 output += "\n\n"
[9283]1339
[10881]1340 # Format (key, required)
1341 key_order = (
1342 ('comment', True),
[13618]1343 ('parent', False),
1344 ('ip', False),
[13601]1345 ('ether', False),
[10881]1346 ('desc', True),
1347 ('sdesc', True),
1348 ('mode', True),
1349 ('type', True),
1350 ('extra_type', False),
1351 ('channel', False),
1352 ('ssid', False),
[13079]1353 ('wlan_mac', False),
[10881]1354 ('dhcp', True),
[13884]1355 ('dhcp_fixed', False),
[10881]1356 ('compass', False),
1357 ('distance', False),
1358 ('ns_ip', False),
[13246]1359 ('repeater_ip', False),
[10881]1360 ('bullet2_ip', False),
1361 ('ns_mac', False),
1362 ('bullet2_mac', False),
1363 ('ns_type', False),
[10892]1364 ('bridge_type', False),
[10881]1365 ('status', True),
1366 )
[8272]1367
[8242]1368 for iface_key in sorted(iface_keys):
[10881]1369 try:
1370 remainder = set(datadump[iface_key].keys()) - set([x[0] for x in key_order])
1371 if remainder:
1372 raise KeyError("invalid keys: %s" % remainder)
[8242]1373
[10881]1374 output += "%s:\n" % iface_key
1375 for key,required in key_order:
1376 if datadump[iface_key].has_key(key):
1377 output += " %-11s: %s\n" % (key, format_yaml_value(datadump[iface_key][key]))
1378 output += "\n\n"
[13403]1379 except Exception:
[10881]1380 print "# Error while processing interface %s" % iface_key
1381 raise
1382
[8242]1383 return output
1384
1385
[8257]1386
[10067]1387def generate_wleiden_yaml(datadump, header=True):
[8267]1388 """ Generate (petty) version of wleiden.yaml"""
[10904]1389 output = generate_header(datadump, "#") if header else ''
1390
[10053]1391 for key in datadump.keys():
1392 if key.startswith('autogen_'):
1393 del datadump[key]
[10054]1394 # Interface autogen cleanups
1395 elif type(datadump[key]) == dict:
1396 for key2 in datadump[key].keys():
1397 if key2.startswith('autogen_'):
1398 del datadump[key][key2]
1399
[8267]1400 output += format_wleiden_yaml(datadump)
1401 return output
1402
[12349]1403def generate_nanostation_config(datadump, iface, ns_type):
[12441]1404 #TODO(rvdz): Make sure the proper nanostation IP and subnet is set
1405 datadump['iface_%s' % iface]['ns_ip'] = datadump['iface_%s' % iface]['ns_ip'].split('/')[0]
1406
[12349]1407 datadump.update(datadump['iface_%s' % iface])
[8267]1408
[12349]1409 return open(os.path.join(os.path.dirname(__file__), 'ns5m.cfg.tmpl'),'r').read() % datadump
1410
[8588]1411def generate_yaml(datadump):
1412 return generate_config(datadump['nodename'], "wleiden.yaml", datadump)
[8267]1413
[8588]1414
[9283]1415
[8298]1416def generate_config(node, config, datadump=None):
[8257]1417 """ Print configuration file 'config' of 'node' """
[8267]1418 output = ""
[8242]1419 try:
1420 # Load config file
[8298]1421 if datadump == None:
1422 datadump = get_yaml(node)
[9283]1423
[8242]1424 if config == 'wleiden.yaml':
[8267]1425 output += generate_wleiden_yaml(datadump)
1426 elif config == 'authorized_keys':
[10051]1427 f = open(os.path.join(NODE_DIR,"global_keys"), 'r')
[8267]1428 output += f.read()
[12433]1429 node_keys = os.path.join(NODE_DIR,node,'authorized_keys')
1430 # Fetch local keys if existing
1431 if os.path.exists(node_keys):
1432 output += open(node_keys, 'r').read()
[8242]1433 f.close()
1434 elif config == 'dnsmasq.conf':
[10281]1435 output += generate_dnsmasq_conf(datadump)
[10410]1436 elif config == 'dhcpd.conf':
1437 output += generate_dhcpd_conf(datadump)
[8242]1438 elif config == 'rc.conf.local':
[10281]1439 output += generate_rc_conf_local(datadump)
[8242]1440 elif config == 'resolv.conf':
[10281]1441 output += generate_resolv_conf(datadump)
[10654]1442 elif config == 'ntp.conf':
1443 output += generate_ntp_conf(datadump)
[10069]1444 elif config == 'motd':
[10281]1445 output += generate_motd(datadump)
[10705]1446 elif config == 'pf.hybrid.conf.local':
1447 output += generate_pf_hybrid_conf_local(datadump)
[13696]1448 elif config == 'unbound.wleiden.conf':
1449 output += generate_unbound_wleiden_conf(datadump)
[12349]1450 elif config.startswith('vr'):
1451 interface, ns_type = config.strip('.yaml').split('-')
1452 output += generate_nanostation_config(datadump, interface, ns_type)
[8242]1453 else:
[9283]1454 assert False, "Config not found!"
[8242]1455 except IOError, e:
[8267]1456 output += "[ERROR] Config file not found"
1457 return output
[8242]1458
1459
[8257]1460
[13937]1461def generate_static(output_dir, logging=True):
1462 items = {'output_dir' : output_dir}
1463 for node in get_hostlist():
1464 items['node'] = node
1465 items['wdir'] = "%(output_dir)s/%(node)s" % items
1466 if not os.path.isdir(items['wdir']):
1467 os.makedirs(items['wdir'])
1468 datadump = get_yaml(node)
1469 f = open("%(wdir)s/index.html" % items, "w")
1470 f.write(generate_node_overview(items['node'], datadump))
1471 f.close()
1472 for config in files:
1473 items['config'] = config
1474 if logging: logger.info("## Generating %(node)s %(config)s" % items)
1475 f = open("%(wdir)s/%(config)s" % items, "w")
1476 f.write(generate_config(node, config, datadump))
1477 f.close()
1478
1479
1480
[11426]1481def process_cgi_request(environ=os.environ):
[8258]1482 """ When calling from CGI """
[11426]1483 response_headers = []
1484 content_type = 'text/plain'
1485
[8258]1486 # Update repository if requested
[11427]1487 form = urlparse.parse_qs(environ['QUERY_STRING']) if environ.has_key('QUERY_STRING') else None
1488 if form and form.has_key("action") and "update" in form["action"]:
[11426]1489 output = "[INFO] Updating subverion, please wait...\n"
[12245]1490 output += subprocess.Popen([SVN, 'cleanup', "%s/.." % NODE_DIR], stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0]
1491 output += subprocess.Popen([SVN, 'up', "%s/.." % NODE_DIR], stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0]
[13937]1492 generate_static(CACHE_DIR, False)
[11426]1493 output += "[INFO] All done, redirecting in 5 seconds"
1494 response_headers += [
1495 ('Refresh', '5; url=.'),
1496 ]
1497 else:
[13935]1498 base_uri = environ['REQUEST_URI']
1499 uri = base_uri.strip('/').split('/')[1:]
[9283]1500
[11426]1501 output = "Template Holder"
1502 if base_uri.endswith('/create/network.kml'):
1503 content_type='application/vnd.google-earth.kml+xml'
1504 output = make_network_kml.make_graph()
[11444]1505 elif base_uri.endswith('/api/get/nodeplanner.json'):
1506 content_type='application/json'
1507 output = make_network_kml.make_nodeplanner_json()
[13935]1508 elif not uri:
[11426]1509 if is_text_request(environ):
1510 output = '\n'.join(get_hostlist())
1511 else:
1512 content_type = 'text/html'
1513 output = generate_title(get_hostlist())
1514 elif len(uri) == 1:
1515 if is_text_request(environ):
1516 output = generate_node(uri[0])
1517 else:
1518 content_type = 'text/html'
[13935]1519 output = open(os.path.join(CACHE_DIR, uri[0], 'index.html'), 'r').read()
[11426]1520 elif len(uri) == 2:
1521 output = generate_config(uri[0], uri[1])
1522 else:
1523 assert False, "Invalid option"
[9283]1524
[11426]1525 # Return response
1526 response_headers += [
1527 ('Content-type', content_type),
1528 ('Content-Length', str(len(output))),
1529 ]
1530 return(response_headers, str(output))
[10270]1531
[10681]1532
[10264]1533def make_dns(output_dir = 'dns', external = False):
[8588]1534 items = dict()
[8598]1535
[8588]1536 # hostname is key, IP is value
[10642]1537 wleiden_zone = defaultdict(list)
[8588]1538 wleiden_cname = dict()
[8598]1539
[8588]1540 pool = dict()
1541 for node in get_hostlist():
1542 datadump = get_yaml(node)
[9283]1543
[13405]1544 fqdn = datadump['nodename']
[10730]1545
1546 if datadump.has_key('rdr_host'):
1547 remote_target = datadump['rdr_host']
1548 elif datadump.has_key('remote_access') and datadump['remote_access']:
1549 remote_target = datadump['remote_access'].split(':')[0]
1550 else:
1551 remote_target = None
[8588]1552
[10730]1553 if remote_target:
1554 try:
1555 parseaddr(remote_target)
1556 wleiden_zone[datadump['nodename'] + '.gw'].append((remote_target, False))
1557 except (IndexError, ValueError):
1558 wleiden_cname[datadump['nodename'] + '.gw'] = remote_target + '.'
1559
1560
[10655]1561 wleiden_zone[fqdn].append((datadump['masterip'], True))
[8588]1562
[8598]1563 # Hacking to get proper DHCP IPs and hostnames
[8588]1564 for iface_key in get_interface_keys(datadump):
[10890]1565 iface_name = iface_key.replace('_','-')
[13647]1566 if 'ip' in datadump[iface_key]:
1567 (ip, cidr) = datadump[iface_key]['ip'].split('/')
[8588]1568 try:
1569 (dhcp_start, dhcp_stop) = datadump[iface_key]['dhcp'].split('-')
[10882]1570 datadump[iface_key]['autogen_netmask'] = cidr2netmask(cidr)
[8588]1571 dhcp_part = ".".join(ip.split('.')[0:3])
1572 if ip != datadump['masterip']:
[13648]1573 wleiden_zone["dhcp-gateway-%s.%s" % (iface_name, fqdn)].append((ip, True))
[8588]1574 for i in range(int(dhcp_start), int(dhcp_stop) + 1):
[10655]1575 wleiden_zone["dhcp-%s-%s.%s" % (i, iface_name, fqdn)].append(("%s.%s" % (dhcp_part, i), True))
[10825]1576 except (AttributeError, ValueError, KeyError):
[8588]1577 # First push it into a pool, to indentify the counter-part later on
1578 addr = parseaddr(ip)
[10461]1579 cidr = int(cidr)
1580 addr = addr & ~((1 << (32 - cidr)) - 1)
[9283]1581 if pool.has_key(addr):
[8588]1582 pool[addr] += [(iface_name, fqdn, ip)]
[9283]1583 else:
[8588]1584 pool[addr] = [(iface_name, fqdn, ip)]
1585 continue
1586
[9286]1587
1588
[9957]1589 # WL uses an /29 to configure an interface. IP's are ordered like this:
[9958]1590 # MasterA (.1) -- DeviceA (.2) <<>> DeviceB (.3) --- SlaveB (.4)
[9957]1591
1592 sn = lambda x: re.sub(r'(?i)^cnode','',x)
1593
[8598]1594 # Automatic naming convention of interlinks namely 2 + remote.lower()
[8588]1595 for (key,value) in pool.iteritems():
[9958]1596 # Make sure they are sorted from low-ip to high-ip
1597 value = sorted(value, key=lambda x: parseaddr(x[2]))
1598
[8588]1599 if len(value) == 1:
1600 (iface_name, fqdn, ip) = value[0]
[10655]1601 wleiden_zone["2unused-%s.%s" % (iface_name, fqdn)].append((ip, True))
[9957]1602
1603 # Device DNS names
1604 if 'cnode' in fqdn.lower():
[10655]1605 wleiden_zone["d-at-%s.%s" % (iface_name, fqdn)].append((showaddr(parseaddr(ip) + 1), False))
1606 wleiden_cname["d-at-%s.%s" % (iface_name,sn(fqdn))] = "d-at-%s.%s" % ((iface_name, fqdn))
[9957]1607
[8588]1608 elif len(value) == 2:
1609 (a_iface_name, a_fqdn, a_ip) = value[0]
1610 (b_iface_name, b_fqdn, b_ip) = value[1]
[10655]1611 wleiden_zone["2%s.%s" % (b_fqdn,a_fqdn)].append((a_ip, True))
1612 wleiden_zone["2%s.%s" % (a_fqdn,b_fqdn)].append((b_ip, True))
[9957]1613
1614 # Device DNS names
1615 if 'cnode' in a_fqdn.lower() and 'cnode' in b_fqdn.lower():
[10655]1616 wleiden_zone["d-at-%s.%s" % (a_iface_name, a_fqdn)].append((showaddr(parseaddr(a_ip) + 1), False))
1617 wleiden_zone["d-at-%s.%s" % (b_iface_name, b_fqdn)].append((showaddr(parseaddr(b_ip) - 1), False))
[9957]1618 wleiden_cname["d-at-%s.%s" % (a_iface_name,sn(a_fqdn))] = "d-at-%s.%s" % (a_iface_name, a_fqdn)
1619 wleiden_cname["d-at-%s.%s" % (b_iface_name,sn(b_fqdn))] = "d-at-%s.%s" % (b_iface_name, b_fqdn)
1620 wleiden_cname["d2%s.%s" % (sn(b_fqdn),sn(a_fqdn))] = "d-at-%s.%s" % (a_iface_name, a_fqdn)
1621 wleiden_cname["d2%s.%s" % (sn(a_fqdn),sn(b_fqdn))] = "d-at-%s.%s" % (b_iface_name, b_fqdn)
1622
[8588]1623 else:
1624 pool_members = [k[1] for k in value]
1625 for item in value:
[9283]1626 (iface_name, fqdn, ip) = item
[10919]1627 wleiden_zone["2ring.%s" % (fqdn)].append((ip, True))
[8598]1628
1629 # Include static DNS entries
1630 # XXX: Should they override the autogenerated results?
1631 # XXX: Convert input to yaml more useable.
1632 # Format:
1633 ##; this is a comment
[13418]1634 ## roomburgh=Roomburgh1
1635 ## apkerk1.Vosko=172.17.176.8 ;this as well
[10642]1636 dns_list = yaml.load(open(os.path.join(NODE_DIR,'../dns/staticDNS.yaml'),'r'))
[9938]1637
1638 # Hack to allow special entries, for development
[10642]1639 wleiden_raw = {}
[9938]1640
[10642]1641 for line in dns_list:
[10660]1642 reverse = False
[10642]1643 k, items = line.items()[0]
[10660]1644 if type(items) == dict:
1645 if items.has_key('reverse'):
1646 reverse = items['reverse']
1647 items = items['a']
1648 else:
1649 items = items['cname']
1650 items = [items] if type(items) != list else items
[10642]1651 for item in items:
1652 if item.startswith('IN '):
1653 wleiden_raw[k] = item
1654 elif valid_addr(item):
[10660]1655 wleiden_zone[k].append((item, reverse))
[8598]1656 else:
[10642]1657 wleiden_cname[k] = item
[9283]1658
[10986]1659 # Hack to get dynamic pool listing
1660 def chunks(l, n):
1661 return [l[i:i+n] for i in range(0, len(l), n)]
1662
1663 ntp_servers = [x[0] for x in get_nameservers()]
1664 for id, chunk in enumerate(chunks(ntp_servers,(len(ntp_servers)/4))):
1665 for ntp_server in chunk:
1666 wleiden_zone['%i.pool.ntp' % id].append((ntp_server, False))
1667
[8598]1668 details = dict()
1669 # 24 updates a day allowed
1670 details['serial'] = time.strftime('%Y%m%d%H')
1671
[10264]1672 if external:
[13940]1673 dns_masters = ['ns1.vanderzwet.net', 'ns1.transip.net', 'ns2.transip.eu']
[10264]1674 else:
[13898]1675 dns_masters = ['druif.wleiden.net'] + ["%s.wleiden.net" % x[1] for x in get_nameservers(max_servers=3)]
[10264]1676
1677 details['master'] = dns_masters[0]
1678 details['ns_servers'] = '\n'.join(['\tNS\t%s.' % x for x in dns_masters])
1679
[8598]1680 dns_header = '''
1681$TTL 3h
[11725]1682%(zone)s. SOA %(master)s. beheer.lijst.wirelessleiden.nl. ( %(serial)s 15m 15m 1w 60s )
[8598]1683 ; Serial, Refresh, Retry, Expire, Neg. cache TTL
1684
[10264]1685%(ns_servers)s
[8598]1686 \n'''
1687
[9283]1688
[10264]1689 if not os.path.isdir(output_dir):
1690 os.makedirs(output_dir)
[8598]1691 details['zone'] = 'wleiden.net'
[9284]1692 f = open(os.path.join(output_dir,"db." + details['zone']), "w")
[8598]1693 f.write(dns_header % details)
1694
[10655]1695 for host,items in wleiden_zone.iteritems():
1696 for ip,reverse in items:
[10730]1697 if ip not in ['0.0.0.0']:
[13645]1698 f.write("%s.wleiden.net. IN A %s\n" % (host.lower(), ip))
[8588]1699 for source,dest in wleiden_cname.iteritems():
[10730]1700 dest = dest if dest.endswith('.') else dest + ".wleiden.net."
1701 f.write("%s.wleiden.net. IN CNAME %s\n" % (source.lower(), dest.lower()))
[9938]1702 for source, dest in wleiden_raw.iteritems():
1703 f.write("%s.wleiden.net. %s\n" % (source, dest))
[8588]1704 f.close()
[9283]1705
[8598]1706 # Create whole bunch of specific sub arpa zones. To keep it compliant
1707 for s in range(16,32):
1708 details['zone'] = '%i.172.in-addr.arpa' % s
[9284]1709 f = open(os.path.join(output_dir,"db." + details['zone']), "w")
[8598]1710 f.write(dns_header % details)
[8588]1711
[8598]1712 #XXX: Not effient, fix to proper data structure and do checks at other
1713 # stages
[10655]1714 for host,items in wleiden_zone.iteritems():
1715 for ip,reverse in items:
1716 if not reverse:
1717 continue
[10642]1718 if valid_addr(ip):
[10655]1719 if valid_addr(ip):
1720 if int(ip.split('.')[1]) == s:
1721 rev_ip = '.'.join(reversed(ip.split('.')))
1722 f.write("%s.in-addr.arpa. IN PTR %s.wleiden.net.\n" % (rev_ip.lower(), host.lower()))
[8598]1723 f.close()
[8588]1724
[8598]1725
[8259]1726def usage():
[10567]1727 print """Usage: %(prog)s <argument>
1728Argument:
[13328]1729\tcleanup = Cleanup all YAML files to specified format
[10567]1730\tstandalone [port] = Run configurator webserver [8000]
1731\tdns [outputdir] = Generate BIND compliant zone files in dns [./dns]
[11326]1732\tnagios-export [--heavy-load] = Generate basic nagios configuration file.
[9589]1733\tfull-export = Generate yaml export script for heatmap.
[10567]1734\tstatic [outputdir] = Generate all config files and store on disk
1735\t with format ./<outputdir>/%%NODE%%/%%FILE%% [./static]
[10872]1736\ttest <node> [<file>] = Receive output for certain node [all files].
1737\ttest-cgi <node> <file> = Receive output of CGI script [all files].
[10567]1738\tlist <status> <items> = List systems which have certain status
[13606]1739\tcreate network.kml = Create Network KML file for use in Google Earth
[10563]1740
[10567]1741Arguments:
1742\t<node> = NodeName (example: HybridRick)
1743\t<file> = %(files)s
1744\t<status> = all|up|down|planned
1745\t<items> = systems|nodes|proxies
1746
[10563]1747NOTE FOR DEVELOPERS; you can test your changes like this:
1748 BEFORE any changes in this code:
1749 $ ./gformat.py static /tmp/pre
1750 AFTER the changes:
1751 $ ./gformat.py static /tmp/post
1752 VIEW differences and VERIFY all are OK:
[10564]1753 $ diff -urI 'Generated' -r /tmp/pre /tmp/post
[10567]1754""" % { 'prog' : sys.argv[0], 'files' : '|'.join(files) }
[8259]1755 exit(0)
1756
1757
[11426]1758def is_text_request(environ=os.environ):
[10107]1759 """ Find out whether we are calling from the CLI or any text based CLI utility """
[13936]1760 if 'CONTENT_TYPE' in os.environ and os.environ['CONTENT_TYPE'] == 'text/plain':
[10107]1761 return True
[8259]1762
[13936]1763 if 'HTTP_USER_AGENT' in environ:
1764 return any([os.environ['HTTP_USER_AGENT'].lower().startswith(x) for x in ['curl', 'fetch', 'wget']])
1765 else:
1766 return False
1767
1768
[10547]1769def switchFormat(setting):
1770 if setting:
1771 return "YES"
1772 else:
1773 return "NO"
1774
[10885]1775def rlinput(prompt, prefill=''):
1776 import readline
1777 readline.set_startup_hook(lambda: readline.insert_text(prefill))
1778 try:
1779 return raw_input(prompt)
1780 finally:
1781 readline.set_startup_hook()
1782
1783def fix_conflict(left, right, default='i'):
1784 while True:
1785 print "## %-30s | %-30s" % (left, right)
1786 c = raw_input("## Solve Conflict (h for help) <l|r|e|i|> [%s]: " % default)
1787 if not c:
1788 c = default
1789
1790 if c in ['l','1']:
1791 return left
1792 elif c in ['r','2']:
1793 return right
1794 elif c in ['e', '3']:
1795 return rlinput("Edit: ", "%30s | %30s" % (left, right))
1796 elif c in ['i', '4']:
1797 return None
1798 else:
1799 print "#ERROR: '%s' is invalid input (left, right, edit or ignore)!" % c
1800
[11427]1801
1802
1803def print_cgi_response(response_headers, output):
1804 for header in response_headers:
1805 print "%s: %s" % header
[11444]1806 print
[11427]1807 print output
1808
1809
1810
[8267]1811def main():
1812 """Hard working sub"""
1813 # Allow easy hacking using the CLI
[13935]1814 if not os.environ.has_key('REQUEST_URI'):
[8267]1815 if len(sys.argv) < 2:
1816 usage()
[9283]1817
[8267]1818 if sys.argv[1] == "standalone":
1819 import SocketServer
1820 import CGIHTTPServer
[10105]1821 # Hop to the right working directory.
1822 os.chdir(os.path.dirname(__file__))
[8267]1823 try:
1824 PORT = int(sys.argv[2])
1825 except (IndexError,ValueError):
1826 PORT = 8000
[9283]1827
[8267]1828 class MyCGIHTTPRequestHandler(CGIHTTPServer.CGIHTTPRequestHandler):
1829 """ Serve this CGI from the root of the webserver """
1830 def is_cgi(self):
1831 if "favicon" in self.path:
1832 return False
[9283]1833
[10364]1834 self.cgi_info = (os.path.basename(__file__), self.path)
[8267]1835 self.path = ''
1836 return True
1837 handler = MyCGIHTTPRequestHandler
[9807]1838 SocketServer.TCPServer.allow_reuse_address = True
[8267]1839 httpd = SocketServer.TCPServer(("", PORT), handler)
1840 httpd.server_name = 'localhost'
1841 httpd.server_port = PORT
[9283]1842
[9728]1843 logger.info("serving at port %s", PORT)
[8860]1844 try:
1845 httpd.serve_forever()
1846 except KeyboardInterrupt:
1847 httpd.shutdown()
[9728]1848 logger.info("All done goodbye")
[8267]1849 elif sys.argv[1] == "test":
[10872]1850 # Basic argument validation
1851 try:
1852 node = sys.argv[2]
1853 except IndexError:
1854 print "Invalid argument"
1855 exit(1)
1856 except IOError as e:
1857 print e
1858 exit(1)
[13403]1859
1860 datadump = get_yaml(node)
[10872]1861
1862
1863 # Get files to generate
1864 gen_files = sys.argv[3:] if len(sys.argv) > 3 else files
1865
1866 # Actual config generation
1867 for config in gen_files:
1868 logger.info("## Generating %s %s", node, config)
1869 print generate_config(node, config, datadump)
1870 elif sys.argv[1] == "test-cgi":
[13935]1871 os.environ['REQUEST_URI'] = "/".join(['config'] + sys.argv[2:])
[8267]1872 os.environ['SCRIPT_NAME'] = __file__
[11427]1873 response_headers, output = process_cgi_request()
1874 print_cgi_response(response_headers, output)
[8296]1875 elif sys.argv[1] == "static":
[13937]1876 generate_static(sys.argv[2] if len(sys.argv) > 2 else "./static")
[9514]1877 elif sys.argv[1] == "wind-export":
1878 items = dict()
1879 for node in get_hostlist():
1880 datadump = get_yaml(node)
1881 sql = """INSERT IGNORE INTO nodes (name, name_ns, longitude, latitude)
1882 VALUES ('%(nodename)s', '%(nodename)s', %(latitude)s, %(longitude)s);""" % datadump;
1883 sql = """INSERT IGNORE INTO users_nodes (user_id, node_id, owner)
1884 VALUES (
1885 (SELECT id FROM users WHERE username = 'rvdzwet'),
1886 (SELECT id FROM nodes WHERE name = '%(nodename)s'),
1887 'Y');""" % datadump
1888 #for config in files:
1889 # items['config'] = config
1890 # print "## Generating %(node)s %(config)s" % items
1891 # f = open("%(wdir)s/%(config)s" % items, "w")
1892 # f.write(generate_config(node, config, datadump))
1893 # f.close()
1894 for node in get_hostlist():
1895 datadump = get_yaml(node)
1896 for iface_key in sorted([elem for elem in datadump.keys() if elem.startswith('iface_')]):
1897 ifacedump = datadump[iface_key]
1898 if ifacedump.has_key('mode') and ifacedump['mode'] == 'ap-wds':
1899 ifacedump['nodename'] = datadump['nodename']
1900 if not ifacedump.has_key('channel') or not ifacedump['channel']:
1901 ifacedump['channel'] = 0
1902 sql = """INSERT INTO links (node_id, type, ssid, protocol, channel, status)
1903 VALUES ((SELECT id FROM nodes WHERE name = '%(nodename)s'), 'ap',
1904 '%(ssid)s', 'IEEE 802.11b', %(channel)s, 'active');""" % ifacedump
[11326]1905 elif sys.argv[1] == "nagios-export":
1906 try:
1907 heavy_load = (sys.argv[2] == "--heavy-load")
1908 except IndexError:
1909 heavy_load = False
1910
1911 hostgroup_details = {
1912 'wleiden' : 'Stichting Wireless Leiden - FreeBSD Nodes',
1913 'wzoeterwoude' : 'Stichting Wireless Leiden - Afdeling Zoeterwoude - Free-WiFi Project',
1914 'walphen' : 'Stichting Wireless Alphen',
[13274]1915 'westeinder' : 'Westeinder Plassen',
[11326]1916 }
1917
[13274]1918 # Convert IP to Host
1919 ip2host = {'root' : 'root'}
1920 for host in get_hostlist():
1921 datadump = get_yaml(host)
1922 ip2host[datadump['masterip']] = datadump['autogen_fqdn']
[13328]1923 for iface in get_interface_keys(datadump):
[13618]1924 if datadump[iface].has_key('autogen_gateway'):
1925 ip2host[datadump[iface]['autogen_gateway']] = datadump['autogen_fqdn']
[13274]1926
1927 # Find dependency tree based on output of lvrouted.mytree of nearest node
[13276]1928 parents = defaultdict(list)
[13274]1929 stack = ['root']
1930 prev_depth = 0
1931 for line in open('lvrouted.mytree').readlines():
1932 depth = line.count('\t')
1933 ip = line.strip().split()[0]
1934
1935 if prev_depth < depth:
[13276]1936 try:
1937 parents[ip2host[ip]].append(ip2host[stack[-1]])
1938 except KeyError as e:
1939 print >> stderr, "# Unable to find %s in configuration files" % e.args[0]
[13274]1940 stack.append(ip)
1941 elif prev_depth > depth:
1942 stack = stack[:(depth - prev_depth)]
[13276]1943 elif prev_depth == depth:
1944 try:
1945 parents[ip2host[ip]].append(ip2host[stack[-1]])
1946 except KeyError as e:
1947 print >> stderr, "# Unable to find %s in configuration files" % e.args[0]
[13274]1948
[13276]1949
[13274]1950 prev_depth = depth
1951 # Observe that some nodes has themself as parent or multiple parents
1952 # for now take only the first parent, other behaviour is yet to be explained
1953
1954
1955
[11326]1956 params = {
[12787]1957 'check_interval' : 5 if heavy_load else 120,
1958 'retry_interval' : 1 if heavy_load else 10,
1959 'max_check_attempts' : 10 if heavy_load else 6,
[13263]1960 'notification_interval': 120 if heavy_load else 240,
[11326]1961 }
1962
1963 print '''\
1964define host {
1965 name wleiden-node ; Default Node Template
1966 use generic-host ; Use the standard template as initial starting point
1967 check_period 24x7 ; By default, FreeBSD hosts are checked round the clock
1968 check_interval %(check_interval)s ; Actively check the host every 5 minutes
1969 retry_interval %(retry_interval)s ; Schedule host check retries at 1 minute intervals
[13263]1970 notification_interval %(notification_interval)s
[11326]1971 max_check_attempts %(max_check_attempts)s ; Check each FreeBSD host 10 times (max)
[12482]1972 check_command check-host-alive ; Default command to check FreeBSD hosts
[11326]1973 register 0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE!
1974}
1975
1976define service {
1977 name wleiden-service ; Default Service Template
1978 use generic-service ; Use the standard template as initial starting point
1979 check_period 24x7 ; By default, FreeBSD hosts are checked round the clock
1980 check_interval %(check_interval)s ; Actively check the host every 5 minutes
1981 retry_interval %(retry_interval)s ; Schedule host check retries at 1 minute intervals
[13263]1982 notification_interval %(notification_interval)s
[11326]1983 max_check_attempts %(max_check_attempts)s ; Check each FreeBSD host 10 times (max)
1984 register 0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE!
1985}
1986
1987# Please make sure to install:
[13264]1988# make -C /usr/ports/net-mgmt/nagios-check_netsnmp install clean
1989#
1990# Recompile net-mgmt/nagios-plugins to support check_snmp
1991# make -C /usr/ports/net-mgmt/nagios-plugins
[11326]1992#
[13264]1993# Install net/bind-tools to allow v2/check_dns_wl to work:
1994# pkg install bind-tools
1995#
[11326]1996define command{
[13264]1997 command_name check_snmp_disk
1998 command_line $USER1$/check_snmp_disk -H $HOSTADDRESS$ -C public
[11326]1999}
2000
2001define command{
2002 command_name check_netsnmp_load
[13264]2003 command_line $USER1$/check_snmp_load.pl -H $HOSTADDRESS$ -C public -w 80 -c 90
[11326]2004}
2005
2006define command{
2007 command_name check_netsnmp_proc
[13264]2008 command_line $USER1$/check_snmp_proc -H $HOSTADDRESS$ -C public
[11326]2009}
2010
[12787]2011define command{
2012 command_name check_by_ssh
2013 command_line $USER1$/check_by_ssh -H $HOSTADDRESS$ -p $ARG1$ -C "$ARG2$ $ARG3$ $ARG4$ $ARG5$ $ARG6$"
2014}
2015
2016define command{
2017 command_name check_dns_wl
2018 command_line $USER1$/v2/check_dns_wl $HOSTADDRESS$ $ARG1$
2019}
2020
[13264]2021define command{
2022 command_name check_snmp_uptime
2023 command_line $USER1$/check_snmp -H $HOSTADDRESS$ -C public -o .1.3.6.1.2.1.1.3.0
2024}
[12787]2025
[13264]2026
[11326]2027# TDB: dhcp leases
2028# /usr/local/libexec/nagios/check_netsnmp -H 192.168.178.47 --oid 1 exec
2029
2030# TDB: internet status
2031# /usr/local/libexec/nagios/check_netsnmp -H 192.168.178.47 --oid 1 file
2032
2033# TDB: Advanced local passive checks
2034# /usr/local/libexec/nagios/check_by_ssh
2035''' % params
2036
2037 print '''\
2038# Service Group, not displayed by default
2039define hostgroup {
2040 hostgroup_name srv_hybrid
2041 alias All Hybrid Nodes
2042 register 0
2043}
2044
2045define service {
2046 use wleiden-service
2047 hostgroup_name srv_hybrid
2048 service_description SSH
2049 check_command check_ssh
2050}
2051
2052define service {
[13278]2053 use wleiden-service,service-pnp
[11326]2054 hostgroup_name srv_hybrid
2055 service_description HTTP
2056 check_command check_http
2057}
2058
[12787]2059define service {
2060 use wleiden-service
2061 hostgroup_name srv_hybrid
2062 service_description DNS
2063 check_command check_dns_wl!"www.wirelessleiden.nl"
2064}
[11326]2065'''
2066
2067 if heavy_load:
2068 print '''\
2069define service {
2070 use wleiden-service
2071 hostgroup_name srv_hybrid
[13264]2072 service_description UPTIME
2073 check_command check_snmp_uptime
[11326]2074}
2075
[13263]2076#define service {
2077# use wleiden-service
2078# hostgroup_name srv_hybrid
2079# service_description NTP
2080# check_command check_ntp_peer
2081#}
[11326]2082
2083define service {
2084 use wleiden-service
2085 hostgroup_name srv_hybrid
2086 service_description LOAD
2087 check_command check_netsnmp_load
2088}
2089
2090define service {
2091 use wleiden-service
2092 hostgroup_name srv_hybrid
2093 service_description PROC
2094 check_command check_netsnmp_proc
2095}
2096
2097define service {
2098 use wleiden-service
2099 hostgroup_name srv_hybrid
2100 service_description DISK
[13264]2101 check_command check_snmp_disk
[11326]2102}
2103'''
2104 for node in get_hostlist():
2105 datadump = get_yaml(node)
2106 if not datadump['status'] == 'up':
2107 continue
2108 if not hostgroup_details.has_key(datadump['monitoring_group']):
2109 hostgroup_details[datadump['monitoring_group']] = datadump['monitoring_group']
2110 print '''\
2111define host {
[13278]2112 use wleiden-node,host-pnp
[13263]2113 contact_groups admins
[11326]2114 host_name %(autogen_fqdn)s
2115 address %(masterip)s
[13274]2116 hostgroups srv_hybrid,%(monitoring_group)s\
2117''' % datadump
[13277]2118 if (len(parents[datadump['autogen_fqdn']]) > 0) and parents[datadump['autogen_fqdn']][0] != 'root':
[13274]2119 print '''\
[13276]2120 parents %(parents)s\
2121''' % { 'parents' : parents[datadump['autogen_fqdn']][0] }
[13274]2122 print '''\
[11326]2123}
[13274]2124'''
[11326]2125
[13274]2126
[11326]2127 for name,alias in hostgroup_details.iteritems():
2128 print '''\
2129define hostgroup {
2130 hostgroup_name %s
2131 alias %s
2132} ''' % (name, alias)
2133
2134
[9589]2135 elif sys.argv[1] == "full-export":
2136 hosts = {}
2137 for node in get_hostlist():
2138 datadump = get_yaml(node)
2139 hosts[datadump['nodename']] = datadump
2140 print yaml.dump(hosts)
2141
[8584]2142 elif sys.argv[1] == "dns":
[10264]2143 make_dns(sys.argv[2] if len(sys.argv) > 2 else 'dns', 'external' in sys.argv)
[9283]2144 elif sys.argv[1] == "cleanup":
[8588]2145 # First generate all datadumps
2146 datadumps = dict()
[10729]2147 ssid_to_node = dict()
[8588]2148 for host in get_hostlist():
[9728]2149 logger.info("# Processing: %s", host)
[10436]2150 # Set some boring default values
2151 datadump = { 'board' : 'UNKNOWN' }
2152 datadump.update(get_yaml(host))
[13405]2153 datadumps[datadump['nodename']] = datadump
[9283]2154
[13327]2155 (poel, errors) = make_relations()
[10729]2156 print "\n".join(["# WARNING: %s" % x for x in errors])
[10455]2157
[10156]2158 for host,datadump in datadumps.iteritems():
[10881]2159 try:
2160 # Convert all yes and no to boolean values
2161 def fix_boolean(dump):
2162 for key in dump.keys():
2163 if type(dump[key]) == dict:
2164 dump[key] = fix_boolean(dump[key])
2165 elif str(dump[key]).lower() in ["yes", "true"]:
2166 dump[key] = True
2167 elif str(dump[key]).lower() in ["no", "false"]:
2168 # Compass richting no (Noord Oost) is valid input
2169 if key != "compass": dump[key] = False
2170 return dump
2171 datadump = fix_boolean(datadump)
[10455]2172
[13325]2173 if 'rdnap_x' in datadump and 'rdnap_y' in datadump:
[13843]2174 if not 'latitude' in datadump and not 'longitude' in datadump:
2175 datadump['latitude'], datadump['longitude'] = map(lambda x: "%.5f" % x, rd2etrs(datadump['rdnap_x'], datadump['rdnap_y']))
[13325]2176 elif 'latitude' in datadump and 'longitude' in datadump:
[13843]2177 if not 'rdnap_x' in datadump and not 'rdnap_y' in datadump:
2178 datadump['rdnap_x'], datadump['rdnap_y'] = etrs2rd(datadump['latitude'], datadump['longitude'])
2179 # TODO: Compare outcome of both coordinate systems and validate against each-other
[10400]2180
[10881]2181 if datadump['nodename'].startswith('Proxy'):
2182 datadump['nodename'] = datadump['nodename'].lower()
[10319]2183
[13328]2184 for iface_key in get_interface_keys(datadump):
[10889]2185 try:
2186 # All our normal wireless cards are normal APs now
2187 if datadump[iface_key]['type'] in ['11a', '11b', '11g', 'wireless']:
2188 datadump[iface_key]['mode'] = 'ap'
2189 # Wireless Leiden SSID have an consistent lowercase/uppercase
2190 if datadump[iface_key].has_key('ssid'):
2191 ssid = datadump[iface_key]['ssid']
2192 prefix = 'ap-WirelessLeiden-'
2193 if ssid.lower().startswith(prefix.lower()):
2194 datadump[iface_key]['ssid'] = prefix + ssid[len(prefix)].upper() + ssid[len(prefix) + 1:]
2195 if datadump[iface_key].has_key('ns_ip') and not datadump[iface_key].has_key('mode'):
2196 datadump[iface_key]['mode'] = 'autogen-FIXME'
2197 if not datadump[iface_key].has_key('comment'):
2198 datadump[iface_key]['comment'] = 'autogen-FIXME'
[10882]2199
[11732]2200 if datadump[iface_key].has_key('ns_mac'):
2201 datadump[iface_key]['ns_mac'] = datadump[iface_key]['ns_mac'].lower()
2202
[10889]2203 if datadump[iface_key]['comment'].startswith('autogen-') and datadump[iface_key].has_key('comment'):
2204 datadump[iface_key] = datadump[iface_key]['desc']
[10882]2205
[11738]2206 # We are not using 802.11b anymore. OFDM is preferred over DSSS
2207 # due to better collision avoidance.
2208 if datadump[iface_key]['type'] == '11b':
2209 datadump[iface_key]['type'] = '11g'
2210
2211 # Setting 802.11g channels to de-facto standards, to avoid
2212 # un-detected sharing with other overlapping channels
2213 #
2214 # Technically we could also use channel 13 in NL, but this is not
2215 # recommended as foreign devices might not be able to select this
2216 # channel. Secondly using 1,5,9,13 instead is going to clash with
2217 # the de-facto usage of 1,6,11.
2218 #
2219 # See: https://en.wikipedia.org/wiki/List_of_WLAN_channels
2220 channels_at_2400Mhz = (1,6,11)
2221 if datadump[iface_key]['type'] == '11g' and datadump[iface_key].has_key('channel'):
2222 datadump[iface_key]['channel'] = int(datadump[iface_key]['channel'])
2223 if datadump[iface_key]['channel'] not in channels_at_2400Mhz:
2224 datadump[iface_key]['channel'] = random.choice(channels_at_2400Mhz)
2225
[11555]2226 # Mandatory interface keys
2227 if not datadump[iface_key].has_key('status'):
2228 datadump[iface_key]['status'] = 'planned'
2229
[10889]2230 x = datadump[iface_key]['comment']
2231 datadump[iface_key]['comment'] = x[0].upper() + x[1:]
[10884]2232
[12478]2233 # Fixing bridge_type if none is found
2234 if datadump[iface_key].get('extra_type', '') == 'eth2wifibridge':
2235 if not 'bridge_type' in datadump[iface_key]:
2236 datadump[iface_key]['bridge_type'] = 'NanoStation M5'
2237
2238 # Making sure description works
[10889]2239 if datadump[iface_key].has_key('desc'):
2240 if datadump[iface_key]['comment'].lower() == datadump[iface_key]['desc'].lower():
[10885]2241 del datadump[iface_key]['desc']
[10889]2242 else:
2243 print "# ERROR: At %s - %s" % (datadump['nodename'], iface_key)
2244 response = fix_conflict(datadump[iface_key]['comment'], datadump[iface_key]['desc'])
2245 if response:
2246 datadump[iface_key]['comment'] = response
2247 del datadump[iface_key]['desc']
[10882]2248
[10889]2249 # Check DHCP configuration
2250 dhcp_type(datadump[iface_key])
2251
2252 # Set the compass value based on the angle between the poels
[13843]2253 if 'ns_ip' in datadump[iface_key] and 'ip' in datadump[iface_key] and not 'compass' in datadump[iface_key]:
[10889]2254 my_pool = poel[network(datadump[iface_key]['ip'])]
2255 remote_hosts = list(set([x[0] for x in my_pool]) - set([host]))
2256 if remote_hosts:
2257 compass_target = remote_hosts[0]
2258 datadump[iface_key]['compass'] = cd_between_hosts(host, compass_target, datadumps)
[13843]2259 # TODO: Compass wanted and actual direction might differ
[12475]2260
2261 # Monitoring Group default
2262 if not 'monitoring_group' in datadump:
2263 datadump['monitoring_group'] = 'wleiden'
2264
[13403]2265 except Exception:
[10889]2266 print "# Error while processing interface %s" % iface_key
2267 raise
[10881]2268 store_yaml(datadump)
[13403]2269 except Exception:
[10881]2270 print "# Error while processing %s" % host
2271 raise
[9971]2272 elif sys.argv[1] == "list":
[10611]2273 use_fqdn = False
[13279]2274 if len(sys.argv) < 4:
[10567]2275 usage()
[13279]2276 if not sys.argv[2] in ["up", "down", "planned", "all"]:
[9971]2277 usage()
[13279]2278 if not sys.argv[3] in ["nodes","proxies","systems"]:
2279 usage()
2280
[10611]2281 if len(sys.argv) > 4:
2282 if sys.argv[4] == "fqdn":
2283 use_fqdn = True
2284 else:
2285 usage()
2286
[13279]2287 for system in get_hostlist():
[9971]2288 datadump = get_yaml(system)
[13279]2289 if sys.argv[3] == 'proxies' and not datadump['service_proxy_ileiden']:
2290 continue
[10611]2291
2292 output = datadump['autogen_fqdn'] if use_fqdn else system
[10567]2293 if sys.argv[2] == "all":
[10611]2294 print output
[10567]2295 elif datadump['status'] == sys.argv[2]:
[10611]2296 print output
[10378]2297 elif sys.argv[1] == "create":
2298 if sys.argv[2] == "network.kml":
2299 print make_network_kml.make_graph()
[10998]2300 elif sys.argv[2] == "host-ips.txt":
2301 for system in get_hostlist():
2302 datadump = get_yaml(system)
2303 ips = [datadump['masterip']]
[13328]2304 for ifkey in get_interface_keys(datadump):
[10998]2305 ips.append(datadump[ifkey]['ip'].split('/')[0])
2306 print system, ' '.join(ips)
[10999]2307 elif sys.argv[2] == "host-pos.txt":
2308 for system in get_hostlist():
2309 datadump = get_yaml(system)
2310 print system, datadump['rdnap_x'], datadump['rdnap_y']
[12233]2311 elif sys.argv[2] == 'ssh_config':
2312 print '''
2313Host *.wleiden.net
2314 User root
2315
2316Host 172.16.*.*
2317 User root
2318'''
2319 for system in get_hostlist():
2320 datadump = get_yaml(system)
2321 print '''\
2322Host %s
2323 User root
2324
2325Host %s
2326 User root
2327
2328Host %s
2329 User root
2330
2331Host %s
2332 User root
2333''' % (system, system.lower(), datadump['nodename'], datadump['nodename'].lower())
[10378]2334 else:
[10998]2335 usage()
2336 else:
[9283]2337 usage()
2338 else:
[10070]2339 # Do not enable debugging for config requests as it highly clutters the output
2340 if not is_text_request():
2341 cgitb.enable()
[11427]2342 response_headers, output = process_cgi_request()
2343 print_cgi_response(response_headers, output)
[9283]2344
2345if __name__ == "__main__":
2346 main()
Note: See TracBrowser for help on using the repository browser.