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