source: genesis/nodes/gformat.py@ 8297

Last change on this file since 8297 was 8297, checked in by rick, 14 years ago

Rewrote into more readable format and assigned to right key

  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 13.5 KB
RevLine 
[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>
6import cgi
[8267]7import cgitb
8import copy
[8242]9import glob
10import os
11import socket
12import string
13import subprocess
14import sys
15import time
16import yaml
17
[8258]18NODE_DIR = os.path.dirname(os.path.realpath(__file__))
[8242]19__version__ = '$Id: gformat.py 8297 2010-08-10 13:56:57Z rick $'
20
[8267]21
[8242]22files = [
23 'authorized_keys',
24 'dnsmasq.conf',
25 'rc.conf.local',
26 'resolv.conf',
27 'wleiden.yaml'
28 ]
29
[8257]30
31
[8267]32def get_proxylist():
33 """Get all available proxies proxyX sorting based on X number"""
34 os.chdir(NODE_DIR)
35 proxylist = sorted(glob.glob("proxy*"),
36 key=lambda name: int(''.join([c for c in name if c in string.digits])),
37 cmp=lambda x,y: x - y)
38 return proxylist
39
40
41
42def get_nodelist():
43 """ Get all available nodes - sorted """
44 os.chdir(NODE_DIR)
45 nodelist = sorted(glob.glob("CNode*"))
46 return nodelist
47
[8296]48def get_hostlist():
49 """ Combined hosts and proxy list"""
50 return get_nodelist() + get_proxylist()
[8267]51
52
53def generate_title(nodelist):
[8257]54 """ Main overview page """
[8259]55 items = {'root' : "." }
[8267]56 output = """
[8257]57<html>
58 <head>
59 <title>Wireless leiden Configurator - GFormat</title>
60 <style type="text/css">
61 th {background-color: #999999}
62 tr:nth-child(odd) {background-color: #cccccc}
63 tr:nth-child(even) {background-color: #ffffff}
64 th, td {padding: 0.1em 1em}
65 </style>
66 </head>
67 <body>
68 <center>
[8259]69 <form type="GET" action="%(root)s">
[8257]70 <input type="hidden" name="action" value="update">
71 <input type="submit" value="Update Configuration Database (SVN)">
72 </form>
73 <table>
74 <caption><h3>Wireless Leiden Configurator</h3></caption>
75 """ % items
[8242]76
[8296]77 for node in nodelist:
[8257]78 items['node'] = node
[8267]79 output += '<tr><td><a href="%(root)s/%(node)s">%(node)s</a></td>' % items
[8257]80 for config in files:
81 items['config'] = config
[8267]82 output += '<td><a href="%(root)s/%(node)s/%(config)s">%(config)s</a></td>' % items
83 output += "</tr>"
84 output += """
[8257]85 </table>
86 <hr />
87 <em>%s</em>
88 </center>
89 </body>
90</html>
91 """ % __version__
[8242]92
[8267]93 return output
[8257]94
95
[8267]96
97def generate_node(node):
[8257]98 """ Print overview of all files available for node """
[8267]99 return "\n".join(files)
[8242]100
[8257]101
102
[8242]103def generate_header(ctag="#"):
104 return """\
105%(ctag)s
106%(ctag)s DO NOT EDIT - Automatically generated by 'gformat'
107%(ctag)s Generated at %(date)s by %(host)s
108%(ctag)s
109""" % { 'ctag' : ctag, 'date' : time.ctime(), 'host' : socket.gethostname() }
110
[8257]111
112
[8242]113def parseaddr(s):
[8257]114 """ Process IPv4 CIDR notation addr to a (binary) number """
[8242]115 f = s.split('.')
116 return (long(f[0]) << 24L) + \
117 (long(f[1]) << 16L) + \
118 (long(f[2]) << 8L) + \
119 long(f[3])
120
[8257]121
122
[8242]123def showaddr(a):
[8257]124 """ Display IPv4 addr in (dotted) CIDR notation """
[8242]125 return "%d.%d.%d.%d" % ((a >> 24) & 0xff, (a >> 16) & 0xff, (a >> 8) & 0xff, a & 0xff)
126
[8257]127
128
[8242]129def netmask2subnet(netmask):
[8257]130 """ Given a 'netmask' return corresponding CIDR """
[8242]131 return showaddr(0xffffffff & (0xffffffff << (32 - int(netmask))))
132
[8257]133
134
[8242]135def generate_dnsmasq_conf(datadump):
[8257]136 """ Generate configuration file '/usr/local/etc/dnsmasq.conf' """
[8242]137 output = generate_header()
138 output += """\
139# DHCP server options
140dhcp-authoritative
141dhcp-fqdn
142domain=dhcp.%(nodename_lower)s.%(domain)s
143domain-needed
144expand-hosts
145
146# Low memory footprint
147cache-size=10000
148 \n""" % datadump
149
150 for iface_key in datadump['iface_keys']:
[8262]151 if not datadump[iface_key].has_key('comment'):
152 datadump[iface_key]['comment'] = None
153 output += "## %(interface)s - %(desc)s - %(comment)s\n" % datadump[iface_key]
[8242]154
155 try:
[8257]156 (dhcp_start, dhcp_stop) = datadump[iface_key]['dhcp'].split('-')
[8242]157 (ip, netmask) = datadump[iface_key]['ip'].split('/')
158 datadump[iface_key]['subnet'] = netmask2subnet(netmask)
[8262]159 except (AttributeError, ValueError):
[8242]160 output += "# not autoritive\n\n"
161 continue
162
163 dhcp_part = ".".join(ip.split('.')[0:3])
164 datadump[iface_key]['dhcp_start'] = dhcp_part + "." + dhcp_start
165 datadump[iface_key]['dhcp_stop'] = dhcp_part + "." + dhcp_stop
166 output += "dhcp-range=%(interface)s,%(dhcp_start)s,%(dhcp_stop)s,%(subnet)s,24h\n\n" % datadump[iface_key]
167
168 return output
169
[8257]170
171
[8242]172def generate_rc_conf_local(datadump):
[8257]173 """ Generate configuration file '/etc/rc.conf.local' """
[8242]174 output = generate_header("#");
175 output += """\
176hostname='%(nodetype)s%(nodename)s.%(domain)s'
177location='%(location)s'
178""" % datadump
179
180 # TProxy configuration
181 output += "\n"
182 try:
183 if datadump['tproxy']:
184 output += """\
185tproxy_enable='YES'
186tproxy_range='%(tproxy)s'
187""" % datadump
188 except KeyError:
189 output += "tproxy_enable='NO'\n"
190
191 output += '\n'
192 # lo0 configuration:
193 # - 172.32.255.1/32 is the proxy.wleiden.net deflector
194 # - masterip is special as it needs to be assigned to at
195 # least one interface, so if not used assign to lo0
196 addrs_list = { 'lo0' : ["127.0.0.1/8", "172.31.255.1/32"] }
197 iface_map = {'lo0' : 'lo0'}
198
[8297]199 masterip_used = False
200 for iface_key in datadump['iface_keys']:
201 if datadump[iface_key]['ip'].startswith(datadump['masterip']):
202 masterip_used = True
203 break
204 if not masterip_used:
205 addrs_list['lo0'].append(datadump['masterip'] + "/32")
206
[8242]207 wlan_count = 0
208 for iface_key in datadump['iface_keys']:
209 ifacedump = datadump[iface_key]
210 interface = ifacedump['interface']
211 # By default no special interface mapping
212 iface_map[interface] = interface
213
214 # Add interface IP to list
215 if addrs_list.has_key(interface):
216 addrs_list[interface].append(ifacedump['ip'])
217 else:
218 addrs_list[interface] = [ifacedump['ip']]
219
220 # Alias only needs IP assignment for now, this might change if we
221 # are going to use virtual accesspoints
222 if "alias" in iface_key:
223 continue
224
225 # XXX: Might want to deduct type directly from interface name
226 if ifacedump['type'] in ['11a', '11b', '11g', 'wireless']:
227 # Create wlanX interface
228 ifacedump['wlanif'] ="wlan%i" % wlan_count
229 iface_map[interface] = ifacedump['wlanif']
230 wlan_count += 1
231
232 # Default to station (client) mode
233 ifacedump['wlanmode'] = "sta"
[8274]234 if ifacedump['mode'] in ['master', 'master-wds']:
[8242]235 ifacedump['wlanmode'] = "ap"
236 # Default to 802.11b mode
237 ifacedump['mode'] = '11b'
238 if ifacedump['type'] in ['11a', '11b' '11g']:
239 ifacedump['mode'] = ifacedump['type']
240
241 if not ifacedump.has_key('channel'):
242 if ifacedump['type'] == '11a':
243 ifacedump['channel'] = 36
244 else:
245 ifacedump['channel'] = 1
246
247 # Allow special hacks at the back like wds and stuff
248 if not ifacedump.has_key('extra'):
249 ifacedump['extra'] = 'regdomain ETSI country NL'
250
251 output += "wlans_%(interface)s='%(wlanif)s'\n" % ifacedump
252 output += ("create_args_%(wlanif)s='wlanmode %(wlanmode)s mode " +\
[8274]253 "%(mode)s ssid %(ssid)s %(extra)s channel %(channel)s'\n") % ifacedump
[8242]254
255 elif ifacedump['type'] in ['ethernet', 'eth']:
256 # No special config needed besides IP
257 pass
258 else:
259 assert False, "Unknown type " + ifacedump['type']
260
261 # Print IP address which needs to be assigned over here
262 output += "\n"
263 for iface,addrs in sorted(addrs_list.iteritems()):
264 output += "ipv4_addrs_%s='%s'\n" % (iface_map[iface], " ".join(addrs))
265
266 return output
267
[8257]268
269
[8242]270def get_yaml(item):
[8257]271 """ Get configuration yaml for 'item'"""
[8258]272 gfile = NODE_DIR + '/%s/wleiden.yaml' % item
[8242]273
274 f = open(gfile, 'r')
275 datadump = yaml.load(f)
276 f.close()
277
278 return datadump
279
[8257]280
281
[8267]282def write_yaml(item, datadump):
283 """ Write configuration yaml for 'item'"""
284 gfile = NODE_DIR + '/%s/wleiden.yaml' % item
285
286 f = open(gfile, 'w')
287 f.write(format_wleiden_yaml(datadump))
288 f.close()
289
290
291
[8242]292def generate_resolv_conf(datadump):
[8257]293 """ Generate configuration file '/etc/resolv.conf' """
[8242]294 output = generate_header("#");
295 output += """\
296search wleiden.net
297# Try local (cache) first
298nameserver 127.0.0.1
299
300# Proxies are recursive nameservers
301# needs to be in resolv.conf for dnsmasq as well
302""" % datadump
303
[8267]304 for proxy in get_proxylist():
[8242]305 proxy_ip = get_yaml(proxy)['masterip']
306 output += "nameserver %-15s # %s\n" % (proxy_ip, proxy)
307 return output
308
309
[8257]310
[8267]311def format_yaml_value(value):
312 """ Get yaml value in right syntax for outputting """
313 if isinstance(value,str):
314 output = "'%s'" % value
315 else:
316 output = value
317 return output
318
319
320
321def format_wleiden_yaml(datadump):
[8242]322 """ Special formatting to ensure it is editable"""
[8267]323 output = "# Genesis config yaml style\n"
[8262]324 output += "# vim:ts=2:et:sw=2:ai\n"
[8242]325 output += "#\n"
326 iface_keys = [elem for elem in datadump.keys() if elem.startswith('iface_')]
327 for key in sorted(set(datadump.keys()) - set(iface_keys)):
[8267]328 output += "%-10s: %s\n" % (key, format_yaml_value(datadump[key]))
[8242]329
330 output += "\n\n"
331
[8272]332 key_order = [ 'comment', 'interface', 'ip', 'desc', 'sdesc', 'mode', 'type',
333 'extra_type', 'channel', 'ssid', 'dhcp' ]
334
[8242]335 for iface_key in sorted(iface_keys):
336 output += "%s:\n" % iface_key
[8272]337 for key in key_order + list(sorted(set(datadump[iface_key].keys()) - set(key_order))):
338 if datadump[iface_key].has_key(key):
339 output += " %-11s: %s\n" % (key, format_yaml_value(datadump[iface_key][key]))
[8242]340 output += "\n\n"
341
342 return output
343
344
[8257]345
[8267]346def generate_wleiden_yaml(datadump):
347 """ Generate (petty) version of wleiden.yaml"""
348 output = generate_header("#")
349 output += format_wleiden_yaml(datadump)
350 return output
351
352
353
354def generate_config(node, config):
[8257]355 """ Print configuration file 'config' of 'node' """
[8267]356 output = ""
[8242]357 try:
358 # Load config file
359 datadump = get_yaml(node)
360
[8267]361 # Preformat certain needed variables for formatting and push those into special object
362 datadump_extra = copy.deepcopy(datadump)
363 if not datadump_extra.has_key('domain'):
364 datadump_extra['domain'] = 'wleiden.net'
365 datadump_extra['nodename_lower'] = datadump_extra['nodename'].lower()
366 datadump_extra['iface_keys'] = sorted([elem for elem in datadump.keys() if elem.startswith('iface_')])
367
[8242]368 if config == 'wleiden.yaml':
[8267]369 output += generate_wleiden_yaml(datadump)
370 elif config == 'authorized_keys':
[8242]371 f = open("global_keys", 'r')
[8267]372 output += f.read()
[8242]373 f.close()
374 elif config == 'dnsmasq.conf':
[8267]375 output += generate_dnsmasq_conf(datadump_extra)
[8242]376 elif config == 'rc.conf.local':
[8267]377 output += generate_rc_conf_local(datadump_extra)
[8242]378 elif config == 'resolv.conf':
[8267]379 output += generate_resolv_conf(datadump_extra)
[8242]380 else:
381 assert False, "Config not found!"
382 except IOError, e:
[8267]383 output += "[ERROR] Config file not found"
384 return output
[8242]385
386
[8257]387
[8258]388def process_cgi_request():
389 """ When calling from CGI """
390 # Update repository if requested
391 form = cgi.FieldStorage()
392 if form.getvalue("action") == "update":
[8259]393 print "Refresh: 5; url=."
[8258]394 print "Content-type:text/plain\r\n\r\n",
395 print "[INFO] Updating subverion, please wait..."
396 print subprocess.Popen(['svn', 'up', NODE_DIR], stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0],
397 print "[INFO] All done, redirecting in 5 seconds"
398 sys.exit(0)
399
400
401 uri = os.environ['PATH_INFO'].strip('/').split('/')
[8267]402 output = ""
[8258]403 if not uri[0]:
[8267]404 output += "Content-type:text/html\r\n\r\n"
[8296]405 output += generate_title(get_hostlist())
[8258]406 elif len(uri) == 1:
[8267]407 output += "Content-type:text/plain\r\n\r\n"
408 output += generate_node(uri[0])
[8258]409 elif len(uri) == 2:
[8267]410 output += "Content-type:text/plain\r\n\r\n"
411 output += generate_config(uri[0], uri[1])
[8258]412 else:
413 assert False, "Invalid option"
[8267]414 print output
[8242]415
[8259]416
417def usage():
[8296]418 print """Usage: %s <standalone [port] |test [test arguments]|static>
[8259]419Examples:
420\tstandalone = Run configurator webserver [default port=8000]
[8296]421\tstatic = Generate all config files and store on disk
422\t with format ./static/%%NODE%%/%%FILE%%
[8259]423\ttest CNodeRick dnsmasq.conf = Receive output of CGI script
424\t for arguments CNodeRick/dnsmasq.conf
425"""
426 exit(0)
427
428
429
[8267]430def main():
431 """Hard working sub"""
432 # Allow easy hacking using the CLI
433 if not os.environ.has_key('PATH_INFO'):
434 if len(sys.argv) < 2:
435 usage()
436
437 if sys.argv[1] == "standalone":
438 import SocketServer
439 import CGIHTTPServer
440 try:
441 PORT = int(sys.argv[2])
442 except (IndexError,ValueError):
443 PORT = 8000
444
445 class MyCGIHTTPRequestHandler(CGIHTTPServer.CGIHTTPRequestHandler):
446 """ Serve this CGI from the root of the webserver """
447 def is_cgi(self):
448 if "favicon" in self.path:
449 return False
450
451 self.cgi_info = (__file__, self.path)
452 self.path = ''
453 return True
454 handler = MyCGIHTTPRequestHandler
455 httpd = SocketServer.TCPServer(("", PORT), handler)
456 httpd.server_name = 'localhost'
457 httpd.server_port = PORT
458
459 print "serving at port", PORT
460 httpd.serve_forever()
461 elif sys.argv[1] == "test":
462 os.environ['PATH_INFO'] = "/".join(sys.argv[2:])
463 os.environ['SCRIPT_NAME'] = __file__
464 process_cgi_request()
[8296]465 elif sys.argv[1] == "static":
466 items = dict()
467 for node in get_hostlist():
468 items['node'] = node
469 items['wdir'] = "./static/%(node)s" % items
470 if not os.path.isdir(items['wdir']):
471 os.makedirs(items['wdir'])
472 for config in files:
473 items['config'] = config
474 print "## Generating %(node)s %(config)s" % items
475 f = open("%(wdir)s/%(config)s" % items, "w")
476 f.write(generate_config(node, config))
477 f.close()
[8267]478 else:
479 usage()
480 else:
481 cgitb.enable()
482 process_cgi_request()
483
[8257]484
[8267]485if __name__ == "__main__":
486 main()
Note: See TracBrowser for help on using the repository browser.