xentaur

view xentaur.py @ 5:83a41d802a52

function write_to has improved.

Several templates of cisco configuration secuences have added.
author igor
date Sun Jul 15 14:34:59 2007 +0300 (2007-07-15)
parents 98c2dffcf484
children e4d8da085a2e
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=0
36 screens=[]
37 for domain in domains:
38 ip="192.168.80."+str(200+N)
39 screens.append("screen -t "+domain+" "+str(N)+" sh -c 'while true; do ssh root@"+ip+" ; done'")
40 N+=1
41 screenlist="\n".join(screens)
43 #hardstatus string "\%{gk}\%c \%{yk}\%M\%d \%{wk}\%?\%-Lw\%?\%{bw}\%n*\%f\%t\%?(\%u)\%?\%{wk}\%?\%+Lw\%?"
44 print """
46 cat <<SCREENRC > screenrc
47 hardstatus on
48 hardstatus alwayslastline
50 screen -t console 0 bash
51 %(screenlist)s
52 SCREENRC
53 """ % { 'screenlist' : screenlist }
55 def graph_node(node):
56 i=0
57 domain_type={}
58 for domain in domains:
59 domain_type[domain]=domain_types[i]
60 i+=1
61 return node+" [label=\" "+node+"\",shapefile=\"shapes/all/"+domain_type[node]+".png\",fontcolor=navy,fontsize=14]"
63 def graph():
64 nodelist=""
65 bridgelist=""
66 linklist=""
67 physicallist=""
68 networklist=""
70 nodelist=";\n ".join(map(graph_node,nodes))
71 if nodelist: nodelist += ";"
73 bridgelist=";\n ".join(set(bridges)-set(hidden_bridges))
74 if bridgelist: bridgelist += ";"
76 links=[]
77 for host, bridges_raw in vbridges_table.iteritems():
78 i=0
79 for this_bridge in bridges_raw:
80 if this_bridge in hidden_bridges:
81 continue
82 if [ host, this_bridge ] in broken_links:
83 links.append(host+" -- "+this_bridge+" [taillabel=\"fa"+str(i)+"/0\",style=dashed]")
84 else:
85 links.append(host+" -- "+this_bridge+" [taillabel=\"fa"+str(i)+"/0\"]")
86 i+=1
88 for link in temporary_links:
89 links.append(link[0]+" -- "+link[1]+" [color=blue,len=10,w=5,weight=5]")
91 linklist=";\n ".join(links)
93 graph_dot = {
94 'nodelist' : nodelist,
95 'bridgelist' : bridgelist,
96 'linklist' : linklist,
97 'physicallist' : physicallist,
98 'networklist' : networklist,
99 }
101 print """
102 cat <<'GRAPH' > xenomips.dot
103 graph G {
104 edge [len=1.25];
105 splines=true;
106 // nodes
108 node [shape=plaintext,color=white,shapefile="shapes/cisco.bmp/router.png"];
109 %(nodelist)s
111 // bridges
113 node [shape=none,shapefile="shapes/cisco.bmp/switch.png"];
114 %(bridgelist)s
116 // physical
118 node [shape=rectangle,color=blue];
119 %(physicallist)s
121 // networks (not bridges, not physical)
122 node [shape=rectangle,color=green];
123 %(networklist)s
125 // links (between nodes and bridges)
126 %(linklist)s
128 };
129 GRAPH
130 neato -Tpng -o xenomips.png xenomips.dot
131 """ % graph_dot
133 def start_all():
134 create_bridges_script()
135 create_screens_script()
136 create_domains_script()
137 graph()
138 print """
139 cat <<NOTE_FOR_USER
140 # To view virtual network map, run:
141 gqview xenomips.png
142 # To attach to VM consoles, run:
143 screen -c screenrc
144 NOTE_FOR_USER
145 """
147 def shell():
148 ipshell = IPShellEmbed()
149 ipshell()
151 def stop_all():
152 destroy_domains_script()
154 def show_usage():
155 print """Usage:
156 xentaur {start|stop|start-bridges|start-domains|stop-domains|screen|graph}
157 """
159 #-----------------------------------------------------------------------
161 def run(program, *args):
162 pid = os.fork()
163 if not pid:
164 os.execvp(program, (program,) + args)
165 return os.wait()[0]
167 def run_command(line):
168 #cmds=line.split()
169 #run(cmds[0],*cmds[1:])
170 run("/bin/sh", "-c", line)
172 def add_domain(name,type):
173 domains.append(name)
174 domain_types.append(type)
176 def brake_link(domain,bridge):
177 broken_links.append([domain,bridge])
179 wt_timeout=0.5
180 def write_to(screen,string):
181 """
182 write_to(screen,string):
184 Type *string* to the screen with the number *screen*
186 """
187 screen_numbers=[] # number of the screens to write to
188 if type(screen) == list:
189 screen_numbers=map(lambda x: domains.index(x)+1, screen)
190 elif type(screen) == int:
191 screen_numbers=[screen]
192 else:
193 screen_numbers=[domains.index(screen)+1]
195 for screen_number in screen_numbers:
196 run_command("screen -X select "+str(screen_number))
197 time.sleep(wt_timeout)
198 for line in string.splitlines():
199 f=open('/tmp/xentaurbuf', 'w')
200 f.write(line+"\n")
201 f.close()
202 run_command("screen -X readreg p /tmp/xentaurbuf")
203 time.sleep(wt_timeout)
204 run_command("nohup screen -X paste p >& /dev/null")
205 time.sleep(wt_timeout)
207 run_command("screen -X select 0")
208 time.sleep(wt_timeout)
210 #-----------------------------------------------------------------------
212 cisco_fa01_up="""
213 ena
214 conf t
215 int fa0/0
216 no shutdown
217 exit
218 int fa1/0
219 no shutdown
220 exit
221 exit
222 exit
223 """
225 cisco_set_ip_on_int="""
226 interface fa%s/0
227 no ip address
228 ip address %s 255.255.255.0
229 exit
230 """
232 #-----------------------------------------------------------------------
234 nodes=domains
236 if len(sys.argv) > 1:
237 if sys.argv[1] == 'start':
238 start_all()
239 if sys.argv[1] == 'stop':
240 stop_all()
241 if sys.argv[1] == 'start-bridges':
242 create_bridges_script()
243 if sys.argv[1] == 'start-domains':
244 create_domains_script()
245 if sys.argv[1] == 'stop-domains':
246 destroy_domains_script()
247 elif sys.argv[1] == 'screen':
248 create_screens_script()
249 elif sys.argv[1] == 'graph':
250 graph()
251 elif sys.argv[1] == 'shell':
252 shell()
254 else:
255 show_usage()
256 sys.exit(1)
258 sys.exit(0)