xentaur

view xentaur.py @ 28:d22ff6f55dc5

autoredraw + link state fix
author igor
date Fri Sep 21 20:48:40 2007 +0300 (2007-09-21)
parents 9821c9a972cd
children 87f19c6d1514
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='fabergeN'
10 exec 'from '+xen_config_name+' import bridges,vbridges_table, hidden_bridges, domains, broken_links, temporary_links, domain_types'
12 from IPython.Shell import IPShellEmbed
15 screenrc=os.environ['HOME']+"/.screenrc_xentaur"
17 def run(program, *args):
18 pid = os.fork()
19 if not pid:
20 os.execvp(program, (program,) + args)
21 return os.wait()[0]
23 def run_command(line):
24 #cmds=line.split()
25 #run(cmds[0],*cmds[1:])
26 run("/bin/sh", "-c", line)
28 def run_command_return_stdout(line):
29 p = os.popen(line)
30 output = p.read()
31 p.close()
32 return output
34 def create_bridges_script():
35 unbound_bridges=bridges
36 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))
37 create_unbound_bridges+="\n"+"\n".join(map(lambda x: "sudo /bin/ip link set "+x+" up", unbound_bridges))
39 print """#!/bin/sh
40 # create unbound bridges
41 %(create_unbound_bridges)s
42 """ % {'create_unbound_bridges' : create_unbound_bridges}
45 def create_domains_script():
46 for N in range(len(domains)):
47 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"
49 def destroy_domains_script():
50 for N in range(len(domains)):
51 print "sudo /usr/sbin/xm shutdown "+domains[N]
53 def create_screens_script():
55 N=1
56 screens=[]
57 for domain in domains:
58 screens.append("screen -t "+domain+" "+str(N)+" sh -c 'while true; do sudo xm console "+domain+" ; echo Retrying in 5 seconds...; sleep 5; done'")
59 N+=1
60 screenlist="\n".join(screens)
62 #
63 # Previous terminal acccess commands:
64 # ip="192.168.80."+str(200+N)
65 # screens.append("screen -t "+domain+" "+str(N)+" sh -c 'while true; do ssh root@"+ip+" ; done'")
66 #
68 #hardstatus string "\%{gk}\%c \%{yk}\%M\%d \%{wk}\%?\%-Lw\%?\%{bw}\%n*\%f\%t\%?(\%u)\%?\%{wk}\%?\%+Lw\%?"
70 f=open(screenrc, "w");
71 f.write("""
72 hardstatus on
73 hardstatus alwayslastline
75 screen -t console 0 bash
76 %s
77 """ % (screenlist))
78 f.close()
80 def graph_node(node):
81 i=0
82 domain_type={}
83 for domain in domains:
84 domain_type[domain]=domain_types[i]
85 i+=1
86 return node+" [label=\" "+node+"\",shapefile=\"shapes/all/"+domain_type[node]+".png\",fontcolor=navy,fontsize=14]"
88 def graph():
89 nodelist=""
90 bridgelist=""
91 linklist=""
92 physicallist=""
93 networklist=""
95 nodelist=";\n ".join(map(graph_node,nodes))
96 if nodelist: nodelist += ";"
98 bridgelist=";\n ".join(set(bridges)-set(hidden_bridges))
99 if bridgelist: bridgelist += ";"
101 links=[]
102 for host, bridges_raw in vbridges_table.iteritems():
103 i=0
104 for this_bridge in bridges_raw:
105 if this_bridge in hidden_bridges or not this_bridge:
106 continue
107 if not [ host, bridges_raw.index(this_bridge), this_bridge ] in temporary_links:
108 links.append(host+" -- "+this_bridge+" [taillabel=\"fa"+str(bridges_raw.index(this_bridge))+"/0\"]")
109 i+=1
110 # if [ host, this_bridge ] in broken_links:
111 # links.append(host+" -- "+this_bridge+" [taillabel=\"fa"+str(i)+"/0\",style=dashed]")
112 # else:
113 # links.append(host+" -- "+this_bridge+" [taillabel=\"fa"+str(i)+"/0\"]")
115 for link in temporary_links:
116 links.append(link[0]+" -- "+link[2]+" [taillabel=\"fa"+str(link[1])+"/0\",color=blue,len=10,w=5,weight=5]")
118 for link in broken_links:
119 links.append(link[0]+" -- "+link[2]+" [taillabel=\"fa"+str(link[1])+"/0\",style=dashed]")
121 linklist=";\n ".join(links)
123 graph_dot = {
124 'nodelist' : nodelist,
125 'bridgelist' : bridgelist,
126 'linklist' : linklist,
127 'physicallist' : physicallist,
128 'networklist' : networklist,
129 }
131 f = open("xenomips.dot", "w");
132 f.write ("""
133 graph G {
134 edge [len=1.25];
135 splines=true;
136 // nodes
138 node [shape=plaintext,color=white,shapefile="shapes/cisco.bmp/router.png"];
139 %(nodelist)s
141 // bridges
143 node [shape=none,shapefile="shapes/all/switch.png"];
144 %(bridgelist)s
146 // physical
148 node [shape=rectangle,color=blue];
149 %(physicallist)s
151 // networks (not bridges, not physical)
152 node [shape=rectangle,color=green];
153 %(networklist)s
155 // links (between nodes and bridges)
156 %(linklist)s
158 };
159 """ % graph_dot)
160 f.close()
161 run_command("neato -Tpng -o xenomips.png xenomips.dot ")
163 def autoredraw():
164 graph()
166 def start_all():
167 create_bridges_script()
168 create_screens_script()
169 create_domains_script()
170 graph()
171 print """
172 cat <<NOTE_FOR_USER
173 # To view virtual network map, run:
174 gqview xenomips.png
175 # To attach to VM consoles, run:
176 screen -c screenrc
177 NOTE_FOR_USER
178 """
180 def shell():
181 ipshell = IPShellEmbed()
182 ipshell()
184 def stop_all():
185 destroy_domains_script()
187 def show_usage():
188 print """Usage:
189 xentaur {start|stop|start-bridges|start-domains|stop-domains|screen|graph}
190 """
192 #-----------------------------------------------------------------------
193 # DOMAINS
195 def get_domain_id(domain):
196 return run_command_return_stdout("sudo xm list | awk '{if ($1 == \"'%s'\") print $2}'" % domain).rstrip("\n")
199 #-----------------------------------------------------------------------
200 # BRIDGES and IFACES
202 def bridge_down(bridge):
203 """
204 Turn the bridge <bridge> down
205 """
206 run_command("sudo ip link set %s down" % bridge)
208 def bridge_up(bridge):
209 """
210 Turn the bridge <bridge> up
211 """
212 run_command("sudo ip link set %s up" % bridge)
214 def show_bridge(bridge):
215 """
216 Show the state of the bridge <bridge>
217 """
218 run_command("sudo ip link show %s" % bridge)
221 def int_disconnect(domain, int_number):
222 """
223 Disconnect the interface with the number <int_number>
224 of the domain <domain> from the bridge to which
225 it is connected
226 """
227 dom_id=get_domain_id(domain)
228 bridge=vbridges_table[domain][int_number]
229 if not bridge:
230 print "Interface %s of the %s domain is not connected" % (int_number, domain)
231 return 1
232 run_command("sudo brctl delif %s vif%s.%s" % (bridge, dom_id, int_number))
233 vbridges_table[domain][int_number]=''
234 if [ domain, int_number, bridge ] in temporary_links:
235 temporary_links.remove([ domain, int_number, bridge ])
236 else:
237 broken_links.append([ domain, int_number, bridge ])
238 autoredraw()
240 def int_connect(domain, int_number, bridge):
241 """
242 Connect the interface with the number <int_number>
243 of the domain <domain> to the bridge <bridge>
244 """
245 dom_id=get_domain_id(domain)
247 if vbridges_table[domain][int_number]:
248 print "Interface %s of the %s domain is connected already to the %s bridge" % (int_number, domain, vbridges_table[domain][int_number])
249 return 1
250 run_command("sudo brctl addif %s vif%s.%s" % (bridge, dom_id, int_number))
251 vbridges_table[domain][int_number]=bridge
252 if [ domain, int_number, bridge ] in broken_links:
253 broken_links.remove([ domain, int_number, bridge ])
254 else:
255 temporary_links.append([ domain, int_number, bridge ])
256 autoredraw()
258 def int_reconnect(domain, int_number, bridge):
259 """
260 Reconnect the interface with the number <int_number>
261 of the domain <domain> from the bridge to which
262 it is connected to the bridge <bridge>
263 """
264 int_disconnect(domain, int_number)
265 int_connect(domain, int_number, bridge)
267 def show_int(domain, int_number):
268 """
269 Show information about the interface <int_nuber>
270 of the domain <domain>
271 """
272 return vbridges_table[domain][int_number]
275 def dump_start(bridge, filter=""):
276 return 0
278 def dump_stop():
279 return 0
281 #-----------------------------------------------------------------------
284 def add_domain(name,type):
285 domains.append(name)
286 domain_types.append(type)
288 def brake_link(domain,bridge):
289 broken_links.append([domain,bridge])
291 wt_timeout=0.5
292 def write_to(screen,string,return_to_screen=""):
293 """
294 write_to(screen,string):
296 Type *string* to the specified screen(s).
297 Screen may be specified with the number *screen*,
298 with array of numbers,
299 with array of names.
301 """
302 screen_numbers=[] # number of the screens to write to
303 if type(screen) == list:
304 screen_numbers=map(lambda x: domains.index(x)+1, screen)
305 elif type(screen) == int:
306 screen_numbers=[screen]
307 else:
308 screen_numbers=[domains.index(screen)+1]
310 for screen_number in screen_numbers:
311 run_command("screen -X select "+str(screen_number))
312 time.sleep(wt_timeout)
313 for line in string.splitlines():
314 f=open('/tmp/xentaurbuf', 'w')
315 f.write(line+"\n")
316 f.close()
317 run_command("screen -X readreg p /tmp/xentaurbuf")
318 time.sleep(wt_timeout)
319 run_command("nohup screen -X paste p >& /dev/null")
320 time.sleep(wt_timeout)
322 if return_to_screen != "":
323 run_command("screen -X select %s" % (return_to_screen))
324 time.sleep(wt_timeout)
326 #-----------------------------------------------------------------------
328 cisco_fa01_up="""
329 ena
330 conf t
331 int fa0/0
332 no shutdown
333 exit
334 int fa1/0
335 no shutdown
336 exit
337 exit
338 exit
339 """
341 cisco_set_ip_on_int="""
342 interface fa%s/0
343 no ip address
344 ip address %s 255.255.255.0
345 exit
346 """
348 #-----------------------------------------------------------------------
350 nodes=domains
352 if len(sys.argv) > 1:
353 if sys.argv[1] == 'start':
354 start_all()
355 if sys.argv[1] == 'stop':
356 stop_all()
357 if sys.argv[1] == 'start-bridges':
358 create_bridges_script()
359 if sys.argv[1] == 'start-domains':
360 create_domains_script()
361 if sys.argv[1] == 'stop-domains':
362 destroy_domains_script()
363 elif sys.argv[1] == 'screen':
364 create_screens_script()
365 elif sys.argv[1] == 'graph':
366 graph()
367 elif sys.argv[1] == 'shell':
368 shell()
370 else:
371 show_usage()
372 sys.exit(1)
374 sys.exit(0)