source: genesis/nodes/gformat.py@ 8263

Last change on this file since 8263 was 8262, checked in by rick, 16 years ago

More pretty and informative

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