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