xentaur

annotate xentaur.py @ 52:3b8886ecd46c

exec timeout 0
author igor
date Thu Oct 25 21:28:59 2007 +0300 (2007-10-25)
parents b9536e00100a
children ec48cf129028
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
igor@41 11 network='snrs_ipsec_rsa_1'
igor@40 12 domain='qua1'
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@49 39 ################################################################################
igor@49 40 #Xentaur command-line commands
igor@49 41
igor@49 42 ## Start
igor@49 43
igor@49 44 def start_bridges():
igor@38 45 unbound_bridges=set(bridges)-set(real_bridges)
igor@41 46 create_unbound_bridges="\n".join(map(lambda x: "sudo brctl show | awk '{print $1}' | grep -qx "+x+" || sudo brctl addbr "+x, unbound_bridges))
igor@41 47 create_unbound_bridges+="\n"+"\n".join(map(lambda x: "sudo brctl stp "+x+" off", unbound_bridges))
igor@41 48 create_unbound_bridges+="\n"+"\n".join(map(lambda x: "sudo ip link set "+x+" up", unbound_bridges))
igor@0 49
igor@0 50 print """#!/bin/sh
igor@0 51 # create unbound bridges
igor@0 52 %(create_unbound_bridges)s
igor@0 53 """ % {'create_unbound_bridges' : create_unbound_bridges}
igor@0 54
igor@49 55 def start_domain(domain):
igor@49 56 print "sudo xm create "+xentaur_path+"/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 57
igor@49 58 def start_domains(doms=domains):
igor@49 59 for domain in doms:
igor@38 60 if not domain in real_nodes:
igor@49 61 start_domain(domain)
igor@0 62
igor@49 63 def start_all():
igor@49 64 graph()
igor@49 65 screen()
igor@49 66 start_bridges()
igor@49 67 start_domains()
igor@49 68
igor@49 69 ## Stop
igor@49 70
igor@49 71 def stop_domain(domain,wait=0):
igor@49 72 if wait:
igor@49 73 print "sudo xm shutdown -w "+domain
igor@49 74 else:
igor@49 75 print "sudo xm shutdown "+domain
igor@49 76
igor@49 77 def stop_domains(doms=domains, wait=0):
igor@49 78 for domain in doms:
igor@38 79 if not domain in real_nodes:
igor@49 80 stop_domain(domain,wait)
igor@0 81
igor@49 82 def stop_bridges():
igor@49 83 ###FIXME###
igor@49 84 return 0
igor@49 85
igor@49 86 def stop_all(wait=0):
igor@49 87 stop_domains(domains, wait)
igor@49 88 stop_bridges()
igor@49 89
igor@49 90 def restart_all():
igor@49 91 stop_all(1)
igor@49 92 start_all()
igor@49 93
igor@49 94 def screen():
igor@7 95 N=1
igor@0 96 screens=[]
igor@0 97 for domain in domains:
igor@11 98 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 99 N+=1
igor@0 100 screenlist="\n".join(screens)
igor@0 101
igor@11 102 #
igor@11 103 # Previous terminal acccess commands:
igor@11 104 # ip="192.168.80."+str(200+N)
igor@11 105 # screens.append("screen -t "+domain+" "+str(N)+" sh -c 'while true; do ssh root@"+ip+" ; done'")
igor@11 106 #
igor@11 107
igor@49 108 hardstatus='hardstatus string "%{rk}Xentaur%{bk}@%H %{gk}%c %{yk}%d.%m %{wk}%?%-Lw%?%{bw}%n*%f%t%?(%u)%?%{wk}%?%+Lw%?"'
igor@10 109
igor@11 110 f=open(screenrc, "w");
igor@10 111 f.write("""
igor@2 112 hardstatus on
igor@2 113 hardstatus alwayslastline
igor@49 114 %s
igor@2 115
igor@0 116 screen -t console 0 bash
igor@10 117 %s
igor@49 118 """ % (hardstatus,screenlist))
igor@10 119 f.close()
igor@49 120 print "# GNU Screen config file is written to: %s" % screenrc
igor@0 121
igor@0 122 def graph_node(node):
igor@0 123 i=0
igor@0 124 domain_type={}
igor@0 125 for domain in domains:
igor@0 126 domain_type[domain]=domain_types[i]
igor@0 127 i+=1
nata@31 128 return node+" [label=\" "+node+"\",shapefile=\"shapes/all/"+domain_type[node]+".png\",fontcolor=black,fontsize=16]"
igor@0 129
igor@29 130 def graph_bridge(bridge):
igor@29 131 if bridge in hidden_bridges:
igor@29 132 return ""
igor@38 133 if bridge in real_bridges:
igor@38 134 return "%s [shape=none,shapefile=\"shapes/all/real_switch.png\"]" % (bridge)
igor@38 135 elif bridge in bridges_turned_down:
igor@29 136 return "%s [shape=none,shapefile=\"shapes/all/switch_turned_down.png\"]" % (bridge)
igor@29 137 else:
igor@29 138 return "%s [shape=none,shapefile=\"shapes/all/switch.png\"]" % (bridge)
igor@29 139
igor@29 140
igor@0 141 def graph():
igor@0 142 nodelist=""
igor@0 143 bridgelist=""
igor@0 144 linklist=""
igor@0 145 physicallist=""
igor@0 146 networklist=""
igor@0 147
igor@0 148 nodelist=";\n ".join(map(graph_node,nodes))
igor@0 149 if nodelist: nodelist += ";"
igor@0 150
igor@29 151 bridgelist=";\n ".join(map(graph_bridge,bridges))
igor@0 152 if bridgelist: bridgelist += ";"
igor@0 153
igor@0 154 links=[]
igor@0 155 for host, bridges_raw in vbridges_table.iteritems():
igor@0 156 i=0
igor@0 157 for this_bridge in bridges_raw:
igor@27 158 if this_bridge in hidden_bridges or not this_bridge:
igor@0 159 continue
igor@28 160 if not [ host, bridges_raw.index(this_bridge), this_bridge ] in temporary_links:
igor@28 161 links.append(host+" -- "+this_bridge+" [taillabel=\"fa"+str(bridges_raw.index(this_bridge))+"/0\"]")
igor@27 162 i+=1
igor@0 163
igor@0 164 for link in temporary_links:
igor@28 165 links.append(link[0]+" -- "+link[2]+" [taillabel=\"fa"+str(link[1])+"/0\",color=blue,len=10,w=5,weight=5]")
igor@0 166
igor@27 167 for link in broken_links:
igor@28 168 links.append(link[0]+" -- "+link[2]+" [taillabel=\"fa"+str(link[1])+"/0\",style=dashed]")
igor@27 169
igor@39 170
igor@39 171 # bridge-bridge links
igor@39 172 for host, bridges_raw in bridge_bridge_table.iteritems():
igor@39 173 i=0
igor@39 174 for this_bridge in bridges_raw:
igor@39 175 if this_bridge in hidden_bridges or not this_bridge:
igor@39 176 continue
igor@39 177 if not [ host, bridges_raw.index(this_bridge), this_bridge ] in temporary_links:
igor@39 178 links.append(host+" -- "+this_bridge)
igor@39 179 i+=1
igor@39 180
igor@39 181
igor@39 182
igor@0 183 linklist=";\n ".join(links)
igor@0 184
igor@0 185 graph_dot = {
igor@0 186 'nodelist' : nodelist,
igor@0 187 'bridgelist' : bridgelist,
igor@0 188 'linklist' : linklist,
igor@0 189 'physicallist' : physicallist,
igor@0 190 'networklist' : networklist,
igor@0 191 }
igor@0 192
igor@46 193 f = open(network+".dot", "w");
igor@10 194 f.write ("""
igor@0 195 graph G {
igor@0 196 edge [len=1.25];
igor@0 197 splines=true;
igor@0 198 // nodes
igor@0 199
igor@0 200 node [shape=plaintext,color=white,shapefile="shapes/cisco.bmp/router.png"];
igor@0 201 %(nodelist)s
igor@0 202
igor@0 203 // bridges
igor@0 204
igor@6 205 node [shape=none,shapefile="shapes/all/switch.png"];
igor@0 206 %(bridgelist)s
igor@0 207
igor@0 208 // physical
igor@0 209
igor@0 210 node [shape=rectangle,color=blue];
igor@0 211 %(physicallist)s
igor@0 212
igor@0 213 // networks (not bridges, not physical)
igor@0 214 node [shape=rectangle,color=green];
igor@0 215 %(networklist)s
igor@0 216
igor@0 217 // links (between nodes and bridges)
igor@0 218 %(linklist)s
igor@0 219
igor@0 220 };
igor@10 221 """ % graph_dot)
igor@10 222 f.close()
igor@46 223 run_command("neato -Tpng -o %s.png %s.dot "%(network,network))
igor@46 224 run_command("neato -Tjpg -o %s.jpg %s.dot "%(network,network))
igor@46 225 run_command("neato -Tsvg -o %s.svg %s.dot "%(network,network))
igor@49 226 print "# Network map is written to files: %s.{png,svg,jpg,dot}" % network
igor@0 227
igor@27 228 def autoredraw():
igor@27 229 graph()
igor@27 230
igor@0 231 def shell():
nata@31 232 autoredraw()
igor@0 233 ipshell = IPShellEmbed()
igor@0 234 ipshell()
igor@0 235
igor@0 236
igor@0 237 def show_usage():
igor@0 238 print """Usage:
igor@49 239 xentaur <command> [<argument>]
igor@49 240
igor@49 241 Commands:
igor@49 242 start-all -- start bridges and domains
igor@49 243 start-domains -- start domains only
igor@49 244 start-bridges -- start bridges only
igor@49 245 stop-all -- stop bridges and domains
igor@49 246 stop-domains -- stop domains only
igor@49 247 stop-bridges -- stop bridges only (domains have to be stopped already)
igor@49 248 restart-all -- restart bridges and domains
igor@49 249
igor@49 250 start <domain> -- start the <domain>
igor@49 251 stop <domain> -- stop the <domain>
igor@49 252
igor@49 253 graph -- generate network scheme (result is in <network>.{png,jpg,svg})
igor@49 254 screen -- generate GNU Screen config file (~/.screenrc_xentaur)
igor@49 255 shell -- run Xentaur shell
igor@49 256
igor@0 257 """
igor@0 258
igor@33 259 def save():
igor@33 260 print "network =", xen_config_name
igor@33 261 print "domains =", domains
igor@33 262 print "domain_types =", domain_types
igor@33 263 print "bridges =", bridges
igor@33 264 print "vbridges_table =", vbridges_table
igor@33 265 print "hidden_bridges =", hidden_bridges
igor@33 266 print "broken_links =", broken_links
igor@33 267 print "temporary_links =", temporary_links
igor@33 268 print "bridges_turned_down =", bridges_turned_down
igor@33 269
igor@33 270 #-----------------------------------------------------------------------
igor@33 271 # CLASSES
igor@33 272
igor@33 273 class Bridge:
igor@33 274 def __init__ (self,name):
igor@33 275 self.name=name
igor@33 276 def up(self):
igor@33 277 bridge_up(self.name)
igor@33 278 def down(self):
igor@33 279 bridge_down(self.name)
igor@33 280 def show(self):
igor@33 281 show_bridge(self.name)
igor@33 282 def dump_start(self,filter=""):
igor@33 283 dump_start(self.name,filter)
igor@33 284
igor@33 285
igor@38 286 class Domain:
igor@38 287 def __init__ (self,name):
igor@38 288 self.name=name
igor@38 289 def start(self):
igor@38 290 return ""
igor@38 291 def stop(self):
igor@38 292 return ""
igor@38 293 def start_commandline(self):
igor@38 294 return ""
igor@38 295 def graphviz(self):
igor@38 296 return ""
igor@38 297 def get_domain_id(self):
igor@38 298 return get_domain_id(self.name)
igor@38 299
igor@23 300 #-----------------------------------------------------------------------
igor@23 301 # DOMAINS
igor@23 302
igor@23 303 def get_domain_id(domain):
igor@23 304 return run_command_return_stdout("sudo xm list | awk '{if ($1 == \"'%s'\") print $2}'" % domain).rstrip("\n")
igor@23 305
igor@22 306
igor@22 307 #-----------------------------------------------------------------------
igor@22 308 # BRIDGES and IFACES
igor@22 309
igor@22 310 def bridge_down(bridge):
igor@22 311 """
igor@22 312 Turn the bridge <bridge> down
igor@22 313 """
igor@38 314 if bridge in real_bridges:
igor@38 315 print "Bridge %s is a real bridge" % (bridge)
igor@38 316 return -1
igor@29 317 if bridge in bridges_turned_down:
igor@29 318 print "Bridge %s is turned down already" % (bridge)
igor@29 319 else:
igor@29 320 bridges_turned_down.append(bridge)
igor@29 321 run_command("sudo ip link set %s down" % bridge)
igor@29 322 autoredraw()
igor@22 323
igor@22 324 def bridge_up(bridge):
igor@22 325 """
igor@22 326 Turn the bridge <bridge> up
igor@22 327 """
igor@38 328 if bridge in real_bridges:
igor@38 329 print "Bridge %s is a real bridge" % (bridge)
igor@38 330 return -1
igor@29 331 if not (bridge in bridges_turned_down):
igor@29 332 print "Bridge %s is turned up already" % (bridge)
igor@29 333 else:
igor@29 334 bridges_turned_down.remove(bridge)
igor@29 335 run_command("sudo ip link set %s up" % bridge)
igor@29 336 autoredraw()
igor@22 337
igor@22 338 def show_bridge(bridge):
igor@22 339 """
igor@22 340 Show the state of the bridge <bridge>
igor@22 341 """
igor@38 342 if bridge in real_bridges:
igor@38 343 print "Bridge %s is a real bridge" % (bridge)
igor@38 344 return -1
igor@22 345 run_command("sudo ip link show %s" % bridge)
igor@22 346
igor@23 347
igor@23 348 def int_disconnect(domain, int_number):
igor@23 349 """
igor@23 350 Disconnect the interface with the number <int_number>
igor@23 351 of the domain <domain> from the bridge to which
igor@23 352 it is connected
igor@23 353 """
igor@23 354 dom_id=get_domain_id(domain)
igor@23 355 bridge=vbridges_table[domain][int_number]
igor@23 356 if not bridge:
igor@23 357 print "Interface %s of the %s domain is not connected" % (int_number, domain)
igor@23 358 return 1
igor@23 359 run_command("sudo brctl delif %s vif%s.%s" % (bridge, dom_id, int_number))
igor@23 360 vbridges_table[domain][int_number]=''
igor@28 361 if [ domain, int_number, bridge ] in temporary_links:
igor@28 362 temporary_links.remove([ domain, int_number, bridge ])
igor@27 363 else:
igor@28 364 broken_links.append([ domain, int_number, bridge ])
igor@27 365 autoredraw()
igor@23 366
igor@23 367 def int_connect(domain, int_number, bridge):
igor@23 368 """
igor@23 369 Connect the interface with the number <int_number>
igor@24 370 of the domain <domain> to the bridge <bridge>
igor@23 371 """
igor@38 372 if bridge in real_bridges:
igor@38 373 print "Bridge %s is a real bridge" % (bridge)
igor@38 374 return -1
igor@38 375
igor@23 376 dom_id=get_domain_id(domain)
igor@23 377 if vbridges_table[domain][int_number]:
igor@23 378 print "Interface %s of the %s domain is connected already to the %s bridge" % (int_number, domain, vbridges_table[domain][int_number])
igor@23 379 return 1
igor@23 380 run_command("sudo brctl addif %s vif%s.%s" % (bridge, dom_id, int_number))
igor@23 381 vbridges_table[domain][int_number]=bridge
igor@28 382 if [ domain, int_number, bridge ] in broken_links:
igor@28 383 broken_links.remove([ domain, int_number, bridge ])
igor@27 384 else:
igor@28 385 temporary_links.append([ domain, int_number, bridge ])
igor@27 386 autoredraw()
igor@23 387
igor@24 388 def int_reconnect(domain, int_number, bridge):
igor@24 389 """
igor@24 390 Reconnect the interface with the number <int_number>
igor@24 391 of the domain <domain> from the bridge to which
igor@24 392 it is connected to the bridge <bridge>
igor@24 393 """
igor@38 394 if bridge in real_bridges:
igor@38 395 print "Bridge %s is a real bridge" % (bridge)
igor@38 396 return -1
igor@38 397
igor@24 398 int_disconnect(domain, int_number)
igor@24 399 int_connect(domain, int_number, bridge)
igor@24 400
igor@24 401 def show_int(domain, int_number):
igor@25 402 """
igor@25 403 Show information about the interface <int_nuber>
igor@25 404 of the domain <domain>
igor@25 405 """
igor@26 406 return vbridges_table[domain][int_number]
igor@24 407
igor@28 408
igor@28 409 def dump_start(bridge, filter=""):
igor@38 410 if bridge in real_bridges:
igor@38 411 print "Bridge %s is a real bridge" % (bridge)
igor@38 412 return -1
igor@32 413 try:
igor@32 414 print "Writing dump... (press Ctrl-C to stop)"
igor@32 415 run_command("sudo tcpdump -w xentaur.dump -i %s %s > /dev/null 2>&1 " % (bridge,filter))
igor@32 416 except:
igor@32 417 print "Done.\n Dump is written to xentaur.dump"
igor@28 418 return 0
igor@28 419
igor@28 420 def dump_stop():
igor@28 421 return 0
igor@33 422
igor@33 423
igor@33 424 #-----------------------------------------------------------------------
igor@33 425 # CONFIGURATION TEMPLATES
igor@33 426
igor@33 427
igor@33 428 def configure_ip_addresses(doms=domains):
igor@40 429
igor@40 430 cisco_set_ip_on_int="""
igor@40 431 \n\n\n
igor@40 432 int fa%s/0
igor@40 433 no ip address
igor@40 434 ip address %s 255.255.255.0
igor@40 435 no shutdown
igor@40 436 exit
igor@40 437 """
igor@40 438
igor@40 439 quagga_set_ip_on_int="""
igor@40 440 int eth%s
igor@40 441 no ip address
igor@40 442 ip address %s/24
igor@40 443 no shutdown
igor@40 444 exit
igor@40 445 """
igor@40 446
igor@40 447 for dom in doms:
igor@40 448 i=domains.index(dom)+1
igor@40 449 if domain_types[domains.index(dom)] == 'quagga':
igor@40 450 command = quagga_set_ip_on_int
igor@40 451 write_to(i,"\nconf t\n")
igor@40 452 j=0
igor@40 453 for br in vbridges_table[dom]:
igor@40 454 write_to(i,command % (j, "192.168.%s.%s"%(bridges.index(br)+1,i)))
igor@40 455 j+=1
igor@40 456 write_to(i,"\nend\n")
igor@40 457 else:
igor@40 458 command = cisco_set_ip_on_int
igor@40 459 write_to(i,"\nena\nconf t\n")
igor@40 460 j=0
igor@40 461 for br in vbridges_table[dom]:
igor@40 462 write_to(i,command % (j, "192.168.%s.%s"%(bridges.index(br)+1,i)))
igor@40 463 j+=1
igor@40 464 write_to(i,"\nend\n")
igor@40 465 return 0
igor@40 466
igor@40 467 def configure_no_ip_addresses(doms=domains):
igor@40 468
igor@40 469 cisco_set_ip_on_int="""
igor@40 470 \n\n\n
igor@40 471 int fa%s/0
igor@40 472 no ip address %s 255.255.255.0
igor@40 473 exit
igor@40 474 """
igor@40 475
igor@40 476 quagga_set_ip_on_int="""
igor@40 477 int eth%s
igor@40 478 no ip address %s/24
igor@40 479 exit
igor@40 480 """
igor@40 481
igor@40 482 for dom in doms:
igor@40 483 i=domains.index(dom)+1
igor@40 484 if domain_types[domains.index(dom)] == 'quagga':
igor@40 485 command = quagga_set_ip_on_int
igor@40 486 write_to(i,"\nconf t\n")
igor@40 487 j=0
igor@40 488 for br in vbridges_table[dom]:
igor@40 489 write_to(i,command % (j, "192.168.%s.%s"%(bridges.index(br)+1,i)))
igor@40 490 j+=1
igor@40 491 write_to(i,"\nend\n")
igor@40 492 else:
igor@40 493 command = cisco_set_ip_on_int
igor@40 494 write_to(i,"\nena\nconf t\n")
igor@40 495 j=0
igor@40 496 for br in vbridges_table[dom]:
igor@40 497 write_to(i,command % (j, "192.168.%s.%s"%(bridges.index(br)+1,i)))
igor@40 498 j+=1
igor@40 499 write_to(i,"\nend\n")
igor@33 500 return 0
igor@33 501
igor@33 502 def configure_ospf(doms=domains):
igor@40 503 for dom in doms:
igor@40 504 if domain_types[domains.index(dom)] == 'quagga':
igor@40 505 write_to(dom,"\n\nconf t\nrouter ospf\nnetwork 192.168.0.0/16 area 0\nend\n")
igor@40 506 else:
igor@40 507 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 508 return 0
igor@33 509
igor@49 510 def configure_hostname(doms=domains):
igor@49 511 for dom in doms:
igor@49 512 if domain_types[domains.index(dom)] == 'quagga':
igor@49 513 write_to(dom,"\n\nconf t\nhostname %s\nend\n" % dom)
igor@49 514 else:
igor@49 515 write_to(dom,"\n\nena\nconf t\nhostname %s\nend\n" % dom)
igor@49 516 return 0
igor@49 517
igor@49 518 def configure_logging_synchronous(doms=domains):
igor@49 519 for dom in domains:
igor@49 520 if domain_types[domains.index(dom)] == 'quagga':
igor@49 521 0
igor@49 522 else:
igor@49 523 write_to(dom,"\n\nena\nconf t\nline console 0\nlogging synchronous\nend\n")
igor@49 524 return 0
igor@49 525
igor@51 526 def configure_exec_timeout_0(doms=domains):
igor@51 527 for dom in domains:
igor@51 528 if domain_types[domains.index(dom)] == 'quagga':
igor@51 529 0
igor@51 530 else:
igor@52 531 write_to(dom,"\n\nena\nconf t\nline console 0\nexec-timeout 0\nend\n")
igor@51 532 return 0
igor@51 533
igor@33 534 def configure_save(doms=domains):
igor@33 535 write_to(doms,"\nwr\n")
igor@33 536
igor@49 537 def configure_root(doms=domains):
igor@49 538 write_to(doms,"root\n")
igor@49 539
igor@0 540 #-----------------------------------------------------------------------
igor@0 541
igor@0 542
igor@0 543 def add_domain(name,type):
igor@0 544 domains.append(name)
igor@0 545 domain_types.append(type)
igor@0 546
igor@0 547 def brake_link(domain,bridge):
igor@0 548 broken_links.append([domain,bridge])
igor@0 549
igor@4 550 wt_timeout=0.5
igor@8 551 def write_to(screen,string,return_to_screen=""):
igor@2 552 """
igor@2 553 write_to(screen,string):
igor@2 554
igor@8 555 Type *string* to the specified screen(s).
igor@8 556 Screen may be specified with the number *screen*,
igor@8 557 with array of numbers,
igor@8 558 with array of names.
igor@2 559
igor@2 560 """
igor@5 561 screen_numbers=[] # number of the screens to write to
igor@5 562 if type(screen) == list:
igor@5 563 screen_numbers=map(lambda x: domains.index(x)+1, screen)
igor@5 564 elif type(screen) == int:
igor@5 565 screen_numbers=[screen]
igor@5 566 else:
igor@5 567 screen_numbers=[domains.index(screen)+1]
igor@5 568
igor@5 569 for screen_number in screen_numbers:
igor@5 570 run_command("screen -X select "+str(screen_number))
igor@5 571 time.sleep(wt_timeout)
igor@5 572 for line in string.splitlines():
igor@5 573 f=open('/tmp/xentaurbuf', 'w')
igor@5 574 f.write(line+"\n")
igor@5 575 f.close()
igor@5 576 run_command("screen -X readreg p /tmp/xentaurbuf")
igor@5 577 time.sleep(wt_timeout)
igor@5 578 run_command("nohup screen -X paste p >& /dev/null")
igor@5 579 time.sleep(wt_timeout)
igor@5 580
igor@8 581 if return_to_screen != "":
igor@8 582 run_command("screen -X select %s" % (return_to_screen))
igor@8 583 time.sleep(wt_timeout)
igor@0 584
igor@49 585 def filter_by_type(doms,type):
igor@49 586 """
igor@49 587 filter_by_type(doms,type)
igor@49 588
igor@49 589 Return only domains of *doms* that have specified *type*
igor@49 590 """
igor@49 591 return filter(lambda x: domain_types[domains.index(x)]==type,domains)
igor@49 592
igor@0 593 #-----------------------------------------------------------------------
igor@0 594
igor@5 595 cisco_fa01_up="""
igor@5 596 ena
igor@5 597 conf t
igor@5 598 int fa0/0
igor@49 599 duplex half
igor@5 600 no shutdown
igor@5 601 exit
igor@5 602 int fa1/0
igor@49 603 duplex half
igor@5 604 no shutdown
igor@5 605 exit
igor@5 606 exit
igor@5 607 exit
igor@5 608 """
igor@5 609
igor@5 610 cisco_set_ip_on_int="""
igor@5 611 interface fa%s/0
igor@5 612 no ip address
igor@5 613 ip address %s 255.255.255.0
igor@5 614 exit
igor@5 615 """
igor@5 616
igor@0 617 nodes=domains
igor@0 618
igor@49 619 if len(sys.argv) == 2:
igor@49 620 if sys.argv[1] == 'start-all':
igor@0 621 start_all()
igor@49 622 elif sys.argv[1] == 'start-domains':
igor@49 623 start_domains()
igor@49 624 elif sys.argv[1] == 'start-bridges':
igor@49 625 start_bridges()
igor@49 626 elif sys.argv[1] == 'stop-all':
igor@0 627 stop_all()
igor@49 628 elif sys.argv[1] == 'stop-domains':
igor@49 629 stop_domains()
igor@49 630 elif sys.argv[1] == 'stop-bridges':
igor@49 631 stop_bridges()
igor@49 632 elif sys.argv[1] == 'restart-all':
igor@49 633 restart_all()
igor@0 634 elif sys.argv[1] == 'screen':
igor@50 635 screen()
igor@0 636 elif sys.argv[1] == 'graph':
igor@0 637 graph()
igor@0 638 elif sys.argv[1] == 'shell':
igor@0 639 shell()
igor@49 640 else:
igor@49 641 show_usage()
igor@49 642 sys.exit(1)
igor@49 643 elif len(sys.argv) == 3:
igor@49 644 if sys.argv[1] == 'start':
igor@49 645 start_domain(sys.argv[2])
igor@49 646 elif sys.argv[1] == 'stop':
igor@49 647 stop_domain(sys.argv[2])
igor@49 648 elif sys.argv[1] == 'restart':
igor@49 649 stop_domain(sys.argv[2])
igor@49 650 start_domain(sys.argv[2])
igor@49 651 else:
igor@49 652 show_usage()
igor@49 653 sys.exit(1)
igor@0 654 else:
igor@0 655 show_usage()
igor@0 656 sys.exit(1)
igor@0 657
igor@0 658 sys.exit(0)
igor@0 659
igor@0 660