xentaur
view xentaur.py @ 1:50d24d4b810d
Xentaurs files have moved to CVS tree.
author | igor |
---|---|
date | Fri Jul 13 16:57:33 2007 +0300 (2007-07-13) |
parents | |
children | e34dd9ee49e5 |
line source
1 #!/usr/bin/python
4 import sys
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
11 def create_bridges_script():
12 unbound_bridges=bridges
13 create_unbound_bridges="\n".join(map(lambda x: "brctl show | awk '{print $1}' | grep -q "+x+" || brctl addbr "+x, unbound_bridges))
14 create_unbound_bridges+="\n"+"\n".join(map(lambda x: "ip link set "+x+" up", unbound_bridges))
16 print """#!/bin/sh
17 # create unbound bridges
18 %(create_unbound_bridges)s
19 """ % {'create_unbound_bridges' : create_unbound_bridges}
22 def create_domains_script():
23 for N in range(len(domains)):
24 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"
26 def destroy_domains_script():
27 for N in range(len(domains)):
28 print "xm shutdown "+domains[N]
30 def create_screens_script():
32 N=0
33 screens=[]
34 for domain in domains:
35 ip="192.168.80."+str(200+N)
36 screens.append("screen -t "+domain+" "+str(N)+" sh -c 'while true; do ssh root@"+ip+" ; done'")
37 N+=1
38 screenlist="\n".join(screens)
40 print """
41 cat <<SCREENRC > screenrc
42 screen -t console 0 bash
43 %(screenlist)s
44 SCREENRC
45 """ % { 'screenlist' : screenlist }
47 def graph_node(node):
48 i=0
49 domain_type={}
50 for domain in domains:
51 domain_type[domain]=domain_types[i]
52 i+=1
53 return node+" [label=\" "+node+"\",shapefile=\"shapes/all/"+domain_type[node]+".png\",fontcolor=navy,fontsize=14]"
55 def graph():
57 nodelist=""
58 bridgelist=""
59 linklist=""
60 physicallist=""
61 networklist=""
63 nodelist=";\n ".join(map(graph_node,nodes))
64 if nodelist: nodelist += ";"
66 bridgelist=";\n ".join(bridges-set(hidden_bridges))
67 if bridgelist: bridgelist += ";"
69 links=[]
70 for host, bridges_raw in vbridges_table.iteritems():
71 i=0
72 for this_bridge in bridges_raw:
73 if this_bridge in hidden_bridges:
74 continue
75 if [ host, this_bridge ] in broken_links:
76 links.append(host+" -- "+this_bridge+" [taillabel=\"fa"+str(i)+"/0\",style=dashed]")
77 else:
78 links.append(host+" -- "+this_bridge+" [taillabel=\"fa"+str(i)+"/0\"]")
79 i+=1
81 for link in temporary_links:
82 links.append(link[0]+" -- "+link[1]+" [color=blue,len=10,w=5,weight=5]")
84 linklist=";\n ".join(links)
86 graph_dot = {
87 'nodelist' : nodelist,
88 'bridgelist' : bridgelist,
89 'linklist' : linklist,
90 'physicallist' : physicallist,
91 'networklist' : networklist,
92 }
94 print """
95 cat <<'GRAPH' > xenomips.dot
96 graph G {
97 edge [len=1.25];
98 splines=true;
99 // nodes
101 node [shape=plaintext,color=white,shapefile="shapes/cisco.bmp/router.png"];
102 %(nodelist)s
104 // bridges
106 node [shape=none,shapefile="shapes/cisco.bmp/switch.png"];
107 %(bridgelist)s
109 // physical
111 node [shape=rectangle,color=blue];
112 %(physicallist)s
114 // networks (not bridges, not physical)
115 node [shape=rectangle,color=green];
116 %(networklist)s
118 // links (between nodes and bridges)
119 %(linklist)s
121 };
122 GRAPH
123 neato -Tpng -o xenomips.png xenomips.dot
124 """ % graph_dot
126 def start_all():
127 create_bridges_script()
128 create_screens_script()
129 create_domains_script()
130 graph()
131 print """
132 cat <<NOTE_FOR_USER
133 # To view virtual network map, run:
134 gqview xenomips.png
135 # To attach to VM consoles, run:
136 screen -c screenrc
137 NOTE_FOR_USER
138 """
140 def shell():
141 ipshell = IPShellEmbed()
142 ipshell()
144 def stop_all():
145 destroy_domains_script(domains)
147 def show_usage():
148 print """Usage:
149 xentaur {start|stop|start-bridges|start-domains|stop-domains|screen|graph}
150 """
152 #-----------------------------------------------------------------------
154 def run(program, *args):
155 pid = os.fork()
156 if not pid:
157 os.execvp(program, (program,) + args)
158 return os.wait()[0]
160 def run_command(line)
161 cmds=line.split()
162 run(cmds[0],cmds[1:])
164 def add_domain(name,type):
165 domains.append(name)
166 domain_types.append(type)
168 def brake_link(domain,bridge):
169 broken_links.append([domain,bridge])
171 def type_to(screen,string)
172 run_command("screen -X readreg p /tmp/xentaurbuf")
173 run_command("screen -X select 1 ")
174 run_command("screen -X paste p ")
175 run_command("sleep 1")
176 run_command("screen -X select 0")
178 #-----------------------------------------------------------------------
180 bridges=[]
181 for domain in vbridges_table.keys():
182 bridges += set(vbridges_table[domain])
183 bridges=set(bridges)
185 nodes=domains
187 if len(sys.argv) > 1:
188 if sys.argv[1] == 'start':
189 start_all()
190 if sys.argv[1] == 'stop':
191 stop_all()
192 if sys.argv[1] == 'start-bridges':
193 create_bridges_script(bridges)
194 if sys.argv[1] == 'start-domains':
195 create_domains_script(domains)
196 if sys.argv[1] == 'stop-domains':
197 destroy_domains_script(domains)
198 elif sys.argv[1] == 'screen':
199 create_screens_script(domains)
200 elif sys.argv[1] == 'graph':
201 graph()
202 elif sys.argv[1] == 'shell':
203 shell()
205 else:
206 show_usage()
207 sys.exit(1)
209 sys.exit(0)