xentaur

annotate xentaur.py @ 37:3245e12b66d5

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