[adf8517] | 1 | #!/usr/bin/python |
---|
| 2 | import federation.topdl as topdl |
---|
| 3 | |
---|
| 4 | # convert dotted quad netmask to CIDR / notation (i.e., 255.255.255.0 -> 24) |
---|
| 5 | # thanks faber |
---|
| 6 | def netmask_to_slash(nm): |
---|
| 7 | m = long(0) |
---|
| 8 | for i, b in enumerate([ int(x) for x in nm.split(".") ]): |
---|
| 9 | m += (b & 0xff) << 8 * (3 - i) |
---|
| 10 | count = 0 |
---|
| 11 | while m & 0x80000000: |
---|
| 12 | m <<= 1 |
---|
| 13 | count += 1 |
---|
| 14 | return count |
---|
| 15 | #Get ip addresses gateway by zeroing the host bits |
---|
| 16 | #thanks to me :) |
---|
| 17 | def get_network(ip,prefix): |
---|
| 18 | counter=32; |
---|
| 19 | result = "" |
---|
| 20 | for x in reversed(ip.split(".")): |
---|
| 21 | if counter > prefix: |
---|
| 22 | b = 0xff; |
---|
| 23 | b <<= (counter-prefix) |
---|
| 24 | result = "%d." % (int(x) & b) + result |
---|
| 25 | else: |
---|
| 26 | result = x + "." + result |
---|
| 27 | counter -= 8; |
---|
| 28 | return result.rstrip('.') |
---|
| 29 | |
---|
| 30 | class klanguage: |
---|
| 31 | |
---|
| 32 | def __init__ (self,rmanager,smanager,pmanager,fncp,user,project,tftpdman="localhost",wolagent="localhost:5959",encd="localhost",sparenodemin=0,sparenoderatio=100): |
---|
| 33 | self.rmanager = rmanager |
---|
| 34 | self.smanager = smanager |
---|
| 35 | self.pmanager = pmanager |
---|
| 36 | self.fncp = fncp |
---|
| 37 | self.tftpdman = tftpdman |
---|
| 38 | self.wolagent = wolagent |
---|
| 39 | self.user = user |
---|
| 40 | self.project = project |
---|
| 41 | self.encd = encd |
---|
| 42 | self.sparenodemin = sparenodemin |
---|
| 43 | self.sparenoderatio = sparenoderatio |
---|
| 44 | self.gut = "" |
---|
| 45 | def to_file(self,name): |
---|
| 46 | f = open(name, 'w+') |
---|
| 47 | f.write(self.gut) |
---|
| 48 | def k_from_topologyxml(self,name): |
---|
| 49 | # load the topology |
---|
| 50 | topo = topdl.topology_from_xml(filename=name, top='experiment') |
---|
| 51 | self.k_from_topology(topo) |
---|
| 52 | def k_from_topology(self,topo): |
---|
| 53 | nodedef = "" |
---|
| 54 | landef = "" |
---|
| 55 | nodeinst = "" |
---|
| 56 | laninst = "" |
---|
| 57 | connection = "" |
---|
| 58 | daemon = "/etc/quagga/daemons" |
---|
| 59 | zebra = "/etc/quagga/zebra.conf" |
---|
| 60 | ospfd = "/etc/quagga/ospfd.conf" |
---|
| 61 | for elt in topo.elements: |
---|
| 62 | if isinstance(elt, topdl.Computer): |
---|
| 63 | name = str(elt.name) |
---|
| 64 | nodedef = nodedef + 'nodeclass %sclass {\nmethod "thru"\npartition 1\nostype "FreeBSD"\n' % name |
---|
| 65 | scenario = "" |
---|
| 66 | interfaces = "" |
---|
| 67 | networks = "" |
---|
| 68 | for (counter,iface) in enumerate(elt.interface): |
---|
| 69 | nodedef = nodedef + "netif media FastEthernet\n" |
---|
| 70 | ip = str(iface.get_attribute('ip4_address')) |
---|
| 71 | mask = str(iface.get_attribute('ip4_netmask')) |
---|
| 72 | ifname = str(iface.name) |
---|
| 73 | substrate = str(iface.substrate[0]) |
---|
| 74 | scenario = scenario + ("callw \"/sbin/ifconfig\" self.netif[%d].rname \"%s\" \"netmask\" \"%s\"\n" %(counter,ip,mask) ) |
---|
| 75 | interfaces += "callw \"/bin/echo\" \"interface\" self.netif[%d].rname >> \"%s\"\n" %(counter,zebra) |
---|
| 76 | connection = connection + "attach %s.netif[%d] %s[0]\n" %(name,counter,substrate) |
---|
| 77 | prefix = netmask_to_slash(mask); |
---|
| 78 | gateway = "%s/%d" % (get_network(ip,int(prefix)),prefix ) |
---|
| 79 | networks += "callw \"/bin/echo\" \" network %s area 0.0.0.2\" >> \"%s\"\n" % (gateway,ospfd) |
---|
| 80 | if scenario != "": |
---|
| 81 | scenario += "callw \"/bin/echo\" \"zebra=yes\" > \"%s\"\n" % (daemon) |
---|
| 82 | scenario += "callw \"/bin/echo\" \"bgpd=no\" >> \"%s\"\n" %(daemon) |
---|
| 83 | scenario += "callw \"/bin/echo\" \"ospfd=yes\" >> \"%s\"\n" %(daemon) |
---|
| 84 | scenario += "callw \"/bin/echo\" \"ospf6d=no\" >> \"%s\"\n" %(daemon) |
---|
| 85 | scenario += "callw \"/bin/echo\" \"ripd=no\" >> \"%s\"\n" %(daemon) |
---|
| 86 | scenario += "callw \"/bin/echo\" \"ripngd=no\" >> \"%s\"\n" %(daemon) |
---|
| 87 | scenario += "callw \"/bin/echo\" \"isisd=no\" >> \"%s\"\n" %(daemon) |
---|
| 88 | scenario += "callw \"/bin/echo\" \"hostname %s\" > \"%s\"\n" % (name,zebra) |
---|
| 89 | scenario += interfaces |
---|
| 90 | scenario += "callw \"/bin/echo\" \"hostname %s\" > \"%s\"\n" % (name,ospfd) |
---|
| 91 | scenario += "callw \"/bin/echo\" \"router ospf\" >> \"%s\"\n" % (ospfd) |
---|
| 92 | scenario += networks |
---|
| 93 | scenario += "callw \"/bin/chown\" \"quagga.quaggavty\" \"/etc/quagga/*.conf\"\n" |
---|
| 94 | scenario += "callw \"/bin/chown\" \"640\" \"/etc/quagga/*.conf\"\n" |
---|
| 95 | scenario += "callw \"/etc/init.d/quagga\" \"restart\"\n" |
---|
| 96 | scenario = "scenario { \n netiffit \"/usr/bin/ifscan\"\n%s\n}\n" % (scenario) |
---|
| 97 | nodedef = nodedef + scenario |
---|
| 98 | nodedef = nodedef + "}\n" |
---|
| 99 | nodeinst = nodeinst + "nodeset %s class %sclass num 1\n" % (name,name) |
---|
| 100 | |
---|
| 101 | #This should be replaced to specificlly define each LAN when required! |
---|
| 102 | lanclass = "ethernet" |
---|
| 103 | landef = "netclass %s {\nmedia FastEthernet\n}\n" % lanclass |
---|
| 104 | for sub in topo.substrates: |
---|
| 105 | sname = str(sub.name) |
---|
| 106 | laninst = laninst + "netset %s class %s num 1\n" % (sname,lanclass) |
---|
| 107 | laninst += "%s[0].ipaddrrange = \"192.168.33.0/24\"\n" % (sname) |
---|
| 108 | self.gut = 'rmanager ipaddr "%s" port "%s"\n' % (self.rmanager.split(':')[0], self.rmanager.split(':')[1]); |
---|
| 109 | self.gut += 'smanager ipaddr "%s" port "%s"\n' % (self.smanager.split(':')[0],self.smanager.split(':')[1] ) |
---|
| 110 | self.gut += 'pmanager ipaddr "%s" port "%s"\n' % (self.pmanager.split(':')[0],self.pmanager.split(':')[1] ) |
---|
| 111 | self.gut += 'fncp ipaddr "%s"\n' % (self.fncp) |
---|
| 112 | self.gut += 'tftpdman ipaddr "%s"\n' % (self.tftpdman) |
---|
| 113 | |
---|
| 114 | self.gut += 'wolagent ipaddr "%s" port "%s" ipaddrrange "%s"\n' % (self.wolagent.split(':')[0],self.wolagent.split(':')[1],self.wolagent.split(':')[2]) |
---|
| 115 | |
---|
| 116 | self.gut += 'user "%s" "%s@starbed.org"\n' % (self.user,self.user) |
---|
| 117 | self.gut += 'project "%s"\n' % (self.project) |
---|
| 118 | self.gut += 'encd ipaddr "%s"\n' % (self.encd) |
---|
| 119 | self.gut += "sparenodemin %d\n" % (self.sparenodemin) |
---|
| 120 | self.gut += "sparenoderatio %d\n" % (self.sparenoderatio) |
---|
| 121 | |
---|
| 122 | self.gut += nodedef |
---|
| 123 | self.gut += landef |
---|
| 124 | self.gut += nodeinst |
---|
| 125 | self.gut += laninst |
---|
| 126 | self.gut += connection |
---|
| 127 | self.gut += "scenario { \n sleep 10000 \n }\n" |
---|