source: genesis/nodes/gformat.py@ 8298

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

Allow datadump to be inserted as well, for speedup purposes

  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 13.5 KB
Line 
1#!/usr/bin/env python
2#
3# vim:ts=2:et:sw=2:ai
4# Wireless Leiden configuration generator, based on yaml files'
5# Rick van der Zwet <info@rickvanderzwet.nl>
6import cgi
7import cgitb
8import copy
9import glob
10import os
11import socket
12import string
13import subprocess
14import sys
15import time
16import yaml
17
18NODE_DIR = os.path.dirname(os.path.realpath(__file__))
19__version__ = '$Id: gformat.py 8298 2010-08-10 13:58:59Z rick $'
20
21
22files = [
23 'authorized_keys',
24 'dnsmasq.conf',
25 'rc.conf.local',
26 'resolv.conf',
27 'wleiden.yaml'
28 ]
29
30
31
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
48def get_hostlist():
49 """ Combined hosts and proxy list"""
50 return get_nodelist() + get_proxylist()
51
52
53def generate_title(nodelist):
54 """ Main overview page """
55 items = {'root' : "." }
56 output = """
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>
69 <form type="GET" action="%(root)s">
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
76
77 for node in nodelist:
78 items['node'] = node
79 output += '<tr><td><a href="%(root)s/%(node)s">%(node)s</a></td>' % items
80 for config in files:
81 items['config'] = config
82 output += '<td><a href="%(root)s/%(node)s/%(config)s">%(config)s</a></td>' % items
83 output += "</tr>"
84 output += """
85 </table>
86 <hr />
87 <em>%s</em>
88 </center>
89 </body>
90</html>
91 """ % __version__
92
93 return output
94
95
96
97def generate_node(node):
98 """ Print overview of all files available for node """
99 return "\n".join(files)
100
101
102
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
111
112
113def parseaddr(s):
114 """ Process IPv4 CIDR notation addr to a (binary) number """
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
121
122
123def showaddr(a):
124 """ Display IPv4 addr in (dotted) CIDR notation """
125 return "%d.%d.%d.%d" % ((a >> 24) & 0xff, (a >> 16) & 0xff, (a >> 8) & 0xff, a & 0xff)
126
127
128
129def netmask2subnet(netmask):
130 """ Given a 'netmask' return corresponding CIDR """
131 return showaddr(0xffffffff & (0xffffffff << (32 - int(netmask))))
132
133
134
135def generate_dnsmasq_conf(datadump):
136 """ Generate configuration file '/usr/local/etc/dnsmasq.conf' """
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']:
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]
154
155 try:
156 (dhcp_start, dhcp_stop) = datadump[iface_key]['dhcp'].split('-')
157 (ip, netmask) = datadump[iface_key]['ip'].split('/')
158 datadump[iface_key]['subnet'] = netmask2subnet(netmask)
159 except (AttributeError, ValueError):
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
170
171
172def generate_rc_conf_local(datadump):
173 """ Generate configuration file '/etc/rc.conf.local' """
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
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
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"
234 if ifacedump['mode'] in ['master', 'master-wds']:
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 " +\
253 "%(mode)s ssid %(ssid)s %(extra)s channel %(channel)s'\n") % ifacedump
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
268
269
270def get_yaml(item):
271 """ Get configuration yaml for 'item'"""
272 gfile = NODE_DIR + '/%s/wleiden.yaml' % item
273
274 f = open(gfile, 'r')
275 datadump = yaml.load(f)
276 f.close()
277
278 return datadump
279
280
281
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
292def generate_resolv_conf(datadump):
293 """ Generate configuration file '/etc/resolv.conf' """
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
304 for proxy in get_proxylist():
305 proxy_ip = get_yaml(proxy)['masterip']
306 output += "nameserver %-15s # %s\n" % (proxy_ip, proxy)
307 return output
308
309
310
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):
322 """ Special formatting to ensure it is editable"""
323 output = "# Genesis config yaml style\n"
324 output += "# vim:ts=2:et:sw=2:ai\n"
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)):
328 output += "%-10s: %s\n" % (key, format_yaml_value(datadump[key]))
329
330 output += "\n\n"
331
332 key_order = [ 'comment', 'interface', 'ip', 'desc', 'sdesc', 'mode', 'type',
333 'extra_type', 'channel', 'ssid', 'dhcp' ]
334
335 for iface_key in sorted(iface_keys):
336 output += "%s:\n" % iface_key
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]))
340 output += "\n\n"
341
342 return output
343
344
345
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, datadump=None):
355 """ Print configuration file 'config' of 'node' """
356 output = ""
357 try:
358 # Load config file
359 if datadump == None:
360 datadump = get_yaml(node)
361
362 # Preformat certain needed variables for formatting and push those into special object
363 datadump_extra = copy.deepcopy(datadump)
364 if not datadump_extra.has_key('domain'):
365 datadump_extra['domain'] = 'wleiden.net'
366 datadump_extra['nodename_lower'] = datadump_extra['nodename'].lower()
367 datadump_extra['iface_keys'] = sorted([elem for elem in datadump.keys() if elem.startswith('iface_')])
368
369 if config == 'wleiden.yaml':
370 output += generate_wleiden_yaml(datadump)
371 elif config == 'authorized_keys':
372 f = open("global_keys", 'r')
373 output += f.read()
374 f.close()
375 elif config == 'dnsmasq.conf':
376 output += generate_dnsmasq_conf(datadump_extra)
377 elif config == 'rc.conf.local':
378 output += generate_rc_conf_local(datadump_extra)
379 elif config == 'resolv.conf':
380 output += generate_resolv_conf(datadump_extra)
381 else:
382 assert False, "Config not found!"
383 except IOError, e:
384 output += "[ERROR] Config file not found"
385 return output
386
387
388
389def process_cgi_request():
390 """ When calling from CGI """
391 # Update repository if requested
392 form = cgi.FieldStorage()
393 if form.getvalue("action") == "update":
394 print "Refresh: 5; url=."
395 print "Content-type:text/plain\r\n\r\n",
396 print "[INFO] Updating subverion, please wait..."
397 print subprocess.Popen(['svn', 'up', NODE_DIR], stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0],
398 print "[INFO] All done, redirecting in 5 seconds"
399 sys.exit(0)
400
401
402 uri = os.environ['PATH_INFO'].strip('/').split('/')
403 output = ""
404 if not uri[0]:
405 output += "Content-type:text/html\r\n\r\n"
406 output += generate_title(get_hostlist())
407 elif len(uri) == 1:
408 output += "Content-type:text/plain\r\n\r\n"
409 output += generate_node(uri[0])
410 elif len(uri) == 2:
411 output += "Content-type:text/plain\r\n\r\n"
412 output += generate_config(uri[0], uri[1])
413 else:
414 assert False, "Invalid option"
415 print output
416
417
418def usage():
419 print """Usage: %s <standalone [port] |test [test arguments]|static>
420Examples:
421\tstandalone = Run configurator webserver [default port=8000]
422\tstatic = Generate all config files and store on disk
423\t with format ./static/%%NODE%%/%%FILE%%
424\ttest CNodeRick dnsmasq.conf = Receive output of CGI script
425\t for arguments CNodeRick/dnsmasq.conf
426"""
427 exit(0)
428
429
430
431def main():
432 """Hard working sub"""
433 # Allow easy hacking using the CLI
434 if not os.environ.has_key('PATH_INFO'):
435 if len(sys.argv) < 2:
436 usage()
437
438 if sys.argv[1] == "standalone":
439 import SocketServer
440 import CGIHTTPServer
441 try:
442 PORT = int(sys.argv[2])
443 except (IndexError,ValueError):
444 PORT = 8000
445
446 class MyCGIHTTPRequestHandler(CGIHTTPServer.CGIHTTPRequestHandler):
447 """ Serve this CGI from the root of the webserver """
448 def is_cgi(self):
449 if "favicon" in self.path:
450 return False
451
452 self.cgi_info = (__file__, self.path)
453 self.path = ''
454 return True
455 handler = MyCGIHTTPRequestHandler
456 httpd = SocketServer.TCPServer(("", PORT), handler)
457 httpd.server_name = 'localhost'
458 httpd.server_port = PORT
459
460 print "serving at port", PORT
461 httpd.serve_forever()
462 elif sys.argv[1] == "test":
463 os.environ['PATH_INFO'] = "/".join(sys.argv[2:])
464 os.environ['SCRIPT_NAME'] = __file__
465 process_cgi_request()
466 elif sys.argv[1] == "static":
467 items = dict()
468 for node in get_hostlist():
469 items['node'] = node
470 items['wdir'] = "./static/%(node)s" % items
471 if not os.path.isdir(items['wdir']):
472 os.makedirs(items['wdir'])
473 datadump = get_yaml(node)
474 for config in files:
475 items['config'] = config
476 print "## Generating %(node)s %(config)s" % items
477 f = open("%(wdir)s/%(config)s" % items, "w")
478 f.write(generate_config(node, config, datadump))
479 f.close()
480 else:
481 usage()
482 else:
483 cgitb.enable()
484 process_cgi_request()
485
486
487if __name__ == "__main__":
488 main()
Note: See TracBrowser for help on using the repository browser.