Changeset 9283 in genesis
- Timestamp:
- Jun 23, 2011, 6:33:00 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
tools/gformat.py
r8948 r9283 33 33 from yaml import Loader, Dumper 34 34 35 35 36 36 37 37 if os.environ.has_key('CONFIGROOT'): 38 38 NODE_DIR = os.environ['CONFIGROOT'] 39 39 else: 40 NODE_DIR = os.path. dirname(__file__) + '/../nodes'40 NODE_DIR = os.path.abspath(os.path.dirname(__file__)) + '/../nodes' 41 41 __version__ = '$Id$' 42 42 43 43 44 files = [ 44 files = [ 45 45 'authorized_keys', 46 46 'dnsmasq.conf', … … 82 82 83 83 def angle_between_points(lat1,lat2,long1,long2): 84 """ 84 """ 85 85 Return Angle in radians between two GPS coordinates 86 86 See: http://stackoverflow.com/questions/3809179/angle-between-2-gps-coordinates … … 103 103 if degrees < p: 104 104 return "n" 105 elif degrees < (90 - p): 105 elif degrees < (90 - p): 106 106 return "no" 107 elif degrees < (90 + p): 107 elif degrees < (90 + p): 108 108 return "o" 109 elif degrees < (180 - p): 109 elif degrees < (180 - p): 110 110 return "zo" 111 elif degrees < (180 + p): 111 elif degrees < (180 + p): 112 112 return "z" 113 elif degrees < (270 - p): 113 elif degrees < (270 - p): 114 114 return "zw" 115 elif degrees < (270 + p): 115 elif degrees < (270 + p): 116 116 return "w" 117 elif degrees < (360 - p): 117 elif degrees < (360 - p): 118 118 return "nw" 119 119 else: … … 123 123 def generate_title(nodelist): 124 124 """ Main overview page """ 125 items = {'root' : "." } 125 items = {'root' : "." } 126 126 output = """ 127 127 <html> … … 173 173 def generate_header(ctag="#"): 174 174 return """\ 175 %(ctag)s 175 %(ctag)s 176 176 %(ctag)s DO NOT EDIT - Automatically generated by 'gformat' 177 177 %(ctag)s Generated at %(date)s by %(host)s 178 %(ctag)s 178 %(ctag)s 179 179 """ % { 'ctag' : ctag, 'date' : time.ctime(), 'host' : socket.gethostname() } 180 180 … … 204 204 ip_canidate = ip_canidate & ~((1 << (32 - mask)) - 1) 205 205 return ip_addr == ip_canidate 206 206 207 207 208 208 … … 218 218 output = generate_header() 219 219 output += """\ 220 # DHCP server options 220 # DHCP server options 221 221 dhcp-authoritative 222 222 dhcp-fqdn … … 246 246 datadump[iface_key]['dhcp_stop'] = dhcp_part + "." + dhcp_stop 247 247 output += "dhcp-range=%(interface)s,%(dhcp_start)s,%(dhcp_stop)s,%(subnet)s,24h\n\n" % datadump[iface_key] 248 248 249 249 return output 250 250 … … 256 256 output += """\ 257 257 hostname='%(nodetype)s%(nodename)s.%(domain)s' 258 location='%(location)s' 258 location='%(location)s' 259 259 """ % datadump 260 260 261 261 # TProxy configuration 262 262 output += "\n" … … 269 269 except KeyError: 270 270 output += "tproxy_enable='NO'\n" 271 271 272 272 output += '\n' 273 273 # lo0 configuration: 274 274 # - 172.32.255.1/32 is the proxy.wleiden.net deflector 275 # - masterip is special as it needs to be assigned to at 275 # - masterip is special as it needs to be assigned to at 276 276 # least one interface, so if not used assign to lo0 277 277 addrs_list = { 'lo0' : ["127.0.0.1/8", "172.31.255.1/32"] } 278 iface_map = {'lo0' : 'lo0'} 278 iface_map = {'lo0' : 'lo0'} 279 279 280 280 masterip_used = False … … 283 283 masterip_used = True 284 284 break 285 if not masterip_used: 285 if not masterip_used: 286 286 addrs_list['lo0'].append(datadump['masterip'] + "/32") 287 287 … … 307 307 if ifacedump['type'] in ['11a', '11b', '11g', 'wireless']: 308 308 # Create wlanX interface 309 ifacedump['wlanif'] ="wlan%i" % wlan_count 309 ifacedump['wlanif'] ="wlan%i" % wlan_count 310 310 iface_map[interface] = ifacedump['wlanif'] 311 311 wlan_count += 1 … … 318 318 ifacedump['mode'] = '11b' 319 319 if ifacedump['type'] in ['11a', '11b' '11g']: 320 ifacedump['mode'] = ifacedump['type'] 320 ifacedump['mode'] = ifacedump['type'] 321 321 322 322 if not ifacedump.has_key('channel'): … … 333 333 output += ("create_args_%(wlanif)s='wlanmode %(wlanmode)s mode " +\ 334 334 "%(mode)s ssid %(ssid)s %(extra)s channel %(channel)s'\n") % ifacedump 335 335 336 336 elif ifacedump['type'] in ['ethernet', 'eth']: 337 337 # No special config needed besides IP … … 340 340 assert False, "Unknown type " + ifacedump['type'] 341 341 342 # Print IP address which needs to be assigned over here 342 # Print IP address which needs to be assigned over here 343 343 output += "\n" 344 344 for iface,addrs in sorted(addrs_list.iteritems()): … … 418 418 nameserver 127.0.0.1 419 419 420 # Proxies are recursive nameservers 420 # Proxies are recursive nameservers 421 421 # needs to be in resolv.conf for dnsmasq as well 422 422 """ % datadump 423 423 424 424 for proxy in get_proxylist(): 425 425 proxy_ip = get_yaml(proxy)['masterip'] … … 435 435 else: 436 436 output = value 437 return output 437 return output 438 438 439 439 … … 441 441 def format_wleiden_yaml(datadump): 442 442 """ Special formatting to ensure it is editable""" 443 output = "# Genesis config yaml style\n" 443 output = "# Genesis config yaml style\n" 444 444 output += "# vim:ts=2:et:sw=2:ai\n" 445 445 output += "#\n" … … 447 447 for key in sorted(set(datadump.keys()) - set(iface_keys)): 448 448 output += "%-10s: %s\n" % (key, format_yaml_value(datadump[key])) 449 449 450 450 output += "\n\n" 451 451 452 452 key_order = [ 'comment', 'interface', 'ip', 'desc', 'sdesc', 'mode', 'type', 453 453 'extra_type', 'channel', 'ssid', 'dhcp' ] … … 457 457 for key in key_order + list(sorted(set(datadump[iface_key].keys()) - set(key_order))): 458 458 if datadump[iface_key].has_key(key): 459 output += " %-11s: %s\n" % (key, format_yaml_value(datadump[iface_key][key])) 459 output += " %-11s: %s\n" % (key, format_yaml_value(datadump[iface_key][key])) 460 460 output += "\n\n" 461 461 … … 473 473 def generate_yaml(datadump): 474 474 return generate_config(datadump['nodename'], "wleiden.yaml", datadump) 475 475 476 476 477 477 … … 483 483 if datadump == None: 484 484 datadump = get_yaml(node) 485 485 486 486 # Preformat certain needed variables for formatting and push those into special object 487 487 datadump_extra = copy.deepcopy(datadump) … … 504 504 output += generate_resolv_conf(datadump_extra) 505 505 else: 506 assert False, "Config not found!" 506 assert False, "Config not found!" 507 507 except IOError, e: 508 508 output += "[ERROR] Config file not found" … … 522 522 print "[INFO] All done, redirecting in 5 seconds" 523 523 sys.exit(0) 524 525 524 525 526 526 uri = os.environ['PATH_INFO'].strip('/').split('/') 527 527 output = "" … … 547 547 fqdn = datadump['nodetype'] + datadump['nodename'] 548 548 return(fqdn) 549 550 549 550 551 551 552 552 def make_dns(): … … 560 560 for node in get_hostlist(): 561 561 datadump = get_yaml(node) 562 562 563 563 # Proxy naming convention is special 564 564 fqdn = get_fqdn(datadump) … … 585 585 netmask = int(netmask) 586 586 addr = addr & ~((1 << (32 - netmask)) - 1) 587 if pool.has_key(addr): 587 if pool.has_key(addr): 588 588 pool[addr] += [(iface_name, fqdn, ip)] 589 else: 589 else: 590 590 pool[addr] = [(iface_name, fqdn, ip)] 591 591 continue … … 604 604 pool_members = [k[1] for k in value] 605 605 for item in value: 606 (iface_name, fqdn, ip) = item 606 (iface_name, fqdn, ip) = item 607 607 pool_name = "2pool-" + showaddr(key).replace('.','-') + "-" + "-".join(sorted(list(set(pool_members) - set([fqdn])))) 608 608 wleiden_zone["%s.%s" % (pool_name, fqdn)] = ip … … 622 622 else: 623 623 wleiden_cname[k] = v 624 624 625 625 details = dict() 626 626 # 24 updates a day allowed … … 635 635 \n''' 636 636 637 637 638 638 if not os.path.isdir('dns'): 639 639 os.makedirs('dns') … … 644 644 for host,ip in wleiden_zone.iteritems(): 645 645 if valid_addr(ip): 646 f.write("%s.wleiden.net. IN A %s \n" % (host.lower(), ip)) 646 f.write("%s.wleiden.net. IN A %s \n" % (host.lower(), ip)) 647 647 for source,dest in wleiden_cname.iteritems(): 648 648 f.write("%s.wleiden.net. IN CNAME %s.wleiden.net.\n" % (source.lower(), dest.lower())) 649 649 f.close() 650 650 651 651 # Create whole bunch of specific sub arpa zones. To keep it compliant 652 652 for s in range(16,32): … … 661 661 if int(ip.split('.')[1]) == s: 662 662 rev_ip = '.'.join(reversed(ip.split('.'))) 663 f.write("%s.in-addr.arpa. IN PTR %s.wleiden.net.\n" % (rev_ip.lower(), host.lower())) 663 f.write("%s.in-addr.arpa. IN PTR %s.wleiden.net.\n" % (rev_ip.lower(), host.lower())) 664 664 f.close() 665 665 … … 672 672 \tstatic = Generate all config files and store on disk 673 673 \t with format ./static/%%NODE%%/%%FILE%% 674 \ttest CNodeRick dnsmasq.conf = Receive output of CGI script 674 \ttest CNodeRick dnsmasq.conf = Receive output of CGI script 675 675 \t for arguments CNodeRick/dnsmasq.conf 676 676 """ … … 685 685 if len(sys.argv) < 2: 686 686 usage() 687 687 688 688 if sys.argv[1] == "standalone": 689 689 import SocketServer … … 695 695 except (IndexError,ValueError): 696 696 PORT = 8000 697 697 698 698 class MyCGIHTTPRequestHandler(CGIHTTPServer.CGIHTTPRequestHandler): 699 699 """ Serve this CGI from the root of the webserver """ … … 701 701 if "favicon" in self.path: 702 702 return False 703 703 704 704 self.cgi_info = (__file__, self.path) 705 705 self.path = '' … … 709 709 httpd.server_name = 'localhost' 710 710 httpd.server_port = PORT 711 711 712 712 print "serving at port", PORT 713 713 try: … … 735 735 elif sys.argv[1] == "dns": 736 736 make_dns() 737 elif sys.argv[1] == "cleanup": 737 elif sys.argv[1] == "cleanup": 738 738 # First generate all datadumps 739 739 datadumps = dict() … … 741 741 datadump = get_yaml(host) 742 742 datadumps[get_fqdn(datadump)] = datadump 743 743 744 744 datadump['latitude'], datadump['longitude'] = rdnap.rd2etrs(datadump['rdnap_x'], datadump['rdnap_y']) 745 745 write_yaml(host, datadump) 746 else: 747 usage() 748 else: 749 cgitb.enable() 750 process_cgi_request() 751 752 753 if __name__ == "__main__": 754 main() 746 else: 747 usage() 748 else: 749 cgitb.enable() 750 process_cgi_request() 751 752 753 if __name__ == "__main__": 754 main()
Note:
See TracChangeset
for help on using the changeset viewer.