source: starbed_plugin/topdltok.py @ d070d9f

Last change on this file since d070d9f was d070d9f, checked in by ABDULLA ALWABEL <abdullaalwabel@…>, 12 years ago

Finalize cleaning!

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