xentaur

annotate xentaur.py @ 38:f71b298798c2

real topologies
author igor
date Fri Sep 28 13:03:15 2007 +0300 (2007-09-28)
parents 7bf7abe92123
children 54b7c7ae50ec
rev   line source
igor@0 1 #!/usr/bin/python
igor@0 2
igor@0 3
igor@2 4 import sys,os,time
igor@35 5
igor@35 6 xentaur_path=os.environ['HOME']+"/xentaur"
igor@35 7
igor@0 8 sys.path.append('/etc/xen')
igor@35 9 sys.path.append(xentaur_path)
igor@11 10
nata@36 11 network='netw'
nata@36 12 domain='dyn3'
igor@35 13 from xendomain import *
igor@35 14
igor@29 15 bridges_turned_down=[]
igor@29 16
igor@0 17 from IPython.Shell import IPShellEmbed
igor@0 18
igor@2 19
igor@2 20 screenrc=os.environ['HOME']+"/.screenrc_xentaur"
igor@2 21
igor@10 22 def run(program, *args):
igor@10 23 pid = os.fork()
igor@10 24 if not pid:
igor@10 25 os.execvp(program, (program,) + args)
igor@10 26 return os.wait()[0]
igor@10 27
igor@10 28 def run_command(line):
igor@10 29 #cmds=line.split()
igor@10 30 #run(cmds[0],*cmds[1:])
igor@10 31 run("/bin/sh", "-c", line)
igor@10 32
igor@23 33 def run_command_return_stdout(line):
igor@23 34 p = os.popen(line)
igor@23 35 output = p.read()
igor@23 36 p.close()
igor@23 37 return output
igor@23 38
igor@0 39 def create_bridges_script():
igor@38 40 unbound_bridges=set(bridges)-set(real_bridges)
igor@4 41 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 42 create_unbound_bridges+="\n"+"\n".join(map(lambda x: "sudo /bin/ip link set "+x+" up", unbound_bridges))
igor@0 43
igor@0 44 print """#!/bin/sh
igor@0 45 # create unbound bridges
igor@0 46 %(create_unbound_bridges)s
igor@0 47 """ % {'create_unbound_bridges' : create_unbound_bridges}
igor@0 48
igor@0 49
igor@0 50 def create_domains_script():
igor@35 51 for domain in domains:
igor@38 52 if not domain in real_nodes:
igor@38 53 print "sudo /usr/sbin/xm create "+xentaur_path+"/xendomain.py "+" domain="+domain+" network="+network+" && sleep 1 && sudo /usr/sbin/xm sched-credit -d $(sudo /usr/sbin/xm list | grep "+domain+" | awk '{print $2}') -c 10 && sleep 1"
igor@0 54
igor@0 55 def destroy_domains_script():
igor@35 56 for domain in domains:
igor@38 57 if not domain in real_nodes:
igor@38 58 print "sudo /usr/sbin/xm shutdown "+domain
igor@0 59
igor@0 60 def create_screens_script():
igor@7 61 N=1
igor@0 62 screens=[]
igor@0 63 for domain in domains:
igor@11 64 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 65 N+=1
igor@0 66 screenlist="\n".join(screens)
igor@0 67
igor@11 68 #
igor@11 69 # Previous terminal acccess commands:
igor@11 70 # ip="192.168.80."+str(200+N)
igor@11 71 # screens.append("screen -t "+domain+" "+str(N)+" sh -c 'while true; do ssh root@"+ip+" ; done'")
igor@11 72 #
igor@11 73
igor@4 74 #hardstatus string "\%{gk}\%c \%{yk}\%M\%d \%{wk}\%?\%-Lw\%?\%{bw}\%n*\%f\%t\%?(\%u)\%?\%{wk}\%?\%+Lw\%?"
igor@10 75
igor@11 76 f=open(screenrc, "w");
igor@10 77 f.write("""
igor@2 78 hardstatus on
igor@2 79 hardstatus alwayslastline
igor@2 80
igor@0 81 screen -t console 0 bash
igor@10 82 %s
igor@10 83 """ % (screenlist))
igor@10 84 f.close()
igor@0 85
igor@0 86 def graph_node(node):
igor@0 87 i=0
igor@0 88 domain_type={}
igor@0 89 for domain in domains:
igor@0 90 domain_type[domain]=domain_types[i]
igor@0 91 i+=1
nata@31 92 return node+" [label=\" "+node+"\",shapefile=\"shapes/all/"+domain_type[node]+".png\",fontcolor=black,fontsize=16]"
igor@0 93
igor@29 94 def graph_bridge(bridge):
igor@29 95 if bridge in hidden_bridges:
igor@29 96 return ""
igor@38 97 if bridge in real_bridges:
igor@38 98 return "%s [shape=none,shapefile=\"shapes/all/real_switch.png\"]" % (bridge)
igor@38 99 elif bridge in bridges_turned_down:
igor@29 100 return "%s [shape=none,shapefile=\"shapes/all/switch_turned_down.png\"]" % (bridge)
igor@29 101 else:
igor@29 102 return "%s [shape=none,shapefile=\"shapes/all/switch.png\"]" % (bridge)
igor@29 103
igor@29 104
igor@0 105 def graph():
igor@0 106 nodelist=""
igor@0 107 bridgelist=""
igor@0 108 linklist=""
igor@0 109 physicallist=""
igor@0 110 networklist=""
igor@0 111
igor@0 112 nodelist=";\n ".join(map(graph_node,nodes))
igor@0 113 if nodelist: nodelist += ";"
igor@0 114
igor@29 115 bridgelist=";\n ".join(map(graph_bridge,bridges))
igor@0 116 if bridgelist: bridgelist += ";"
igor@0 117
igor@0 118 links=[]
igor@0 119 for host, bridges_raw in vbridges_table.iteritems():
igor@0 120 i=0
igor@0 121 for this_bridge in bridges_raw:
igor@27 122 if this_bridge in hidden_bridges or not this_bridge:
igor@0 123 continue
igor@28 124 if not [ host, bridges_raw.index(this_bridge), this_bridge ] in temporary_links:
igor@28 125 links.append(host+" -- "+this_bridge+" [taillabel=\"fa"+str(bridges_raw.index(this_bridge))+"/0\"]")
igor@27 126 i+=1
igor@27 127 # if [ host, this_bridge ] in broken_links:
igor@27 128 # links.append(host+" -- "+this_bridge+" [taillabel=\"fa"+str(i)+"/0\",style=dashed]")
igor@27 129 # else:
igor@27 130 # links.append(host+" -- "+this_bridge+" [taillabel=\"fa"+str(i)+"/0\"]")
igor@0 131
igor@0 132 for link in temporary_links:
igor@28 133 links.append(link[0]+" -- "+link[2]+" [taillabel=\"fa"+str(link[1])+"/0\",color=blue,len=10,w=5,weight=5]")
igor@0 134
igor@27 135 for link in broken_links:
igor@28 136 links.append(link[0]+" -- "+link[2]+" [taillabel=\"fa"+str(link[1])+"/0\",style=dashed]")
igor@27 137
igor@0 138 linklist=";\n ".join(links)
igor@0 139
igor@0 140 graph_dot = {
igor@0 141 'nodelist' : nodelist,
igor@0 142 'bridgelist' : bridgelist,
igor@0 143 'linklist' : linklist,
igor@0 144 'physicallist' : physicallist,
igor@0 145 'networklist' : networklist,
igor@0 146 }
igor@0 147
igor@10 148 f = open("xenomips.dot", "w");
igor@10 149 f.write ("""
igor@0 150 graph G {
igor@0 151 edge [len=1.25];
igor@0 152 splines=true;
igor@0 153 // nodes
igor@0 154
igor@0 155 node [shape=plaintext,color=white,shapefile="shapes/cisco.bmp/router.png"];
igor@0 156 %(nodelist)s
igor@0 157
igor@0 158 // bridges
igor@0 159
igor@6 160 node [shape=none,shapefile="shapes/all/switch.png"];
igor@0 161 %(bridgelist)s
igor@0 162
igor@0 163 // physical
igor@0 164
igor@0 165 node [shape=rectangle,color=blue];
igor@0 166 %(physicallist)s
igor@0 167
igor@0 168 // networks (not bridges, not physical)
igor@0 169 node [shape=rectangle,color=green];
igor@0 170 %(networklist)s
igor@0 171
igor@0 172 // links (between nodes and bridges)
igor@0 173 %(linklist)s
igor@0 174
igor@0 175 };
igor@10 176 """ % graph_dot)
igor@10 177 f.close()
igor@10 178 run_command("neato -Tpng -o xenomips.png xenomips.dot ")
igor@0 179
igor@27 180 def autoredraw():
igor@27 181 graph()
igor@27 182
igor@0 183 def start_all():
igor@0 184 create_bridges_script()
igor@0 185 create_screens_script()
igor@0 186 create_domains_script()
igor@0 187 graph()
igor@0 188 print """
igor@0 189 cat <<NOTE_FOR_USER
igor@0 190 # To view virtual network map, run:
igor@0 191 gqview xenomips.png
igor@0 192 # To attach to VM consoles, run:
igor@0 193 screen -c screenrc
igor@0 194 NOTE_FOR_USER
igor@0 195 """
igor@0 196
igor@0 197 def shell():
nata@31 198 autoredraw()
igor@0 199 ipshell = IPShellEmbed()
igor@0 200 ipshell()
igor@0 201
igor@0 202 def stop_all():
igor@4 203 destroy_domains_script()
igor@0 204
igor@0 205 def show_usage():
igor@0 206 print """Usage:
igor@0 207 xentaur {start|stop|start-bridges|start-domains|stop-domains|screen|graph}
igor@0 208 """
igor@0 209
igor@33 210 def save():
igor@33 211 print "network =", xen_config_name
igor@33 212 print "domains =", domains
igor@33 213 print "domain_types =", domain_types
igor@33 214 print "bridges =", bridges
igor@33 215 print "vbridges_table =", vbridges_table
igor@33 216 print "hidden_bridges =", hidden_bridges
igor@33 217 print "broken_links =", broken_links
igor@33 218 print "temporary_links =", temporary_links
igor@33 219 print "bridges_turned_down =", bridges_turned_down
igor@33 220
igor@33 221 #-----------------------------------------------------------------------
igor@33 222 # CLASSES
igor@33 223
igor@33 224 class Bridge:
igor@33 225 def __init__ (self,name):
igor@33 226 self.name=name
igor@33 227 def up(self):
igor@33 228 bridge_up(self.name)
igor@33 229 def down(self):
igor@33 230 bridge_down(self.name)
igor@33 231 def show(self):
igor@33 232 show_bridge(self.name)
igor@33 233 def dump_start(self,filter=""):
igor@33 234 dump_start(self.name,filter)
igor@33 235
igor@33 236
igor@38 237 class Domain:
igor@38 238 def __init__ (self,name):
igor@38 239 self.name=name
igor@38 240 def start(self):
igor@38 241 return ""
igor@38 242 def stop(self):
igor@38 243 return ""
igor@38 244 def start_commandline(self):
igor@38 245 return ""
igor@38 246 def graphviz(self):
igor@38 247 return ""
igor@38 248 def get_domain_id(self):
igor@38 249 return get_domain_id(self.name)
igor@38 250
igor@23 251 #-----------------------------------------------------------------------
igor@23 252 # DOMAINS
igor@23 253
igor@23 254 def get_domain_id(domain):
igor@23 255 return run_command_return_stdout("sudo xm list | awk '{if ($1 == \"'%s'\") print $2}'" % domain).rstrip("\n")
igor@23 256
igor@22 257
igor@22 258 #-----------------------------------------------------------------------
igor@22 259 # BRIDGES and IFACES
igor@22 260
igor@22 261 def bridge_down(bridge):
igor@22 262 """
igor@22 263 Turn the bridge <bridge> down
igor@22 264 """
igor@38 265 if bridge in real_bridges:
igor@38 266 print "Bridge %s is a real bridge" % (bridge)
igor@38 267 return -1
igor@29 268 if bridge in bridges_turned_down:
igor@29 269 print "Bridge %s is turned down already" % (bridge)
igor@29 270 else:
igor@29 271 bridges_turned_down.append(bridge)
igor@29 272 run_command("sudo ip link set %s down" % bridge)
igor@29 273 autoredraw()
igor@22 274
igor@22 275 def bridge_up(bridge):
igor@22 276 """
igor@22 277 Turn the bridge <bridge> up
igor@22 278 """
igor@38 279 if bridge in real_bridges:
igor@38 280 print "Bridge %s is a real bridge" % (bridge)
igor@38 281 return -1
igor@29 282 if not (bridge in bridges_turned_down):
igor@29 283 print "Bridge %s is turned up already" % (bridge)
igor@29 284 else:
igor@29 285 bridges_turned_down.remove(bridge)
igor@29 286 run_command("sudo ip link set %s up" % bridge)
igor@29 287 autoredraw()
igor@22 288
igor@22 289 def show_bridge(bridge):
igor@22 290 """
igor@22 291 Show the state of the bridge <bridge>
igor@22 292 """
igor@38 293 if bridge in real_bridges:
igor@38 294 print "Bridge %s is a real bridge" % (bridge)
igor@38 295 return -1
igor@22 296 run_command("sudo ip link show %s" % bridge)
igor@22 297
igor@23 298
igor@23 299 def int_disconnect(domain, int_number):
igor@23 300 """
igor@23 301 Disconnect the interface with the number <int_number>
igor@23 302 of the domain <domain> from the bridge to which
igor@23 303 it is connected
igor@23 304 """
igor@23 305 dom_id=get_domain_id(domain)
igor@23 306 bridge=vbridges_table[domain][int_number]
igor@23 307 if not bridge:
igor@23 308 print "Interface %s of the %s domain is not connected" % (int_number, domain)
igor@23 309 return 1
igor@23 310 run_command("sudo brctl delif %s vif%s.%s" % (bridge, dom_id, int_number))
igor@23 311 vbridges_table[domain][int_number]=''
igor@28 312 if [ domain, int_number, bridge ] in temporary_links:
igor@28 313 temporary_links.remove([ domain, int_number, bridge ])
igor@27 314 else:
igor@28 315 broken_links.append([ domain, int_number, bridge ])
igor@27 316 autoredraw()
igor@23 317
igor@23 318 def int_connect(domain, int_number, bridge):
igor@23 319 """
igor@23 320 Connect the interface with the number <int_number>
igor@24 321 of the domain <domain> to the bridge <bridge>
igor@23 322 """
igor@38 323 if bridge in real_bridges:
igor@38 324 print "Bridge %s is a real bridge" % (bridge)
igor@38 325 return -1
igor@38 326
igor@23 327 dom_id=get_domain_id(domain)
igor@23 328 if vbridges_table[domain][int_number]:
igor@23 329 print "Interface %s of the %s domain is connected already to the %s bridge" % (int_number, domain, vbridges_table[domain][int_number])
igor@23 330 return 1
igor@23 331 run_command("sudo brctl addif %s vif%s.%s" % (bridge, dom_id, int_number))
igor@23 332 vbridges_table[domain][int_number]=bridge
igor@28 333 if [ domain, int_number, bridge ] in broken_links:
igor@28 334 broken_links.remove([ domain, int_number, bridge ])
igor@27 335 else:
igor@28 336 temporary_links.append([ domain, int_number, bridge ])
igor@27 337 autoredraw()
igor@23 338
igor@24 339 def int_reconnect(domain, int_number, bridge):
igor@24 340 """
igor@24 341 Reconnect the interface with the number <int_number>
igor@24 342 of the domain <domain> from the bridge to which
igor@24 343 it is connected to the bridge <bridge>
igor@24 344 """
igor@38 345 if bridge in real_bridges:
igor@38 346 print "Bridge %s is a real bridge" % (bridge)
igor@38 347 return -1
igor@38 348
igor@24 349 int_disconnect(domain, int_number)
igor@24 350 int_connect(domain, int_number, bridge)
igor@24 351
igor@24 352 def show_int(domain, int_number):
igor@25 353 """
igor@25 354 Show information about the interface <int_nuber>
igor@25 355 of the domain <domain>
igor@25 356 """
igor@26 357 return vbridges_table[domain][int_number]
igor@24 358
igor@28 359
igor@28 360 def dump_start(bridge, filter=""):
igor@38 361 if bridge in real_bridges:
igor@38 362 print "Bridge %s is a real bridge" % (bridge)
igor@38 363 return -1
igor@32 364 try:
igor@32 365 print "Writing dump... (press Ctrl-C to stop)"
igor@32 366 run_command("sudo tcpdump -w xentaur.dump -i %s %s > /dev/null 2>&1 " % (bridge,filter))
igor@32 367 except:
igor@32 368 print "Done.\n Dump is written to xentaur.dump"
igor@28 369 return 0
igor@28 370
igor@28 371 def dump_stop():
igor@28 372 return 0
igor@33 373
igor@33 374
igor@33 375 #-----------------------------------------------------------------------
igor@33 376 # CONFIGURATION TEMPLATES
igor@33 377
igor@33 378
igor@33 379 def configure_ip_addresses(doms=domains):
igor@33 380 return 0
igor@33 381
igor@33 382 def configure_ospf(doms=domains):
igor@33 383 return 0
igor@33 384
igor@33 385 def configure_save(doms=domains):
igor@33 386 write_to(doms,"\nwr\n")
igor@33 387
igor@0 388 #-----------------------------------------------------------------------
igor@0 389
igor@0 390
igor@0 391 def add_domain(name,type):
igor@0 392 domains.append(name)
igor@0 393 domain_types.append(type)
igor@0 394
igor@0 395 def brake_link(domain,bridge):
igor@0 396 broken_links.append([domain,bridge])
igor@0 397
igor@4 398 wt_timeout=0.5
igor@8 399 def write_to(screen,string,return_to_screen=""):
igor@2 400 """
igor@2 401 write_to(screen,string):
igor@2 402
igor@8 403 Type *string* to the specified screen(s).
igor@8 404 Screen may be specified with the number *screen*,
igor@8 405 with array of numbers,
igor@8 406 with array of names.
igor@2 407
igor@2 408 """
igor@5 409 screen_numbers=[] # number of the screens to write to
igor@5 410 if type(screen) == list:
igor@5 411 screen_numbers=map(lambda x: domains.index(x)+1, screen)
igor@5 412 elif type(screen) == int:
igor@5 413 screen_numbers=[screen]
igor@5 414 else:
igor@5 415 screen_numbers=[domains.index(screen)+1]
igor@5 416
igor@5 417 for screen_number in screen_numbers:
igor@5 418 run_command("screen -X select "+str(screen_number))
igor@5 419 time.sleep(wt_timeout)
igor@5 420 for line in string.splitlines():
igor@5 421 f=open('/tmp/xentaurbuf', 'w')
igor@5 422 f.write(line+"\n")
igor@5 423 f.close()
igor@5 424 run_command("screen -X readreg p /tmp/xentaurbuf")
igor@5 425 time.sleep(wt_timeout)
igor@5 426 run_command("nohup screen -X paste p >& /dev/null")
igor@5 427 time.sleep(wt_timeout)
igor@5 428
igor@8 429 if return_to_screen != "":
igor@8 430 run_command("screen -X select %s" % (return_to_screen))
igor@8 431 time.sleep(wt_timeout)
igor@0 432
igor@0 433 #-----------------------------------------------------------------------
igor@0 434
igor@5 435 cisco_fa01_up="""
igor@5 436 ena
igor@5 437 conf t
igor@5 438 int fa0/0
igor@5 439 no shutdown
igor@5 440 exit
igor@5 441 int fa1/0
igor@5 442 no shutdown
igor@5 443 exit
igor@5 444 exit
igor@5 445 exit
igor@5 446 """
igor@5 447
igor@5 448 cisco_set_ip_on_int="""
igor@5 449 interface fa%s/0
igor@5 450 no ip address
igor@5 451 ip address %s 255.255.255.0
igor@5 452 exit
igor@5 453 """
igor@5 454
igor@33 455
igor@0 456
igor@0 457 nodes=domains
igor@0 458
igor@0 459 if len(sys.argv) > 1:
igor@0 460 if sys.argv[1] == 'start':
igor@0 461 start_all()
igor@0 462 if sys.argv[1] == 'stop':
igor@0 463 stop_all()
igor@0 464 if sys.argv[1] == 'start-bridges':
igor@3 465 create_bridges_script()
igor@0 466 if sys.argv[1] == 'start-domains':
igor@3 467 create_domains_script()
igor@0 468 if sys.argv[1] == 'stop-domains':
igor@3 469 destroy_domains_script()
igor@0 470 elif sys.argv[1] == 'screen':
igor@3 471 create_screens_script()
igor@0 472 elif sys.argv[1] == 'graph':
igor@0 473 graph()
igor@0 474 elif sys.argv[1] == 'shell':
igor@0 475 shell()
igor@0 476
igor@0 477 else:
igor@0 478 show_usage()
igor@0 479 sys.exit(1)
igor@0 480
igor@0 481 sys.exit(0)
igor@0 482
igor@0 483