source: genesis/nodes/gformat.py@ 8272

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