rev |
line source |
igor@0
|
1 #!/usr/bin/python
|
igor@60
|
2 # vim: set fileencoding=utf-8 :
|
igor@0
|
3
|
igor@2
|
4 import sys,os,time
|
igor@35
|
5
|
igor@35
|
6 xentaur_path=os.environ['HOME']+"/xentaur"
|
igor@35
|
7
|
igor@0
|
8 sys.path.append('/etc/xen')
|
igor@35
|
9 sys.path.append(xentaur_path)
|
igor@11
|
10
|
igor@56
|
11 #network='snrs_ipsec_rsa_1'
|
igor@56
|
12 node_object={}
|
igor@56
|
13 link_object={}
|
igor@56
|
14 bridge_object={}
|
igor@56
|
15
|
igor@61
|
16 network='mini'
|
igor@61
|
17 domain='dyn1'
|
igor@61
|
18 #network='snrs'
|
igor@61
|
19 #domain='dyn1'
|
igor@35
|
20 from xendomain import *
|
igor@35
|
21
|
igor@29
|
22 bridges_turned_down=[]
|
igor@29
|
23
|
igor@0
|
24 from IPython.Shell import IPShellEmbed
|
igor@0
|
25
|
igor@2
|
26
|
igor@2
|
27 screenrc=os.environ['HOME']+"/.screenrc_xentaur"
|
igor@2
|
28
|
igor@10
|
29 def run(program, *args):
|
igor@10
|
30 pid = os.fork()
|
igor@10
|
31 if not pid:
|
igor@10
|
32 os.execvp(program, (program,) + args)
|
igor@10
|
33 return os.wait()[0]
|
igor@10
|
34
|
igor@10
|
35 def run_command(line):
|
igor@10
|
36 #cmds=line.split()
|
igor@10
|
37 #run(cmds[0],*cmds[1:])
|
igor@10
|
38 run("/bin/sh", "-c", line)
|
igor@10
|
39
|
igor@23
|
40 def run_command_return_stdout(line):
|
igor@23
|
41 p = os.popen(line)
|
igor@23
|
42 output = p.read()
|
igor@23
|
43 p.close()
|
igor@23
|
44 return output
|
igor@23
|
45
|
igor@49
|
46 ################################################################################
|
igor@49
|
47 #Xentaur command-line commands
|
igor@49
|
48
|
igor@49
|
49 ## Start
|
igor@49
|
50
|
igor@49
|
51 def start_bridges():
|
igor@38
|
52 unbound_bridges=set(bridges)-set(real_bridges)
|
igor@56
|
53 script=""
|
igor@56
|
54 script="\n".join(map(lambda x: "sudo brctl show | awk '{print $1}' | grep -qx "+x+" || sudo brctl addbr "+x, unbound_bridges))
|
igor@56
|
55 script+="\n"+"\n".join(map(lambda x: "sudo brctl stp "+x+" off", unbound_bridges))
|
igor@56
|
56 script+="\n"+"\n".join(map(lambda x: "sudo ip link set "+x+" up", unbound_bridges))
|
igor@0
|
57
|
igor@0
|
58 print """#!/bin/sh
|
igor@0
|
59 # create unbound bridges
|
igor@56
|
60 %s
|
igor@56
|
61 """ % (script)
|
igor@0
|
62
|
igor@49
|
63 def start_domain(domain):
|
igor@49
|
64 print "sudo xm create "+xentaur_path+"/xendomain.py "+" domain="+domain+" network="+network+" && sleep 1 && sudo xm sched-credit -d $(sudo xm list | grep "+domain+" | awk '{print $2}') -c 10 && sleep 1"
|
igor@0
|
65
|
igor@49
|
66 def start_domains(doms=domains):
|
igor@49
|
67 for domain in doms:
|
igor@38
|
68 if not domain in real_nodes:
|
igor@49
|
69 start_domain(domain)
|
igor@0
|
70
|
igor@49
|
71 def start_all():
|
igor@49
|
72 graph()
|
igor@49
|
73 screen()
|
igor@49
|
74 start_bridges()
|
igor@49
|
75 start_domains()
|
igor@49
|
76
|
igor@49
|
77 ## Stop
|
igor@49
|
78
|
igor@49
|
79 def stop_domain(domain,wait=0):
|
igor@49
|
80 if wait:
|
igor@49
|
81 print "sudo xm shutdown -w "+domain
|
igor@49
|
82 else:
|
igor@49
|
83 print "sudo xm shutdown "+domain
|
igor@49
|
84
|
igor@49
|
85 def stop_domains(doms=domains, wait=0):
|
igor@49
|
86 for domain in doms:
|
igor@38
|
87 if not domain in real_nodes:
|
igor@49
|
88 stop_domain(domain,wait)
|
igor@0
|
89
|
igor@49
|
90 def stop_bridges():
|
igor@49
|
91 ###FIXME###
|
igor@49
|
92 return 0
|
igor@49
|
93
|
igor@49
|
94 def stop_all(wait=0):
|
igor@49
|
95 stop_domains(domains, wait)
|
igor@49
|
96 stop_bridges()
|
igor@49
|
97
|
igor@49
|
98 def restart_all():
|
igor@49
|
99 stop_all(1)
|
igor@49
|
100 start_all()
|
igor@49
|
101
|
igor@56
|
102 ####################################################
|
igor@56
|
103
|
igor@56
|
104 def create_objects():
|
igor@56
|
105 create_node_objects()
|
igor@56
|
106 create_bridge_objects()
|
igor@56
|
107 create_link_objects()
|
igor@56
|
108
|
igor@56
|
109 def create_node_objects():
|
igor@56
|
110 for dom in domains:
|
igor@56
|
111 node_object[dom]=Node(dom)
|
igor@56
|
112
|
igor@56
|
113 def create_bridge_objects():
|
igor@56
|
114 for bridge in bridges:
|
igor@56
|
115 bridge_object[bridge]=Bridge(bridge)
|
igor@56
|
116
|
igor@56
|
117 def create_link_objects():
|
igor@56
|
118
|
igor@56
|
119 for node, bridges_raw in vbridges_table.iteritems():
|
igor@56
|
120 interface=0
|
igor@56
|
121 j=0
|
igor@56
|
122 for this_bridge in bridges_raw:
|
igor@56
|
123 int_label=""
|
igor@56
|
124 if this_bridge.find(':') != -1:
|
igor@56
|
125 res = this_bridge.split(':')
|
igor@56
|
126 this_bridge= res[0]
|
igor@56
|
127 bridges_raw[j] = this_bridge
|
igor@56
|
128 int_label = res[1]
|
igor@56
|
129 if not [ node, bridges_raw.index(this_bridge), this_bridge ] in temporary_links:
|
igor@56
|
130 name="%s %s %s" % (node,interface,this_bridge)
|
igor@56
|
131 link_object[name]=Link(name,node,interface,this_bridge,int_label)
|
igor@56
|
132 interface+=1
|
igor@56
|
133 vbridges_table[node]=bridges_raw
|
igor@56
|
134
|
igor@56
|
135 for node, bridges_raw in bridge_bridge_table.iteritems():
|
igor@56
|
136 interface=0
|
igor@56
|
137 j=0
|
igor@56
|
138 for this_bridge in bridges_raw:
|
igor@56
|
139 int_label=""
|
igor@56
|
140 if this_bridge.find(':') != -1:
|
igor@56
|
141 res = this_bridge.split(':')
|
igor@56
|
142 this_bridge= res[0]
|
igor@56
|
143 bridges_raw[j] = this_bridge
|
igor@56
|
144 int_label = res[1]
|
igor@56
|
145 if not [ node, bridges_raw.index(this_bridge), this_bridge ] in temporary_links:
|
igor@56
|
146 name="%s %s %s" % (node,interface,this_bridge)
|
igor@56
|
147 link_object[name]=Link(name,node,interface,this_bridge,int_label)
|
igor@56
|
148 interface+=1
|
igor@56
|
149 bridge_bridge_table[node]=bridges_raw
|
igor@56
|
150
|
igor@56
|
151 for node,interface,bridge in temporary_links:
|
igor@56
|
152 name="%s %s %s" % (node,interface,bridge)
|
igor@56
|
153 link_object[name]=Link(name,node,interface,this_bridge)
|
igor@56
|
154
|
igor@56
|
155 for node,interface,bridge in broken_links:
|
igor@56
|
156 name="%s %s %s" % (node,interface,bridge)
|
igor@56
|
157 link_object[name]=Link(name,node,interface,this_bridge)
|
igor@56
|
158
|
igor@56
|
159
|
igor@56
|
160 ####################################################
|
igor@56
|
161
|
igor@49
|
162 def screen():
|
igor@60
|
163 wait_seconds=1
|
igor@0
|
164 screens=[]
|
igor@0
|
165 for domain in domains:
|
igor@56
|
166 screens.append("screen -t %s %s sh -c 'while true; do %s ; echo Retrying in %s secods...; sleep %s ; clear; done'" %
|
igor@60
|
167 (domain,domains.index(domain)+1,node_object[domain].console_string(),wait_seconds,wait_seconds))
|
igor@0
|
168 screenlist="\n".join(screens)
|
igor@0
|
169
|
igor@49
|
170 hardstatus='hardstatus string "%{rk}Xentaur%{bk}@%H %{gk}%c %{yk}%d.%m %{wk}%?%-Lw%?%{bw}%n*%f%t%?(%u)%?%{wk}%?%+Lw%?"'
|
igor@10
|
171
|
igor@11
|
172 f=open(screenrc, "w");
|
igor@10
|
173 f.write("""
|
igor@2
|
174 hardstatus on
|
igor@2
|
175 hardstatus alwayslastline
|
igor@49
|
176 %s
|
igor@2
|
177
|
igor@60
|
178 screen -t console 0 sh -c 'while true; do cd %s; ./xentaur.py shell ; echo Retrying in %s secods...; sleep %s ; clear; done'
|
igor@60
|
179 #screen -t xentaur - sh -c 'while true; do bash ; echo Retrying in %s secods...; sleep %s ; clear; done'
|
igor@10
|
180 %s
|
igor@60
|
181 """ % (hardstatus,xentaur_path,wait_seconds,wait_seconds,wait_seconds,wait_seconds,screenlist))
|
igor@10
|
182 f.close()
|
igor@49
|
183 print "# GNU Screen config file is written to: %s" % screenrc
|
igor@0
|
184
|
igor@0
|
185 def graph():
|
igor@0
|
186 nodelist=""
|
igor@0
|
187 bridgelist=""
|
igor@0
|
188 linklist=""
|
igor@0
|
189 physicallist=""
|
igor@0
|
190 networklist=""
|
igor@0
|
191
|
igor@56
|
192 nodelist=";\n ".join(map(lambda node: node_object[node].graphviz_string(),nodes))
|
igor@0
|
193 if nodelist: nodelist += ";"
|
igor@56
|
194 bridgelist=";\n ".join(map(lambda bridge: bridge_object[bridge].graphviz_string(),bridges))
|
igor@0
|
195 if bridgelist: bridgelist += ";"
|
igor@56
|
196 linklist=";\n ".join(map(lambda link: link_object[link].graphviz_string(),link_object.keys()))
|
igor@56
|
197 if linklist: linklist += ";"
|
igor@0
|
198
|
igor@46
|
199 f = open(network+".dot", "w");
|
igor@10
|
200 f.write ("""
|
igor@0
|
201 graph G {
|
igor@0
|
202 edge [len=1.25];
|
igor@0
|
203 splines=true;
|
igor@0
|
204 // nodes
|
igor@56
|
205 // node [shape=plaintext,color=white,shapefile="shapes/cisco.bmp/router.png"];
|
igor@56
|
206 %s
|
igor@0
|
207
|
igor@0
|
208 // bridges
|
igor@56
|
209 // node [shape=none,shapefile="shapes/all/switch.png"];
|
igor@56
|
210 %s
|
igor@0
|
211
|
igor@0
|
212 // physical
|
igor@0
|
213 node [shape=rectangle,color=blue];
|
igor@56
|
214 %s
|
igor@0
|
215
|
igor@0
|
216 // networks (not bridges, not physical)
|
igor@0
|
217 node [shape=rectangle,color=green];
|
igor@56
|
218 %s
|
igor@0
|
219
|
igor@0
|
220 // links (between nodes and bridges)
|
igor@56
|
221 %s
|
igor@0
|
222
|
igor@0
|
223 };
|
igor@56
|
224 """ % (nodelist, bridgelist, physicallist, networklist, linklist))
|
igor@10
|
225 f.close()
|
igor@46
|
226 run_command("neato -Tpng -o %s.png %s.dot "%(network,network))
|
igor@46
|
227 run_command("neato -Tjpg -o %s.jpg %s.dot "%(network,network))
|
igor@46
|
228 run_command("neato -Tsvg -o %s.svg %s.dot "%(network,network))
|
igor@56
|
229 run_command("neato -Tcmapx -o %s.cmapx -NURL=http://google.com %s.dot "%(network,network))
|
igor@49
|
230 print "# Network map is written to files: %s.{png,svg,jpg,dot}" % network
|
igor@0
|
231
|
igor@27
|
232 def autoredraw():
|
igor@27
|
233 graph()
|
igor@27
|
234
|
igor@0
|
235 def shell():
|
nata@31
|
236 autoredraw()
|
igor@0
|
237 ipshell = IPShellEmbed()
|
igor@0
|
238 ipshell()
|
igor@0
|
239
|
igor@60
|
240 def version():
|
igor@61
|
241 print """
|
igor@61
|
242 Xentaur 0.1-PRE
|
igor@61
|
243
|
igor@61
|
244 ,--,
|
igor@61
|
245 _ ___/ /\\|
|
igor@61
|
246 ,;`( )__, ) ~
|
igor@61
|
247 // .// '--;
|
igor@61
|
248 ' / \ |
|
igor@61
|
249
|
igor@61
|
250 """
|
igor@61
|
251 # print "Xentaur 0.1-PRE"
|
igor@61
|
252 # print "(Godzilla-mutant) _"
|
igor@61
|
253 # print " / * \\"
|
igor@61
|
254 # print " / .-"
|
igor@61
|
255 # print " / |"
|
igor@61
|
256 # print " | \\ \\\\ \\"
|
igor@61
|
257 # print " _ -------| \\ \\\\ \\"
|
igor@61
|
258 # print " / / \\_\\ -"
|
igor@61
|
259 # print "/ |\\ | |"
|
igor@61
|
260 # print "| | \\ .-----. | \\ |"
|
igor@61
|
261 # print " | / \\ \\ \\ \\"
|
igor@61
|
262 # print " \\/|.\\ \\ \\ \\ \\"
|
igor@61
|
263 # print " \\| - . \\_\\ \\_\\"
|
igor@61
|
264 # print "-----------------------------------------------"
|
igor@60
|
265
|
igor@60
|
266
|
igor@60
|
267 def info():
|
igor@60
|
268 version()
|
igor@60
|
269
|
igor@60
|
270 print "Network name: ", network
|
igor@60
|
271 print "-----------------------------------------------"
|
igor@60
|
272 print
|
igor@60
|
273 print "Nodes: ", len(domains)
|
igor@60
|
274 print " * virtual nodes: ", len(domains)-len(real_nodes)
|
igor@60
|
275 print " * real nodes:", len(real_nodes)
|
igor@60
|
276 print
|
igor@60
|
277 print "Bridges:", len(bridges)
|
igor@60
|
278 print " * virtual bridges:", len(bridges)-len(real_bridges)-len(cross_bridges)
|
igor@60
|
279 print " * real switches:", len(real_bridges)
|
igor@60
|
280 print " * direct links:", len(cross_bridges)
|
igor@60
|
281
|
igor@0
|
282 def show_usage():
|
igor@0
|
283 print """Usage:
|
igor@49
|
284 xentaur <command> [<argument>]
|
igor@49
|
285
|
igor@49
|
286 Commands:
|
igor@49
|
287 start-all -- start bridges and domains
|
igor@49
|
288 start-domains -- start domains only
|
igor@49
|
289 start-bridges -- start bridges only
|
igor@49
|
290 stop-all -- stop bridges and domains
|
igor@49
|
291 stop-domains -- stop domains only
|
igor@49
|
292 stop-bridges -- stop bridges only (domains have to be stopped already)
|
igor@49
|
293 restart-all -- restart bridges and domains
|
igor@49
|
294
|
igor@49
|
295 start <domain> -- start the <domain>
|
igor@49
|
296 stop <domain> -- stop the <domain>
|
igor@49
|
297
|
igor@49
|
298 graph -- generate network scheme (result is in <network>.{png,jpg,svg})
|
igor@49
|
299 screen -- generate GNU Screen config file (~/.screenrc_xentaur)
|
igor@49
|
300 shell -- run Xentaur shell
|
igor@49
|
301
|
igor@0
|
302 """
|
igor@0
|
303
|
igor@33
|
304 def save():
|
igor@33
|
305 print "network =", xen_config_name
|
igor@33
|
306 print "domains =", domains
|
igor@33
|
307 print "domain_types =", domain_types
|
igor@33
|
308 print "bridges =", bridges
|
igor@33
|
309 print "vbridges_table =", vbridges_table
|
igor@33
|
310 print "hidden_bridges =", hidden_bridges
|
igor@33
|
311 print "broken_links =", broken_links
|
igor@33
|
312 print "temporary_links =", temporary_links
|
igor@33
|
313 print "bridges_turned_down =", bridges_turned_down
|
igor@33
|
314
|
igor@33
|
315 #-----------------------------------------------------------------------
|
igor@33
|
316 # CLASSES
|
igor@33
|
317
|
igor@33
|
318 class Bridge:
|
igor@33
|
319 def __init__ (self,name):
|
igor@33
|
320 self.name=name
|
igor@33
|
321 def up(self):
|
igor@33
|
322 bridge_up(self.name)
|
igor@33
|
323 def down(self):
|
igor@33
|
324 bridge_down(self.name)
|
igor@33
|
325 def show(self):
|
igor@33
|
326 show_bridge(self.name)
|
igor@33
|
327 def dump_start(self,filter=""):
|
igor@33
|
328 dump_start(self.name,filter)
|
igor@33
|
329
|
igor@56
|
330 def is_hidden(self):
|
igor@56
|
331 return self.name in hidden_bridges
|
igor@56
|
332 def is_real(self):
|
igor@56
|
333 return self.name in real_bridges
|
igor@56
|
334 def is_turned_down(self):
|
igor@56
|
335 return self.name in bridges_turned_down
|
igor@56
|
336 def is_cross(self):
|
igor@56
|
337 return self.name in cross_bridges
|
igor@33
|
338
|
igor@56
|
339 def graphviz_string(self):
|
igor@56
|
340 if self.is_hidden():
|
igor@56
|
341 return ""
|
igor@56
|
342 elif self.is_cross():
|
igor@56
|
343 return "%s [shape=circle,height=0.03,color=black,fillcolor=black,style=filled,label=\"\"]" % (self.name)
|
igor@56
|
344 elif self.is_real():
|
igor@56
|
345 return "%s [color=white,shape=none,shapefile=\"shapes/all/real_switch.png\"]" % (self.name)
|
igor@56
|
346 elif self.is_turned_down():
|
igor@56
|
347 return "%s [color=white,shape=none,shapefile=\"shapes/all/switch_turned_down.png\"]" % (self.name)
|
igor@56
|
348 else:
|
igor@56
|
349 return "%s [color=white,shape=none,shapefile=\"shapes/all/switch.png\"]" % (self.name)
|
igor@56
|
350
|
igor@56
|
351
|
igor@56
|
352 class Node:
|
igor@38
|
353 def __init__ (self,name):
|
igor@38
|
354 self.name=name
|
igor@56
|
355 self.type=domain_types[domains.index(name)]
|
igor@38
|
356 def start(self):
|
igor@38
|
357 return ""
|
igor@38
|
358 def stop(self):
|
igor@38
|
359 return ""
|
igor@38
|
360 def start_commandline(self):
|
igor@38
|
361 return ""
|
igor@38
|
362 def get_domain_id(self):
|
igor@38
|
363 return get_domain_id(self.name)
|
igor@56
|
364 def graphviz_string(self):
|
igor@56
|
365 return self.name+" [color=white,shape=plaintext,label=\" "+self.name+"\",shapefile=\"shapes/all/"+\
|
igor@56
|
366 domain_types[domains.index(self.name)]+".png\",fontcolor=black,fontsize=16,target=\"http://google.com\"]"
|
igor@56
|
367 def console_string(self):
|
igor@56
|
368 if self.type == 'quagga' or self.type == 'xenomips':
|
igor@56
|
369 return "sudo xm console "+self.name
|
igor@56
|
370 elif self.name in real_bridges or self.name in real_nodes:
|
igor@56
|
371 return "echo Press enter to connect; read line; "+connection_table[self.name]
|
igor@56
|
372
|
igor@56
|
373
|
igor@56
|
374 class Link:
|
igor@56
|
375 def __init__ (self,name,node,interface,bridge,label=""):
|
igor@56
|
376 self.name=name
|
igor@56
|
377 self.node=node
|
igor@56
|
378 self.interface=interface
|
igor@56
|
379 self.bridge=bridge
|
igor@56
|
380 self.label=label
|
igor@56
|
381
|
igor@56
|
382 def is_temporary(self):
|
igor@56
|
383 return [self.node,self.interface,self.bridge] in temporary_links
|
igor@56
|
384
|
igor@56
|
385 def is_broken(self):
|
igor@56
|
386 return ([self.node,self.interface,self.bridge] in broken_links)
|
igor@56
|
387
|
igor@56
|
388 def graphviz_string(self):
|
igor@56
|
389 if self.is_temporary():
|
igor@56
|
390 return self.node+" -- "+self.bridge+" [taillabel=\"fa"+str(self.interface)+"/0\",color=blue,len=10,w=5,weight=5]"
|
igor@56
|
391 if self.is_broken():
|
igor@56
|
392 return self.node+" -- "+self.bridge+" [taillabel=\"fa"+str(self.interface)+"/0\",style=dashed]"
|
igor@56
|
393
|
igor@56
|
394 ip="\\n.%s.%s" % (bridges.index(self.bridge)+1, domains.index(self.node)+1)
|
igor@56
|
395 if domain_types[domains.index(self.node)] == 'xenomips':
|
igor@56
|
396 int_name="fa"+str(self.interface)+"/0"
|
igor@56
|
397 else:
|
igor@56
|
398 int_name="eth"+str(self.interface)
|
igor@56
|
399 if self.label != "":
|
igor@56
|
400 int_name = self.label
|
igor@56
|
401 return self.node+" -- "+self.bridge+" [taillabel=\""+int_name+ip+"\",fontsize=14,fontname=fixed]"
|
igor@56
|
402
|
igor@38
|
403
|
igor@23
|
404 #-----------------------------------------------------------------------
|
igor@23
|
405 # DOMAINS
|
igor@23
|
406
|
igor@23
|
407 def get_domain_id(domain):
|
igor@23
|
408 return run_command_return_stdout("sudo xm list | awk '{if ($1 == \"'%s'\") print $2}'" % domain).rstrip("\n")
|
igor@23
|
409
|
igor@22
|
410
|
igor@22
|
411 #-----------------------------------------------------------------------
|
igor@22
|
412 # BRIDGES and IFACES
|
igor@22
|
413
|
igor@22
|
414 def bridge_down(bridge):
|
igor@22
|
415 """
|
igor@22
|
416 Turn the bridge <bridge> down
|
igor@22
|
417 """
|
igor@38
|
418 if bridge in real_bridges:
|
igor@38
|
419 print "Bridge %s is a real bridge" % (bridge)
|
igor@38
|
420 return -1
|
igor@29
|
421 if bridge in bridges_turned_down:
|
igor@29
|
422 print "Bridge %s is turned down already" % (bridge)
|
igor@29
|
423 else:
|
igor@29
|
424 bridges_turned_down.append(bridge)
|
igor@29
|
425 run_command("sudo ip link set %s down" % bridge)
|
igor@29
|
426 autoredraw()
|
igor@22
|
427
|
igor@22
|
428 def bridge_up(bridge):
|
igor@22
|
429 """
|
igor@22
|
430 Turn the bridge <bridge> up
|
igor@22
|
431 """
|
igor@38
|
432 if bridge in real_bridges:
|
igor@38
|
433 print "Bridge %s is a real bridge" % (bridge)
|
igor@38
|
434 return -1
|
igor@29
|
435 if not (bridge in bridges_turned_down):
|
igor@29
|
436 print "Bridge %s is turned up already" % (bridge)
|
igor@29
|
437 else:
|
igor@29
|
438 bridges_turned_down.remove(bridge)
|
igor@29
|
439 run_command("sudo ip link set %s up" % bridge)
|
igor@29
|
440 autoredraw()
|
igor@22
|
441
|
igor@22
|
442 def show_bridge(bridge):
|
igor@22
|
443 """
|
igor@22
|
444 Show the state of the bridge <bridge>
|
igor@22
|
445 """
|
igor@38
|
446 if bridge in real_bridges:
|
igor@38
|
447 print "Bridge %s is a real bridge" % (bridge)
|
igor@38
|
448 return -1
|
igor@22
|
449 run_command("sudo ip link show %s" % bridge)
|
igor@22
|
450
|
igor@23
|
451
|
igor@23
|
452 def int_disconnect(domain, int_number):
|
igor@23
|
453 """
|
igor@23
|
454 Disconnect the interface with the number <int_number>
|
igor@23
|
455 of the domain <domain> from the bridge to which
|
igor@23
|
456 it is connected
|
igor@23
|
457 """
|
igor@23
|
458 dom_id=get_domain_id(domain)
|
igor@23
|
459 bridge=vbridges_table[domain][int_number]
|
igor@23
|
460 if not bridge:
|
igor@23
|
461 print "Interface %s of the %s domain is not connected" % (int_number, domain)
|
igor@23
|
462 return 1
|
igor@23
|
463 run_command("sudo brctl delif %s vif%s.%s" % (bridge, dom_id, int_number))
|
igor@23
|
464 vbridges_table[domain][int_number]=''
|
igor@28
|
465 if [ domain, int_number, bridge ] in temporary_links:
|
igor@28
|
466 temporary_links.remove([ domain, int_number, bridge ])
|
igor@27
|
467 else:
|
igor@28
|
468 broken_links.append([ domain, int_number, bridge ])
|
igor@27
|
469 autoredraw()
|
igor@23
|
470
|
igor@23
|
471 def int_connect(domain, int_number, bridge):
|
igor@23
|
472 """
|
igor@23
|
473 Connect the interface with the number <int_number>
|
igor@24
|
474 of the domain <domain> to the bridge <bridge>
|
igor@23
|
475 """
|
igor@38
|
476 if bridge in real_bridges:
|
igor@38
|
477 print "Bridge %s is a real bridge" % (bridge)
|
igor@38
|
478 return -1
|
igor@38
|
479
|
igor@23
|
480 dom_id=get_domain_id(domain)
|
igor@23
|
481 if vbridges_table[domain][int_number]:
|
igor@23
|
482 print "Interface %s of the %s domain is connected already to the %s bridge" % (int_number, domain, vbridges_table[domain][int_number])
|
igor@23
|
483 return 1
|
igor@23
|
484 run_command("sudo brctl addif %s vif%s.%s" % (bridge, dom_id, int_number))
|
igor@23
|
485 vbridges_table[domain][int_number]=bridge
|
igor@28
|
486 if [ domain, int_number, bridge ] in broken_links:
|
igor@28
|
487 broken_links.remove([ domain, int_number, bridge ])
|
igor@27
|
488 else:
|
igor@28
|
489 temporary_links.append([ domain, int_number, bridge ])
|
igor@27
|
490 autoredraw()
|
igor@23
|
491
|
igor@24
|
492 def int_reconnect(domain, int_number, bridge):
|
igor@24
|
493 """
|
igor@24
|
494 Reconnect the interface with the number <int_number>
|
igor@24
|
495 of the domain <domain> from the bridge to which
|
igor@24
|
496 it is connected to the bridge <bridge>
|
igor@24
|
497 """
|
igor@38
|
498 if bridge in real_bridges:
|
igor@38
|
499 print "Bridge %s is a real bridge" % (bridge)
|
igor@38
|
500 return -1
|
igor@38
|
501
|
igor@24
|
502 int_disconnect(domain, int_number)
|
igor@24
|
503 int_connect(domain, int_number, bridge)
|
igor@24
|
504
|
igor@24
|
505 def show_int(domain, int_number):
|
igor@25
|
506 """
|
igor@25
|
507 Show information about the interface <int_nuber>
|
igor@25
|
508 of the domain <domain>
|
igor@25
|
509 """
|
igor@26
|
510 return vbridges_table[domain][int_number]
|
igor@24
|
511
|
igor@28
|
512
|
igor@28
|
513 def dump_start(bridge, filter=""):
|
igor@38
|
514 if bridge in real_bridges:
|
igor@38
|
515 print "Bridge %s is a real bridge" % (bridge)
|
igor@38
|
516 return -1
|
igor@32
|
517 try:
|
igor@32
|
518 print "Writing dump... (press Ctrl-C to stop)"
|
igor@32
|
519 run_command("sudo tcpdump -w xentaur.dump -i %s %s > /dev/null 2>&1 " % (bridge,filter))
|
igor@32
|
520 except:
|
igor@32
|
521 print "Done.\n Dump is written to xentaur.dump"
|
igor@28
|
522 return 0
|
igor@28
|
523
|
igor@28
|
524 def dump_stop():
|
igor@28
|
525 return 0
|
igor@33
|
526
|
igor@33
|
527
|
igor@33
|
528 #-----------------------------------------------------------------------
|
igor@33
|
529 # CONFIGURATION TEMPLATES
|
igor@33
|
530
|
igor@33
|
531
|
igor@33
|
532 def configure_ip_addresses(doms=domains):
|
igor@40
|
533
|
igor@40
|
534 cisco_set_ip_on_int="""
|
igor@40
|
535 \n\n\n
|
igor@40
|
536 int fa%s/0
|
igor@40
|
537 no ip address
|
igor@40
|
538 ip address %s 255.255.255.0
|
igor@40
|
539 no shutdown
|
igor@40
|
540 exit
|
igor@40
|
541 """
|
igor@40
|
542
|
igor@40
|
543 quagga_set_ip_on_int="""
|
igor@40
|
544 int eth%s
|
igor@40
|
545 no ip address
|
igor@40
|
546 ip address %s/24
|
igor@40
|
547 no shutdown
|
igor@40
|
548 exit
|
igor@40
|
549 """
|
igor@40
|
550
|
igor@40
|
551 for dom in doms:
|
igor@40
|
552 i=domains.index(dom)+1
|
igor@40
|
553 if domain_types[domains.index(dom)] == 'quagga':
|
igor@40
|
554 command = quagga_set_ip_on_int
|
igor@40
|
555 write_to(i,"\nconf t\n")
|
igor@40
|
556 j=0
|
igor@40
|
557 for br in vbridges_table[dom]:
|
igor@40
|
558 write_to(i,command % (j, "192.168.%s.%s"%(bridges.index(br)+1,i)))
|
igor@40
|
559 j+=1
|
igor@40
|
560 write_to(i,"\nend\n")
|
igor@40
|
561 else:
|
igor@40
|
562 command = cisco_set_ip_on_int
|
igor@40
|
563 write_to(i,"\nena\nconf t\n")
|
igor@40
|
564 j=0
|
igor@40
|
565 for br in vbridges_table[dom]:
|
igor@40
|
566 write_to(i,command % (j, "192.168.%s.%s"%(bridges.index(br)+1,i)))
|
igor@40
|
567 j+=1
|
igor@40
|
568 write_to(i,"\nend\n")
|
igor@40
|
569 return 0
|
igor@40
|
570
|
igor@40
|
571 def configure_no_ip_addresses(doms=domains):
|
igor@40
|
572
|
igor@40
|
573 cisco_set_ip_on_int="""
|
igor@40
|
574 \n\n\n
|
igor@40
|
575 int fa%s/0
|
igor@40
|
576 no ip address %s 255.255.255.0
|
igor@40
|
577 exit
|
igor@40
|
578 """
|
igor@40
|
579
|
igor@40
|
580 quagga_set_ip_on_int="""
|
igor@40
|
581 int eth%s
|
igor@40
|
582 no ip address %s/24
|
igor@40
|
583 exit
|
igor@40
|
584 """
|
igor@40
|
585
|
igor@40
|
586 for dom in doms:
|
igor@40
|
587 i=domains.index(dom)+1
|
igor@40
|
588 if domain_types[domains.index(dom)] == 'quagga':
|
igor@40
|
589 command = quagga_set_ip_on_int
|
igor@40
|
590 write_to(i,"\nconf t\n")
|
igor@40
|
591 j=0
|
igor@40
|
592 for br in vbridges_table[dom]:
|
igor@40
|
593 write_to(i,command % (j, "192.168.%s.%s"%(bridges.index(br)+1,i)))
|
igor@40
|
594 j+=1
|
igor@40
|
595 write_to(i,"\nend\n")
|
igor@40
|
596 else:
|
igor@40
|
597 command = cisco_set_ip_on_int
|
igor@40
|
598 write_to(i,"\nena\nconf t\n")
|
igor@40
|
599 j=0
|
igor@40
|
600 for br in vbridges_table[dom]:
|
igor@40
|
601 write_to(i,command % (j, "192.168.%s.%s"%(bridges.index(br)+1,i)))
|
igor@40
|
602 j+=1
|
igor@40
|
603 write_to(i,"\nend\n")
|
igor@33
|
604 return 0
|
igor@33
|
605
|
igor@33
|
606 def configure_ospf(doms=domains):
|
igor@40
|
607 for dom in doms:
|
igor@40
|
608 if domain_types[domains.index(dom)] == 'quagga':
|
igor@40
|
609 write_to(dom,"\n\nconf t\nrouter ospf\nnetwork 192.168.0.0/16 area 0\nend\n")
|
igor@40
|
610 else:
|
igor@40
|
611 write_to(dom,"\n\nena\nconf t\nrouter ospf 1\nnetwork 192.168.0.0 0.0.255.255 area 0\nend\n")
|
igor@33
|
612 return 0
|
igor@33
|
613
|
igor@49
|
614 def configure_hostname(doms=domains):
|
igor@49
|
615 for dom in doms:
|
igor@49
|
616 if domain_types[domains.index(dom)] == 'quagga':
|
igor@49
|
617 write_to(dom,"\n\nconf t\nhostname %s\nend\n" % dom)
|
igor@49
|
618 else:
|
igor@49
|
619 write_to(dom,"\n\nena\nconf t\nhostname %s\nend\n" % dom)
|
igor@49
|
620 return 0
|
igor@49
|
621
|
igor@49
|
622 def configure_logging_synchronous(doms=domains):
|
igor@49
|
623 for dom in domains:
|
igor@49
|
624 if domain_types[domains.index(dom)] == 'quagga':
|
igor@49
|
625 0
|
igor@49
|
626 else:
|
igor@49
|
627 write_to(dom,"\n\nena\nconf t\nline console 0\nlogging synchronous\nend\n")
|
igor@49
|
628 return 0
|
igor@49
|
629
|
igor@51
|
630 def configure_exec_timeout_0(doms=domains):
|
igor@51
|
631 for dom in domains:
|
igor@51
|
632 if domain_types[domains.index(dom)] == 'quagga':
|
igor@51
|
633 0
|
igor@51
|
634 else:
|
igor@52
|
635 write_to(dom,"\n\nena\nconf t\nline console 0\nexec-timeout 0\nend\n")
|
igor@51
|
636 return 0
|
igor@51
|
637
|
igor@55
|
638 def configure_no_cdp_log_mismatch_duplex(doms=domains):
|
igor@55
|
639 for dom in filter_by_type(domains,'xenomips'):
|
igor@55
|
640 write_to(dom,"\n\nena\nconf t\nno cdp log mismatch duplex\nend\n")
|
igor@55
|
641
|
igor@33
|
642 def configure_save(doms=domains):
|
igor@33
|
643 write_to(doms,"\nwr\n")
|
igor@33
|
644
|
igor@49
|
645 def configure_root(doms=domains):
|
igor@49
|
646 write_to(doms,"root\n")
|
igor@49
|
647
|
igor@0
|
648 #-----------------------------------------------------------------------
|
igor@0
|
649
|
igor@0
|
650
|
igor@0
|
651 def add_domain(name,type):
|
igor@0
|
652 domains.append(name)
|
igor@0
|
653 domain_types.append(type)
|
igor@0
|
654
|
igor@0
|
655 def brake_link(domain,bridge):
|
igor@0
|
656 broken_links.append([domain,bridge])
|
igor@0
|
657
|
igor@4
|
658 wt_timeout=0.5
|
igor@8
|
659 def write_to(screen,string,return_to_screen=""):
|
igor@2
|
660 """
|
igor@2
|
661 write_to(screen,string):
|
igor@2
|
662
|
igor@8
|
663 Type *string* to the specified screen(s).
|
igor@8
|
664 Screen may be specified with the number *screen*,
|
igor@8
|
665 with array of numbers,
|
igor@8
|
666 with array of names.
|
igor@2
|
667
|
igor@2
|
668 """
|
igor@5
|
669 screen_numbers=[] # number of the screens to write to
|
igor@5
|
670 if type(screen) == list:
|
igor@5
|
671 screen_numbers=map(lambda x: domains.index(x)+1, screen)
|
igor@5
|
672 elif type(screen) == int:
|
igor@5
|
673 screen_numbers=[screen]
|
igor@5
|
674 else:
|
igor@5
|
675 screen_numbers=[domains.index(screen)+1]
|
igor@5
|
676
|
igor@5
|
677 for screen_number in screen_numbers:
|
igor@5
|
678 run_command("screen -X select "+str(screen_number))
|
igor@5
|
679 time.sleep(wt_timeout)
|
igor@5
|
680 for line in string.splitlines():
|
igor@5
|
681 f=open('/tmp/xentaurbuf', 'w')
|
igor@5
|
682 f.write(line+"\n")
|
igor@5
|
683 f.close()
|
igor@5
|
684 run_command("screen -X readreg p /tmp/xentaurbuf")
|
igor@5
|
685 time.sleep(wt_timeout)
|
igor@5
|
686 run_command("nohup screen -X paste p >& /dev/null")
|
igor@5
|
687 time.sleep(wt_timeout)
|
igor@5
|
688
|
igor@8
|
689 if return_to_screen != "":
|
igor@8
|
690 run_command("screen -X select %s" % (return_to_screen))
|
igor@8
|
691 time.sleep(wt_timeout)
|
igor@0
|
692
|
igor@49
|
693 def filter_by_type(doms,type):
|
igor@49
|
694 """
|
igor@49
|
695 filter_by_type(doms,type)
|
igor@49
|
696
|
igor@49
|
697 Return only domains of *doms* that have specified *type*
|
igor@49
|
698 """
|
igor@49
|
699 return filter(lambda x: domain_types[domains.index(x)]==type,domains)
|
igor@49
|
700
|
igor@0
|
701 #-----------------------------------------------------------------------
|
igor@0
|
702
|
igor@5
|
703 cisco_fa01_up="""
|
igor@5
|
704 ena
|
igor@5
|
705 conf t
|
igor@5
|
706 int fa0/0
|
igor@49
|
707 duplex half
|
igor@5
|
708 no shutdown
|
igor@5
|
709 exit
|
igor@5
|
710 int fa1/0
|
igor@49
|
711 duplex half
|
igor@5
|
712 no shutdown
|
igor@5
|
713 exit
|
igor@5
|
714 exit
|
igor@5
|
715 exit
|
igor@5
|
716 """
|
igor@5
|
717
|
igor@5
|
718 cisco_set_ip_on_int="""
|
igor@5
|
719 interface fa%s/0
|
igor@5
|
720 no ip address
|
igor@5
|
721 ip address %s 255.255.255.0
|
igor@5
|
722 exit
|
igor@5
|
723 """
|
igor@5
|
724
|
igor@0
|
725 nodes=domains
|
igor@0
|
726
|
igor@56
|
727 create_objects()
|
igor@56
|
728
|
igor@56
|
729
|
igor@49
|
730 if len(sys.argv) == 2:
|
igor@49
|
731 if sys.argv[1] == 'start-all':
|
igor@0
|
732 start_all()
|
igor@49
|
733 elif sys.argv[1] == 'start-domains':
|
igor@49
|
734 start_domains()
|
igor@49
|
735 elif sys.argv[1] == 'start-bridges':
|
igor@49
|
736 start_bridges()
|
igor@49
|
737 elif sys.argv[1] == 'stop-all':
|
igor@0
|
738 stop_all()
|
igor@49
|
739 elif sys.argv[1] == 'stop-domains':
|
igor@49
|
740 stop_domains()
|
igor@49
|
741 elif sys.argv[1] == 'stop-bridges':
|
igor@49
|
742 stop_bridges()
|
igor@49
|
743 elif sys.argv[1] == 'restart-all':
|
igor@49
|
744 restart_all()
|
igor@0
|
745 elif sys.argv[1] == 'screen':
|
igor@50
|
746 screen()
|
igor@0
|
747 elif sys.argv[1] == 'graph':
|
igor@0
|
748 graph()
|
igor@0
|
749 elif sys.argv[1] == 'shell':
|
igor@0
|
750 shell()
|
igor@60
|
751 elif sys.argv[1] == 'info':
|
igor@60
|
752 info()
|
igor@49
|
753 else:
|
igor@49
|
754 show_usage()
|
igor@49
|
755 sys.exit(1)
|
igor@49
|
756 elif len(sys.argv) == 3:
|
igor@49
|
757 if sys.argv[1] == 'start':
|
igor@49
|
758 start_domain(sys.argv[2])
|
igor@49
|
759 elif sys.argv[1] == 'stop':
|
igor@49
|
760 stop_domain(sys.argv[2])
|
igor@49
|
761 elif sys.argv[1] == 'restart':
|
igor@49
|
762 stop_domain(sys.argv[2])
|
igor@49
|
763 start_domain(sys.argv[2])
|
igor@49
|
764 else:
|
igor@49
|
765 show_usage()
|
igor@49
|
766 sys.exit(1)
|
igor@0
|
767 else:
|
igor@0
|
768 show_usage()
|
igor@0
|
769 sys.exit(1)
|
igor@0
|
770
|
igor@0
|
771 sys.exit(0)
|
igor@0
|
772
|
igor@0
|
773
|