1 | #!/usr/bin/python |
---|
2 | #import federation.topdl as topdl |
---|
3 | from deter import topdl |
---|
4 | |
---|
5 | # convert dotted quad netmask to CIDR / notation (i.e., 255.255.255.0 -> 24) |
---|
6 | # thanks faber |
---|
7 | def 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 :) |
---|
18 | def 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 | |
---|
31 | class 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 | #WE handle portal configuration manually since we do it in the commander! |
---|
47 | self.portal_iip = None |
---|
48 | self.portal_substrate = None |
---|
49 | def isPortal(self): |
---|
50 | if self.portal_iip and self.portal_substrate: |
---|
51 | return True |
---|
52 | else: |
---|
53 | return False |
---|
54 | def getPortalInfo(self): |
---|
55 | return (self.portal_iip,self.portal_substrate) |
---|
56 | def to_file(self,name): |
---|
57 | f = open(name, 'w+') |
---|
58 | f.write(self.gut) |
---|
59 | def k_from_topologyxml(self,name): |
---|
60 | # load the topology |
---|
61 | topo = topdl.topology_from_xml(filename=name, top='experiment') |
---|
62 | self.k_from_topology(topo) |
---|
63 | def k_from_topology(self,topo): |
---|
64 | nodedef = "" |
---|
65 | landef = "" |
---|
66 | nodeinst = "" |
---|
67 | laninst = "" |
---|
68 | connection = "" |
---|
69 | daemon = "/etc/quagga/daemons" |
---|
70 | zebra = "/etc/quagga/zebra.conf" |
---|
71 | ospfd = "/etc/quagga/ospfd.conf" |
---|
72 | for elt in topo.elements: |
---|
73 | if isinstance(elt, topdl.Computer) and (not elt.get_attribute('portal') or elt.get_attribute('portal') == 'false'): |
---|
74 | name = str(elt.name) |
---|
75 | nodedef = nodedef + 'nodeclass %sclass {\nmethod "thru"\npartition 1\nostype "FreeBSD"\n' % name |
---|
76 | scenario = "" |
---|
77 | interfaces = "" |
---|
78 | networks = "" |
---|
79 | for (counter,iface) in enumerate(elt.interface): |
---|
80 | nodedef = nodedef + "netif media FastEthernet\n" |
---|
81 | ip = str(iface.get_attribute('ip4_address')) |
---|
82 | mask = str(iface.get_attribute('ip4_netmask')) |
---|
83 | ifname = str(iface.name) |
---|
84 | substrate = str(iface.substrate[0]) |
---|
85 | scenario = scenario + ("callw \"/sbin/ifconfig\" self.netif[%d].rname \"%s\" \"netmask\" \"%s\"\n" %(counter,ip,mask) ) |
---|
86 | interfaces += "callw \"/bin/echo\" \"interface\" self.netif[%d].rname >> \"%s\"\n" %(counter,zebra) |
---|
87 | connection = connection + "attach %s.netif[%d] %s[0]\n" %(name,counter,substrate) |
---|
88 | prefix = netmask_to_slash(mask); |
---|
89 | gateway = "%s/%d" % (get_network(ip,int(prefix)),prefix ) |
---|
90 | networks += "callw \"/bin/echo\" \" network %s area 0.0.0.2\" >> \"%s\"\n" % (gateway,ospfd) |
---|
91 | if scenario != "": |
---|
92 | scenario += "callw \"/bin/echo\" \"zebra=yes\" > \"%s\"\n" % (daemon) |
---|
93 | scenario += "callw \"/bin/echo\" \"bgpd=no\" >> \"%s\"\n" %(daemon) |
---|
94 | scenario += "callw \"/bin/echo\" \"ospfd=yes\" >> \"%s\"\n" %(daemon) |
---|
95 | scenario += "callw \"/bin/echo\" \"ospf6d=no\" >> \"%s\"\n" %(daemon) |
---|
96 | scenario += "callw \"/bin/echo\" \"ripd=no\" >> \"%s\"\n" %(daemon) |
---|
97 | scenario += "callw \"/bin/echo\" \"ripngd=no\" >> \"%s\"\n" %(daemon) |
---|
98 | scenario += "callw \"/bin/echo\" \"isisd=no\" >> \"%s\"\n" %(daemon) |
---|
99 | scenario += "callw \"/bin/echo\" \"hostname %s\" > \"%s\"\n" % (name,zebra) |
---|
100 | scenario += interfaces |
---|
101 | scenario += "callw \"/bin/echo\" \"hostname %s\" > \"%s\"\n" % (name,ospfd) |
---|
102 | scenario += "callw \"/bin/echo\" \"router ospf\" >> \"%s\"\n" % (ospfd) |
---|
103 | scenario += networks |
---|
104 | scenario += "callw \"/bin/chown\" \"quagga.quaggavty\" \"/etc/quagga/*.conf\"\n" |
---|
105 | scenario += "callw \"/bin/chown\" \"640\" \"/etc/quagga/*.conf\"\n" |
---|
106 | scenario += "callw \"/etc/init.d/quagga\" \"restart\"\n" |
---|
107 | scenario = "scenario { \n netiffit \"/usr/bin/ifscan\"\n%s\n}\n" % (scenario) |
---|
108 | nodedef = nodedef + scenario |
---|
109 | nodedef = nodedef + "}\n" |
---|
110 | nodeinst = nodeinst + "nodeset %s class %sclass num 1\n" % (name,name) |
---|
111 | elif isinstance(elt, topdl.Computer) and elt.get_attribute('portal') == 'true': |
---|
112 | for iface in elt.interface: |
---|
113 | #We need a portal! we handle outside kuma! |
---|
114 | if not iface.get_attribute('portal'): |
---|
115 | self.portal_iip = str(iface.get_attribute('ip4_address')) |
---|
116 | self.portal_substrate = str(iface.substrate[0]) |
---|
117 | #This should be replaced to specificlly define each LAN when required! |
---|
118 | lanclass = "ethernet" |
---|
119 | landef = "netclass %s {\nmedia FastEthernet\n}\n" % lanclass |
---|
120 | for sub in topo.substrates: |
---|
121 | sname = str(sub.name) |
---|
122 | sname = sname.replace('-','') |
---|
123 | laninst = laninst + "netset %s class %s num 1\n" % (sname,lanclass) |
---|
124 | laninst += "%s[0].ipaddrrange = \"192.168.33.0/24\"\n" % (sname) |
---|
125 | self.gut = 'rmanager ipaddr "%s" port "%s"\n' % (self.rmanager.split(':')[0], self.rmanager.split(':')[1]); |
---|
126 | self.gut += 'smanager ipaddr "%s" port "%s"\n' % (self.smanager.split(':')[0],self.smanager.split(':')[1] ) |
---|
127 | self.gut += 'pmanager ipaddr "%s" port "%s"\n' % (self.pmanager.split(':')[0],self.pmanager.split(':')[1] ) |
---|
128 | self.gut += 'fncp ipaddr "%s"\n' % (self.fncp) |
---|
129 | self.gut += 'tftpdman ipaddr "%s"\n' % (self.tftpdman) |
---|
130 | |
---|
131 | self.gut += 'wolagent ipaddr "%s" port "%s" ipaddrrange "%s"\n' % (self.wolagent.split(':')[0],self.wolagent.split(':')[1],self.wolagent.split(':')[2]) |
---|
132 | |
---|
133 | self.gut += 'user "%s" "%s@starbed.org"\n' % (self.user,self.user) |
---|
134 | self.gut += 'project "%s"\n' % (self.project) |
---|
135 | self.gut += 'encd ipaddr "%s"\n' % (self.encd) |
---|
136 | self.gut += "sparenodemin %d\n" % (self.sparenodemin) |
---|
137 | self.gut += "sparenoderatio %d\n" % (self.sparenoderatio) |
---|
138 | |
---|
139 | self.gut += nodedef |
---|
140 | self.gut += landef |
---|
141 | self.gut += nodeinst |
---|
142 | self.gut += laninst |
---|
143 | self.gut += connection |
---|
144 | self.gut += "scenario { \n sleep 10000 \n }\n" |
---|