xentaur
view xentaur.py @ 27:9821c9a972cd
autoredraw + link state
author | igor |
---|---|
date | Fri Sep 21 20:22:10 2007 +0300 (2007-09-21) |
parents | 9cef8b150473 |
children | d22ff6f55dc5 |
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, this_bridge ] in temporary_links:
108 links.append(host+" -- "+this_bridge+" [taillabel=\"fa"+str(i)+"/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[1]+" [color=blue,len=10,w=5,weight=5]")
118 for link in broken_links:
119 links.append(link[0]+" -- "+link[1]+" [taillabel=\"fa"+str(i)+"/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, bridge ] in temporary_links:
235 temporary_links.remove([ domain, bridge ])
236 else:
237 broken_links.append([ domain, 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, bridge ] in broken_links:
253 broken_links.remove([ domain, bridge ])
254 else:
255 temporary_links.append([ domain, 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]
274 #-----------------------------------------------------------------------
277 def add_domain(name,type):
278 domains.append(name)
279 domain_types.append(type)
281 def brake_link(domain,bridge):
282 broken_links.append([domain,bridge])
284 wt_timeout=0.5
285 def write_to(screen,string,return_to_screen=""):
286 """
287 write_to(screen,string):
289 Type *string* to the specified screen(s).
290 Screen may be specified with the number *screen*,
291 with array of numbers,
292 with array of names.
294 """
295 screen_numbers=[] # number of the screens to write to
296 if type(screen) == list:
297 screen_numbers=map(lambda x: domains.index(x)+1, screen)
298 elif type(screen) == int:
299 screen_numbers=[screen]
300 else:
301 screen_numbers=[domains.index(screen)+1]
303 for screen_number in screen_numbers:
304 run_command("screen -X select "+str(screen_number))
305 time.sleep(wt_timeout)
306 for line in string.splitlines():
307 f=open('/tmp/xentaurbuf', 'w')
308 f.write(line+"\n")
309 f.close()
310 run_command("screen -X readreg p /tmp/xentaurbuf")
311 time.sleep(wt_timeout)
312 run_command("nohup screen -X paste p >& /dev/null")
313 time.sleep(wt_timeout)
315 if return_to_screen != "":
316 run_command("screen -X select %s" % (return_to_screen))
317 time.sleep(wt_timeout)
319 #-----------------------------------------------------------------------
321 cisco_fa01_up="""
322 ena
323 conf t
324 int fa0/0
325 no shutdown
326 exit
327 int fa1/0
328 no shutdown
329 exit
330 exit
331 exit
332 """
334 cisco_set_ip_on_int="""
335 interface fa%s/0
336 no ip address
337 ip address %s 255.255.255.0
338 exit
339 """
341 #-----------------------------------------------------------------------
343 nodes=domains
345 if len(sys.argv) > 1:
346 if sys.argv[1] == 'start':
347 start_all()
348 if sys.argv[1] == 'stop':
349 stop_all()
350 if sys.argv[1] == 'start-bridges':
351 create_bridges_script()
352 if sys.argv[1] == 'start-domains':
353 create_domains_script()
354 if sys.argv[1] == 'stop-domains':
355 destroy_domains_script()
356 elif sys.argv[1] == 'screen':
357 create_screens_script()
358 elif sys.argv[1] == 'graph':
359 graph()
360 elif sys.argv[1] == 'shell':
361 shell()
363 else:
364 show_usage()
365 sys.exit(1)
367 sys.exit(0)