xentaur

annotate xentaur.py @ 28:d22ff6f55dc5

autoredraw + link state fix
author igor
date Fri Sep 21 20:48:40 2007 +0300 (2007-09-21)
parents 9821c9a972cd
children 87f19c6d1514
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@27 105 if this_bridge in hidden_bridges or not this_bridge:
igor@0 106 continue
igor@28 107 if not [ host, bridges_raw.index(this_bridge), this_bridge ] in temporary_links:
igor@28 108 links.append(host+" -- "+this_bridge+" [taillabel=\"fa"+str(bridges_raw.index(this_bridge))+"/0\"]")
igor@27 109 i+=1
igor@27 110 # if [ host, this_bridge ] in broken_links:
igor@27 111 # links.append(host+" -- "+this_bridge+" [taillabel=\"fa"+str(i)+"/0\",style=dashed]")
igor@27 112 # else:
igor@27 113 # links.append(host+" -- "+this_bridge+" [taillabel=\"fa"+str(i)+"/0\"]")
igor@0 114
igor@0 115 for link in temporary_links:
igor@28 116 links.append(link[0]+" -- "+link[2]+" [taillabel=\"fa"+str(link[1])+"/0\",color=blue,len=10,w=5,weight=5]")
igor@0 117
igor@27 118 for link in broken_links:
igor@28 119 links.append(link[0]+" -- "+link[2]+" [taillabel=\"fa"+str(link[1])+"/0\",style=dashed]")
igor@27 120
igor@0 121 linklist=";\n ".join(links)
igor@0 122
igor@0 123 graph_dot = {
igor@0 124 'nodelist' : nodelist,
igor@0 125 'bridgelist' : bridgelist,
igor@0 126 'linklist' : linklist,
igor@0 127 'physicallist' : physicallist,
igor@0 128 'networklist' : networklist,
igor@0 129 }
igor@0 130
igor@10 131 f = open("xenomips.dot", "w");
igor@10 132 f.write ("""
igor@0 133 graph G {
igor@0 134 edge [len=1.25];
igor@0 135 splines=true;
igor@0 136 // nodes
igor@0 137
igor@0 138 node [shape=plaintext,color=white,shapefile="shapes/cisco.bmp/router.png"];
igor@0 139 %(nodelist)s
igor@0 140
igor@0 141 // bridges
igor@0 142
igor@6 143 node [shape=none,shapefile="shapes/all/switch.png"];
igor@0 144 %(bridgelist)s
igor@0 145
igor@0 146 // physical
igor@0 147
igor@0 148 node [shape=rectangle,color=blue];
igor@0 149 %(physicallist)s
igor@0 150
igor@0 151 // networks (not bridges, not physical)
igor@0 152 node [shape=rectangle,color=green];
igor@0 153 %(networklist)s
igor@0 154
igor@0 155 // links (between nodes and bridges)
igor@0 156 %(linklist)s
igor@0 157
igor@0 158 };
igor@10 159 """ % graph_dot)
igor@10 160 f.close()
igor@10 161 run_command("neato -Tpng -o xenomips.png xenomips.dot ")
igor@0 162
igor@27 163 def autoredraw():
igor@27 164 graph()
igor@27 165
igor@0 166 def start_all():
igor@0 167 create_bridges_script()
igor@0 168 create_screens_script()
igor@0 169 create_domains_script()
igor@0 170 graph()
igor@0 171 print """
igor@0 172 cat <<NOTE_FOR_USER
igor@0 173 # To view virtual network map, run:
igor@0 174 gqview xenomips.png
igor@0 175 # To attach to VM consoles, run:
igor@0 176 screen -c screenrc
igor@0 177 NOTE_FOR_USER
igor@0 178 """
igor@0 179
igor@0 180 def shell():
igor@0 181 ipshell = IPShellEmbed()
igor@0 182 ipshell()
igor@0 183
igor@0 184 def stop_all():
igor@4 185 destroy_domains_script()
igor@0 186
igor@0 187 def show_usage():
igor@0 188 print """Usage:
igor@0 189 xentaur {start|stop|start-bridges|start-domains|stop-domains|screen|graph}
igor@0 190 """
igor@0 191
igor@23 192 #-----------------------------------------------------------------------
igor@23 193 # DOMAINS
igor@23 194
igor@23 195 def get_domain_id(domain):
igor@23 196 return run_command_return_stdout("sudo xm list | awk '{if ($1 == \"'%s'\") print $2}'" % domain).rstrip("\n")
igor@23 197
igor@22 198
igor@22 199 #-----------------------------------------------------------------------
igor@22 200 # BRIDGES and IFACES
igor@22 201
igor@22 202 def bridge_down(bridge):
igor@22 203 """
igor@22 204 Turn the bridge <bridge> down
igor@22 205 """
igor@22 206 run_command("sudo ip link set %s down" % bridge)
igor@22 207
igor@22 208 def bridge_up(bridge):
igor@22 209 """
igor@22 210 Turn the bridge <bridge> up
igor@22 211 """
igor@22 212 run_command("sudo ip link set %s up" % bridge)
igor@22 213
igor@22 214 def show_bridge(bridge):
igor@22 215 """
igor@22 216 Show the state of the bridge <bridge>
igor@22 217 """
igor@22 218 run_command("sudo ip link show %s" % bridge)
igor@22 219
igor@23 220
igor@23 221 def int_disconnect(domain, int_number):
igor@23 222 """
igor@23 223 Disconnect the interface with the number <int_number>
igor@23 224 of the domain <domain> from the bridge to which
igor@23 225 it is connected
igor@23 226 """
igor@23 227 dom_id=get_domain_id(domain)
igor@23 228 bridge=vbridges_table[domain][int_number]
igor@23 229 if not bridge:
igor@23 230 print "Interface %s of the %s domain is not connected" % (int_number, domain)
igor@23 231 return 1
igor@23 232 run_command("sudo brctl delif %s vif%s.%s" % (bridge, dom_id, int_number))
igor@23 233 vbridges_table[domain][int_number]=''
igor@28 234 if [ domain, int_number, bridge ] in temporary_links:
igor@28 235 temporary_links.remove([ domain, int_number, bridge ])
igor@27 236 else:
igor@28 237 broken_links.append([ domain, int_number, bridge ])
igor@27 238 autoredraw()
igor@23 239
igor@23 240 def int_connect(domain, int_number, bridge):
igor@23 241 """
igor@23 242 Connect the interface with the number <int_number>
igor@24 243 of the domain <domain> to the bridge <bridge>
igor@23 244 """
igor@23 245 dom_id=get_domain_id(domain)
igor@23 246
igor@23 247 if vbridges_table[domain][int_number]:
igor@23 248 print "Interface %s of the %s domain is connected already to the %s bridge" % (int_number, domain, vbridges_table[domain][int_number])
igor@23 249 return 1
igor@23 250 run_command("sudo brctl addif %s vif%s.%s" % (bridge, dom_id, int_number))
igor@23 251 vbridges_table[domain][int_number]=bridge
igor@28 252 if [ domain, int_number, bridge ] in broken_links:
igor@28 253 broken_links.remove([ domain, int_number, bridge ])
igor@27 254 else:
igor@28 255 temporary_links.append([ domain, int_number, bridge ])
igor@27 256 autoredraw()
igor@23 257
igor@24 258 def int_reconnect(domain, int_number, bridge):
igor@24 259 """
igor@24 260 Reconnect the interface with the number <int_number>
igor@24 261 of the domain <domain> from the bridge to which
igor@24 262 it is connected to the bridge <bridge>
igor@24 263 """
igor@24 264 int_disconnect(domain, int_number)
igor@24 265 int_connect(domain, int_number, bridge)
igor@24 266
igor@24 267 def show_int(domain, int_number):
igor@25 268 """
igor@25 269 Show information about the interface <int_nuber>
igor@25 270 of the domain <domain>
igor@25 271 """
igor@26 272 return vbridges_table[domain][int_number]
igor@24 273
igor@28 274
igor@28 275 def dump_start(bridge, filter=""):
igor@28 276 return 0
igor@28 277
igor@28 278 def dump_stop():
igor@28 279 return 0
igor@28 280
igor@0 281 #-----------------------------------------------------------------------
igor@0 282
igor@0 283
igor@0 284 def add_domain(name,type):
igor@0 285 domains.append(name)
igor@0 286 domain_types.append(type)
igor@0 287
igor@0 288 def brake_link(domain,bridge):
igor@0 289 broken_links.append([domain,bridge])
igor@0 290
igor@4 291 wt_timeout=0.5
igor@8 292 def write_to(screen,string,return_to_screen=""):
igor@2 293 """
igor@2 294 write_to(screen,string):
igor@2 295
igor@8 296 Type *string* to the specified screen(s).
igor@8 297 Screen may be specified with the number *screen*,
igor@8 298 with array of numbers,
igor@8 299 with array of names.
igor@2 300
igor@2 301 """
igor@5 302 screen_numbers=[] # number of the screens to write to
igor@5 303 if type(screen) == list:
igor@5 304 screen_numbers=map(lambda x: domains.index(x)+1, screen)
igor@5 305 elif type(screen) == int:
igor@5 306 screen_numbers=[screen]
igor@5 307 else:
igor@5 308 screen_numbers=[domains.index(screen)+1]
igor@5 309
igor@5 310 for screen_number in screen_numbers:
igor@5 311 run_command("screen -X select "+str(screen_number))
igor@5 312 time.sleep(wt_timeout)
igor@5 313 for line in string.splitlines():
igor@5 314 f=open('/tmp/xentaurbuf', 'w')
igor@5 315 f.write(line+"\n")
igor@5 316 f.close()
igor@5 317 run_command("screen -X readreg p /tmp/xentaurbuf")
igor@5 318 time.sleep(wt_timeout)
igor@5 319 run_command("nohup screen -X paste p >& /dev/null")
igor@5 320 time.sleep(wt_timeout)
igor@5 321
igor@8 322 if return_to_screen != "":
igor@8 323 run_command("screen -X select %s" % (return_to_screen))
igor@8 324 time.sleep(wt_timeout)
igor@0 325
igor@0 326 #-----------------------------------------------------------------------
igor@0 327
igor@5 328 cisco_fa01_up="""
igor@5 329 ena
igor@5 330 conf t
igor@5 331 int fa0/0
igor@5 332 no shutdown
igor@5 333 exit
igor@5 334 int fa1/0
igor@5 335 no shutdown
igor@5 336 exit
igor@5 337 exit
igor@5 338 exit
igor@5 339 """
igor@5 340
igor@5 341 cisco_set_ip_on_int="""
igor@5 342 interface fa%s/0
igor@5 343 no ip address
igor@5 344 ip address %s 255.255.255.0
igor@5 345 exit
igor@5 346 """
igor@5 347
igor@5 348 #-----------------------------------------------------------------------
igor@0 349
igor@0 350 nodes=domains
igor@0 351
igor@0 352 if len(sys.argv) > 1:
igor@0 353 if sys.argv[1] == 'start':
igor@0 354 start_all()
igor@0 355 if sys.argv[1] == 'stop':
igor@0 356 stop_all()
igor@0 357 if sys.argv[1] == 'start-bridges':
igor@3 358 create_bridges_script()
igor@0 359 if sys.argv[1] == 'start-domains':
igor@3 360 create_domains_script()
igor@0 361 if sys.argv[1] == 'stop-domains':
igor@3 362 destroy_domains_script()
igor@0 363 elif sys.argv[1] == 'screen':
igor@3 364 create_screens_script()
igor@0 365 elif sys.argv[1] == 'graph':
igor@0 366 graph()
igor@0 367 elif sys.argv[1] == 'shell':
igor@0 368 shell()
igor@0 369
igor@0 370 else:
igor@0 371 show_usage()
igor@0 372 sys.exit(1)
igor@0 373
igor@0 374 sys.exit(0)
igor@0 375
igor@0 376