xentaur

annotate xentaur.py @ 24:fcd08a4f3216

reconnect and show
author igor
date Fri Sep 21 16:19:12 2007 +0300 (2007-09-21)
parents 6bb2a29e91be
children a268a7754150
rev   line source
igor@0 1 #!/usr/bin/python
igor@0 2
igor@0 3
igor@2 4 import sys,os,time
igor@0 5 import xenomips_vars
igor@0 6 xenomips_vars.N='1'
igor@0 7 sys.path.append('/etc/xen')
igor@11 8
igor@11 9 xen_config_name='fabergeN'
igor@21 10 exec 'from '+xen_config_name+' import bridges,vbridges_table, hidden_bridges, domains, broken_links, temporary_links, domain_types'
igor@11 11
igor@0 12 from IPython.Shell import IPShellEmbed
igor@0 13
igor@2 14
igor@2 15 screenrc=os.environ['HOME']+"/.screenrc_xentaur"
igor@2 16
igor@10 17 def run(program, *args):
igor@10 18 pid = os.fork()
igor@10 19 if not pid:
igor@10 20 os.execvp(program, (program,) + args)
igor@10 21 return os.wait()[0]
igor@10 22
igor@10 23 def run_command(line):
igor@10 24 #cmds=line.split()
igor@10 25 #run(cmds[0],*cmds[1:])
igor@10 26 run("/bin/sh", "-c", line)
igor@10 27
igor@23 28 def run_command_return_stdout(line):
igor@23 29 p = os.popen(line)
igor@23 30 output = p.read()
igor@23 31 p.close()
igor@23 32 return output
igor@23 33
igor@0 34 def create_bridges_script():
igor@0 35 unbound_bridges=bridges
igor@4 36 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))
igor@4 37 create_unbound_bridges+="\n"+"\n".join(map(lambda x: "sudo /bin/ip link set "+x+" up", unbound_bridges))
igor@0 38
igor@0 39 print """#!/bin/sh
igor@0 40 # create unbound bridges
igor@0 41 %(create_unbound_bridges)s
igor@0 42 """ % {'create_unbound_bridges' : create_unbound_bridges}
igor@0 43
igor@0 44
igor@0 45 def create_domains_script():
igor@0 46 for N in range(len(domains)):
igor@11 47 print "sudo /usr/sbin/xm create "+xen_config_name+" 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"
igor@0 48
igor@0 49 def destroy_domains_script():
igor@0 50 for N in range(len(domains)):
igor@4 51 print "sudo /usr/sbin/xm shutdown "+domains[N]
igor@0 52
igor@0 53 def create_screens_script():
igor@0 54
igor@7 55 N=1
igor@0 56 screens=[]
igor@0 57 for domain in domains:
igor@11 58 screens.append("screen -t "+domain+" "+str(N)+" sh -c 'while true; do sudo xm console "+domain+" ; echo Retrying in 5 seconds...; sleep 5; done'")
igor@0 59 N+=1
igor@0 60 screenlist="\n".join(screens)
igor@0 61
igor@11 62 #
igor@11 63 # Previous terminal acccess commands:
igor@11 64 # ip="192.168.80."+str(200+N)
igor@11 65 # screens.append("screen -t "+domain+" "+str(N)+" sh -c 'while true; do ssh root@"+ip+" ; done'")
igor@11 66 #
igor@11 67
igor@4 68 #hardstatus string "\%{gk}\%c \%{yk}\%M\%d \%{wk}\%?\%-Lw\%?\%{bw}\%n*\%f\%t\%?(\%u)\%?\%{wk}\%?\%+Lw\%?"
igor@10 69
igor@11 70 f=open(screenrc, "w");
igor@10 71 f.write("""
igor@2 72 hardstatus on
igor@2 73 hardstatus alwayslastline
igor@2 74
igor@0 75 screen -t console 0 bash
igor@10 76 %s
igor@10 77 """ % (screenlist))
igor@10 78 f.close()
igor@0 79
igor@0 80 def graph_node(node):
igor@0 81 i=0
igor@0 82 domain_type={}
igor@0 83 for domain in domains:
igor@0 84 domain_type[domain]=domain_types[i]
igor@0 85 i+=1
igor@0 86 return node+" [label=\" "+node+"\",shapefile=\"shapes/all/"+domain_type[node]+".png\",fontcolor=navy,fontsize=14]"
igor@0 87
igor@0 88 def graph():
igor@0 89 nodelist=""
igor@0 90 bridgelist=""
igor@0 91 linklist=""
igor@0 92 physicallist=""
igor@0 93 networklist=""
igor@0 94
igor@0 95 nodelist=";\n ".join(map(graph_node,nodes))
igor@0 96 if nodelist: nodelist += ";"
igor@0 97
igor@5 98 bridgelist=";\n ".join(set(bridges)-set(hidden_bridges))
igor@0 99 if bridgelist: bridgelist += ";"
igor@0 100
igor@0 101 links=[]
igor@0 102 for host, bridges_raw in vbridges_table.iteritems():
igor@0 103 i=0
igor@0 104 for this_bridge in bridges_raw:
igor@0 105 if this_bridge in hidden_bridges:
igor@0 106 continue
igor@0 107 if [ host, this_bridge ] in broken_links:
igor@0 108 links.append(host+" -- "+this_bridge+" [taillabel=\"fa"+str(i)+"/0\",style=dashed]")
igor@0 109 else:
igor@0 110 links.append(host+" -- "+this_bridge+" [taillabel=\"fa"+str(i)+"/0\"]")
igor@0 111 i+=1
igor@0 112
igor@0 113 for link in temporary_links:
igor@0 114 links.append(link[0]+" -- "+link[1]+" [color=blue,len=10,w=5,weight=5]")
igor@0 115
igor@0 116 linklist=";\n ".join(links)
igor@0 117
igor@0 118 graph_dot = {
igor@0 119 'nodelist' : nodelist,
igor@0 120 'bridgelist' : bridgelist,
igor@0 121 'linklist' : linklist,
igor@0 122 'physicallist' : physicallist,
igor@0 123 'networklist' : networklist,
igor@0 124 }
igor@0 125
igor@10 126 f = open("xenomips.dot", "w");
igor@10 127 f.write ("""
igor@0 128 graph G {
igor@0 129 edge [len=1.25];
igor@0 130 splines=true;
igor@0 131 // nodes
igor@0 132
igor@0 133 node [shape=plaintext,color=white,shapefile="shapes/cisco.bmp/router.png"];
igor@0 134 %(nodelist)s
igor@0 135
igor@0 136 // bridges
igor@0 137
igor@6 138 node [shape=none,shapefile="shapes/all/switch.png"];
igor@0 139 %(bridgelist)s
igor@0 140
igor@0 141 // physical
igor@0 142
igor@0 143 node [shape=rectangle,color=blue];
igor@0 144 %(physicallist)s
igor@0 145
igor@0 146 // networks (not bridges, not physical)
igor@0 147 node [shape=rectangle,color=green];
igor@0 148 %(networklist)s
igor@0 149
igor@0 150 // links (between nodes and bridges)
igor@0 151 %(linklist)s
igor@0 152
igor@0 153 };
igor@10 154 """ % graph_dot)
igor@10 155 f.close()
igor@10 156 run_command("neato -Tpng -o xenomips.png xenomips.dot ")
igor@0 157
igor@0 158 def start_all():
igor@0 159 create_bridges_script()
igor@0 160 create_screens_script()
igor@0 161 create_domains_script()
igor@0 162 graph()
igor@0 163 print """
igor@0 164 cat <<NOTE_FOR_USER
igor@0 165 # To view virtual network map, run:
igor@0 166 gqview xenomips.png
igor@0 167 # To attach to VM consoles, run:
igor@0 168 screen -c screenrc
igor@0 169 NOTE_FOR_USER
igor@0 170 """
igor@0 171
igor@0 172 def shell():
igor@0 173 ipshell = IPShellEmbed()
igor@0 174 ipshell()
igor@0 175
igor@0 176 def stop_all():
igor@4 177 destroy_domains_script()
igor@0 178
igor@0 179 def show_usage():
igor@0 180 print """Usage:
igor@0 181 xentaur {start|stop|start-bridges|start-domains|stop-domains|screen|graph}
igor@0 182 """
igor@0 183
igor@23 184 #-----------------------------------------------------------------------
igor@23 185 # DOMAINS
igor@23 186
igor@23 187 def get_domain_id(domain):
igor@23 188 return run_command_return_stdout("sudo xm list | awk '{if ($1 == \"'%s'\") print $2}'" % domain).rstrip("\n")
igor@23 189
igor@22 190
igor@22 191 #-----------------------------------------------------------------------
igor@22 192 # BRIDGES and IFACES
igor@22 193
igor@22 194 def bridge_down(bridge):
igor@22 195 """
igor@22 196 Turn the bridge <bridge> down
igor@22 197 """
igor@22 198 run_command("sudo ip link set %s down" % bridge)
igor@22 199
igor@22 200 def bridge_up(bridge):
igor@22 201 """
igor@22 202 Turn the bridge <bridge> up
igor@22 203 """
igor@22 204 run_command("sudo ip link set %s up" % bridge)
igor@22 205
igor@22 206 def show_bridge(bridge):
igor@22 207 """
igor@22 208 Show the state of the bridge <bridge>
igor@22 209 """
igor@22 210 run_command("sudo ip link show %s" % bridge)
igor@22 211
igor@23 212
igor@23 213 def int_disconnect(domain, int_number):
igor@23 214 """
igor@23 215 Disconnect the interface with the number <int_number>
igor@23 216 of the domain <domain> from the bridge to which
igor@23 217 it is connected
igor@23 218 """
igor@23 219 dom_id=get_domain_id(domain)
igor@23 220 bridge=vbridges_table[domain][int_number]
igor@23 221 if not bridge:
igor@23 222 print "Interface %s of the %s domain is not connected" % (int_number, domain)
igor@23 223 return 1
igor@23 224 run_command("sudo brctl delif %s vif%s.%s" % (bridge, dom_id, int_number))
igor@23 225 vbridges_table[domain][int_number]=''
igor@23 226
igor@23 227 def int_connect(domain, int_number, bridge):
igor@23 228 """
igor@23 229 Connect the interface with the number <int_number>
igor@24 230 of the domain <domain> to the bridge <bridge>
igor@23 231 """
igor@23 232 dom_id=get_domain_id(domain)
igor@23 233
igor@23 234 if vbridges_table[domain][int_number]:
igor@23 235 print "Interface %s of the %s domain is connected already to the %s bridge" % (int_number, domain, vbridges_table[domain][int_number])
igor@23 236 return 1
igor@23 237 run_command("sudo brctl addif %s vif%s.%s" % (bridge, dom_id, int_number))
igor@23 238 vbridges_table[domain][int_number]=bridge
igor@23 239
igor@24 240 def int_reconnect(domain, int_number, bridge):
igor@24 241 """
igor@24 242 Reconnect the interface with the number <int_number>
igor@24 243 of the domain <domain> from the bridge to which
igor@24 244 it is connected to the bridge <bridge>
igor@24 245 """
igor@24 246 int_disconnect(domain, int_number)
igor@24 247 int_connect(domain, int_number, bridge)
igor@24 248
igor@24 249 def show_int(domain, int_number):
igor@24 250 vbridges_table[domain][int_number]
igor@24 251
igor@0 252 #-----------------------------------------------------------------------
igor@0 253
igor@0 254
igor@0 255 def add_domain(name,type):
igor@0 256 domains.append(name)
igor@0 257 domain_types.append(type)
igor@0 258
igor@0 259 def brake_link(domain,bridge):
igor@0 260 broken_links.append([domain,bridge])
igor@0 261
igor@4 262 wt_timeout=0.5
igor@8 263 def write_to(screen,string,return_to_screen=""):
igor@2 264 """
igor@2 265 write_to(screen,string):
igor@2 266
igor@8 267 Type *string* to the specified screen(s).
igor@8 268 Screen may be specified with the number *screen*,
igor@8 269 with array of numbers,
igor@8 270 with array of names.
igor@2 271
igor@2 272 """
igor@5 273 screen_numbers=[] # number of the screens to write to
igor@5 274 if type(screen) == list:
igor@5 275 screen_numbers=map(lambda x: domains.index(x)+1, screen)
igor@5 276 elif type(screen) == int:
igor@5 277 screen_numbers=[screen]
igor@5 278 else:
igor@5 279 screen_numbers=[domains.index(screen)+1]
igor@5 280
igor@5 281 for screen_number in screen_numbers:
igor@5 282 run_command("screen -X select "+str(screen_number))
igor@5 283 time.sleep(wt_timeout)
igor@5 284 for line in string.splitlines():
igor@5 285 f=open('/tmp/xentaurbuf', 'w')
igor@5 286 f.write(line+"\n")
igor@5 287 f.close()
igor@5 288 run_command("screen -X readreg p /tmp/xentaurbuf")
igor@5 289 time.sleep(wt_timeout)
igor@5 290 run_command("nohup screen -X paste p >& /dev/null")
igor@5 291 time.sleep(wt_timeout)
igor@5 292
igor@8 293 if return_to_screen != "":
igor@8 294 run_command("screen -X select %s" % (return_to_screen))
igor@8 295 time.sleep(wt_timeout)
igor@0 296
igor@0 297 #-----------------------------------------------------------------------
igor@0 298
igor@5 299 cisco_fa01_up="""
igor@5 300 ena
igor@5 301 conf t
igor@5 302 int fa0/0
igor@5 303 no shutdown
igor@5 304 exit
igor@5 305 int fa1/0
igor@5 306 no shutdown
igor@5 307 exit
igor@5 308 exit
igor@5 309 exit
igor@5 310 """
igor@5 311
igor@5 312 cisco_set_ip_on_int="""
igor@5 313 interface fa%s/0
igor@5 314 no ip address
igor@5 315 ip address %s 255.255.255.0
igor@5 316 exit
igor@5 317 """
igor@5 318
igor@5 319 #-----------------------------------------------------------------------
igor@0 320
igor@0 321 nodes=domains
igor@0 322
igor@0 323 if len(sys.argv) > 1:
igor@0 324 if sys.argv[1] == 'start':
igor@0 325 start_all()
igor@0 326 if sys.argv[1] == 'stop':
igor@0 327 stop_all()
igor@0 328 if sys.argv[1] == 'start-bridges':
igor@3 329 create_bridges_script()
igor@0 330 if sys.argv[1] == 'start-domains':
igor@3 331 create_domains_script()
igor@0 332 if sys.argv[1] == 'stop-domains':
igor@3 333 destroy_domains_script()
igor@0 334 elif sys.argv[1] == 'screen':
igor@3 335 create_screens_script()
igor@0 336 elif sys.argv[1] == 'graph':
igor@0 337 graph()
igor@0 338 elif sys.argv[1] == 'shell':
igor@0 339 shell()
igor@0 340
igor@0 341 else:
igor@0 342 show_usage()
igor@0 343 sys.exit(1)
igor@0 344
igor@0 345 sys.exit(0)
igor@0 346
igor@0 347