Changeset 8588 in genesis for nodes/gformat.py
- Timestamp:
- Oct 30, 2010, 2:28:19 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
nodes/gformat.py
r8585 r8588 14 14 import sys 15 15 import time 16 import make_map 16 17 from pprint import pprint 17 18 try: … … 21 22 print "[ERROR] Please install the python-yaml or devel/py-yaml package" 22 23 exit(1) 24 25 try: 26 from yaml import CLoader as Loader 27 from yaml import CDumper as Dumper 28 except ImportError: 29 from yaml import Loader, Dumper 30 23 31 24 32 … … 65 73 """ Combined hosts and proxy list""" 66 74 return get_nodelist() + get_proxylist() 75 76 def angle_between_points(lat1,lat2,long1,long2): 77 """ 78 Return Angle in radians between two GPS coordinates 79 See: http://stackoverflow.com/questions/3809179/angle-between-2-gps-coordinates 80 """ 81 dy = lat2 - lat1 82 dx = math.cos(math.pi/180*lat1)*(long2 - long1) 83 angle = math.atan2(dy,dx) 84 return angle 85 86 def angle_to_cd(angle): 87 """ Return Dutch Cardinal Direction estimation in 'one digit' of radian angle """ 88 89 # For easy conversion get positive degree 90 degrees = math.degrees(angle) 91 if degrees < 0: 92 360 - abs(degrees) 93 94 # Numbers can be confusing calculate from the 4 main directions 95 p = 22.5 96 if degrees < p: 97 return "n" 98 elif degrees < (90 - p): 99 return "no" 100 elif degrees < (90 + p): 101 return "o" 102 elif degrees < (180 - p): 103 return "zo" 104 elif degrees < (180 + p): 105 return "z" 106 elif degrees < (270 - p): 107 return "zw" 108 elif degrees < (270 + p): 109 return "w" 110 elif degrees < (360 - p): 111 return "nw" 112 else: 113 return "n" 67 114 68 115 … … 300 347 301 348 f = open(gfile, 'r') 302 datadump = yaml.load(f )349 datadump = yaml.load(f,Loader=Loader) 303 350 f.close() 304 351 305 352 return datadump 353 354 def store_yaml(datadump): 355 """ Store configuration yaml for 'item'""" 356 gfile = NODE_DIR + '/%s/wleiden.yaml' % item 357 358 f = open(gfile, 'w') 359 f.write(generate_wleiden_yaml(datadump)) 360 f.close() 306 361 307 362 … … 408 463 return output 409 464 465 466 def generate_yaml(datadump): 467 return generate_config(datadump['nodename'], "wleiden.yaml", datadump) 468 410 469 411 470 … … 472 531 assert False, "Invalid option" 473 532 print output 533 534 def get_fqdn(datadump): 535 # Proxy naming convention is special 536 if datadump['nodetype'] == 'Proxy': 537 fqdn = datadump['nodename'] 538 else: 539 # By default the full name is listed and also a shortname CNAME for easy use. 540 fqdn = datadump['nodetype'] + datadump['nodename'] 541 return(fqdn) 542 543 544 545 def make_dns(): 546 items = dict() 547 # hostname is key, IP is value 548 wleiden_zone = dict() 549 wleiden_cname = dict() 550 pool = dict() 551 for node in get_hostlist(): 552 datadump = get_yaml(node) 553 554 # Proxy naming convention is special 555 fqdn = get_fqdn(datadump) 556 if datadump['nodetype'] == 'CNode': 557 wleiden_cname[datadump['nodename']] = fqdn 558 559 wleiden_zone[fqdn] = datadump['masterip'] 560 561 #items['node'] = node 562 #items['wdir'] = "./static/%(node)s" % items 563 #if not os.path.isdir(items['wdir']): 564 # os.makedirs(items['wdir']) 565 for iface_key in get_interface_keys(datadump): 566 iface_name = datadump[iface_key]['interface'].replace(':',"_alias_") 567 (ip, netmask) = datadump[iface_key]['ip'].split('/') 568 try: 569 (dhcp_start, dhcp_stop) = datadump[iface_key]['dhcp'].split('-') 570 datadump[iface_key]['subnet'] = netmask2subnet(netmask) 571 dhcp_part = ".".join(ip.split('.')[0:3]) 572 if ip != datadump['masterip']: 573 wleiden_zone["dhcp-gateway-%s.%s" % (iface_name, fqdn)] = ip 574 for i in range(int(dhcp_start), int(dhcp_stop) + 1): 575 wleiden_zone["dhcp-%s-%s.%s" % (i, iface_name, fqdn)] = "%s.%s" % (dhcp_part, i) 576 except (AttributeError, ValueError): 577 # First push it into a pool, to indentify the counter-part later on 578 addr = parseaddr(ip) 579 netmask = int(netmask) 580 addr = addr & ~((1 << (32 - netmask)) - 1) 581 if pool.has_key(addr): 582 pool[addr] += [(iface_name, fqdn, ip)] 583 else: 584 pool[addr] = [(iface_name, fqdn, ip)] 585 continue 586 587 # wleiden_zone["2%s-%s.%s" % ("unused", iface_name, fqdn)] = ip 588 # #XXX: automatic naming convention namely 2 + remote.lower() 589 for (key,value) in pool.iteritems(): 590 if len(value) == 1: 591 (iface_name, fqdn, ip) = value[0] 592 wleiden_zone["2unused-%s.%s" % (iface_name, fqdn)] = ip 593 elif len(value) == 2: 594 (a_iface_name, a_fqdn, a_ip) = value[0] 595 (b_iface_name, b_fqdn, b_ip) = value[1] 596 wleiden_zone["2%s.%s" % (b_fqdn,a_fqdn)] = a_ip 597 wleiden_zone["2%s.%s" % (a_fqdn,b_fqdn)] = b_ip 598 else: 599 pool_members = [k[1] for k in value] 600 for item in value: 601 (iface_name, fqdn, ip) = item 602 pool_name = "2pool-" + showaddr(key).replace('.','-') + "-" + "_".join(sorted(list(set(pool_members) - set([fqdn])))) 603 wleiden_zone["%s.%s" % (pool_name, fqdn)] = ip 604 605 606 f = open("db.wleiden.net", "w") 607 for host,ip in wleiden_zone.iteritems(): 608 f.write("%s.wleiden.net. A %s ~\n" % (host, ip)) 609 for source,dest in wleiden_cname.iteritems(): 610 f.write("%s.wleiden.net. CNAME %s.wleiden.net. ~\n" % (source, dest)) 611 f.close() 612 f = open("db.172.in-addr.arpa", "w") 613 for host,ip in wleiden_zone.iteritems(): 614 rev_ip = '.'.join(reversed(ip.split('.'))) 615 f.write("%s.in-addr.arpa. PTR %s.wleiden.net. ~\n" % (rev_ip, host)) 616 f.close() 474 617 475 618 … … 537 680 f.close() 538 681 elif sys.argv[1] == "dns": 539 items = dict() 540 # hostname is key, IP is value 541 wleiden_zone = dict() 542 wleiden_cname = dict() 543 pool = dict() 544 for node in get_hostlist(): 545 datadump = get_yaml(node) 546 547 # Proxy naming convention is special 548 if datadump['nodetype'] == 'Proxy': 549 fqdn = datadump['nodename'] 550 else: 551 # By default the full name is listed and also a shortname CNAME for easy use. 552 fqdn = datadump['nodetype'] + datadump['nodename'] 553 wleiden_cname[datadump['nodename']] = fqdn 554 wleiden_zone[fqdn] = datadump['masterip'] 555 556 #items['node'] = node 557 #items['wdir'] = "./static/%(node)s" % items 558 #if not os.path.isdir(items['wdir']): 559 # os.makedirs(items['wdir']) 560 for iface_key in get_interface_keys(datadump): 561 iface_name = datadump[iface_key]['interface'].replace(':',"_alias_") 562 (ip, netmask) = datadump[iface_key]['ip'].split('/') 563 try: 564 (dhcp_start, dhcp_stop) = datadump[iface_key]['dhcp'].split('-') 565 datadump[iface_key]['subnet'] = netmask2subnet(netmask) 566 dhcp_part = ".".join(ip.split('.')[0:3]) 567 if ip != datadump['masterip']: 568 wleiden_zone["dhcp-gateway-%s.%s" % (iface_name, fqdn)] = ip 569 for i in range(int(dhcp_start), int(dhcp_stop) + 1): 570 wleiden_zone["dhcp-%s-%s.%s" % (i, iface_name, fqdn)] = "%s.%s" % (dhcp_part, i) 571 except (AttributeError, ValueError): 572 # First push it into a pool, to indentify the counter-part later on 573 addr = parseaddr(ip) 574 netmask = int(netmask) 575 addr = addr & ~((1 << (32 - netmask)) - 1) 576 if pool.has_key(addr): 577 pool[addr] += [(iface_name, fqdn, ip)] 578 else: 579 pool[addr] = [(iface_name, fqdn, ip)] 580 continue 581 582 # wleiden_zone["2%s-%s.%s" % ("unused", iface_name, fqdn)] = ip 583 # #XXX: automatic naming convention namely 2 + remote.lower() 584 for (key,value) in pool.iteritems(): 585 if len(value) == 1: 586 (iface_name, fqdn, ip) = value[0] 587 wleiden_zone["2unused-%s.%s" % (iface_name, fqdn)] = ip 588 elif len(value) == 2: 589 (a_iface_name, a_fqdn, a_ip) = value[0] 590 (b_iface_name, b_fqdn, b_ip) = value[1] 591 wleiden_zone["2%s.%s" % (b_fqdn,a_fqdn)] = a_ip 592 wleiden_zone["2%s.%s" % (a_fqdn,b_fqdn)] = b_ip 593 else: 594 pool_members = [k[1] for k in value] 595 for item in value: 596 (iface_name, fqdn, ip) = item 597 pool_name = "2pool-" + showaddr(key).replace('.','-') + "-" + "_".join(sorted(list(set(pool_members) - set([fqdn])))) 598 wleiden_zone["%s.%s" % (pool_name, fqdn)] = ip 599 600 601 f = open("db.wleiden.net", "w") 602 for host,ip in wleiden_zone.iteritems(): 603 f.write("%s.wleiden.net. A %s ~\n" % (host, ip)) 604 for source,dest in wleiden_cname.iteritems(): 605 f.write("%s.wleiden.net. CNAME %s.wleiden.net.\n" % (source, dest)) 606 f.close() 607 f = open("db.172.in-addr.arpa", "w") 608 for host,ip in wleiden_zone.iteritems(): 609 rev_ip = '.'.join(reversed(ip.split('.'))) 610 f.write("%s.in-addr.arpa. PTR %s.wleiden.net. ~\n" % (rev_ip, host)) 611 f.close() 612 #pprint(pool) 613 #pprint(wleiden_zone) 614 #for config in files: 615 # items['config'] = config 616 # print "## Generating %(node)s %(config)s" % items 617 # f = open("%(wdir)s/%(config)s" % items, "w") 618 # f.write(generate_config(node, config, datadump)) 619 # f.close() 620 else: 621 usage() 622 else: 623 cgitb.enable() 624 process_cgi_request() 625 626 627 if __name__ == "__main__": 628 main() 682 make_dns() 683 elif sys.argv[1] == "cleanup": 684 # First generate all datadumps 685 datadumps = dict() 686 for host in get_hostlist(): 687 datadump = get_yaml(host) 688 datadumps[get_fqdn(datadump)] = datadump 689 690 datadump['latitude'], datadump['longitude'] = make_map.rd2etrs(datadump['rdnap_x'], datadump['rdnap_y']) 691 write_yaml(host, datadump) 692 else: 693 usage() 694 else: 695 cgitb.enable() 696 process_cgi_request() 697 698 699 if __name__ == "__main__": 700 main()
Note:
See TracChangeset
for help on using the changeset viewer.