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