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