xentaur

view xentaur.py @ 11:1ede117a35ec

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