xentaur

view xentaur.py @ 34:dd8c79bc715c

xendomain.py added
author igor
date Tue Sep 25 20:12:24 2007 +0300 (2007-09-25)
parents bf2d7c5bb340
children 244f1602496a
line source
1 #!/usr/bin/python
4 import sys,os,time
5 import xenomips_vars
6 xenomips_vars.N='1'
7 sys.path.append('/etc/xen')
9 xen_config_name='four_quaggasN'
10 try:
11 exec 'from '+xen_config_name+' import bridges,vbridges_table, hidden_bridges, domains, broken_links, temporary_links, domain_types'
12 except:
13 print "Can't find module: %s " % (xen_config_name)
14 sys.exit(0)
16 bridges_turned_down=[]
18 from IPython.Shell import IPShellEmbed
21 screenrc=os.environ['HOME']+"/.screenrc_xentaur"
23 def run(program, *args):
24 pid = os.fork()
25 if not pid:
26 os.execvp(program, (program,) + args)
27 return os.wait()[0]
29 def run_command(line):
30 #cmds=line.split()
31 #run(cmds[0],*cmds[1:])
32 run("/bin/sh", "-c", line)
34 def run_command_return_stdout(line):
35 p = os.popen(line)
36 output = p.read()
37 p.close()
38 return output
40 def create_bridges_script():
41 unbound_bridges=bridges
42 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))
43 create_unbound_bridges+="\n"+"\n".join(map(lambda x: "sudo /bin/ip link set "+x+" up", unbound_bridges))
45 print """#!/bin/sh
46 # create unbound bridges
47 %(create_unbound_bridges)s
48 """ % {'create_unbound_bridges' : create_unbound_bridges}
51 def create_domains_script():
52 for N in range(len(domains)):
53 print "sudo /usr/sbin/xm create "+xen_config_name+" N="+str(N)+" && sleep 1 && sudo /usr/sbin/xm sched-credit -d $(sudo /usr/sbin/xm list | grep "+domains[N]+" | awk '{print $2}') -c 10 && sleep 1"
55 def destroy_domains_script():
56 for N in range(len(domains)):
57 print "sudo /usr/sbin/xm shutdown "+domains[N]
59 def create_screens_script():
60 N=1
61 screens=[]
62 for domain in domains:
63 screens.append("screen -t "+domain+" "+str(N)+" sh -c 'while true; do sudo xm console "+domain+" ; echo Retrying in 5 seconds...; sleep 5; done'")
64 N+=1
65 screenlist="\n".join(screens)
67 #
68 # Previous terminal acccess commands:
69 # ip="192.168.80."+str(200+N)
70 # screens.append("screen -t "+domain+" "+str(N)+" sh -c 'while true; do ssh root@"+ip+" ; done'")
71 #
73 #hardstatus string "\%{gk}\%c \%{yk}\%M\%d \%{wk}\%?\%-Lw\%?\%{bw}\%n*\%f\%t\%?(\%u)\%?\%{wk}\%?\%+Lw\%?"
75 f=open(screenrc, "w");
76 f.write("""
77 hardstatus on
78 hardstatus alwayslastline
80 screen -t console 0 bash
81 %s
82 """ % (screenlist))
83 f.close()
85 def graph_node(node):
86 i=0
87 domain_type={}
88 for domain in domains:
89 domain_type[domain]=domain_types[i]
90 i+=1
91 return node+" [label=\" "+node+"\",shapefile=\"shapes/all/"+domain_type[node]+".png\",fontcolor=black,fontsize=16]"
93 def graph_bridge(bridge):
94 if bridge in hidden_bridges:
95 return ""
96 if bridge in bridges_turned_down:
97 return "%s [shape=none,shapefile=\"shapes/all/switch_turned_down.png\"]" % (bridge)
98 else:
99 return "%s [shape=none,shapefile=\"shapes/all/switch.png\"]" % (bridge)
102 def graph():
103 nodelist=""
104 bridgelist=""
105 linklist=""
106 physicallist=""
107 networklist=""
109 nodelist=";\n ".join(map(graph_node,nodes))
110 if nodelist: nodelist += ";"
112 bridgelist=";\n ".join(map(graph_bridge,bridges))
113 if bridgelist: bridgelist += ";"
115 links=[]
116 for host, bridges_raw in vbridges_table.iteritems():
117 i=0
118 for this_bridge in bridges_raw:
119 if this_bridge in hidden_bridges or not this_bridge:
120 continue
121 if not [ host, bridges_raw.index(this_bridge), this_bridge ] in temporary_links:
122 links.append(host+" -- "+this_bridge+" [taillabel=\"fa"+str(bridges_raw.index(this_bridge))+"/0\"]")
123 i+=1
124 # if [ host, this_bridge ] in broken_links:
125 # links.append(host+" -- "+this_bridge+" [taillabel=\"fa"+str(i)+"/0\",style=dashed]")
126 # else:
127 # links.append(host+" -- "+this_bridge+" [taillabel=\"fa"+str(i)+"/0\"]")
129 for link in temporary_links:
130 links.append(link[0]+" -- "+link[2]+" [taillabel=\"fa"+str(link[1])+"/0\",color=blue,len=10,w=5,weight=5]")
132 for link in broken_links:
133 links.append(link[0]+" -- "+link[2]+" [taillabel=\"fa"+str(link[1])+"/0\",style=dashed]")
135 linklist=";\n ".join(links)
137 graph_dot = {
138 'nodelist' : nodelist,
139 'bridgelist' : bridgelist,
140 'linklist' : linklist,
141 'physicallist' : physicallist,
142 'networklist' : networklist,
143 }
145 f = open("xenomips.dot", "w");
146 f.write ("""
147 graph G {
148 edge [len=1.25];
149 splines=true;
150 // nodes
152 node [shape=plaintext,color=white,shapefile="shapes/cisco.bmp/router.png"];
153 %(nodelist)s
155 // bridges
157 node [shape=none,shapefile="shapes/all/switch.png"];
158 %(bridgelist)s
160 // physical
162 node [shape=rectangle,color=blue];
163 %(physicallist)s
165 // networks (not bridges, not physical)
166 node [shape=rectangle,color=green];
167 %(networklist)s
169 // links (between nodes and bridges)
170 %(linklist)s
172 };
173 """ % graph_dot)
174 f.close()
175 run_command("neato -Tpng -o xenomips.png xenomips.dot ")
177 def autoredraw():
178 graph()
180 def start_all():
181 create_bridges_script()
182 create_screens_script()
183 create_domains_script()
184 graph()
185 print """
186 cat <<NOTE_FOR_USER
187 # To view virtual network map, run:
188 gqview xenomips.png
189 # To attach to VM consoles, run:
190 screen -c screenrc
191 NOTE_FOR_USER
192 """
194 def shell():
195 autoredraw()
196 ipshell = IPShellEmbed()
197 ipshell()
199 def stop_all():
200 destroy_domains_script()
202 def show_usage():
203 print """Usage:
204 xentaur {start|stop|start-bridges|start-domains|stop-domains|screen|graph}
205 """
207 def save():
208 print "network =", xen_config_name
209 print "domains =", domains
210 print "domain_types =", domain_types
211 print "bridges =", bridges
212 print "vbridges_table =", vbridges_table
213 print "hidden_bridges =", hidden_bridges
214 print "broken_links =", broken_links
215 print "temporary_links =", temporary_links
216 print "bridges_turned_down =", bridges_turned_down
218 #-----------------------------------------------------------------------
219 # CLASSES
221 class Bridge:
222 def __init__ (self,name):
223 self.name=name
224 def up(self):
225 bridge_up(self.name)
226 def down(self):
227 bridge_down(self.name)
228 def show(self):
229 show_bridge(self.name)
230 def dump_start(self,filter=""):
231 dump_start(self.name,filter)
234 #-----------------------------------------------------------------------
235 # DOMAINS
237 def get_domain_id(domain):
238 return run_command_return_stdout("sudo xm list | awk '{if ($1 == \"'%s'\") print $2}'" % domain).rstrip("\n")
241 #-----------------------------------------------------------------------
242 # BRIDGES and IFACES
244 def bridge_down(bridge):
245 """
246 Turn the bridge <bridge> down
247 """
248 if bridge in bridges_turned_down:
249 print "Bridge %s is turned down already" % (bridge)
250 else:
251 bridges_turned_down.append(bridge)
252 run_command("sudo ip link set %s down" % bridge)
253 autoredraw()
255 def bridge_up(bridge):
256 """
257 Turn the bridge <bridge> up
258 """
259 if not (bridge in bridges_turned_down):
260 print "Bridge %s is turned up already" % (bridge)
261 else:
262 bridges_turned_down.remove(bridge)
263 run_command("sudo ip link set %s up" % bridge)
264 autoredraw()
266 def show_bridge(bridge):
267 """
268 Show the state of the bridge <bridge>
269 """
270 run_command("sudo ip link show %s" % bridge)
273 def int_disconnect(domain, int_number):
274 """
275 Disconnect the interface with the number <int_number>
276 of the domain <domain> from the bridge to which
277 it is connected
278 """
279 dom_id=get_domain_id(domain)
280 bridge=vbridges_table[domain][int_number]
281 if not bridge:
282 print "Interface %s of the %s domain is not connected" % (int_number, domain)
283 return 1
284 run_command("sudo brctl delif %s vif%s.%s" % (bridge, dom_id, int_number))
285 vbridges_table[domain][int_number]=''
286 if [ domain, int_number, bridge ] in temporary_links:
287 temporary_links.remove([ domain, int_number, bridge ])
288 else:
289 broken_links.append([ domain, int_number, bridge ])
290 autoredraw()
292 def int_connect(domain, int_number, bridge):
293 """
294 Connect the interface with the number <int_number>
295 of the domain <domain> to the bridge <bridge>
296 """
297 dom_id=get_domain_id(domain)
299 if vbridges_table[domain][int_number]:
300 print "Interface %s of the %s domain is connected already to the %s bridge" % (int_number, domain, vbridges_table[domain][int_number])
301 return 1
302 run_command("sudo brctl addif %s vif%s.%s" % (bridge, dom_id, int_number))
303 vbridges_table[domain][int_number]=bridge
304 if [ domain, int_number, bridge ] in broken_links:
305 broken_links.remove([ domain, int_number, bridge ])
306 else:
307 temporary_links.append([ domain, int_number, bridge ])
308 autoredraw()
310 def int_reconnect(domain, int_number, bridge):
311 """
312 Reconnect the interface with the number <int_number>
313 of the domain <domain> from the bridge to which
314 it is connected to the bridge <bridge>
315 """
316 int_disconnect(domain, int_number)
317 int_connect(domain, int_number, bridge)
319 def show_int(domain, int_number):
320 """
321 Show information about the interface <int_nuber>
322 of the domain <domain>
323 """
324 return vbridges_table[domain][int_number]
327 def dump_start(bridge, filter=""):
328 try:
329 print "Writing dump... (press Ctrl-C to stop)"
330 run_command("sudo tcpdump -w xentaur.dump -i %s %s > /dev/null 2>&1 " % (bridge,filter))
331 except:
332 print "Done.\n Dump is written to xentaur.dump"
333 return 0
335 def dump_stop():
336 return 0
339 #-----------------------------------------------------------------------
340 # CONFIGURATION TEMPLATES
343 def configure_ip_addresses(doms=domains):
344 return 0
346 def configure_ospf(doms=domains):
347 return 0
349 def configure_save(doms=domains):
350 write_to(doms,"\nwr\n")
352 #-----------------------------------------------------------------------
355 def add_domain(name,type):
356 domains.append(name)
357 domain_types.append(type)
359 def brake_link(domain,bridge):
360 broken_links.append([domain,bridge])
362 wt_timeout=0.5
363 def write_to(screen,string,return_to_screen=""):
364 """
365 write_to(screen,string):
367 Type *string* to the specified screen(s).
368 Screen may be specified with the number *screen*,
369 with array of numbers,
370 with array of names.
372 """
373 screen_numbers=[] # number of the screens to write to
374 if type(screen) == list:
375 screen_numbers=map(lambda x: domains.index(x)+1, screen)
376 elif type(screen) == int:
377 screen_numbers=[screen]
378 else:
379 screen_numbers=[domains.index(screen)+1]
381 for screen_number in screen_numbers:
382 run_command("screen -X select "+str(screen_number))
383 time.sleep(wt_timeout)
384 for line in string.splitlines():
385 f=open('/tmp/xentaurbuf', 'w')
386 f.write(line+"\n")
387 f.close()
388 run_command("screen -X readreg p /tmp/xentaurbuf")
389 time.sleep(wt_timeout)
390 run_command("nohup screen -X paste p >& /dev/null")
391 time.sleep(wt_timeout)
393 if return_to_screen != "":
394 run_command("screen -X select %s" % (return_to_screen))
395 time.sleep(wt_timeout)
397 #-----------------------------------------------------------------------
399 cisco_fa01_up="""
400 ena
401 conf t
402 int fa0/0
403 no shutdown
404 exit
405 int fa1/0
406 no shutdown
407 exit
408 exit
409 exit
410 """
412 cisco_set_ip_on_int="""
413 interface fa%s/0
414 no ip address
415 ip address %s 255.255.255.0
416 exit
417 """
421 nodes=domains
423 if len(sys.argv) > 1:
424 if sys.argv[1] == 'start':
425 start_all()
426 if sys.argv[1] == 'stop':
427 stop_all()
428 if sys.argv[1] == 'start-bridges':
429 create_bridges_script()
430 if sys.argv[1] == 'start-domains':
431 create_domains_script()
432 if sys.argv[1] == 'stop-domains':
433 destroy_domains_script()
434 elif sys.argv[1] == 'screen':
435 create_screens_script()
436 elif sys.argv[1] == 'graph':
437 graph()
438 elif sys.argv[1] == 'shell':
439 shell()
441 else:
442 show_usage()
443 sys.exit(1)
445 sys.exit(0)