xentaur

view xentaur.py @ 7:825cd93472de

Dodelal
author igor
date Thu Aug 02 20:46:09 2007 +0300 (2007-08-02)
parents e4d8da085a2e
children 88a212771d27
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):
182 """
183 write_to(screen,string):
185 Type *string* to the screen with the number *screen*
187 """
188 screen_numbers=[] # number of the screens to write to
189 if type(screen) == list:
190 screen_numbers=map(lambda x: domains.index(x)+1, screen)
191 elif type(screen) == int:
192 screen_numbers=[screen]
193 else:
194 screen_numbers=[domains.index(screen)+1]
196 for screen_number in screen_numbers:
197 run_command("screen -X select "+str(screen_number))
198 time.sleep(wt_timeout)
199 for line in string.splitlines():
200 f=open('/tmp/xentaurbuf', 'w')
201 f.write(line+"\n")
202 f.close()
203 run_command("screen -X readreg p /tmp/xentaurbuf")
204 time.sleep(wt_timeout)
205 run_command("nohup screen -X paste p >& /dev/null")
206 time.sleep(wt_timeout)
208 run_command("screen -X select 0")
209 time.sleep(wt_timeout)
211 #-----------------------------------------------------------------------
213 cisco_fa01_up="""
214 ena
215 conf t
216 int fa0/0
217 no shutdown
218 exit
219 int fa1/0
220 no shutdown
221 exit
222 exit
223 exit
224 """
226 cisco_set_ip_on_int="""
227 interface fa%s/0
228 no ip address
229 ip address %s 255.255.255.0
230 exit
231 """
233 #-----------------------------------------------------------------------
235 nodes=domains
237 if len(sys.argv) > 1:
238 if sys.argv[1] == 'start':
239 start_all()
240 if sys.argv[1] == 'stop':
241 stop_all()
242 if sys.argv[1] == 'start-bridges':
243 create_bridges_script()
244 if sys.argv[1] == 'start-domains':
245 create_domains_script()
246 if sys.argv[1] == 'stop-domains':
247 destroy_domains_script()
248 elif sys.argv[1] == 'screen':
249 create_screens_script()
250 elif sys.argv[1] == 'graph':
251 graph()
252 elif sys.argv[1] == 'shell':
253 shell()
255 else:
256 show_usage()
257 sys.exit(1)
259 sys.exit(0)