source: genesis/nodes/gformat.py@ 8320

Last change on this file since 8320 was 8319, checked in by rick, 15 years ago

Initial status output

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