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@5
|
8 from xenomipsN import bridges,vbridges_table, hidden_bridges, domains, broken_links, temporary_links, domain_types
|
igor@0
|
9 from IPython.Shell import IPShellEmbed
|
igor@0
|
10
|
igor@2
|
11
|
igor@2
|
12 screenrc=os.environ['HOME']+"/.screenrc_xentaur"
|
igor@2
|
13
|
igor@0
|
14 def create_bridges_script():
|
igor@0
|
15 unbound_bridges=bridges
|
igor@4
|
16 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
|
17 create_unbound_bridges+="\n"+"\n".join(map(lambda x: "sudo /bin/ip link set "+x+" up", unbound_bridges))
|
igor@0
|
18
|
igor@0
|
19 print """#!/bin/sh
|
igor@0
|
20 # create unbound bridges
|
igor@0
|
21 %(create_unbound_bridges)s
|
igor@0
|
22 """ % {'create_unbound_bridges' : create_unbound_bridges}
|
igor@0
|
23
|
igor@0
|
24
|
igor@0
|
25 def create_domains_script():
|
igor@0
|
26 for N in range(len(domains)):
|
igor@4
|
27 print "sudo /usr/sbin/xm create xenomipsN 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
|
28
|
igor@0
|
29 def destroy_domains_script():
|
igor@0
|
30 for N in range(len(domains)):
|
igor@4
|
31 print "sudo /usr/sbin/xm shutdown "+domains[N]
|
igor@0
|
32
|
igor@0
|
33 def create_screens_script():
|
igor@0
|
34
|
igor@7
|
35 N=1
|
igor@0
|
36 screens=[]
|
igor@0
|
37 for domain in domains:
|
igor@7
|
38 screens.append("screen -t "+domain+" "+str(N)+" xm console "+domain)
|
igor@7
|
39 # ip="192.168.80."+str(200+N)
|
igor@7
|
40 # screens.append("screen -t "+domain+" "+str(N)+" sh -c 'while true; do ssh root@"+ip+" ; done'")
|
igor@0
|
41 N+=1
|
igor@0
|
42 screenlist="\n".join(screens)
|
igor@0
|
43
|
igor@4
|
44 #hardstatus string "\%{gk}\%c \%{yk}\%M\%d \%{wk}\%?\%-Lw\%?\%{bw}\%n*\%f\%t\%?(\%u)\%?\%{wk}\%?\%+Lw\%?"
|
igor@0
|
45 print """
|
igor@4
|
46
|
igor@0
|
47 cat <<SCREENRC > screenrc
|
igor@2
|
48 hardstatus on
|
igor@2
|
49 hardstatus alwayslastline
|
igor@2
|
50
|
igor@0
|
51 screen -t console 0 bash
|
igor@0
|
52 %(screenlist)s
|
igor@0
|
53 SCREENRC
|
igor@0
|
54 """ % { 'screenlist' : screenlist }
|
igor@0
|
55
|
igor@0
|
56 def graph_node(node):
|
igor@0
|
57 i=0
|
igor@0
|
58 domain_type={}
|
igor@0
|
59 for domain in domains:
|
igor@0
|
60 domain_type[domain]=domain_types[i]
|
igor@0
|
61 i+=1
|
igor@0
|
62 return node+" [label=\" "+node+"\",shapefile=\"shapes/all/"+domain_type[node]+".png\",fontcolor=navy,fontsize=14]"
|
igor@0
|
63
|
igor@0
|
64 def graph():
|
igor@0
|
65 nodelist=""
|
igor@0
|
66 bridgelist=""
|
igor@0
|
67 linklist=""
|
igor@0
|
68 physicallist=""
|
igor@0
|
69 networklist=""
|
igor@0
|
70
|
igor@0
|
71 nodelist=";\n ".join(map(graph_node,nodes))
|
igor@0
|
72 if nodelist: nodelist += ";"
|
igor@0
|
73
|
igor@5
|
74 bridgelist=";\n ".join(set(bridges)-set(hidden_bridges))
|
igor@0
|
75 if bridgelist: bridgelist += ";"
|
igor@0
|
76
|
igor@0
|
77 links=[]
|
igor@0
|
78 for host, bridges_raw in vbridges_table.iteritems():
|
igor@0
|
79 i=0
|
igor@0
|
80 for this_bridge in bridges_raw:
|
igor@0
|
81 if this_bridge in hidden_bridges:
|
igor@0
|
82 continue
|
igor@0
|
83 if [ host, this_bridge ] in broken_links:
|
igor@0
|
84 links.append(host+" -- "+this_bridge+" [taillabel=\"fa"+str(i)+"/0\",style=dashed]")
|
igor@0
|
85 else:
|
igor@0
|
86 links.append(host+" -- "+this_bridge+" [taillabel=\"fa"+str(i)+"/0\"]")
|
igor@0
|
87 i+=1
|
igor@0
|
88
|
igor@0
|
89 for link in temporary_links:
|
igor@0
|
90 links.append(link[0]+" -- "+link[1]+" [color=blue,len=10,w=5,weight=5]")
|
igor@0
|
91
|
igor@0
|
92 linklist=";\n ".join(links)
|
igor@0
|
93
|
igor@0
|
94 graph_dot = {
|
igor@0
|
95 'nodelist' : nodelist,
|
igor@0
|
96 'bridgelist' : bridgelist,
|
igor@0
|
97 'linklist' : linklist,
|
igor@0
|
98 'physicallist' : physicallist,
|
igor@0
|
99 'networklist' : networklist,
|
igor@0
|
100 }
|
igor@0
|
101
|
igor@0
|
102 print """
|
igor@0
|
103 cat <<'GRAPH' > xenomips.dot
|
igor@0
|
104 graph G {
|
igor@0
|
105 edge [len=1.25];
|
igor@0
|
106 splines=true;
|
igor@0
|
107 // nodes
|
igor@0
|
108
|
igor@0
|
109 node [shape=plaintext,color=white,shapefile="shapes/cisco.bmp/router.png"];
|
igor@0
|
110 %(nodelist)s
|
igor@0
|
111
|
igor@0
|
112 // bridges
|
igor@0
|
113
|
igor@6
|
114 node [shape=none,shapefile="shapes/all/switch.png"];
|
igor@0
|
115 %(bridgelist)s
|
igor@0
|
116
|
igor@0
|
117 // physical
|
igor@0
|
118
|
igor@0
|
119 node [shape=rectangle,color=blue];
|
igor@0
|
120 %(physicallist)s
|
igor@0
|
121
|
igor@0
|
122 // networks (not bridges, not physical)
|
igor@0
|
123 node [shape=rectangle,color=green];
|
igor@0
|
124 %(networklist)s
|
igor@0
|
125
|
igor@0
|
126 // links (between nodes and bridges)
|
igor@0
|
127 %(linklist)s
|
igor@0
|
128
|
igor@0
|
129 };
|
igor@0
|
130 GRAPH
|
igor@0
|
131 neato -Tpng -o xenomips.png xenomips.dot
|
igor@0
|
132 """ % graph_dot
|
igor@0
|
133
|
igor@0
|
134 def start_all():
|
igor@0
|
135 create_bridges_script()
|
igor@0
|
136 create_screens_script()
|
igor@0
|
137 create_domains_script()
|
igor@0
|
138 graph()
|
igor@0
|
139 print """
|
igor@0
|
140 cat <<NOTE_FOR_USER
|
igor@0
|
141 # To view virtual network map, run:
|
igor@0
|
142 gqview xenomips.png
|
igor@0
|
143 # To attach to VM consoles, run:
|
igor@0
|
144 screen -c screenrc
|
igor@0
|
145 NOTE_FOR_USER
|
igor@0
|
146 """
|
igor@0
|
147
|
igor@0
|
148 def shell():
|
igor@0
|
149 ipshell = IPShellEmbed()
|
igor@0
|
150 ipshell()
|
igor@0
|
151
|
igor@0
|
152 def stop_all():
|
igor@4
|
153 destroy_domains_script()
|
igor@0
|
154
|
igor@0
|
155 def show_usage():
|
igor@0
|
156 print """Usage:
|
igor@0
|
157 xentaur {start|stop|start-bridges|start-domains|stop-domains|screen|graph}
|
igor@0
|
158 """
|
igor@0
|
159
|
igor@0
|
160 #-----------------------------------------------------------------------
|
igor@0
|
161
|
igor@0
|
162 def run(program, *args):
|
igor@0
|
163 pid = os.fork()
|
igor@0
|
164 if not pid:
|
igor@0
|
165 os.execvp(program, (program,) + args)
|
igor@0
|
166 return os.wait()[0]
|
igor@0
|
167
|
igor@2
|
168 def run_command(line):
|
igor@2
|
169 #cmds=line.split()
|
igor@2
|
170 #run(cmds[0],*cmds[1:])
|
igor@2
|
171 run("/bin/sh", "-c", line)
|
igor@0
|
172
|
igor@0
|
173 def add_domain(name,type):
|
igor@0
|
174 domains.append(name)
|
igor@0
|
175 domain_types.append(type)
|
igor@0
|
176
|
igor@0
|
177 def brake_link(domain,bridge):
|
igor@0
|
178 broken_links.append([domain,bridge])
|
igor@0
|
179
|
igor@4
|
180 wt_timeout=0.5
|
igor@8
|
181 def write_to(screen,string,return_to_screen=""):
|
igor@2
|
182 """
|
igor@2
|
183 write_to(screen,string):
|
igor@2
|
184
|
igor@8
|
185 Type *string* to the specified screen(s).
|
igor@8
|
186 Screen may be specified with the number *screen*,
|
igor@8
|
187 with array of numbers,
|
igor@8
|
188 with array of names.
|
igor@2
|
189
|
igor@2
|
190 """
|
igor@5
|
191 screen_numbers=[] # number of the screens to write to
|
igor@5
|
192 if type(screen) == list:
|
igor@5
|
193 screen_numbers=map(lambda x: domains.index(x)+1, screen)
|
igor@5
|
194 elif type(screen) == int:
|
igor@5
|
195 screen_numbers=[screen]
|
igor@5
|
196 else:
|
igor@5
|
197 screen_numbers=[domains.index(screen)+1]
|
igor@5
|
198
|
igor@5
|
199 for screen_number in screen_numbers:
|
igor@5
|
200 run_command("screen -X select "+str(screen_number))
|
igor@5
|
201 time.sleep(wt_timeout)
|
igor@5
|
202 for line in string.splitlines():
|
igor@5
|
203 f=open('/tmp/xentaurbuf', 'w')
|
igor@5
|
204 f.write(line+"\n")
|
igor@5
|
205 f.close()
|
igor@5
|
206 run_command("screen -X readreg p /tmp/xentaurbuf")
|
igor@5
|
207 time.sleep(wt_timeout)
|
igor@5
|
208 run_command("nohup screen -X paste p >& /dev/null")
|
igor@5
|
209 time.sleep(wt_timeout)
|
igor@5
|
210
|
igor@8
|
211 if return_to_screen != "":
|
igor@8
|
212 run_command("screen -X select %s" % (return_to_screen))
|
igor@8
|
213 time.sleep(wt_timeout)
|
igor@0
|
214
|
igor@0
|
215 #-----------------------------------------------------------------------
|
igor@0
|
216
|
igor@5
|
217 cisco_fa01_up="""
|
igor@5
|
218 ena
|
igor@5
|
219 conf t
|
igor@5
|
220 int fa0/0
|
igor@5
|
221 no shutdown
|
igor@5
|
222 exit
|
igor@5
|
223 int fa1/0
|
igor@5
|
224 no shutdown
|
igor@5
|
225 exit
|
igor@5
|
226 exit
|
igor@5
|
227 exit
|
igor@5
|
228 """
|
igor@5
|
229
|
igor@5
|
230 cisco_set_ip_on_int="""
|
igor@5
|
231 interface fa%s/0
|
igor@5
|
232 no ip address
|
igor@5
|
233 ip address %s 255.255.255.0
|
igor@5
|
234 exit
|
igor@5
|
235 """
|
igor@5
|
236
|
igor@5
|
237 #-----------------------------------------------------------------------
|
igor@0
|
238
|
igor@0
|
239 nodes=domains
|
igor@0
|
240
|
igor@0
|
241 if len(sys.argv) > 1:
|
igor@0
|
242 if sys.argv[1] == 'start':
|
igor@0
|
243 start_all()
|
igor@0
|
244 if sys.argv[1] == 'stop':
|
igor@0
|
245 stop_all()
|
igor@0
|
246 if sys.argv[1] == 'start-bridges':
|
igor@3
|
247 create_bridges_script()
|
igor@0
|
248 if sys.argv[1] == 'start-domains':
|
igor@3
|
249 create_domains_script()
|
igor@0
|
250 if sys.argv[1] == 'stop-domains':
|
igor@3
|
251 destroy_domains_script()
|
igor@0
|
252 elif sys.argv[1] == 'screen':
|
igor@3
|
253 create_screens_script()
|
igor@0
|
254 elif sys.argv[1] == 'graph':
|
igor@0
|
255 graph()
|
igor@0
|
256 elif sys.argv[1] == 'shell':
|
igor@0
|
257 shell()
|
igor@0
|
258
|
igor@0
|
259 else:
|
igor@0
|
260 show_usage()
|
igor@0
|
261 sys.exit(1)
|
igor@0
|
262
|
igor@0
|
263 sys.exit(0)
|
igor@0
|
264
|
igor@0
|
265
|