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