xentaur

annotate xentaur.py @ 66:aaf034af3a35

Merge of Xgurulla into Xentaur code. Not completed yet!!!

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