xentaur
annotate xentaur.py @ 56:308b524d9a70
Многочисленные изменения
Перевод на ОО-модель
Various changes
Transit to OO-model
Real devices can be configured
Interfaces names
Cross bridges
Перевод на ОО-модель
Various changes
Transit to OO-model
Real devices can be configured
Interfaces names
Cross bridges
author | igor |
---|---|
date | Sat Nov 10 19:58:39 2007 +0200 (2007-11-10) |
parents | ec48cf129028 |
children | e7b9761c03e8 |
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@56 | 11 #network='snrs_ipsec_rsa_1' |
igor@56 | 12 node_object={} |
igor@56 | 13 link_object={} |
igor@56 | 14 bridge_object={} |
igor@56 | 15 |
igor@56 | 16 network='icnd2' |
igor@56 | 17 domain='sw1' |
igor@35 | 18 from xendomain import * |
igor@35 | 19 |
igor@29 | 20 bridges_turned_down=[] |
igor@29 | 21 |
igor@0 | 22 from IPython.Shell import IPShellEmbed |
igor@0 | 23 |
igor@2 | 24 |
igor@2 | 25 screenrc=os.environ['HOME']+"/.screenrc_xentaur" |
igor@2 | 26 |
igor@10 | 27 def run(program, *args): |
igor@10 | 28 pid = os.fork() |
igor@10 | 29 if not pid: |
igor@10 | 30 os.execvp(program, (program,) + args) |
igor@10 | 31 return os.wait()[0] |
igor@10 | 32 |
igor@10 | 33 def run_command(line): |
igor@10 | 34 #cmds=line.split() |
igor@10 | 35 #run(cmds[0],*cmds[1:]) |
igor@10 | 36 run("/bin/sh", "-c", line) |
igor@10 | 37 |
igor@23 | 38 def run_command_return_stdout(line): |
igor@23 | 39 p = os.popen(line) |
igor@23 | 40 output = p.read() |
igor@23 | 41 p.close() |
igor@23 | 42 return output |
igor@23 | 43 |
igor@49 | 44 ################################################################################ |
igor@49 | 45 #Xentaur command-line commands |
igor@49 | 46 |
igor@49 | 47 ## Start |
igor@49 | 48 |
igor@49 | 49 def start_bridges(): |
igor@38 | 50 unbound_bridges=set(bridges)-set(real_bridges) |
igor@56 | 51 script="" |
igor@56 | 52 script="\n".join(map(lambda x: "sudo brctl show | awk '{print $1}' | grep -qx "+x+" || sudo brctl addbr "+x, unbound_bridges)) |
igor@56 | 53 script+="\n"+"\n".join(map(lambda x: "sudo brctl stp "+x+" off", unbound_bridges)) |
igor@56 | 54 script+="\n"+"\n".join(map(lambda x: "sudo ip link set "+x+" up", unbound_bridges)) |
igor@0 | 55 |
igor@0 | 56 print """#!/bin/sh |
igor@0 | 57 # create unbound bridges |
igor@56 | 58 %s |
igor@56 | 59 """ % (script) |
igor@0 | 60 |
igor@49 | 61 def start_domain(domain): |
igor@49 | 62 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 | 63 |
igor@49 | 64 def start_domains(doms=domains): |
igor@49 | 65 for domain in doms: |
igor@38 | 66 if not domain in real_nodes: |
igor@49 | 67 start_domain(domain) |
igor@0 | 68 |
igor@49 | 69 def start_all(): |
igor@49 | 70 graph() |
igor@49 | 71 screen() |
igor@49 | 72 start_bridges() |
igor@49 | 73 start_domains() |
igor@49 | 74 |
igor@49 | 75 ## Stop |
igor@49 | 76 |
igor@49 | 77 def stop_domain(domain,wait=0): |
igor@49 | 78 if wait: |
igor@49 | 79 print "sudo xm shutdown -w "+domain |
igor@49 | 80 else: |
igor@49 | 81 print "sudo xm shutdown "+domain |
igor@49 | 82 |
igor@49 | 83 def stop_domains(doms=domains, wait=0): |
igor@49 | 84 for domain in doms: |
igor@38 | 85 if not domain in real_nodes: |
igor@49 | 86 stop_domain(domain,wait) |
igor@0 | 87 |
igor@49 | 88 def stop_bridges(): |
igor@49 | 89 ###FIXME### |
igor@49 | 90 return 0 |
igor@49 | 91 |
igor@49 | 92 def stop_all(wait=0): |
igor@49 | 93 stop_domains(domains, wait) |
igor@49 | 94 stop_bridges() |
igor@49 | 95 |
igor@49 | 96 def restart_all(): |
igor@49 | 97 stop_all(1) |
igor@49 | 98 start_all() |
igor@49 | 99 |
igor@56 | 100 #################################################### |
igor@56 | 101 |
igor@56 | 102 def create_objects(): |
igor@56 | 103 create_node_objects() |
igor@56 | 104 create_bridge_objects() |
igor@56 | 105 create_link_objects() |
igor@56 | 106 |
igor@56 | 107 def create_node_objects(): |
igor@56 | 108 for dom in domains: |
igor@56 | 109 node_object[dom]=Node(dom) |
igor@56 | 110 |
igor@56 | 111 def create_bridge_objects(): |
igor@56 | 112 for bridge in bridges: |
igor@56 | 113 bridge_object[bridge]=Bridge(bridge) |
igor@56 | 114 |
igor@56 | 115 def create_link_objects(): |
igor@56 | 116 |
igor@56 | 117 for node, bridges_raw in vbridges_table.iteritems(): |
igor@56 | 118 interface=0 |
igor@56 | 119 j=0 |
igor@56 | 120 for this_bridge in bridges_raw: |
igor@56 | 121 int_label="" |
igor@56 | 122 if this_bridge.find(':') != -1: |
igor@56 | 123 res = this_bridge.split(':') |
igor@56 | 124 this_bridge= res[0] |
igor@56 | 125 bridges_raw[j] = this_bridge |
igor@56 | 126 int_label = res[1] |
igor@56 | 127 if not [ node, bridges_raw.index(this_bridge), this_bridge ] in temporary_links: |
igor@56 | 128 name="%s %s %s" % (node,interface,this_bridge) |
igor@56 | 129 link_object[name]=Link(name,node,interface,this_bridge,int_label) |
igor@56 | 130 interface+=1 |
igor@56 | 131 vbridges_table[node]=bridges_raw |
igor@56 | 132 |
igor@56 | 133 for node, bridges_raw in bridge_bridge_table.iteritems(): |
igor@56 | 134 interface=0 |
igor@56 | 135 j=0 |
igor@56 | 136 for this_bridge in bridges_raw: |
igor@56 | 137 int_label="" |
igor@56 | 138 if this_bridge.find(':') != -1: |
igor@56 | 139 res = this_bridge.split(':') |
igor@56 | 140 this_bridge= res[0] |
igor@56 | 141 bridges_raw[j] = this_bridge |
igor@56 | 142 int_label = res[1] |
igor@56 | 143 if not [ node, bridges_raw.index(this_bridge), this_bridge ] in temporary_links: |
igor@56 | 144 name="%s %s %s" % (node,interface,this_bridge) |
igor@56 | 145 link_object[name]=Link(name,node,interface,this_bridge,int_label) |
igor@56 | 146 interface+=1 |
igor@56 | 147 bridge_bridge_table[node]=bridges_raw |
igor@56 | 148 |
igor@56 | 149 for node,interface,bridge in temporary_links: |
igor@56 | 150 name="%s %s %s" % (node,interface,bridge) |
igor@56 | 151 link_object[name]=Link(name,node,interface,this_bridge) |
igor@56 | 152 |
igor@56 | 153 for node,interface,bridge in broken_links: |
igor@56 | 154 name="%s %s %s" % (node,interface,bridge) |
igor@56 | 155 link_object[name]=Link(name,node,interface,this_bridge) |
igor@56 | 156 |
igor@56 | 157 |
igor@56 | 158 #################################################### |
igor@56 | 159 |
igor@49 | 160 def screen(): |
igor@56 | 161 wait_seconds=5 |
igor@0 | 162 screens=[] |
igor@0 | 163 for domain in domains: |
igor@56 | 164 screens.append("screen -t %s %s sh -c 'while true; do %s ; echo Retrying in %s secods...; sleep %s ; clear; done'" % |
igor@56 | 165 (domain,domains.index(domain),node_object[domain].console_string(),wait_seconds,wait_seconds)) |
igor@0 | 166 screenlist="\n".join(screens) |
igor@0 | 167 |
igor@49 | 168 hardstatus='hardstatus string "%{rk}Xentaur%{bk}@%H %{gk}%c %{yk}%d.%m %{wk}%?%-Lw%?%{bw}%n*%f%t%?(%u)%?%{wk}%?%+Lw%?"' |
igor@10 | 169 |
igor@11 | 170 f=open(screenrc, "w"); |
igor@10 | 171 f.write(""" |
igor@2 | 172 hardstatus on |
igor@2 | 173 hardstatus alwayslastline |
igor@49 | 174 %s |
igor@2 | 175 |
igor@0 | 176 screen -t console 0 bash |
igor@10 | 177 %s |
igor@49 | 178 """ % (hardstatus,screenlist)) |
igor@10 | 179 f.close() |
igor@49 | 180 print "# GNU Screen config file is written to: %s" % screenrc |
igor@0 | 181 |
igor@0 | 182 def graph(): |
igor@0 | 183 nodelist="" |
igor@0 | 184 bridgelist="" |
igor@0 | 185 linklist="" |
igor@0 | 186 physicallist="" |
igor@0 | 187 networklist="" |
igor@0 | 188 |
igor@56 | 189 nodelist=";\n ".join(map(lambda node: node_object[node].graphviz_string(),nodes)) |
igor@0 | 190 if nodelist: nodelist += ";" |
igor@56 | 191 bridgelist=";\n ".join(map(lambda bridge: bridge_object[bridge].graphviz_string(),bridges)) |
igor@0 | 192 if bridgelist: bridgelist += ";" |
igor@56 | 193 linklist=";\n ".join(map(lambda link: link_object[link].graphviz_string(),link_object.keys())) |
igor@56 | 194 if linklist: linklist += ";" |
igor@0 | 195 |
igor@46 | 196 f = open(network+".dot", "w"); |
igor@10 | 197 f.write (""" |
igor@0 | 198 graph G { |
igor@0 | 199 edge [len=1.25]; |
igor@0 | 200 splines=true; |
igor@0 | 201 // nodes |
igor@56 | 202 // node [shape=plaintext,color=white,shapefile="shapes/cisco.bmp/router.png"]; |
igor@56 | 203 %s |
igor@0 | 204 |
igor@0 | 205 // bridges |
igor@56 | 206 // node [shape=none,shapefile="shapes/all/switch.png"]; |
igor@56 | 207 %s |
igor@0 | 208 |
igor@0 | 209 // physical |
igor@0 | 210 node [shape=rectangle,color=blue]; |
igor@56 | 211 %s |
igor@0 | 212 |
igor@0 | 213 // networks (not bridges, not physical) |
igor@0 | 214 node [shape=rectangle,color=green]; |
igor@56 | 215 %s |
igor@0 | 216 |
igor@0 | 217 // links (between nodes and bridges) |
igor@56 | 218 %s |
igor@0 | 219 |
igor@0 | 220 }; |
igor@56 | 221 """ % (nodelist, bridgelist, physicallist, networklist, linklist)) |
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@56 | 226 run_command("neato -Tcmapx -o %s.cmapx -NURL=http://google.com %s.dot "%(network,network)) |
igor@49 | 227 print "# Network map is written to files: %s.{png,svg,jpg,dot}" % network |
igor@0 | 228 |
igor@27 | 229 def autoredraw(): |
igor@27 | 230 graph() |
igor@27 | 231 |
igor@0 | 232 def shell(): |
nata@31 | 233 autoredraw() |
igor@0 | 234 ipshell = IPShellEmbed() |
igor@0 | 235 ipshell() |
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@56 | 285 def is_hidden(self): |
igor@56 | 286 return self.name in hidden_bridges |
igor@56 | 287 def is_real(self): |
igor@56 | 288 return self.name in real_bridges |
igor@56 | 289 def is_turned_down(self): |
igor@56 | 290 return self.name in bridges_turned_down |
igor@56 | 291 def is_cross(self): |
igor@56 | 292 return self.name in cross_bridges |
igor@33 | 293 |
igor@56 | 294 def graphviz_string(self): |
igor@56 | 295 if self.is_hidden(): |
igor@56 | 296 return "" |
igor@56 | 297 elif self.is_cross(): |
igor@56 | 298 return "%s [shape=circle,height=0.03,color=black,fillcolor=black,style=filled,label=\"\"]" % (self.name) |
igor@56 | 299 elif self.is_real(): |
igor@56 | 300 return "%s [color=white,shape=none,shapefile=\"shapes/all/real_switch.png\"]" % (self.name) |
igor@56 | 301 elif self.is_turned_down(): |
igor@56 | 302 return "%s [color=white,shape=none,shapefile=\"shapes/all/switch_turned_down.png\"]" % (self.name) |
igor@56 | 303 else: |
igor@56 | 304 return "%s [color=white,shape=none,shapefile=\"shapes/all/switch.png\"]" % (self.name) |
igor@56 | 305 |
igor@56 | 306 |
igor@56 | 307 class Node: |
igor@38 | 308 def __init__ (self,name): |
igor@38 | 309 self.name=name |
igor@56 | 310 self.type=domain_types[domains.index(name)] |
igor@38 | 311 def start(self): |
igor@38 | 312 return "" |
igor@38 | 313 def stop(self): |
igor@38 | 314 return "" |
igor@38 | 315 def start_commandline(self): |
igor@38 | 316 return "" |
igor@38 | 317 def get_domain_id(self): |
igor@38 | 318 return get_domain_id(self.name) |
igor@56 | 319 def graphviz_string(self): |
igor@56 | 320 return self.name+" [color=white,shape=plaintext,label=\" "+self.name+"\",shapefile=\"shapes/all/"+\ |
igor@56 | 321 domain_types[domains.index(self.name)]+".png\",fontcolor=black,fontsize=16,target=\"http://google.com\"]" |
igor@56 | 322 def console_string(self): |
igor@56 | 323 if self.type == 'quagga' or self.type == 'xenomips': |
igor@56 | 324 return "sudo xm console "+self.name |
igor@56 | 325 elif self.name in real_bridges or self.name in real_nodes: |
igor@56 | 326 return "echo Press enter to connect; read line; "+connection_table[self.name] |
igor@56 | 327 |
igor@56 | 328 |
igor@56 | 329 class Link: |
igor@56 | 330 def __init__ (self,name,node,interface,bridge,label=""): |
igor@56 | 331 self.name=name |
igor@56 | 332 self.node=node |
igor@56 | 333 self.interface=interface |
igor@56 | 334 self.bridge=bridge |
igor@56 | 335 self.label=label |
igor@56 | 336 |
igor@56 | 337 def is_temporary(self): |
igor@56 | 338 return [self.node,self.interface,self.bridge] in temporary_links |
igor@56 | 339 |
igor@56 | 340 def is_broken(self): |
igor@56 | 341 return ([self.node,self.interface,self.bridge] in broken_links) |
igor@56 | 342 |
igor@56 | 343 def graphviz_string(self): |
igor@56 | 344 if self.is_temporary(): |
igor@56 | 345 return self.node+" -- "+self.bridge+" [taillabel=\"fa"+str(self.interface)+"/0\",color=blue,len=10,w=5,weight=5]" |
igor@56 | 346 if self.is_broken(): |
igor@56 | 347 return self.node+" -- "+self.bridge+" [taillabel=\"fa"+str(self.interface)+"/0\",style=dashed]" |
igor@56 | 348 |
igor@56 | 349 ip="\\n.%s.%s" % (bridges.index(self.bridge)+1, domains.index(self.node)+1) |
igor@56 | 350 if domain_types[domains.index(self.node)] == 'xenomips': |
igor@56 | 351 int_name="fa"+str(self.interface)+"/0" |
igor@56 | 352 else: |
igor@56 | 353 int_name="eth"+str(self.interface) |
igor@56 | 354 if self.label != "": |
igor@56 | 355 int_name = self.label |
igor@56 | 356 return self.node+" -- "+self.bridge+" [taillabel=\""+int_name+ip+"\",fontsize=14,fontname=fixed]" |
igor@56 | 357 |
igor@38 | 358 |
igor@23 | 359 #----------------------------------------------------------------------- |
igor@23 | 360 # DOMAINS |
igor@23 | 361 |
igor@23 | 362 def get_domain_id(domain): |
igor@23 | 363 return run_command_return_stdout("sudo xm list | awk '{if ($1 == \"'%s'\") print $2}'" % domain).rstrip("\n") |
igor@23 | 364 |
igor@22 | 365 |
igor@22 | 366 #----------------------------------------------------------------------- |
igor@22 | 367 # BRIDGES and IFACES |
igor@22 | 368 |
igor@22 | 369 def bridge_down(bridge): |
igor@22 | 370 """ |
igor@22 | 371 Turn the bridge <bridge> down |
igor@22 | 372 """ |
igor@38 | 373 if bridge in real_bridges: |
igor@38 | 374 print "Bridge %s is a real bridge" % (bridge) |
igor@38 | 375 return -1 |
igor@29 | 376 if bridge in bridges_turned_down: |
igor@29 | 377 print "Bridge %s is turned down already" % (bridge) |
igor@29 | 378 else: |
igor@29 | 379 bridges_turned_down.append(bridge) |
igor@29 | 380 run_command("sudo ip link set %s down" % bridge) |
igor@29 | 381 autoredraw() |
igor@22 | 382 |
igor@22 | 383 def bridge_up(bridge): |
igor@22 | 384 """ |
igor@22 | 385 Turn the bridge <bridge> up |
igor@22 | 386 """ |
igor@38 | 387 if bridge in real_bridges: |
igor@38 | 388 print "Bridge %s is a real bridge" % (bridge) |
igor@38 | 389 return -1 |
igor@29 | 390 if not (bridge in bridges_turned_down): |
igor@29 | 391 print "Bridge %s is turned up already" % (bridge) |
igor@29 | 392 else: |
igor@29 | 393 bridges_turned_down.remove(bridge) |
igor@29 | 394 run_command("sudo ip link set %s up" % bridge) |
igor@29 | 395 autoredraw() |
igor@22 | 396 |
igor@22 | 397 def show_bridge(bridge): |
igor@22 | 398 """ |
igor@22 | 399 Show the state of the bridge <bridge> |
igor@22 | 400 """ |
igor@38 | 401 if bridge in real_bridges: |
igor@38 | 402 print "Bridge %s is a real bridge" % (bridge) |
igor@38 | 403 return -1 |
igor@22 | 404 run_command("sudo ip link show %s" % bridge) |
igor@22 | 405 |
igor@23 | 406 |
igor@23 | 407 def int_disconnect(domain, int_number): |
igor@23 | 408 """ |
igor@23 | 409 Disconnect the interface with the number <int_number> |
igor@23 | 410 of the domain <domain> from the bridge to which |
igor@23 | 411 it is connected |
igor@23 | 412 """ |
igor@23 | 413 dom_id=get_domain_id(domain) |
igor@23 | 414 bridge=vbridges_table[domain][int_number] |
igor@23 | 415 if not bridge: |
igor@23 | 416 print "Interface %s of the %s domain is not connected" % (int_number, domain) |
igor@23 | 417 return 1 |
igor@23 | 418 run_command("sudo brctl delif %s vif%s.%s" % (bridge, dom_id, int_number)) |
igor@23 | 419 vbridges_table[domain][int_number]='' |
igor@28 | 420 if [ domain, int_number, bridge ] in temporary_links: |
igor@28 | 421 temporary_links.remove([ domain, int_number, bridge ]) |
igor@27 | 422 else: |
igor@28 | 423 broken_links.append([ domain, int_number, bridge ]) |
igor@27 | 424 autoredraw() |
igor@23 | 425 |
igor@23 | 426 def int_connect(domain, int_number, bridge): |
igor@23 | 427 """ |
igor@23 | 428 Connect the interface with the number <int_number> |
igor@24 | 429 of the domain <domain> to the bridge <bridge> |
igor@23 | 430 """ |
igor@38 | 431 if bridge in real_bridges: |
igor@38 | 432 print "Bridge %s is a real bridge" % (bridge) |
igor@38 | 433 return -1 |
igor@38 | 434 |
igor@23 | 435 dom_id=get_domain_id(domain) |
igor@23 | 436 if vbridges_table[domain][int_number]: |
igor@23 | 437 print "Interface %s of the %s domain is connected already to the %s bridge" % (int_number, domain, vbridges_table[domain][int_number]) |
igor@23 | 438 return 1 |
igor@23 | 439 run_command("sudo brctl addif %s vif%s.%s" % (bridge, dom_id, int_number)) |
igor@23 | 440 vbridges_table[domain][int_number]=bridge |
igor@28 | 441 if [ domain, int_number, bridge ] in broken_links: |
igor@28 | 442 broken_links.remove([ domain, int_number, bridge ]) |
igor@27 | 443 else: |
igor@28 | 444 temporary_links.append([ domain, int_number, bridge ]) |
igor@27 | 445 autoredraw() |
igor@23 | 446 |
igor@24 | 447 def int_reconnect(domain, int_number, bridge): |
igor@24 | 448 """ |
igor@24 | 449 Reconnect the interface with the number <int_number> |
igor@24 | 450 of the domain <domain> from the bridge to which |
igor@24 | 451 it is connected to the bridge <bridge> |
igor@24 | 452 """ |
igor@38 | 453 if bridge in real_bridges: |
igor@38 | 454 print "Bridge %s is a real bridge" % (bridge) |
igor@38 | 455 return -1 |
igor@38 | 456 |
igor@24 | 457 int_disconnect(domain, int_number) |
igor@24 | 458 int_connect(domain, int_number, bridge) |
igor@24 | 459 |
igor@24 | 460 def show_int(domain, int_number): |
igor@25 | 461 """ |
igor@25 | 462 Show information about the interface <int_nuber> |
igor@25 | 463 of the domain <domain> |
igor@25 | 464 """ |
igor@26 | 465 return vbridges_table[domain][int_number] |
igor@24 | 466 |
igor@28 | 467 |
igor@28 | 468 def dump_start(bridge, filter=""): |
igor@38 | 469 if bridge in real_bridges: |
igor@38 | 470 print "Bridge %s is a real bridge" % (bridge) |
igor@38 | 471 return -1 |
igor@32 | 472 try: |
igor@32 | 473 print "Writing dump... (press Ctrl-C to stop)" |
igor@32 | 474 run_command("sudo tcpdump -w xentaur.dump -i %s %s > /dev/null 2>&1 " % (bridge,filter)) |
igor@32 | 475 except: |
igor@32 | 476 print "Done.\n Dump is written to xentaur.dump" |
igor@28 | 477 return 0 |
igor@28 | 478 |
igor@28 | 479 def dump_stop(): |
igor@28 | 480 return 0 |
igor@33 | 481 |
igor@33 | 482 |
igor@33 | 483 #----------------------------------------------------------------------- |
igor@33 | 484 # CONFIGURATION TEMPLATES |
igor@33 | 485 |
igor@33 | 486 |
igor@33 | 487 def configure_ip_addresses(doms=domains): |
igor@40 | 488 |
igor@40 | 489 cisco_set_ip_on_int=""" |
igor@40 | 490 \n\n\n |
igor@40 | 491 int fa%s/0 |
igor@40 | 492 no ip address |
igor@40 | 493 ip address %s 255.255.255.0 |
igor@40 | 494 no shutdown |
igor@40 | 495 exit |
igor@40 | 496 """ |
igor@40 | 497 |
igor@40 | 498 quagga_set_ip_on_int=""" |
igor@40 | 499 int eth%s |
igor@40 | 500 no ip address |
igor@40 | 501 ip address %s/24 |
igor@40 | 502 no shutdown |
igor@40 | 503 exit |
igor@40 | 504 """ |
igor@40 | 505 |
igor@40 | 506 for dom in doms: |
igor@40 | 507 i=domains.index(dom)+1 |
igor@40 | 508 if domain_types[domains.index(dom)] == 'quagga': |
igor@40 | 509 command = quagga_set_ip_on_int |
igor@40 | 510 write_to(i,"\nconf t\n") |
igor@40 | 511 j=0 |
igor@40 | 512 for br in vbridges_table[dom]: |
igor@40 | 513 write_to(i,command % (j, "192.168.%s.%s"%(bridges.index(br)+1,i))) |
igor@40 | 514 j+=1 |
igor@40 | 515 write_to(i,"\nend\n") |
igor@40 | 516 else: |
igor@40 | 517 command = cisco_set_ip_on_int |
igor@40 | 518 write_to(i,"\nena\nconf t\n") |
igor@40 | 519 j=0 |
igor@40 | 520 for br in vbridges_table[dom]: |
igor@40 | 521 write_to(i,command % (j, "192.168.%s.%s"%(bridges.index(br)+1,i))) |
igor@40 | 522 j+=1 |
igor@40 | 523 write_to(i,"\nend\n") |
igor@40 | 524 return 0 |
igor@40 | 525 |
igor@40 | 526 def configure_no_ip_addresses(doms=domains): |
igor@40 | 527 |
igor@40 | 528 cisco_set_ip_on_int=""" |
igor@40 | 529 \n\n\n |
igor@40 | 530 int fa%s/0 |
igor@40 | 531 no ip address %s 255.255.255.0 |
igor@40 | 532 exit |
igor@40 | 533 """ |
igor@40 | 534 |
igor@40 | 535 quagga_set_ip_on_int=""" |
igor@40 | 536 int eth%s |
igor@40 | 537 no ip address %s/24 |
igor@40 | 538 exit |
igor@40 | 539 """ |
igor@40 | 540 |
igor@40 | 541 for dom in doms: |
igor@40 | 542 i=domains.index(dom)+1 |
igor@40 | 543 if domain_types[domains.index(dom)] == 'quagga': |
igor@40 | 544 command = quagga_set_ip_on_int |
igor@40 | 545 write_to(i,"\nconf t\n") |
igor@40 | 546 j=0 |
igor@40 | 547 for br in vbridges_table[dom]: |
igor@40 | 548 write_to(i,command % (j, "192.168.%s.%s"%(bridges.index(br)+1,i))) |
igor@40 | 549 j+=1 |
igor@40 | 550 write_to(i,"\nend\n") |
igor@40 | 551 else: |
igor@40 | 552 command = cisco_set_ip_on_int |
igor@40 | 553 write_to(i,"\nena\nconf t\n") |
igor@40 | 554 j=0 |
igor@40 | 555 for br in vbridges_table[dom]: |
igor@40 | 556 write_to(i,command % (j, "192.168.%s.%s"%(bridges.index(br)+1,i))) |
igor@40 | 557 j+=1 |
igor@40 | 558 write_to(i,"\nend\n") |
igor@33 | 559 return 0 |
igor@33 | 560 |
igor@33 | 561 def configure_ospf(doms=domains): |
igor@40 | 562 for dom in doms: |
igor@40 | 563 if domain_types[domains.index(dom)] == 'quagga': |
igor@40 | 564 write_to(dom,"\n\nconf t\nrouter ospf\nnetwork 192.168.0.0/16 area 0\nend\n") |
igor@40 | 565 else: |
igor@40 | 566 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 | 567 return 0 |
igor@33 | 568 |
igor@49 | 569 def configure_hostname(doms=domains): |
igor@49 | 570 for dom in doms: |
igor@49 | 571 if domain_types[domains.index(dom)] == 'quagga': |
igor@49 | 572 write_to(dom,"\n\nconf t\nhostname %s\nend\n" % dom) |
igor@49 | 573 else: |
igor@49 | 574 write_to(dom,"\n\nena\nconf t\nhostname %s\nend\n" % dom) |
igor@49 | 575 return 0 |
igor@49 | 576 |
igor@49 | 577 def configure_logging_synchronous(doms=domains): |
igor@49 | 578 for dom in domains: |
igor@49 | 579 if domain_types[domains.index(dom)] == 'quagga': |
igor@49 | 580 0 |
igor@49 | 581 else: |
igor@49 | 582 write_to(dom,"\n\nena\nconf t\nline console 0\nlogging synchronous\nend\n") |
igor@49 | 583 return 0 |
igor@49 | 584 |
igor@51 | 585 def configure_exec_timeout_0(doms=domains): |
igor@51 | 586 for dom in domains: |
igor@51 | 587 if domain_types[domains.index(dom)] == 'quagga': |
igor@51 | 588 0 |
igor@51 | 589 else: |
igor@52 | 590 write_to(dom,"\n\nena\nconf t\nline console 0\nexec-timeout 0\nend\n") |
igor@51 | 591 return 0 |
igor@51 | 592 |
igor@55 | 593 def configure_no_cdp_log_mismatch_duplex(doms=domains): |
igor@55 | 594 for dom in filter_by_type(domains,'xenomips'): |
igor@55 | 595 write_to(dom,"\n\nena\nconf t\nno cdp log mismatch duplex\nend\n") |
igor@55 | 596 |
igor@33 | 597 def configure_save(doms=domains): |
igor@33 | 598 write_to(doms,"\nwr\n") |
igor@33 | 599 |
igor@49 | 600 def configure_root(doms=domains): |
igor@49 | 601 write_to(doms,"root\n") |
igor@49 | 602 |
igor@0 | 603 #----------------------------------------------------------------------- |
igor@0 | 604 |
igor@0 | 605 |
igor@0 | 606 def add_domain(name,type): |
igor@0 | 607 domains.append(name) |
igor@0 | 608 domain_types.append(type) |
igor@0 | 609 |
igor@0 | 610 def brake_link(domain,bridge): |
igor@0 | 611 broken_links.append([domain,bridge]) |
igor@0 | 612 |
igor@4 | 613 wt_timeout=0.5 |
igor@8 | 614 def write_to(screen,string,return_to_screen=""): |
igor@2 | 615 """ |
igor@2 | 616 write_to(screen,string): |
igor@2 | 617 |
igor@8 | 618 Type *string* to the specified screen(s). |
igor@8 | 619 Screen may be specified with the number *screen*, |
igor@8 | 620 with array of numbers, |
igor@8 | 621 with array of names. |
igor@2 | 622 |
igor@2 | 623 """ |
igor@5 | 624 screen_numbers=[] # number of the screens to write to |
igor@5 | 625 if type(screen) == list: |
igor@5 | 626 screen_numbers=map(lambda x: domains.index(x)+1, screen) |
igor@5 | 627 elif type(screen) == int: |
igor@5 | 628 screen_numbers=[screen] |
igor@5 | 629 else: |
igor@5 | 630 screen_numbers=[domains.index(screen)+1] |
igor@5 | 631 |
igor@5 | 632 for screen_number in screen_numbers: |
igor@5 | 633 run_command("screen -X select "+str(screen_number)) |
igor@5 | 634 time.sleep(wt_timeout) |
igor@5 | 635 for line in string.splitlines(): |
igor@5 | 636 f=open('/tmp/xentaurbuf', 'w') |
igor@5 | 637 f.write(line+"\n") |
igor@5 | 638 f.close() |
igor@5 | 639 run_command("screen -X readreg p /tmp/xentaurbuf") |
igor@5 | 640 time.sleep(wt_timeout) |
igor@5 | 641 run_command("nohup screen -X paste p >& /dev/null") |
igor@5 | 642 time.sleep(wt_timeout) |
igor@5 | 643 |
igor@8 | 644 if return_to_screen != "": |
igor@8 | 645 run_command("screen -X select %s" % (return_to_screen)) |
igor@8 | 646 time.sleep(wt_timeout) |
igor@0 | 647 |
igor@49 | 648 def filter_by_type(doms,type): |
igor@49 | 649 """ |
igor@49 | 650 filter_by_type(doms,type) |
igor@49 | 651 |
igor@49 | 652 Return only domains of *doms* that have specified *type* |
igor@49 | 653 """ |
igor@49 | 654 return filter(lambda x: domain_types[domains.index(x)]==type,domains) |
igor@49 | 655 |
igor@0 | 656 #----------------------------------------------------------------------- |
igor@0 | 657 |
igor@5 | 658 cisco_fa01_up=""" |
igor@5 | 659 ena |
igor@5 | 660 conf t |
igor@5 | 661 int fa0/0 |
igor@49 | 662 duplex half |
igor@5 | 663 no shutdown |
igor@5 | 664 exit |
igor@5 | 665 int fa1/0 |
igor@49 | 666 duplex half |
igor@5 | 667 no shutdown |
igor@5 | 668 exit |
igor@5 | 669 exit |
igor@5 | 670 exit |
igor@5 | 671 """ |
igor@5 | 672 |
igor@5 | 673 cisco_set_ip_on_int=""" |
igor@5 | 674 interface fa%s/0 |
igor@5 | 675 no ip address |
igor@5 | 676 ip address %s 255.255.255.0 |
igor@5 | 677 exit |
igor@5 | 678 """ |
igor@5 | 679 |
igor@0 | 680 nodes=domains |
igor@0 | 681 |
igor@56 | 682 create_objects() |
igor@56 | 683 |
igor@56 | 684 |
igor@49 | 685 if len(sys.argv) == 2: |
igor@49 | 686 if sys.argv[1] == 'start-all': |
igor@0 | 687 start_all() |
igor@49 | 688 elif sys.argv[1] == 'start-domains': |
igor@49 | 689 start_domains() |
igor@49 | 690 elif sys.argv[1] == 'start-bridges': |
igor@49 | 691 start_bridges() |
igor@49 | 692 elif sys.argv[1] == 'stop-all': |
igor@0 | 693 stop_all() |
igor@49 | 694 elif sys.argv[1] == 'stop-domains': |
igor@49 | 695 stop_domains() |
igor@49 | 696 elif sys.argv[1] == 'stop-bridges': |
igor@49 | 697 stop_bridges() |
igor@49 | 698 elif sys.argv[1] == 'restart-all': |
igor@49 | 699 restart_all() |
igor@0 | 700 elif sys.argv[1] == 'screen': |
igor@50 | 701 screen() |
igor@0 | 702 elif sys.argv[1] == 'graph': |
igor@0 | 703 graph() |
igor@0 | 704 elif sys.argv[1] == 'shell': |
igor@0 | 705 shell() |
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 |