xentaur

annotate xentaur.py @ 67:6c145935ece5

Fixed path scripts and configuration templates moved to a single file.
author Igor Chubin <igor@chub.in>
date Mon Jan 11 13:01:35 2010 +0200 (2010-01-11)
parents aaf034af3a35
children f652fab38c7a
rev   line source
igor@0 1 #!/usr/bin/python
igor@60 2 # vim: set fileencoding=utf-8 :
igor@0 3
igor@2 4 import sys,os,time
igor@66 5 from IPython.Shell import IPShellEmbed
igor@35 6
igor@66 7 path_xentaur="%s/hg/xentaur" % os.environ['HOME']
igor@66 8 path_shapes=path_xentaur+'/shapes'
igor@66 9 path_scripts=path_xentaur+'/files'
igor@66 10
igor@66 11 network='net1'
igor@66 12
igor@66 13 path_network=os.environ['HOME']+"/.xentaur/"+network
igor@66 14 if not os.path.exists(path_network):
igor@66 15 os.makedirs(path_network)
igor@66 16 screenrc=path_network+"/.screenrc_xentaur"
igor@35 17
igor@0 18 sys.path.append('/etc/xen')
igor@66 19 sys.path.append(path_xentaur)
igor@66 20 sys.path.append(path_network)
igor@63 21 sys.path.append('.')
igor@11 22
igor@67 23
igor@56 24 node_object={}
igor@56 25 link_object={}
igor@56 26 bridge_object={}
igor@66 27 ec2_node={}
igor@56 28
igor@67 29 # ec2 settings
igor@67 30 ec2_dns_domain='ec2.xgu.ru'
igor@67 31 ec2_zone="us-east-1a"
igor@67 32
nata@65 33 domain='dyn1'
igor@35 34 from xendomain import *
igor@35 35
igor@67 36 execfile(path_xentaur+"/configuration_templates.py")
igor@67 37
igor@29 38 bridges_turned_down=[]
igor@29 39
igor@10 40 def run(program, *args):
igor@10 41 pid = os.fork()
igor@10 42 if not pid:
igor@10 43 os.execvp(program, (program,) + args)
igor@10 44 return os.wait()[0]
igor@10 45
igor@10 46 def run_command(line):
igor@10 47 #cmds=line.split()
igor@10 48 #run(cmds[0],*cmds[1:])
igor@10 49 run("/bin/sh", "-c", line)
igor@10 50
igor@23 51 def run_command_return_stdout(line):
igor@23 52 p = os.popen(line)
igor@23 53 output = p.read()
igor@23 54 p.close()
igor@23 55 return output
igor@23 56
igor@49 57 ################################################################################
igor@49 58 #Xentaur command-line commands
igor@49 59
igor@49 60 ## Start
igor@49 61
igor@49 62 def start_bridges():
igor@38 63 unbound_bridges=set(bridges)-set(real_bridges)
igor@56 64 script=""
igor@56 65 script="\n".join(map(lambda x: "sudo brctl show | awk '{print $1}' | grep -qx "+x+" || sudo brctl addbr "+x, unbound_bridges))
igor@56 66 script+="\n"+"\n".join(map(lambda x: "sudo brctl stp "+x+" off", unbound_bridges))
igor@56 67 script+="\n"+"\n".join(map(lambda x: "sudo ip link set "+x+" up", unbound_bridges))
igor@0 68
igor@0 69 print """#!/bin/sh
igor@0 70 # create unbound bridges
igor@56 71 %s
igor@56 72 """ % (script)
igor@0 73
igor@49 74 def start_domain(domain):
igor@66 75 print "sudo xm create "+path_xentaur+"/xendomain.py "+" domain="+domain+" network="+network+" && sleep 1 && sudo xm sched-credit -d $(sudo xm list | grep "+domain+" | awk '{print $2}') -c 10 && sleep 1"
igor@0 76
igor@49 77 def start_domains(doms=domains):
igor@49 78 for domain in doms:
igor@38 79 if not domain in real_nodes:
igor@49 80 start_domain(domain)
igor@0 81
igor@49 82 def start_all():
igor@49 83 graph()
igor@49 84 screen()
igor@66 85 html()
igor@49 86 start_bridges()
igor@49 87 start_domains()
igor@49 88
igor@66 89 def ec2_assign_nodes_to_instances():
igor@66 90 domain_number=0
igor@66 91 for dom in domains:
igor@67 92 node_name=network+"-node%s"%(domain_number/3)
igor@67 93 ec2_node[dom]=node_name+"."+ec2_dns_domain
igor@66 94 domain_number+=1
igor@66 95
igor@66 96 def make_start_emulators():
igor@66 97 s=""
igor@66 98 ios="/mnt/ios/C7200-AD.BIN"
igor@66 99 ec2_assign_nodes_to_instances()
igor@66 100 for dom in domains:
igor@66 101 node_name=ec2_node[dom]
igor@66 102 i=0
igor@66 103 line='dynamips '+ios
igor@66 104 line2=''
igor@66 105 for iface in vbridges_table[dom]:
igor@66 106 bridge=vbridges_table[dom][i]
igor@66 107 if i>0:
igor@66 108 line += " -p %s:PA-FE-TX "%i
igor@66 109 line += " -s %s:0:tap:%s_%s "% (i,dom,iface)
igor@66 110 line2 += "sleep 2; brctl addif %s %s; "%(bridge, dom+"_"+iface)
igor@66 111 line2 +='ifconfig %s up; ifconfig %s promisc ;' %(bridge, bridge)
igor@66 112 line2 += "ifconfig %s_%s up; "%(dom, iface)
igor@66 113 i+=1
igor@66 114 s += "ssh %s \"mkdir -p /mnt/%s; cd /mnt/%s; screen -S %s- -d -m %s\"\n"%(node_name,dom,dom,dom,line)
igor@66 115 s += "ssh %s \"%s\"\n" % (node_name, line2)
igor@66 116 return s
igor@66 117
igor@66 118 def make_start_instances_config(net=network):
igor@66 119 f = open(path_network+"/start-instances-"+network, "w");
igor@67 120 f.write("""
igor@67 121 NETWORK=%(network)s
igor@67 122 INSTANCES_NUMBER=%(instances_number)s
igor@67 123 INSTANCE_AMI=ami-b21ff8db
igor@67 124 INSTANCE_AMI=ami-7cfd1a15
igor@67 125 VOLUME_NAME=vol-28d13141
igor@67 126 SSH_SECRET_KEY=~/.ec2/id_rsa-pstam-keypair
igor@67 127 SSH_KEYPAIR=pstam-keypair
igor@67 128 EC2_ZONE=%(ec2_zone)s
igor@67 129 DOMAIN=%(ec2_dns_domain)s
igor@67 130 SCRIPTS_PATH=%(path_scripts)s
igor@67 131 """ % {
igor@67 132 "network" : network,
igor@67 133 "instances_number" : len(set(ec2_node.values())),
igor@67 134 "path_scripts" : path_scripts,
igor@67 135
igor@67 136 "ec2_dns_domain" : ec2_dns_domain,
igor@67 137 "ec2_zone" : ec2_zone,
igor@67 138 })
igor@66 139 f.write("start_emulators()\n{\n%s\n}\n"%make_start_emulators())
igor@66 140 f.close()
igor@66 141
igor@66 142 def start_network(net=network):
igor@66 143 make_start_instances_config()
igor@67 144 run_command("cd %s; env NETWORK=%s sh %s start" % (path_network, network, path_scripts+"/ec2-instances"))
igor@66 145
igor@66 146 def stop_network(net=network):
igor@67 147 run_command("cd %s; env NETWORK=%s sh %s stop" % (path_network, network, path_scripts+"/ec2-instances"))
igor@66 148
igor@49 149 ## Stop
igor@49 150
igor@49 151 def stop_domain(domain,wait=0):
igor@49 152 if wait:
igor@49 153 print "sudo xm shutdown -w "+domain
igor@49 154 else:
igor@49 155 print "sudo xm shutdown "+domain
igor@49 156
igor@49 157 def stop_domains(doms=domains, wait=0):
igor@49 158 for domain in doms:
igor@38 159 if not domain in real_nodes:
igor@49 160 stop_domain(domain,wait)
igor@0 161
igor@49 162 def stop_bridges():
igor@49 163 ###FIXME###
igor@49 164 return 0
igor@49 165
igor@49 166 def stop_all(wait=0):
igor@49 167 stop_domains(domains, wait)
igor@49 168 stop_bridges()
igor@49 169
igor@49 170 def restart_all():
igor@49 171 stop_all(1)
igor@49 172 start_all()
igor@49 173
igor@56 174 ####################################################
igor@56 175
igor@56 176 def create_objects():
igor@56 177 create_node_objects()
igor@56 178 create_bridge_objects()
igor@56 179 create_link_objects()
igor@56 180
igor@56 181 def create_node_objects():
igor@56 182 for dom in domains:
igor@56 183 node_object[dom]=Node(dom)
igor@56 184
igor@56 185 def create_bridge_objects():
igor@56 186 for bridge in bridges:
igor@56 187 bridge_object[bridge]=Bridge(bridge)
igor@56 188
igor@56 189 def create_link_objects():
igor@56 190
igor@56 191 for node, bridges_raw in vbridges_table.iteritems():
igor@56 192 interface=0
igor@56 193 j=0
igor@56 194 for this_bridge in bridges_raw:
igor@56 195 int_label=""
igor@56 196 if this_bridge.find(':') != -1:
igor@56 197 res = this_bridge.split(':')
igor@56 198 this_bridge= res[0]
igor@56 199 bridges_raw[j] = this_bridge
igor@56 200 int_label = res[1]
igor@56 201 if not [ node, bridges_raw.index(this_bridge), this_bridge ] in temporary_links:
igor@56 202 name="%s %s %s" % (node,interface,this_bridge)
igor@56 203 link_object[name]=Link(name,node,interface,this_bridge,int_label)
igor@56 204 interface+=1
igor@56 205 vbridges_table[node]=bridges_raw
igor@56 206
igor@56 207 for node, bridges_raw in bridge_bridge_table.iteritems():
igor@56 208 interface=0
igor@56 209 j=0
igor@56 210 for this_bridge in bridges_raw:
igor@56 211 int_label=""
igor@56 212 if this_bridge.find(':') != -1:
igor@56 213 res = this_bridge.split(':')
igor@63 214 this_bridge= res[2]
igor@56 215 bridges_raw[j] = this_bridge
igor@56 216 int_label = res[1]
igor@56 217 if not [ node, bridges_raw.index(this_bridge), this_bridge ] in temporary_links:
igor@56 218 name="%s %s %s" % (node,interface,this_bridge)
igor@56 219 link_object[name]=Link(name,node,interface,this_bridge,int_label)
igor@56 220 interface+=1
igor@56 221 bridge_bridge_table[node]=bridges_raw
igor@56 222
igor@56 223 for node,interface,bridge in temporary_links:
igor@56 224 name="%s %s %s" % (node,interface,bridge)
igor@56 225 link_object[name]=Link(name,node,interface,this_bridge)
igor@56 226
igor@56 227 for node,interface,bridge in broken_links:
igor@56 228 name="%s %s %s" % (node,interface,bridge)
igor@56 229 link_object[name]=Link(name,node,interface,this_bridge)
igor@56 230
igor@56 231
igor@56 232 ####################################################
igor@56 233
igor@49 234 def screen():
igor@60 235 wait_seconds=1
igor@0 236 screens=[]
igor@0 237 for domain in domains:
igor@66 238 screens.append("screen -t %(domain)s %(domain_number)s %(console_string)s" % {
igor@66 239 'domain' : domain,
igor@66 240 'domain_number' : domains.index(domain)+1,
igor@66 241 'console_string': node_object[domain].console_string(),
igor@66 242 'wait_interval' : wait_seconds } )
igor@66 243
igor@0 244 screenlist="\n".join(screens)
igor@0 245
igor@49 246 hardstatus='hardstatus string "%{rk}Xentaur%{bk}@%H %{gk}%c %{yk}%d.%m %{wk}%?%-Lw%?%{bw}%n*%f%t%?(%u)%?%{wk}%?%+Lw%?"'
igor@11 247 f=open(screenrc, "w");
igor@10 248 f.write("""
igor@2 249 hardstatus on
igor@2 250 hardstatus alwayslastline
igor@49 251 %s
igor@2 252
nata@65 253 screen -t console 0 sh -c 'while true; do %s/xentaur.py shell ; echo Retrying in %s secods...; sleep %s ; clear; done'
igor@10 254 %s
igor@66 255 """ % (hardstatus,path_xentaur,wait_seconds,wait_seconds,screenlist))
igor@10 256 f.close()
igor@49 257 print "# GNU Screen config file is written to: %s" % screenrc
igor@0 258
igor@0 259 def graph():
igor@0 260 nodelist=""
igor@0 261 bridgelist=""
igor@0 262 linklist=""
igor@0 263 physicallist=""
igor@0 264 networklist=""
igor@0 265
igor@56 266 nodelist=";\n ".join(map(lambda node: node_object[node].graphviz_string(),nodes))
igor@0 267 if nodelist: nodelist += ";"
igor@56 268 bridgelist=";\n ".join(map(lambda bridge: bridge_object[bridge].graphviz_string(),bridges))
igor@0 269 if bridgelist: bridgelist += ";"
igor@56 270 linklist=";\n ".join(map(lambda link: link_object[link].graphviz_string(),link_object.keys()))
igor@56 271 if linklist: linklist += ";"
igor@0 272
igor@66 273 f = open(path_network+"/"+network+".dot", "w");
igor@10 274 f.write ("""
igor@0 275 graph G {
igor@0 276 edge [len=1.25];
igor@0 277 splines=true;
igor@0 278 // nodes
igor@56 279 %s
igor@0 280
igor@0 281 // bridges
igor@56 282 %s
igor@0 283
igor@0 284 // physical
igor@0 285 node [shape=rectangle,color=blue];
igor@56 286 %s
igor@0 287
igor@0 288 // networks (not bridges, not physical)
igor@0 289 node [shape=rectangle,color=green];
igor@56 290 %s
igor@0 291
igor@0 292 // links (between nodes and bridges)
igor@56 293 %s
igor@0 294
igor@0 295 };
igor@56 296 """ % (nodelist, bridgelist, physicallist, networklist, linklist))
igor@10 297 f.close()
igor@66 298 run_command("neato -Tpng -o %s.png %s.dot "%(path_network+"/"+network,path_network+"/"+network))
igor@66 299 run_command("neato -Tjpg -o %s.jpg %s.dot "%(path_network+"/"+network,path_network+"/"+network))
igor@66 300 run_command("neato -Tsvg -o %s.svg %s.dot "%(path_network+"/"+network,path_network+"/"+network))
igor@66 301 #run_command("neato -Tcmapx -o %s.cmapx -NURL=http://google.com %s.dot "%(path_network+"/"+network,path_network+"/"+network))
igor@66 302 print "# Network map is written to files: %s.{png,svg,jpg,dot}" % (path_network+"/"+network)
igor@66 303
igor@66 304 def html():
igor@66 305 f = open(path_network+"/index.html", "w");
igor@66 306 f.write ("""
igor@66 307 <html>
igor@66 308 <head>
igor@66 309 <title>Network %s map</network>
igor@66 310 <body>
igor@66 311 <img src="%s.png" /><br/>
igor@66 312 <pre>
igor@66 313 nodes=%s
igor@66 314 bridges=%s
igor@66 315 vbridges_table=%s
igor@66 316 </pre>
igor@66 317 </body>
igor@66 318 </head>
igor@66 319 </html>
igor@66 320 """ % (network, network, domains, bridges, vbridges_table))
igor@66 321 f.close()
igor@66 322 #run_command("cp %s.html /var/www/ec2/network/%s/index.html"%(network,network))
igor@0 323
igor@27 324 def autoredraw():
igor@27 325 graph()
igor@66 326 html()
igor@66 327 screen()
igor@27 328
igor@0 329 def shell():
nata@31 330 autoredraw()
igor@66 331 ipshell = IPShellEmbed(['-noconfirm_exit'])
igor@0 332 ipshell()
igor@0 333
igor@60 334 def version():
igor@61 335 print """
igor@61 336 Xentaur 0.1-PRE
igor@61 337
igor@61 338 ,--,
igor@61 339 _ ___/ /\\|
igor@61 340 ,;`( )__, ) ~
igor@61 341 // .// '--;
igor@61 342 ' / \ |
igor@61 343
igor@61 344 """
igor@60 345
igor@60 346 def info():
igor@60 347 version()
igor@60 348
igor@60 349 print "Network name: ", network
igor@60 350 print "-----------------------------------------------"
igor@60 351 print
igor@60 352 print "Nodes: ", len(domains)
igor@60 353 print " * virtual nodes: ", len(domains)-len(real_nodes)
igor@60 354 print " * real nodes:", len(real_nodes)
igor@60 355 print
igor@60 356 print "Bridges:", len(bridges)
igor@60 357 print " * virtual bridges:", len(bridges)-len(real_bridges)-len(cross_bridges)
igor@60 358 print " * real switches:", len(real_bridges)
igor@60 359 print " * direct links:", len(cross_bridges)
igor@60 360
igor@0 361 def show_usage():
igor@0 362 print """Usage:
igor@49 363 xentaur <command> [<argument>]
igor@49 364
igor@49 365 Commands:
igor@49 366 start-all -- start bridges and domains
igor@49 367 start-domains -- start domains only
igor@49 368 start-bridges -- start bridges only
igor@49 369 stop-all -- stop bridges and domains
igor@49 370 stop-domains -- stop domains only
igor@49 371 stop-bridges -- stop bridges only (domains have to be stopped already)
igor@49 372 restart-all -- restart bridges and domains
igor@49 373
igor@49 374 start <domain> -- start the <domain>
igor@49 375 stop <domain> -- stop the <domain>
igor@49 376
igor@49 377 graph -- generate network scheme (result is in <network>.{png,jpg,svg})
igor@49 378 screen -- generate GNU Screen config file (~/.screenrc_xentaur)
igor@49 379 shell -- run Xentaur shell
igor@49 380
igor@0 381 """
igor@0 382
igor@33 383 def save():
igor@33 384 print "network =", xen_config_name
igor@33 385 print "domains =", domains
igor@33 386 print "domain_types =", domain_types
igor@33 387 print "bridges =", bridges
igor@33 388 print "vbridges_table =", vbridges_table
igor@33 389 print "hidden_bridges =", hidden_bridges
igor@33 390 print "broken_links =", broken_links
igor@33 391 print "temporary_links =", temporary_links
igor@33 392 print "bridges_turned_down =", bridges_turned_down
igor@33 393
igor@33 394 #-----------------------------------------------------------------------
igor@33 395 # CLASSES
igor@33 396
igor@33 397 class Bridge:
igor@33 398 def __init__ (self,name):
igor@33 399 self.name=name
igor@33 400 def up(self):
igor@33 401 bridge_up(self.name)
igor@33 402 def down(self):
igor@33 403 bridge_down(self.name)
igor@33 404 def show(self):
igor@33 405 show_bridge(self.name)
igor@33 406 def dump_start(self,filter=""):
igor@33 407 dump_start(self.name,filter)
igor@33 408
igor@56 409 def is_hidden(self):
igor@56 410 return self.name in hidden_bridges
igor@56 411 def is_real(self):
igor@56 412 return self.name in real_bridges
igor@56 413 def is_turned_down(self):
igor@56 414 return self.name in bridges_turned_down
igor@56 415 def is_cross(self):
igor@56 416 return self.name in cross_bridges
igor@33 417
igor@56 418 def graphviz_string(self):
igor@56 419 if self.is_hidden():
igor@63 420 return "//"
igor@56 421 elif self.is_cross():
igor@56 422 return "%s [shape=circle,height=0.03,color=black,fillcolor=black,style=filled,label=\"\"]" % (self.name)
igor@56 423 elif self.is_real():
igor@63 424 return "%s [color=white,shape=none,shapefile=\"%s/all/real_switch.png\"]" % (self.name, path_shapes)
igor@56 425 elif self.is_turned_down():
igor@63 426 return "%s [color=white,shape=none,shapefile=\"%s/all/switch_turned_down.png\"]" % (self.name, path_shapes)
igor@56 427 else:
igor@63 428 return "%s [color=white,shape=none,shapefile=\"%s/all/switch.png\"]" % (self.name, path_shapes)
igor@56 429
igor@56 430
igor@56 431 class Node:
igor@38 432 def __init__ (self,name):
igor@38 433 self.name=name
igor@56 434 self.type=domain_types[domains.index(name)]
igor@38 435 def start(self):
igor@38 436 return ""
igor@38 437 def stop(self):
igor@38 438 return ""
igor@38 439 def start_commandline(self):
igor@38 440 return ""
igor@38 441 def get_domain_id(self):
igor@38 442 return get_domain_id(self.name)
igor@56 443 def graphviz_string(self):
igor@63 444 return self.name+" [color=white,shape=plaintext,label=\" "+self.name+"\",shapefile=\""+path_shapes+"/all/"+\
igor@56 445 domain_types[domains.index(self.name)]+".png\",fontcolor=black,fontsize=16,target=\"http://google.com\"]"
igor@56 446 def console_string(self):
igor@66 447 ec2=True # FIXME
igor@66 448 ec2_assign_nodes_to_instances()
igor@66 449 if ec2:
igor@66 450 return path_scripts+"/node-terminal-session INSTANCE EC2_NODE %s %s %s"%(ec2_node[self.name], network, self.name)
igor@66 451
nata@65 452 if self.type in [ 'quagga', 'dynamips', 'freebsd', 'linux' ]:
igor@56 453 return "sudo xm console "+self.name
igor@56 454 elif self.name in real_bridges or self.name in real_nodes:
igor@56 455 return "echo Press enter to connect; read line; "+connection_table[self.name]
igor@56 456
igor@56 457
igor@56 458 class Link:
igor@56 459 def __init__ (self,name,node,interface,bridge,label=""):
igor@56 460 self.name=name
igor@56 461 self.node=node
igor@56 462 self.interface=interface
igor@56 463 self.bridge=bridge
igor@56 464 self.label=label
igor@56 465
igor@56 466 def is_temporary(self):
igor@56 467 return [self.node,self.interface,self.bridge] in temporary_links
igor@56 468
igor@56 469 def is_broken(self):
igor@56 470 return ([self.node,self.interface,self.bridge] in broken_links)
igor@56 471
igor@63 472 def is_hidden(self):
igor@63 473 return self.bridge in hidden_bridges
igor@63 474
igor@56 475 def graphviz_string(self):
igor@63 476 if self.is_hidden():
igor@63 477 return "//"
igor@56 478 if self.is_temporary():
igor@56 479 return self.node+" -- "+self.bridge+" [taillabel=\"fa"+str(self.interface)+"/0\",color=blue,len=10,w=5,weight=5]"
igor@56 480 if self.is_broken():
igor@56 481 return self.node+" -- "+self.bridge+" [taillabel=\"fa"+str(self.interface)+"/0\",style=dashed]"
igor@56 482
igor@56 483 ip="\\n.%s.%s" % (bridges.index(self.bridge)+1, domains.index(self.node)+1)
igor@66 484 if domain_types[domains.index(self.node)] == 'dynamips':
igor@56 485 int_name="fa"+str(self.interface)+"/0"
igor@56 486 else:
igor@56 487 int_name="eth"+str(self.interface)
igor@56 488 if self.label != "":
igor@56 489 int_name = self.label
igor@56 490 return self.node+" -- "+self.bridge+" [taillabel=\""+int_name+ip+"\",fontsize=14,fontname=fixed]"
igor@56 491
igor@38 492
igor@23 493 #-----------------------------------------------------------------------
igor@23 494 # DOMAINS
igor@23 495
igor@23 496 def get_domain_id(domain):
igor@23 497 return run_command_return_stdout("sudo xm list | awk '{if ($1 == \"'%s'\") print $2}'" % domain).rstrip("\n")
igor@23 498
igor@22 499
igor@22 500 #-----------------------------------------------------------------------
igor@22 501 # BRIDGES and IFACES
igor@22 502
igor@22 503 def bridge_down(bridge):
igor@22 504 """
igor@22 505 Turn the bridge <bridge> down
igor@22 506 """
igor@38 507 if bridge in real_bridges:
igor@38 508 print "Bridge %s is a real bridge" % (bridge)
igor@38 509 return -1
igor@29 510 if bridge in bridges_turned_down:
igor@29 511 print "Bridge %s is turned down already" % (bridge)
igor@29 512 else:
igor@29 513 bridges_turned_down.append(bridge)
igor@29 514 run_command("sudo ip link set %s down" % bridge)
igor@29 515 autoredraw()
igor@22 516
igor@22 517 def bridge_up(bridge):
igor@22 518 """
igor@22 519 Turn the bridge <bridge> up
igor@22 520 """
igor@38 521 if bridge in real_bridges:
igor@38 522 print "Bridge %s is a real bridge" % (bridge)
igor@38 523 return -1
igor@29 524 if not (bridge in bridges_turned_down):
igor@29 525 print "Bridge %s is turned up already" % (bridge)
igor@29 526 else:
igor@29 527 bridges_turned_down.remove(bridge)
igor@29 528 run_command("sudo ip link set %s up" % bridge)
igor@29 529 autoredraw()
igor@22 530
igor@22 531 def show_bridge(bridge):
igor@22 532 """
igor@22 533 Show the state of the bridge <bridge>
igor@22 534 """
igor@38 535 if bridge in real_bridges:
igor@38 536 print "Bridge %s is a real bridge" % (bridge)
igor@38 537 return -1
igor@22 538 run_command("sudo ip link show %s" % bridge)
igor@22 539
igor@23 540
igor@23 541 def int_disconnect(domain, int_number):
igor@23 542 """
igor@23 543 Disconnect the interface with the number <int_number>
igor@23 544 of the domain <domain> from the bridge to which
igor@23 545 it is connected
igor@23 546 """
igor@23 547 dom_id=get_domain_id(domain)
igor@23 548 bridge=vbridges_table[domain][int_number]
igor@23 549 if not bridge:
igor@23 550 print "Interface %s of the %s domain is not connected" % (int_number, domain)
igor@23 551 return 1
igor@23 552 run_command("sudo brctl delif %s vif%s.%s" % (bridge, dom_id, int_number))
igor@23 553 vbridges_table[domain][int_number]=''
igor@28 554 if [ domain, int_number, bridge ] in temporary_links:
igor@28 555 temporary_links.remove([ domain, int_number, bridge ])
igor@27 556 else:
igor@28 557 broken_links.append([ domain, int_number, bridge ])
igor@27 558 autoredraw()
igor@23 559
igor@23 560 def int_connect(domain, int_number, bridge):
igor@23 561 """
igor@23 562 Connect the interface with the number <int_number>
igor@24 563 of the domain <domain> to the bridge <bridge>
igor@23 564 """
igor@38 565 if bridge in real_bridges:
igor@38 566 print "Bridge %s is a real bridge" % (bridge)
igor@38 567 return -1
igor@38 568
igor@23 569 dom_id=get_domain_id(domain)
igor@23 570 if vbridges_table[domain][int_number]:
igor@23 571 print "Interface %s of the %s domain is connected already to the %s bridge" % (int_number, domain, vbridges_table[domain][int_number])
igor@23 572 return 1
igor@23 573 run_command("sudo brctl addif %s vif%s.%s" % (bridge, dom_id, int_number))
igor@23 574 vbridges_table[domain][int_number]=bridge
igor@28 575 if [ domain, int_number, bridge ] in broken_links:
igor@28 576 broken_links.remove([ domain, int_number, bridge ])
igor@27 577 else:
igor@28 578 temporary_links.append([ domain, int_number, bridge ])
igor@27 579 autoredraw()
igor@23 580
igor@24 581 def int_reconnect(domain, int_number, bridge):
igor@24 582 """
igor@24 583 Reconnect the interface with the number <int_number>
igor@24 584 of the domain <domain> from the bridge to which
igor@24 585 it is connected to the bridge <bridge>
igor@24 586 """
igor@38 587 if bridge in real_bridges:
igor@38 588 print "Bridge %s is a real bridge" % (bridge)
igor@38 589 return -1
igor@38 590
igor@24 591 int_disconnect(domain, int_number)
igor@24 592 int_connect(domain, int_number, bridge)
igor@24 593
igor@24 594 def show_int(domain, int_number):
igor@25 595 """
igor@25 596 Show information about the interface <int_nuber>
igor@25 597 of the domain <domain>
igor@25 598 """
igor@26 599 return vbridges_table[domain][int_number]
igor@24 600
igor@28 601 def dump_start(bridge, filter=""):
igor@38 602 if bridge in real_bridges:
igor@38 603 print "Bridge %s is a real bridge" % (bridge)
igor@38 604 return -1
igor@32 605 try:
igor@32 606 print "Writing dump... (press Ctrl-C to stop)"
igor@32 607 run_command("sudo tcpdump -w xentaur.dump -i %s %s > /dev/null 2>&1 " % (bridge,filter))
igor@32 608 except:
igor@32 609 print "Done.\n Dump is written to xentaur.dump"
igor@28 610 return 0
igor@28 611
igor@28 612 def dump_stop():
igor@28 613 return 0
igor@33 614
igor@33 615
igor@33 616 #-----------------------------------------------------------------------
igor@0 617
igor@0 618 def add_domain(name,type):
igor@0 619 domains.append(name)
igor@0 620 domain_types.append(type)
igor@0 621
igor@67 622 def break_link(domain,bridge):
igor@0 623 broken_links.append([domain,bridge])
igor@0 624
igor@67 625 #-----------------------------------------------------------------------
igor@67 626
igor@67 627 wt_timeout=1
igor@8 628 def write_to(screen,string,return_to_screen=""):
igor@2 629 """
igor@2 630 write_to(screen,string):
igor@2 631
igor@8 632 Type *string* to the specified screen(s).
igor@8 633 Screen may be specified with the number *screen*,
igor@8 634 with array of numbers,
igor@8 635 with array of names.
igor@2 636
igor@2 637 """
igor@5 638 screen_numbers=[] # number of the screens to write to
igor@5 639 if type(screen) == list:
igor@5 640 screen_numbers=map(lambda x: domains.index(x)+1, screen)
igor@5 641 elif type(screen) == int:
igor@5 642 screen_numbers=[screen]
igor@5 643 else:
igor@5 644 screen_numbers=[domains.index(screen)+1]
igor@67 645
igor@5 646 for screen_number in screen_numbers:
igor@5 647 run_command("screen -X select "+str(screen_number))
igor@5 648 time.sleep(wt_timeout)
igor@5 649 for line in string.splitlines():
igor@5 650 f=open('/tmp/xentaurbuf', 'w')
igor@5 651 f.write(line+"\n")
igor@5 652 f.close()
igor@5 653 run_command("screen -X readreg p /tmp/xentaurbuf")
igor@5 654 time.sleep(wt_timeout)
igor@5 655 run_command("nohup screen -X paste p >& /dev/null")
igor@5 656 time.sleep(wt_timeout)
igor@67 657
igor@8 658 if return_to_screen != "":
igor@8 659 run_command("screen -X select %s" % (return_to_screen))
igor@8 660 time.sleep(wt_timeout)
igor@0 661
igor@49 662 def filter_by_type(doms,type):
igor@49 663 """
igor@49 664 filter_by_type(doms,type)
igor@49 665
igor@49 666 Return only domains of *doms* that have specified *type*
igor@49 667 """
igor@49 668 return filter(lambda x: domain_types[domains.index(x)]==type,domains)
igor@49 669
igor@0 670 #-----------------------------------------------------------------------
igor@0 671
igor@0 672 nodes=domains
igor@56 673 create_objects()
igor@56 674
igor@49 675 if len(sys.argv) == 2:
igor@49 676 if sys.argv[1] == 'start-all':
igor@0 677 start_all()
igor@49 678 elif sys.argv[1] == 'start-domains':
igor@49 679 start_domains()
igor@49 680 elif sys.argv[1] == 'start-bridges':
igor@49 681 start_bridges()
igor@49 682 elif sys.argv[1] == 'stop-all':
igor@0 683 stop_all()
igor@49 684 elif sys.argv[1] == 'stop-domains':
igor@49 685 stop_domains()
igor@49 686 elif sys.argv[1] == 'stop-bridges':
igor@49 687 stop_bridges()
igor@49 688 elif sys.argv[1] == 'restart-all':
igor@49 689 restart_all()
igor@0 690 elif sys.argv[1] == 'screen':
igor@50 691 screen()
igor@0 692 elif sys.argv[1] == 'graph':
igor@0 693 graph()
igor@0 694 elif sys.argv[1] == 'shell':
igor@0 695 shell()
igor@60 696 elif sys.argv[1] == 'info':
igor@60 697 info()
igor@49 698 else:
igor@49 699 show_usage()
igor@49 700 sys.exit(1)
igor@49 701 elif len(sys.argv) == 3:
igor@49 702 if sys.argv[1] == 'start':
igor@49 703 start_domain(sys.argv[2])
igor@49 704 elif sys.argv[1] == 'stop':
igor@49 705 stop_domain(sys.argv[2])
igor@49 706 elif sys.argv[1] == 'restart':
igor@49 707 stop_domain(sys.argv[2])
igor@49 708 start_domain(sys.argv[2])
igor@49 709 else:
igor@49 710 show_usage()
igor@49 711 sys.exit(1)
igor@0 712 else:
igor@0 713 show_usage()
igor@0 714 sys.exit(1)
igor@0 715
igor@0 716 sys.exit(0)
igor@0 717
igor@0 718