xentaur

annotate xentaur.py @ 68:f652fab38c7a

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