xentaur
view xentaur.py @ 3:2c6cfa822bd8
Исправлены некоторые ошибки
author | igor |
---|---|
date | Fri Jul 13 20:57:09 2007 +0300 (2007-07-13) |
parents | e34dd9ee49e5 |
children | 98c2dffcf484 |
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: "brctl show | awk '{print $1}' | grep -q "+x+" || brctl addbr "+x, unbound_bridges))
17 create_unbound_bridges+="\n"+"\n".join(map(lambda x: "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 "xm create xenomipsN N="+str(N)+" && sleep 1 && xm sched-credit -d $(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 "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 print """
44 cat <<SCREENRC > screenrc
45 hardstatus on
46 hardstatus alwayslastline
47 hardstatus string "%{gk}%c %{yk}%M%d %{wk}%?%-Lw%?%{bw}%n*%f%t%?(%u)%?%{wk}%?%+Lw%?"
49 screen -t console 0 bash
50 %(screenlist)s
51 SCREENRC
52 """ % { 'screenlist' : screenlist }
54 def graph_node(node):
55 i=0
56 domain_type={}
57 for domain in domains:
58 domain_type[domain]=domain_types[i]
59 i+=1
60 return node+" [label=\" "+node+"\",shapefile=\"shapes/all/"+domain_type[node]+".png\",fontcolor=navy,fontsize=14]"
62 def graph():
63 nodelist=""
64 bridgelist=""
65 linklist=""
66 physicallist=""
67 networklist=""
69 nodelist=";\n ".join(map(graph_node,nodes))
70 if nodelist: nodelist += ";"
72 bridgelist=";\n ".join(bridges-set(hidden_bridges))
73 if bridgelist: bridgelist += ";"
75 links=[]
76 for host, bridges_raw in vbridges_table.iteritems():
77 i=0
78 for this_bridge in bridges_raw:
79 if this_bridge in hidden_bridges:
80 continue
81 if [ host, this_bridge ] in broken_links:
82 links.append(host+" -- "+this_bridge+" [taillabel=\"fa"+str(i)+"/0\",style=dashed]")
83 else:
84 links.append(host+" -- "+this_bridge+" [taillabel=\"fa"+str(i)+"/0\"]")
85 i+=1
87 for link in temporary_links:
88 links.append(link[0]+" -- "+link[1]+" [color=blue,len=10,w=5,weight=5]")
90 linklist=";\n ".join(links)
92 graph_dot = {
93 'nodelist' : nodelist,
94 'bridgelist' : bridgelist,
95 'linklist' : linklist,
96 'physicallist' : physicallist,
97 'networklist' : networklist,
98 }
100 print """
101 cat <<'GRAPH' > xenomips.dot
102 graph G {
103 edge [len=1.25];
104 splines=true;
105 // nodes
107 node [shape=plaintext,color=white,shapefile="shapes/cisco.bmp/router.png"];
108 %(nodelist)s
110 // bridges
112 node [shape=none,shapefile="shapes/cisco.bmp/switch.png"];
113 %(bridgelist)s
115 // physical
117 node [shape=rectangle,color=blue];
118 %(physicallist)s
120 // networks (not bridges, not physical)
121 node [shape=rectangle,color=green];
122 %(networklist)s
124 // links (between nodes and bridges)
125 %(linklist)s
127 };
128 GRAPH
129 neato -Tpng -o xenomips.png xenomips.dot
130 """ % graph_dot
132 def start_all():
133 create_bridges_script()
134 create_screens_script()
135 create_domains_script()
136 graph()
137 print """
138 cat <<NOTE_FOR_USER
139 # To view virtual network map, run:
140 gqview xenomips.png
141 # To attach to VM consoles, run:
142 screen -c screenrc
143 NOTE_FOR_USER
144 """
146 def shell():
147 ipshell = IPShellEmbed()
148 ipshell()
150 def stop_all():
151 destroy_domains_script(domains)
153 def show_usage():
154 print """Usage:
155 xentaur {start|stop|start-bridges|start-domains|stop-domains|screen|graph}
156 """
158 #-----------------------------------------------------------------------
160 def run(program, *args):
161 pid = os.fork()
162 if not pid:
163 os.execvp(program, (program,) + args)
164 return os.wait()[0]
166 def run_command(line):
167 #cmds=line.split()
168 #run(cmds[0],*cmds[1:])
169 run("/bin/sh", "-c", line)
171 def add_domain(name,type):
172 domains.append(name)
173 domain_types.append(type)
175 def brake_link(domain,bridge):
176 broken_links.append([domain,bridge])
178 def write_to(screen,string):
179 """
180 write_to(screen,string):
182 Type *string* to the screen with the number *screen*
184 """
185 screen_number=screen
186 f=open('/tmp/xentaurbuf', 'w')
187 f.write(string)
188 f.close()
189 run_command("screen -X readreg p /tmp/xentaurbuf")
190 run_command("screen -X select "+str(screen_number))
191 run_command("nohup screen -X paste p >& /dev/null")
192 time.sleep(0.05)
193 run_command("screen -X select 0")
195 #-----------------------------------------------------------------------
197 bridges=[]
198 for domain in vbridges_table.keys():
199 bridges += set(vbridges_table[domain])
200 bridges=set(bridges)
202 nodes=domains
204 if len(sys.argv) > 1:
205 if sys.argv[1] == 'start':
206 start_all()
207 if sys.argv[1] == 'stop':
208 stop_all()
209 if sys.argv[1] == 'start-bridges':
210 create_bridges_script()
211 if sys.argv[1] == 'start-domains':
212 create_domains_script()
213 if sys.argv[1] == 'stop-domains':
214 destroy_domains_script()
215 elif sys.argv[1] == 'screen':
216 create_screens_script()
217 elif sys.argv[1] == 'graph':
218 graph()
219 elif sys.argv[1] == 'shell':
220 shell()
222 else:
223 show_usage()
224 sys.exit(1)
226 sys.exit(0)