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