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
|
nata@31
|
9 xen_config_name='netw'
|
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
|
nata@31
|
88 return node+" [label=\" "+node+"\",shapefile=\"shapes/all/"+domain_type[node]+".png\",fontcolor=black,fontsize=16]"
|
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():
|
nata@31
|
192 autoredraw()
|
igor@0
|
193 ipshell = IPShellEmbed()
|
igor@0
|
194 ipshell()
|
igor@0
|
195
|
igor@0
|
196 def stop_all():
|
igor@4
|
197 destroy_domains_script()
|
igor@0
|
198
|
igor@0
|
199 def show_usage():
|
igor@0
|
200 print """Usage:
|
igor@0
|
201 xentaur {start|stop|start-bridges|start-domains|stop-domains|screen|graph}
|
igor@0
|
202 """
|
igor@0
|
203
|
igor@23
|
204 #-----------------------------------------------------------------------
|
igor@23
|
205 # DOMAINS
|
igor@23
|
206
|
igor@23
|
207 def get_domain_id(domain):
|
igor@23
|
208 return run_command_return_stdout("sudo xm list | awk '{if ($1 == \"'%s'\") print $2}'" % domain).rstrip("\n")
|
igor@23
|
209
|
igor@22
|
210
|
igor@22
|
211 #-----------------------------------------------------------------------
|
igor@22
|
212 # BRIDGES and IFACES
|
igor@22
|
213
|
igor@22
|
214 def bridge_down(bridge):
|
igor@22
|
215 """
|
igor@22
|
216 Turn the bridge <bridge> down
|
igor@22
|
217 """
|
igor@29
|
218 if bridge in bridges_turned_down:
|
igor@29
|
219 print "Bridge %s is turned down already" % (bridge)
|
igor@29
|
220 else:
|
igor@29
|
221 bridges_turned_down.append(bridge)
|
igor@29
|
222 run_command("sudo ip link set %s down" % bridge)
|
igor@29
|
223 autoredraw()
|
igor@22
|
224
|
igor@22
|
225 def bridge_up(bridge):
|
igor@22
|
226 """
|
igor@22
|
227 Turn the bridge <bridge> up
|
igor@22
|
228 """
|
igor@29
|
229 if not (bridge in bridges_turned_down):
|
igor@29
|
230 print "Bridge %s is turned up already" % (bridge)
|
igor@29
|
231 else:
|
igor@29
|
232 bridges_turned_down.remove(bridge)
|
igor@29
|
233 run_command("sudo ip link set %s up" % bridge)
|
igor@29
|
234 autoredraw()
|
igor@22
|
235
|
igor@22
|
236 def show_bridge(bridge):
|
igor@22
|
237 """
|
igor@22
|
238 Show the state of the bridge <bridge>
|
igor@22
|
239 """
|
igor@22
|
240 run_command("sudo ip link show %s" % bridge)
|
igor@22
|
241
|
igor@23
|
242
|
igor@23
|
243 def int_disconnect(domain, int_number):
|
igor@23
|
244 """
|
igor@23
|
245 Disconnect the interface with the number <int_number>
|
igor@23
|
246 of the domain <domain> from the bridge to which
|
igor@23
|
247 it is connected
|
igor@23
|
248 """
|
igor@23
|
249 dom_id=get_domain_id(domain)
|
igor@23
|
250 bridge=vbridges_table[domain][int_number]
|
igor@23
|
251 if not bridge:
|
igor@23
|
252 print "Interface %s of the %s domain is not connected" % (int_number, domain)
|
igor@23
|
253 return 1
|
igor@23
|
254 run_command("sudo brctl delif %s vif%s.%s" % (bridge, dom_id, int_number))
|
igor@23
|
255 vbridges_table[domain][int_number]=''
|
igor@28
|
256 if [ domain, int_number, bridge ] in temporary_links:
|
igor@28
|
257 temporary_links.remove([ domain, int_number, bridge ])
|
igor@27
|
258 else:
|
igor@28
|
259 broken_links.append([ domain, int_number, bridge ])
|
igor@27
|
260 autoredraw()
|
igor@23
|
261
|
igor@23
|
262 def int_connect(domain, int_number, bridge):
|
igor@23
|
263 """
|
igor@23
|
264 Connect the interface with the number <int_number>
|
igor@24
|
265 of the domain <domain> to the bridge <bridge>
|
igor@23
|
266 """
|
igor@23
|
267 dom_id=get_domain_id(domain)
|
igor@23
|
268
|
igor@23
|
269 if vbridges_table[domain][int_number]:
|
igor@23
|
270 print "Interface %s of the %s domain is connected already to the %s bridge" % (int_number, domain, vbridges_table[domain][int_number])
|
igor@23
|
271 return 1
|
igor@23
|
272 run_command("sudo brctl addif %s vif%s.%s" % (bridge, dom_id, int_number))
|
igor@23
|
273 vbridges_table[domain][int_number]=bridge
|
igor@28
|
274 if [ domain, int_number, bridge ] in broken_links:
|
igor@28
|
275 broken_links.remove([ domain, int_number, bridge ])
|
igor@27
|
276 else:
|
igor@28
|
277 temporary_links.append([ domain, int_number, bridge ])
|
igor@27
|
278 autoredraw()
|
igor@23
|
279
|
igor@24
|
280 def int_reconnect(domain, int_number, bridge):
|
igor@24
|
281 """
|
igor@24
|
282 Reconnect the interface with the number <int_number>
|
igor@24
|
283 of the domain <domain> from the bridge to which
|
igor@24
|
284 it is connected to the bridge <bridge>
|
igor@24
|
285 """
|
igor@24
|
286 int_disconnect(domain, int_number)
|
igor@24
|
287 int_connect(domain, int_number, bridge)
|
igor@24
|
288
|
igor@24
|
289 def show_int(domain, int_number):
|
igor@25
|
290 """
|
igor@25
|
291 Show information about the interface <int_nuber>
|
igor@25
|
292 of the domain <domain>
|
igor@25
|
293 """
|
igor@26
|
294 return vbridges_table[domain][int_number]
|
igor@24
|
295
|
igor@28
|
296
|
igor@28
|
297 def dump_start(bridge, filter=""):
|
igor@29
|
298 run_command("sudo tcpdump -w xentaur.dump -i %s %s" % (bridge,filter))
|
igor@28
|
299 return 0
|
igor@28
|
300
|
igor@28
|
301 def dump_stop():
|
igor@28
|
302 return 0
|
igor@28
|
303
|
igor@0
|
304 #-----------------------------------------------------------------------
|
igor@0
|
305
|
igor@0
|
306
|
igor@0
|
307 def add_domain(name,type):
|
igor@0
|
308 domains.append(name)
|
igor@0
|
309 domain_types.append(type)
|
igor@0
|
310
|
igor@0
|
311 def brake_link(domain,bridge):
|
igor@0
|
312 broken_links.append([domain,bridge])
|
igor@0
|
313
|
igor@4
|
314 wt_timeout=0.5
|
igor@8
|
315 def write_to(screen,string,return_to_screen=""):
|
igor@2
|
316 """
|
igor@2
|
317 write_to(screen,string):
|
igor@2
|
318
|
igor@8
|
319 Type *string* to the specified screen(s).
|
igor@8
|
320 Screen may be specified with the number *screen*,
|
igor@8
|
321 with array of numbers,
|
igor@8
|
322 with array of names.
|
igor@2
|
323
|
igor@2
|
324 """
|
igor@5
|
325 screen_numbers=[] # number of the screens to write to
|
igor@5
|
326 if type(screen) == list:
|
igor@5
|
327 screen_numbers=map(lambda x: domains.index(x)+1, screen)
|
igor@5
|
328 elif type(screen) == int:
|
igor@5
|
329 screen_numbers=[screen]
|
igor@5
|
330 else:
|
igor@5
|
331 screen_numbers=[domains.index(screen)+1]
|
igor@5
|
332
|
igor@5
|
333 for screen_number in screen_numbers:
|
igor@5
|
334 run_command("screen -X select "+str(screen_number))
|
igor@5
|
335 time.sleep(wt_timeout)
|
igor@5
|
336 for line in string.splitlines():
|
igor@5
|
337 f=open('/tmp/xentaurbuf', 'w')
|
igor@5
|
338 f.write(line+"\n")
|
igor@5
|
339 f.close()
|
igor@5
|
340 run_command("screen -X readreg p /tmp/xentaurbuf")
|
igor@5
|
341 time.sleep(wt_timeout)
|
igor@5
|
342 run_command("nohup screen -X paste p >& /dev/null")
|
igor@5
|
343 time.sleep(wt_timeout)
|
igor@5
|
344
|
igor@8
|
345 if return_to_screen != "":
|
igor@8
|
346 run_command("screen -X select %s" % (return_to_screen))
|
igor@8
|
347 time.sleep(wt_timeout)
|
igor@0
|
348
|
igor@0
|
349 #-----------------------------------------------------------------------
|
igor@0
|
350
|
igor@5
|
351 cisco_fa01_up="""
|
igor@5
|
352 ena
|
igor@5
|
353 conf t
|
igor@5
|
354 int fa0/0
|
igor@5
|
355 no shutdown
|
igor@5
|
356 exit
|
igor@5
|
357 int fa1/0
|
igor@5
|
358 no shutdown
|
igor@5
|
359 exit
|
igor@5
|
360 exit
|
igor@5
|
361 exit
|
igor@5
|
362 """
|
igor@5
|
363
|
igor@5
|
364 cisco_set_ip_on_int="""
|
igor@5
|
365 interface fa%s/0
|
igor@5
|
366 no ip address
|
igor@5
|
367 ip address %s 255.255.255.0
|
igor@5
|
368 exit
|
igor@5
|
369 """
|
igor@5
|
370
|
igor@5
|
371 #-----------------------------------------------------------------------
|
igor@0
|
372
|
igor@0
|
373 nodes=domains
|
igor@0
|
374
|
igor@0
|
375 if len(sys.argv) > 1:
|
igor@0
|
376 if sys.argv[1] == 'start':
|
igor@0
|
377 start_all()
|
igor@0
|
378 if sys.argv[1] == 'stop':
|
igor@0
|
379 stop_all()
|
igor@0
|
380 if sys.argv[1] == 'start-bridges':
|
igor@3
|
381 create_bridges_script()
|
igor@0
|
382 if sys.argv[1] == 'start-domains':
|
igor@3
|
383 create_domains_script()
|
igor@0
|
384 if sys.argv[1] == 'stop-domains':
|
igor@3
|
385 destroy_domains_script()
|
igor@0
|
386 elif sys.argv[1] == 'screen':
|
igor@3
|
387 create_screens_script()
|
igor@0
|
388 elif sys.argv[1] == 'graph':
|
igor@0
|
389 graph()
|
igor@0
|
390 elif sys.argv[1] == 'shell':
|
igor@0
|
391 shell()
|
igor@0
|
392
|
igor@0
|
393 else:
|
igor@0
|
394 show_usage()
|
igor@0
|
395 sys.exit(1)
|
igor@0
|
396
|
igor@0
|
397 sys.exit(0)
|
igor@0
|
398
|
igor@0
|
399
|