xentaur
view xentaur.py @ 10:f7fa09a390ed
Работаем с файлами напрямую
author | igor |
---|---|
date | Thu Aug 30 14:45:50 2007 +0300 (2007-08-30) |
parents | 88a212771d27 |
children | 1ede117a35ec |
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 run(program, *args):
15 pid = os.fork()
16 if not pid:
17 os.execvp(program, (program,) + args)
18 return os.wait()[0]
20 def run_command(line):
21 #cmds=line.split()
22 #run(cmds[0],*cmds[1:])
23 run("/bin/sh", "-c", line)
25 def create_bridges_script():
26 unbound_bridges=bridges
27 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))
28 create_unbound_bridges+="\n"+"\n".join(map(lambda x: "sudo /bin/ip link set "+x+" up", unbound_bridges))
30 print """#!/bin/sh
31 # create unbound bridges
32 %(create_unbound_bridges)s
33 """ % {'create_unbound_bridges' : create_unbound_bridges}
36 def create_domains_script():
37 for N in range(len(domains)):
38 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"
40 def destroy_domains_script():
41 for N in range(len(domains)):
42 print "sudo /usr/sbin/xm shutdown "+domains[N]
44 def create_screens_script():
46 N=1
47 screens=[]
48 for domain in domains:
49 screens.append("screen -t "+domain+" "+str(N)+" xm console "+domain)
50 # ip="192.168.80."+str(200+N)
51 # screens.append("screen -t "+domain+" "+str(N)+" sh -c 'while true; do ssh root@"+ip+" ; done'")
52 N+=1
53 screenlist="\n".join(screens)
55 #hardstatus string "\%{gk}\%c \%{yk}\%M\%d \%{wk}\%?\%-Lw\%?\%{bw}\%n*\%f\%t\%?(\%u)\%?\%{wk}\%?\%+Lw\%?"
57 f=open("screenrc", "w");
58 f.write("""
59 hardstatus on
60 hardstatus alwayslastline
62 screen -t console 0 bash
63 %s
64 """ % (screenlist))
65 f.close()
67 def graph_node(node):
68 i=0
69 domain_type={}
70 for domain in domains:
71 domain_type[domain]=domain_types[i]
72 i+=1
73 return node+" [label=\" "+node+"\",shapefile=\"shapes/all/"+domain_type[node]+".png\",fontcolor=navy,fontsize=14]"
75 def graph():
76 nodelist=""
77 bridgelist=""
78 linklist=""
79 physicallist=""
80 networklist=""
82 nodelist=";\n ".join(map(graph_node,nodes))
83 if nodelist: nodelist += ";"
85 bridgelist=";\n ".join(set(bridges)-set(hidden_bridges))
86 if bridgelist: bridgelist += ";"
88 links=[]
89 for host, bridges_raw in vbridges_table.iteritems():
90 i=0
91 for this_bridge in bridges_raw:
92 if this_bridge in hidden_bridges:
93 continue
94 if [ host, this_bridge ] in broken_links:
95 links.append(host+" -- "+this_bridge+" [taillabel=\"fa"+str(i)+"/0\",style=dashed]")
96 else:
97 links.append(host+" -- "+this_bridge+" [taillabel=\"fa"+str(i)+"/0\"]")
98 i+=1
100 for link in temporary_links:
101 links.append(link[0]+" -- "+link[1]+" [color=blue,len=10,w=5,weight=5]")
103 linklist=";\n ".join(links)
105 graph_dot = {
106 'nodelist' : nodelist,
107 'bridgelist' : bridgelist,
108 'linklist' : linklist,
109 'physicallist' : physicallist,
110 'networklist' : networklist,
111 }
113 f = open("xenomips.dot", "w");
114 f.write ("""
115 graph G {
116 edge [len=1.25];
117 splines=true;
118 // nodes
120 node [shape=plaintext,color=white,shapefile="shapes/cisco.bmp/router.png"];
121 %(nodelist)s
123 // bridges
125 node [shape=none,shapefile="shapes/all/switch.png"];
126 %(bridgelist)s
128 // physical
130 node [shape=rectangle,color=blue];
131 %(physicallist)s
133 // networks (not bridges, not physical)
134 node [shape=rectangle,color=green];
135 %(networklist)s
137 // links (between nodes and bridges)
138 %(linklist)s
140 };
141 """ % graph_dot)
142 f.close()
143 run_command("neato -Tpng -o xenomips.png xenomips.dot ")
145 def start_all():
146 create_bridges_script()
147 create_screens_script()
148 create_domains_script()
149 graph()
150 print """
151 cat <<NOTE_FOR_USER
152 # To view virtual network map, run:
153 gqview xenomips.png
154 # To attach to VM consoles, run:
155 screen -c screenrc
156 NOTE_FOR_USER
157 """
159 def shell():
160 ipshell = IPShellEmbed()
161 ipshell()
163 def stop_all():
164 destroy_domains_script()
166 def show_usage():
167 print """Usage:
168 xentaur {start|stop|start-bridges|start-domains|stop-domains|screen|graph}
169 """
171 #-----------------------------------------------------------------------
174 def add_domain(name,type):
175 domains.append(name)
176 domain_types.append(type)
178 def brake_link(domain,bridge):
179 broken_links.append([domain,bridge])
181 wt_timeout=0.5
182 def write_to(screen,string,return_to_screen=""):
183 """
184 write_to(screen,string):
186 Type *string* to the specified screen(s).
187 Screen may be specified with the number *screen*,
188 with array of numbers,
189 with array of names.
191 """
192 screen_numbers=[] # number of the screens to write to
193 if type(screen) == list:
194 screen_numbers=map(lambda x: domains.index(x)+1, screen)
195 elif type(screen) == int:
196 screen_numbers=[screen]
197 else:
198 screen_numbers=[domains.index(screen)+1]
200 for screen_number in screen_numbers:
201 run_command("screen -X select "+str(screen_number))
202 time.sleep(wt_timeout)
203 for line in string.splitlines():
204 f=open('/tmp/xentaurbuf', 'w')
205 f.write(line+"\n")
206 f.close()
207 run_command("screen -X readreg p /tmp/xentaurbuf")
208 time.sleep(wt_timeout)
209 run_command("nohup screen -X paste p >& /dev/null")
210 time.sleep(wt_timeout)
212 if return_to_screen != "":
213 run_command("screen -X select %s" % (return_to_screen))
214 time.sleep(wt_timeout)
216 #-----------------------------------------------------------------------
218 cisco_fa01_up="""
219 ena
220 conf t
221 int fa0/0
222 no shutdown
223 exit
224 int fa1/0
225 no shutdown
226 exit
227 exit
228 exit
229 """
231 cisco_set_ip_on_int="""
232 interface fa%s/0
233 no ip address
234 ip address %s 255.255.255.0
235 exit
236 """
238 #-----------------------------------------------------------------------
240 nodes=domains
242 if len(sys.argv) > 1:
243 if sys.argv[1] == 'start':
244 start_all()
245 if sys.argv[1] == 'stop':
246 stop_all()
247 if sys.argv[1] == 'start-bridges':
248 create_bridges_script()
249 if sys.argv[1] == 'start-domains':
250 create_domains_script()
251 if sys.argv[1] == 'stop-domains':
252 destroy_domains_script()
253 elif sys.argv[1] == 'screen':
254 create_screens_script()
255 elif sys.argv[1] == 'graph':
256 graph()
257 elif sys.argv[1] == 'shell':
258 shell()
260 else:
261 show_usage()
262 sys.exit(1)
264 sys.exit(0)