xentaur

view xentaur.py @ 38:f71b298798c2

real topologies
author igor
date Fri Sep 28 13:03:15 2007 +0300 (2007-09-28)
parents 7bf7abe92123
children 54b7c7ae50ec
line source
1 #!/usr/bin/python
4 import sys,os,time
6 xentaur_path=os.environ['HOME']+"/xentaur"
8 sys.path.append('/etc/xen')
9 sys.path.append(xentaur_path)
11 network='netw'
12 domain='dyn3'
13 from xendomain import *
15 bridges_turned_down=[]
17 from IPython.Shell import IPShellEmbed
20 screenrc=os.environ['HOME']+"/.screenrc_xentaur"
22 def run(program, *args):
23 pid = os.fork()
24 if not pid:
25 os.execvp(program, (program,) + args)
26 return os.wait()[0]
28 def run_command(line):
29 #cmds=line.split()
30 #run(cmds[0],*cmds[1:])
31 run("/bin/sh", "-c", line)
33 def run_command_return_stdout(line):
34 p = os.popen(line)
35 output = p.read()
36 p.close()
37 return output
39 def create_bridges_script():
40 unbound_bridges=set(bridges)-set(real_bridges)
41 create_unbound_bridges="\n".join(map(lambda x: "sudo /usr/sbin/brctl show | awk '{print $1}' | grep -q "+x+" || sudo /usr/sbin/brctl addbr "+x, unbound_bridges))
42 create_unbound_bridges+="\n"+"\n".join(map(lambda x: "sudo /bin/ip link set "+x+" up", unbound_bridges))
44 print """#!/bin/sh
45 # create unbound bridges
46 %(create_unbound_bridges)s
47 """ % {'create_unbound_bridges' : create_unbound_bridges}
50 def create_domains_script():
51 for domain in domains:
52 if not domain in real_nodes:
53 print "sudo /usr/sbin/xm create "+xentaur_path+"/xendomain.py "+" domain="+domain+" network="+network+" && sleep 1 && sudo /usr/sbin/xm sched-credit -d $(sudo /usr/sbin/xm list | grep "+domain+" | awk '{print $2}') -c 10 && sleep 1"
55 def destroy_domains_script():
56 for domain in domains:
57 if not domain in real_nodes:
58 print "sudo /usr/sbin/xm shutdown "+domain
60 def create_screens_script():
61 N=1
62 screens=[]
63 for domain in domains:
64 screens.append("screen -t "+domain+" "+str(N)+" sh -c 'while true; do sudo xm console "+domain+" ; echo Retrying in 5 seconds...; sleep 5; done'")
65 N+=1
66 screenlist="\n".join(screens)
68 #
69 # Previous terminal acccess commands:
70 # ip="192.168.80."+str(200+N)
71 # screens.append("screen -t "+domain+" "+str(N)+" sh -c 'while true; do ssh root@"+ip+" ; done'")
72 #
74 #hardstatus string "\%{gk}\%c \%{yk}\%M\%d \%{wk}\%?\%-Lw\%?\%{bw}\%n*\%f\%t\%?(\%u)\%?\%{wk}\%?\%+Lw\%?"
76 f=open(screenrc, "w");
77 f.write("""
78 hardstatus on
79 hardstatus alwayslastline
81 screen -t console 0 bash
82 %s
83 """ % (screenlist))
84 f.close()
86 def graph_node(node):
87 i=0
88 domain_type={}
89 for domain in domains:
90 domain_type[domain]=domain_types[i]
91 i+=1
92 return node+" [label=\" "+node+"\",shapefile=\"shapes/all/"+domain_type[node]+".png\",fontcolor=black,fontsize=16]"
94 def graph_bridge(bridge):
95 if bridge in hidden_bridges:
96 return ""
97 if bridge in real_bridges:
98 return "%s [shape=none,shapefile=\"shapes/all/real_switch.png\"]" % (bridge)
99 elif bridge in bridges_turned_down:
100 return "%s [shape=none,shapefile=\"shapes/all/switch_turned_down.png\"]" % (bridge)
101 else:
102 return "%s [shape=none,shapefile=\"shapes/all/switch.png\"]" % (bridge)
105 def graph():
106 nodelist=""
107 bridgelist=""
108 linklist=""
109 physicallist=""
110 networklist=""
112 nodelist=";\n ".join(map(graph_node,nodes))
113 if nodelist: nodelist += ";"
115 bridgelist=";\n ".join(map(graph_bridge,bridges))
116 if bridgelist: bridgelist += ";"
118 links=[]
119 for host, bridges_raw in vbridges_table.iteritems():
120 i=0
121 for this_bridge in bridges_raw:
122 if this_bridge in hidden_bridges or not this_bridge:
123 continue
124 if not [ host, bridges_raw.index(this_bridge), this_bridge ] in temporary_links:
125 links.append(host+" -- "+this_bridge+" [taillabel=\"fa"+str(bridges_raw.index(this_bridge))+"/0\"]")
126 i+=1
127 # if [ host, this_bridge ] in broken_links:
128 # links.append(host+" -- "+this_bridge+" [taillabel=\"fa"+str(i)+"/0\",style=dashed]")
129 # else:
130 # links.append(host+" -- "+this_bridge+" [taillabel=\"fa"+str(i)+"/0\"]")
132 for link in temporary_links:
133 links.append(link[0]+" -- "+link[2]+" [taillabel=\"fa"+str(link[1])+"/0\",color=blue,len=10,w=5,weight=5]")
135 for link in broken_links:
136 links.append(link[0]+" -- "+link[2]+" [taillabel=\"fa"+str(link[1])+"/0\",style=dashed]")
138 linklist=";\n ".join(links)
140 graph_dot = {
141 'nodelist' : nodelist,
142 'bridgelist' : bridgelist,
143 'linklist' : linklist,
144 'physicallist' : physicallist,
145 'networklist' : networklist,
146 }
148 f = open("xenomips.dot", "w");
149 f.write ("""
150 graph G {
151 edge [len=1.25];
152 splines=true;
153 // nodes
155 node [shape=plaintext,color=white,shapefile="shapes/cisco.bmp/router.png"];
156 %(nodelist)s
158 // bridges
160 node [shape=none,shapefile="shapes/all/switch.png"];
161 %(bridgelist)s
163 // physical
165 node [shape=rectangle,color=blue];
166 %(physicallist)s
168 // networks (not bridges, not physical)
169 node [shape=rectangle,color=green];
170 %(networklist)s
172 // links (between nodes and bridges)
173 %(linklist)s
175 };
176 """ % graph_dot)
177 f.close()
178 run_command("neato -Tpng -o xenomips.png xenomips.dot ")
180 def autoredraw():
181 graph()
183 def start_all():
184 create_bridges_script()
185 create_screens_script()
186 create_domains_script()
187 graph()
188 print """
189 cat <<NOTE_FOR_USER
190 # To view virtual network map, run:
191 gqview xenomips.png
192 # To attach to VM consoles, run:
193 screen -c screenrc
194 NOTE_FOR_USER
195 """
197 def shell():
198 autoredraw()
199 ipshell = IPShellEmbed()
200 ipshell()
202 def stop_all():
203 destroy_domains_script()
205 def show_usage():
206 print """Usage:
207 xentaur {start|stop|start-bridges|start-domains|stop-domains|screen|graph}
208 """
210 def save():
211 print "network =", xen_config_name
212 print "domains =", domains
213 print "domain_types =", domain_types
214 print "bridges =", bridges
215 print "vbridges_table =", vbridges_table
216 print "hidden_bridges =", hidden_bridges
217 print "broken_links =", broken_links
218 print "temporary_links =", temporary_links
219 print "bridges_turned_down =", bridges_turned_down
221 #-----------------------------------------------------------------------
222 # CLASSES
224 class Bridge:
225 def __init__ (self,name):
226 self.name=name
227 def up(self):
228 bridge_up(self.name)
229 def down(self):
230 bridge_down(self.name)
231 def show(self):
232 show_bridge(self.name)
233 def dump_start(self,filter=""):
234 dump_start(self.name,filter)
237 class Domain:
238 def __init__ (self,name):
239 self.name=name
240 def start(self):
241 return ""
242 def stop(self):
243 return ""
244 def start_commandline(self):
245 return ""
246 def graphviz(self):
247 return ""
248 def get_domain_id(self):
249 return get_domain_id(self.name)
251 #-----------------------------------------------------------------------
252 # DOMAINS
254 def get_domain_id(domain):
255 return run_command_return_stdout("sudo xm list | awk '{if ($1 == \"'%s'\") print $2}'" % domain).rstrip("\n")
258 #-----------------------------------------------------------------------
259 # BRIDGES and IFACES
261 def bridge_down(bridge):
262 """
263 Turn the bridge <bridge> down
264 """
265 if bridge in real_bridges:
266 print "Bridge %s is a real bridge" % (bridge)
267 return -1
268 if bridge in bridges_turned_down:
269 print "Bridge %s is turned down already" % (bridge)
270 else:
271 bridges_turned_down.append(bridge)
272 run_command("sudo ip link set %s down" % bridge)
273 autoredraw()
275 def bridge_up(bridge):
276 """
277 Turn the bridge <bridge> up
278 """
279 if bridge in real_bridges:
280 print "Bridge %s is a real bridge" % (bridge)
281 return -1
282 if not (bridge in bridges_turned_down):
283 print "Bridge %s is turned up already" % (bridge)
284 else:
285 bridges_turned_down.remove(bridge)
286 run_command("sudo ip link set %s up" % bridge)
287 autoredraw()
289 def show_bridge(bridge):
290 """
291 Show the state of the bridge <bridge>
292 """
293 if bridge in real_bridges:
294 print "Bridge %s is a real bridge" % (bridge)
295 return -1
296 run_command("sudo ip link show %s" % bridge)
299 def int_disconnect(domain, int_number):
300 """
301 Disconnect the interface with the number <int_number>
302 of the domain <domain> from the bridge to which
303 it is connected
304 """
305 dom_id=get_domain_id(domain)
306 bridge=vbridges_table[domain][int_number]
307 if not bridge:
308 print "Interface %s of the %s domain is not connected" % (int_number, domain)
309 return 1
310 run_command("sudo brctl delif %s vif%s.%s" % (bridge, dom_id, int_number))
311 vbridges_table[domain][int_number]=''
312 if [ domain, int_number, bridge ] in temporary_links:
313 temporary_links.remove([ domain, int_number, bridge ])
314 else:
315 broken_links.append([ domain, int_number, bridge ])
316 autoredraw()
318 def int_connect(domain, int_number, bridge):
319 """
320 Connect the interface with the number <int_number>
321 of the domain <domain> to the bridge <bridge>
322 """
323 if bridge in real_bridges:
324 print "Bridge %s is a real bridge" % (bridge)
325 return -1
327 dom_id=get_domain_id(domain)
328 if vbridges_table[domain][int_number]:
329 print "Interface %s of the %s domain is connected already to the %s bridge" % (int_number, domain, vbridges_table[domain][int_number])
330 return 1
331 run_command("sudo brctl addif %s vif%s.%s" % (bridge, dom_id, int_number))
332 vbridges_table[domain][int_number]=bridge
333 if [ domain, int_number, bridge ] in broken_links:
334 broken_links.remove([ domain, int_number, bridge ])
335 else:
336 temporary_links.append([ domain, int_number, bridge ])
337 autoredraw()
339 def int_reconnect(domain, int_number, bridge):
340 """
341 Reconnect the interface with the number <int_number>
342 of the domain <domain> from the bridge to which
343 it is connected to the bridge <bridge>
344 """
345 if bridge in real_bridges:
346 print "Bridge %s is a real bridge" % (bridge)
347 return -1
349 int_disconnect(domain, int_number)
350 int_connect(domain, int_number, bridge)
352 def show_int(domain, int_number):
353 """
354 Show information about the interface <int_nuber>
355 of the domain <domain>
356 """
357 return vbridges_table[domain][int_number]
360 def dump_start(bridge, filter=""):
361 if bridge in real_bridges:
362 print "Bridge %s is a real bridge" % (bridge)
363 return -1
364 try:
365 print "Writing dump... (press Ctrl-C to stop)"
366 run_command("sudo tcpdump -w xentaur.dump -i %s %s > /dev/null 2>&1 " % (bridge,filter))
367 except:
368 print "Done.\n Dump is written to xentaur.dump"
369 return 0
371 def dump_stop():
372 return 0
375 #-----------------------------------------------------------------------
376 # CONFIGURATION TEMPLATES
379 def configure_ip_addresses(doms=domains):
380 return 0
382 def configure_ospf(doms=domains):
383 return 0
385 def configure_save(doms=domains):
386 write_to(doms,"\nwr\n")
388 #-----------------------------------------------------------------------
391 def add_domain(name,type):
392 domains.append(name)
393 domain_types.append(type)
395 def brake_link(domain,bridge):
396 broken_links.append([domain,bridge])
398 wt_timeout=0.5
399 def write_to(screen,string,return_to_screen=""):
400 """
401 write_to(screen,string):
403 Type *string* to the specified screen(s).
404 Screen may be specified with the number *screen*,
405 with array of numbers,
406 with array of names.
408 """
409 screen_numbers=[] # number of the screens to write to
410 if type(screen) == list:
411 screen_numbers=map(lambda x: domains.index(x)+1, screen)
412 elif type(screen) == int:
413 screen_numbers=[screen]
414 else:
415 screen_numbers=[domains.index(screen)+1]
417 for screen_number in screen_numbers:
418 run_command("screen -X select "+str(screen_number))
419 time.sleep(wt_timeout)
420 for line in string.splitlines():
421 f=open('/tmp/xentaurbuf', 'w')
422 f.write(line+"\n")
423 f.close()
424 run_command("screen -X readreg p /tmp/xentaurbuf")
425 time.sleep(wt_timeout)
426 run_command("nohup screen -X paste p >& /dev/null")
427 time.sleep(wt_timeout)
429 if return_to_screen != "":
430 run_command("screen -X select %s" % (return_to_screen))
431 time.sleep(wt_timeout)
433 #-----------------------------------------------------------------------
435 cisco_fa01_up="""
436 ena
437 conf t
438 int fa0/0
439 no shutdown
440 exit
441 int fa1/0
442 no shutdown
443 exit
444 exit
445 exit
446 """
448 cisco_set_ip_on_int="""
449 interface fa%s/0
450 no ip address
451 ip address %s 255.255.255.0
452 exit
453 """
457 nodes=domains
459 if len(sys.argv) > 1:
460 if sys.argv[1] == 'start':
461 start_all()
462 if sys.argv[1] == 'stop':
463 stop_all()
464 if sys.argv[1] == 'start-bridges':
465 create_bridges_script()
466 if sys.argv[1] == 'start-domains':
467 create_domains_script()
468 if sys.argv[1] == 'stop-domains':
469 destroy_domains_script()
470 elif sys.argv[1] == 'screen':
471 create_screens_script()
472 elif sys.argv[1] == 'graph':
473 graph()
474 elif sys.argv[1] == 'shell':
475 shell()
477 else:
478 show_usage()
479 sys.exit(1)
481 sys.exit(0)