xentaur
view xentaur.py @ 4:98c2dffcf484
The games with the write screen timeouts
author | igor |
---|---|
date | Sun Jul 15 10:57:30 2007 +0300 (2007-07-15) |
parents | 2c6cfa822bd8 |
children | 83a41d802a52 |
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 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(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_number=screen
188 run_command("screen -X select "+str(screen_number))
189 time.sleep(wt_timeout)
190 for line in string.splitlines():
191 f=open('/tmp/xentaurbuf', 'w')
192 f.write(line+"\n")
193 f.close()
194 run_command("screen -X readreg p /tmp/xentaurbuf")
195 time.sleep(wt_timeout)
196 run_command("nohup screen -X paste p >& /dev/null")
197 time.sleep(wt_timeout)
199 run_command("screen -X select 0")
200 #time.sleep(wt_timeout)
202 #-----------------------------------------------------------------------
204 bridges=[]
205 for domain in vbridges_table.keys():
206 bridges += set(vbridges_table[domain])
207 bridges=set(bridges)
209 nodes=domains
211 if len(sys.argv) > 1:
212 if sys.argv[1] == 'start':
213 start_all()
214 if sys.argv[1] == 'stop':
215 stop_all()
216 if sys.argv[1] == 'start-bridges':
217 create_bridges_script()
218 if sys.argv[1] == 'start-domains':
219 create_domains_script()
220 if sys.argv[1] == 'stop-domains':
221 destroy_domains_script()
222 elif sys.argv[1] == 'screen':
223 create_screens_script()
224 elif sys.argv[1] == 'graph':
225 graph()
226 elif sys.argv[1] == 'shell':
227 shell()
229 else:
230 show_usage()
231 sys.exit(1)
233 sys.exit(0)