igor@0: #!/usr/bin/python igor@60: # vim: set fileencoding=utf-8 : igor@0: igor@2: import sys,os,time igor@66: from IPython.Shell import IPShellEmbed igor@35: igor@66: path_xentaur="%s/hg/xentaur" % os.environ['HOME'] igor@66: path_shapes=path_xentaur+'/shapes' igor@66: path_scripts=path_xentaur+'/files' igor@66: igor@66: network='net1' igor@66: igor@66: path_network=os.environ['HOME']+"/.xentaur/"+network igor@66: if not os.path.exists(path_network): igor@66: os.makedirs(path_network) igor@66: screenrc=path_network+"/.screenrc_xentaur" igor@35: igor@0: sys.path.append('/etc/xen') igor@66: sys.path.append(path_xentaur) igor@66: sys.path.append(path_network) igor@63: sys.path.append('.') igor@11: igor@56: node_object={} igor@56: link_object={} igor@56: bridge_object={} igor@66: ec2_node={} igor@56: nata@65: domain='dyn1' igor@35: from xendomain import * igor@35: igor@29: bridges_turned_down=[] igor@29: igor@10: def run(program, *args): igor@10: pid = os.fork() igor@10: if not pid: igor@10: os.execvp(program, (program,) + args) igor@10: return os.wait()[0] igor@10: igor@10: def run_command(line): igor@10: #cmds=line.split() igor@10: #run(cmds[0],*cmds[1:]) igor@10: run("/bin/sh", "-c", line) igor@10: igor@23: def run_command_return_stdout(line): igor@23: p = os.popen(line) igor@23: output = p.read() igor@23: p.close() igor@23: return output igor@23: igor@49: ################################################################################ igor@49: #Xentaur command-line commands igor@49: igor@49: ## Start igor@49: igor@49: def start_bridges(): igor@38: unbound_bridges=set(bridges)-set(real_bridges) igor@56: script="" igor@56: script="\n".join(map(lambda x: "sudo brctl show | awk '{print $1}' | grep -qx "+x+" || sudo brctl addbr "+x, unbound_bridges)) igor@56: script+="\n"+"\n".join(map(lambda x: "sudo brctl stp "+x+" off", unbound_bridges)) igor@56: script+="\n"+"\n".join(map(lambda x: "sudo ip link set "+x+" up", unbound_bridges)) igor@0: igor@0: print """#!/bin/sh igor@0: # create unbound bridges igor@56: %s igor@56: """ % (script) igor@0: igor@49: def start_domain(domain): igor@66: 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: igor@49: def start_domains(doms=domains): igor@49: for domain in doms: igor@38: if not domain in real_nodes: igor@49: start_domain(domain) igor@0: igor@49: def start_all(): igor@49: graph() igor@49: screen() igor@66: html() igor@49: start_bridges() igor@49: start_domains() igor@49: igor@66: def ec2_assign_nodes_to_instances(): igor@66: domain_number=0 igor@66: for dom in domains: igor@66: node_name=network+"-node%s"%(domain_number/2) igor@66: ec2_node[dom]=node_name igor@66: domain_number+=1 igor@66: igor@66: def make_start_emulators(): igor@66: s="" igor@66: #s+="cat screenrc_template > screenrc_$NETWORK\n" igor@66: #s+="echo \"screen -t console 0 sh -c 'cd ~/xentaur; ./xentaur.py shell'\" >> screenrc_$NETWORK\n" igor@66: ios="/mnt/ios/C7200-AD.BIN" igor@66: ec2_assign_nodes_to_instances() igor@66: for dom in domains: igor@66: node_name=ec2_node[dom] igor@66: i=0 igor@66: line='dynamips '+ios igor@66: line2='' igor@66: for iface in vbridges_table[dom]: igor@66: bridge=vbridges_table[dom][i] igor@66: if i>0: igor@66: line += " -p %s:PA-FE-TX "%i igor@66: line += " -s %s:0:tap:%s_%s "% (i,dom,iface) igor@66: line2 += "sleep 2; brctl addif %s %s; "%(bridge, dom+"_"+iface) igor@66: line2 +='ifconfig %s up; ifconfig %s promisc ;' %(bridge, bridge) igor@66: line2 += "ifconfig %s_%s up; "%(dom, iface) igor@66: i+=1 igor@66: 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: s += "ssh %s \"%s\"\n" % (node_name, line2) igor@66: #s += "echo \"screen %s ssh -t %s 'screen -r %s-'\" >> screenrc_%s\n" % (domain_number, node_name, dom, network) igor@66: #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: return s igor@66: igor@66: def make_start_instances_config(net=network): igor@66: f = open(path_network+"/start-instances-"+network, "w"); igor@66: f.write("INSTANCES_NUMBER=%s\n"%((len(domains)+1)/2)) igor@66: f.write("start_emulators()\n{\n%s\n}\n"%make_start_emulators()) igor@66: f.close() igor@66: igor@66: def start_network(net=network): igor@66: make_start_instances_config() igor@66: #run_command("cd %s; env NETWORK=%s sh %s instances start" % (path_network, network, path_scrips+"/ec2-instances")) igor@66: igor@66: def stop_network(net=network): igor@66: run_command("cd %s; env NETWORK=%s sh instances stop" % (path_network, network)) igor@66: igor@49: ## Stop igor@49: igor@49: def stop_domain(domain,wait=0): igor@49: if wait: igor@49: print "sudo xm shutdown -w "+domain igor@49: else: igor@49: print "sudo xm shutdown "+domain igor@49: igor@49: def stop_domains(doms=domains, wait=0): igor@49: for domain in doms: igor@38: if not domain in real_nodes: igor@49: stop_domain(domain,wait) igor@0: igor@49: def stop_bridges(): igor@49: ###FIXME### igor@49: return 0 igor@49: igor@49: def stop_all(wait=0): igor@49: stop_domains(domains, wait) igor@49: stop_bridges() igor@49: igor@49: def restart_all(): igor@49: stop_all(1) igor@49: start_all() igor@49: igor@56: #################################################### igor@56: igor@56: def create_objects(): igor@56: create_node_objects() igor@56: create_bridge_objects() igor@56: create_link_objects() igor@56: igor@56: def create_node_objects(): igor@56: for dom in domains: igor@56: node_object[dom]=Node(dom) igor@56: igor@56: def create_bridge_objects(): igor@56: for bridge in bridges: igor@56: bridge_object[bridge]=Bridge(bridge) igor@56: igor@56: def create_link_objects(): igor@56: igor@56: for node, bridges_raw in vbridges_table.iteritems(): igor@56: interface=0 igor@56: j=0 igor@56: for this_bridge in bridges_raw: igor@56: int_label="" igor@56: if this_bridge.find(':') != -1: igor@56: res = this_bridge.split(':') igor@56: this_bridge= res[0] igor@56: bridges_raw[j] = this_bridge igor@56: int_label = res[1] igor@56: if not [ node, bridges_raw.index(this_bridge), this_bridge ] in temporary_links: igor@56: name="%s %s %s" % (node,interface,this_bridge) igor@56: link_object[name]=Link(name,node,interface,this_bridge,int_label) igor@56: interface+=1 igor@56: vbridges_table[node]=bridges_raw igor@56: igor@56: for node, bridges_raw in bridge_bridge_table.iteritems(): igor@56: interface=0 igor@56: j=0 igor@56: for this_bridge in bridges_raw: igor@56: int_label="" igor@56: if this_bridge.find(':') != -1: igor@56: res = this_bridge.split(':') igor@63: this_bridge= res[2] igor@56: bridges_raw[j] = this_bridge igor@56: int_label = res[1] igor@56: if not [ node, bridges_raw.index(this_bridge), this_bridge ] in temporary_links: igor@56: name="%s %s %s" % (node,interface,this_bridge) igor@56: link_object[name]=Link(name,node,interface,this_bridge,int_label) igor@56: interface+=1 igor@56: bridge_bridge_table[node]=bridges_raw igor@56: igor@56: for node,interface,bridge in temporary_links: igor@56: name="%s %s %s" % (node,interface,bridge) igor@56: link_object[name]=Link(name,node,interface,this_bridge) igor@56: igor@56: for node,interface,bridge in broken_links: igor@56: name="%s %s %s" % (node,interface,bridge) igor@56: link_object[name]=Link(name,node,interface,this_bridge) igor@56: igor@56: igor@56: #################################################### igor@56: igor@49: def screen(): igor@60: wait_seconds=1 igor@0: screens=[] igor@0: for domain in domains: igor@66: screens.append("screen -t %(domain)s %(domain_number)s %(console_string)s" % { igor@66: 'domain' : domain, igor@66: 'domain_number' : domains.index(domain)+1, igor@66: 'console_string': node_object[domain].console_string(), igor@66: 'wait_interval' : wait_seconds } ) igor@66: igor@0: screenlist="\n".join(screens) igor@0: igor@49: hardstatus='hardstatus string "%{rk}Xentaur%{bk}@%H %{gk}%c %{yk}%d.%m %{wk}%?%-Lw%?%{bw}%n*%f%t%?(%u)%?%{wk}%?%+Lw%?"' igor@10: igor@11: f=open(screenrc, "w"); igor@10: f.write(""" igor@2: hardstatus on igor@2: hardstatus alwayslastline igor@49: %s igor@2: nata@65: screen -t console 0 sh -c 'while true; do %s/xentaur.py shell ; echo Retrying in %s secods...; sleep %s ; clear; done' igor@10: %s igor@66: """ % (hardstatus,path_xentaur,wait_seconds,wait_seconds,screenlist)) igor@10: f.close() igor@49: print "# GNU Screen config file is written to: %s" % screenrc igor@0: igor@0: def graph(): igor@0: nodelist="" igor@0: bridgelist="" igor@0: linklist="" igor@0: physicallist="" igor@0: networklist="" igor@0: igor@56: nodelist=";\n ".join(map(lambda node: node_object[node].graphviz_string(),nodes)) igor@0: if nodelist: nodelist += ";" igor@56: bridgelist=";\n ".join(map(lambda bridge: bridge_object[bridge].graphviz_string(),bridges)) igor@0: if bridgelist: bridgelist += ";" igor@56: linklist=";\n ".join(map(lambda link: link_object[link].graphviz_string(),link_object.keys())) igor@56: if linklist: linklist += ";" igor@0: igor@66: f = open(path_network+"/"+network+".dot", "w"); igor@10: f.write (""" igor@0: graph G { igor@0: edge [len=1.25]; igor@0: splines=true; igor@0: // nodes igor@56: %s igor@0: igor@0: // bridges igor@56: %s igor@0: igor@0: // physical igor@0: node [shape=rectangle,color=blue]; igor@56: %s igor@0: igor@0: // networks (not bridges, not physical) igor@0: node [shape=rectangle,color=green]; igor@56: %s igor@0: igor@0: // links (between nodes and bridges) igor@56: %s igor@0: igor@0: }; igor@56: """ % (nodelist, bridgelist, physicallist, networklist, linklist)) igor@10: f.close() igor@66: run_command("neato -Tpng -o %s.png %s.dot "%(path_network+"/"+network,path_network+"/"+network)) igor@66: run_command("neato -Tjpg -o %s.jpg %s.dot "%(path_network+"/"+network,path_network+"/"+network)) igor@66: run_command("neato -Tsvg -o %s.svg %s.dot "%(path_network+"/"+network,path_network+"/"+network)) igor@66: #run_command("neato -Tcmapx -o %s.cmapx -NURL=http://google.com %s.dot "%(path_network+"/"+network,path_network+"/"+network)) igor@66: print "# Network map is written to files: %s.{png,svg,jpg,dot}" % (path_network+"/"+network) igor@66: igor@66: def html(): igor@66: f = open(path_network+"/index.html", "w"); igor@66: f.write (""" igor@66: igor@66: igor@66: Network %s map</network> igor@66: <body> igor@66: <img src="%s.png" /><br/> igor@66: <pre> igor@66: nodes=%s igor@66: bridges=%s igor@66: vbridges_table=%s igor@66: </pre> igor@66: </body> igor@66: </head> igor@66: </html> igor@66: """ % (network, network, domains, bridges, vbridges_table)) igor@66: f.close() igor@66: #run_command("cp %s.html /var/www/ec2/network/%s/index.html"%(network,network)) igor@0: igor@27: def autoredraw(): igor@27: graph() igor@66: html() igor@66: screen() igor@27: igor@0: def shell(): nata@31: autoredraw() igor@66: ipshell = IPShellEmbed(['-noconfirm_exit']) igor@0: ipshell() igor@0: igor@60: def version(): igor@61: print """ igor@61: Xentaur 0.1-PRE igor@61: igor@61: ,--, igor@61: _ ___/ /\\| igor@61: ,;`( )__, ) ~ igor@61: // .// '--; igor@61: ' / \ | igor@61: igor@61: """ igor@60: igor@60: def info(): igor@60: version() igor@60: igor@60: print "Network name: ", network igor@60: print "-----------------------------------------------" igor@60: print igor@60: print "Nodes: ", len(domains) igor@60: print " * virtual nodes: ", len(domains)-len(real_nodes) igor@60: print " * real nodes:", len(real_nodes) igor@60: print igor@60: print "Bridges:", len(bridges) igor@60: print " * virtual bridges:", len(bridges)-len(real_bridges)-len(cross_bridges) igor@60: print " * real switches:", len(real_bridges) igor@60: print " * direct links:", len(cross_bridges) igor@60: igor@0: def show_usage(): igor@0: print """Usage: igor@49: xentaur <command> [<argument>] igor@49: igor@49: Commands: igor@49: start-all -- start bridges and domains igor@49: start-domains -- start domains only igor@49: start-bridges -- start bridges only igor@49: stop-all -- stop bridges and domains igor@49: stop-domains -- stop domains only igor@49: stop-bridges -- stop bridges only (domains have to be stopped already) igor@49: restart-all -- restart bridges and domains igor@49: igor@49: start <domain> -- start the <domain> igor@49: stop <domain> -- stop the <domain> igor@49: igor@49: graph -- generate network scheme (result is in <network>.{png,jpg,svg}) igor@49: screen -- generate GNU Screen config file (~/.screenrc_xentaur) igor@49: shell -- run Xentaur shell igor@49: igor@0: """ igor@0: igor@33: def save(): igor@33: print "network =", xen_config_name igor@33: print "domains =", domains igor@33: print "domain_types =", domain_types igor@33: print "bridges =", bridges igor@33: print "vbridges_table =", vbridges_table igor@33: print "hidden_bridges =", hidden_bridges igor@33: print "broken_links =", broken_links igor@33: print "temporary_links =", temporary_links igor@33: print "bridges_turned_down =", bridges_turned_down igor@33: igor@33: #----------------------------------------------------------------------- igor@33: # CLASSES igor@33: igor@33: class Bridge: igor@33: def __init__ (self,name): igor@33: self.name=name igor@33: def up(self): igor@33: bridge_up(self.name) igor@33: def down(self): igor@33: bridge_down(self.name) igor@33: def show(self): igor@33: show_bridge(self.name) igor@33: def dump_start(self,filter=""): igor@33: dump_start(self.name,filter) igor@33: igor@56: def is_hidden(self): igor@56: return self.name in hidden_bridges igor@56: def is_real(self): igor@56: return self.name in real_bridges igor@56: def is_turned_down(self): igor@56: return self.name in bridges_turned_down igor@56: def is_cross(self): igor@56: return self.name in cross_bridges igor@33: igor@56: def graphviz_string(self): igor@56: if self.is_hidden(): igor@63: return "//" igor@56: elif self.is_cross(): igor@56: return "%s [shape=circle,height=0.03,color=black,fillcolor=black,style=filled,label=\"\"]" % (self.name) igor@56: elif self.is_real(): igor@63: return "%s [color=white,shape=none,shapefile=\"%s/all/real_switch.png\"]" % (self.name, path_shapes) igor@56: elif self.is_turned_down(): igor@63: return "%s [color=white,shape=none,shapefile=\"%s/all/switch_turned_down.png\"]" % (self.name, path_shapes) igor@56: else: igor@63: return "%s [color=white,shape=none,shapefile=\"%s/all/switch.png\"]" % (self.name, path_shapes) igor@56: igor@56: igor@56: class Node: igor@38: def __init__ (self,name): igor@38: self.name=name igor@56: self.type=domain_types[domains.index(name)] igor@38: def start(self): igor@38: return "" igor@38: def stop(self): igor@38: return "" igor@38: def start_commandline(self): igor@38: return "" igor@38: def get_domain_id(self): igor@38: return get_domain_id(self.name) igor@56: def graphviz_string(self): igor@63: return self.name+" [color=white,shape=plaintext,label=\" "+self.name+"\",shapefile=\""+path_shapes+"/all/"+\ igor@56: domain_types[domains.index(self.name)]+".png\",fontcolor=black,fontsize=16,target=\"http://google.com\"]" igor@56: def console_string(self): igor@66: ec2=True # FIXME igor@66: ec2_assign_nodes_to_instances() igor@66: if ec2: igor@66: return path_scripts+"/node-terminal-session INSTANCE EC2_NODE %s %s %s"%(ec2_node[self.name], network, self.name) igor@66: nata@65: if self.type in [ 'quagga', 'dynamips', 'freebsd', 'linux' ]: igor@56: return "sudo xm console "+self.name igor@56: elif self.name in real_bridges or self.name in real_nodes: igor@56: return "echo Press enter to connect; read line; "+connection_table[self.name] igor@56: igor@56: igor@56: class Link: igor@56: def __init__ (self,name,node,interface,bridge,label=""): igor@56: self.name=name igor@56: self.node=node igor@56: self.interface=interface igor@56: self.bridge=bridge igor@56: self.label=label igor@56: igor@56: def is_temporary(self): igor@56: return [self.node,self.interface,self.bridge] in temporary_links igor@56: igor@56: def is_broken(self): igor@56: return ([self.node,self.interface,self.bridge] in broken_links) igor@56: igor@63: def is_hidden(self): igor@63: return self.bridge in hidden_bridges igor@63: igor@56: def graphviz_string(self): igor@63: if self.is_hidden(): igor@63: return "//" igor@56: if self.is_temporary(): igor@56: return self.node+" -- "+self.bridge+" [taillabel=\"fa"+str(self.interface)+"/0\",color=blue,len=10,w=5,weight=5]" igor@56: if self.is_broken(): igor@56: return self.node+" -- "+self.bridge+" [taillabel=\"fa"+str(self.interface)+"/0\",style=dashed]" igor@56: igor@56: ip="\\n.%s.%s" % (bridges.index(self.bridge)+1, domains.index(self.node)+1) igor@66: if domain_types[domains.index(self.node)] == 'dynamips': igor@56: int_name="fa"+str(self.interface)+"/0" igor@56: else: igor@56: int_name="eth"+str(self.interface) igor@56: if self.label != "": igor@56: int_name = self.label igor@56: return self.node+" -- "+self.bridge+" [taillabel=\""+int_name+ip+"\",fontsize=14,fontname=fixed]" igor@56: igor@38: igor@23: #----------------------------------------------------------------------- igor@23: # DOMAINS igor@23: igor@23: def get_domain_id(domain): igor@23: return run_command_return_stdout("sudo xm list | awk '{if ($1 == \"'%s'\") print $2}'" % domain).rstrip("\n") igor@23: igor@22: igor@22: #----------------------------------------------------------------------- igor@22: # BRIDGES and IFACES igor@22: igor@22: def bridge_down(bridge): igor@22: """ igor@22: Turn the bridge <bridge> down igor@22: """ igor@38: if bridge in real_bridges: igor@38: print "Bridge %s is a real bridge" % (bridge) igor@38: return -1 igor@29: if bridge in bridges_turned_down: igor@29: print "Bridge %s is turned down already" % (bridge) igor@29: else: igor@29: bridges_turned_down.append(bridge) igor@29: run_command("sudo ip link set %s down" % bridge) igor@29: autoredraw() igor@22: igor@22: def bridge_up(bridge): igor@22: """ igor@22: Turn the bridge <bridge> up igor@22: """ igor@38: if bridge in real_bridges: igor@38: print "Bridge %s is a real bridge" % (bridge) igor@38: return -1 igor@29: if not (bridge in bridges_turned_down): igor@29: print "Bridge %s is turned up already" % (bridge) igor@29: else: igor@29: bridges_turned_down.remove(bridge) igor@29: run_command("sudo ip link set %s up" % bridge) igor@29: autoredraw() igor@22: igor@22: def show_bridge(bridge): igor@22: """ igor@22: Show the state of the bridge <bridge> igor@22: """ igor@38: if bridge in real_bridges: igor@38: print "Bridge %s is a real bridge" % (bridge) igor@38: return -1 igor@22: run_command("sudo ip link show %s" % bridge) igor@22: igor@23: igor@23: def int_disconnect(domain, int_number): igor@23: """ igor@23: Disconnect the interface with the number <int_number> igor@23: of the domain <domain> from the bridge to which igor@23: it is connected igor@23: """ igor@23: dom_id=get_domain_id(domain) igor@23: bridge=vbridges_table[domain][int_number] igor@23: if not bridge: igor@23: print "Interface %s of the %s domain is not connected" % (int_number, domain) igor@23: return 1 igor@23: run_command("sudo brctl delif %s vif%s.%s" % (bridge, dom_id, int_number)) igor@23: vbridges_table[domain][int_number]='' igor@28: if [ domain, int_number, bridge ] in temporary_links: igor@28: temporary_links.remove([ domain, int_number, bridge ]) igor@27: else: igor@28: broken_links.append([ domain, int_number, bridge ]) igor@27: autoredraw() igor@23: igor@23: def int_connect(domain, int_number, bridge): igor@23: """ igor@23: Connect the interface with the number <int_number> igor@24: of the domain <domain> to the bridge <bridge> igor@23: """ igor@38: if bridge in real_bridges: igor@38: print "Bridge %s is a real bridge" % (bridge) igor@38: return -1 igor@38: igor@23: dom_id=get_domain_id(domain) igor@23: if vbridges_table[domain][int_number]: igor@23: print "Interface %s of the %s domain is connected already to the %s bridge" % (int_number, domain, vbridges_table[domain][int_number]) igor@23: return 1 igor@23: run_command("sudo brctl addif %s vif%s.%s" % (bridge, dom_id, int_number)) igor@23: vbridges_table[domain][int_number]=bridge igor@28: if [ domain, int_number, bridge ] in broken_links: igor@28: broken_links.remove([ domain, int_number, bridge ]) igor@27: else: igor@28: temporary_links.append([ domain, int_number, bridge ]) igor@27: autoredraw() igor@23: igor@24: def int_reconnect(domain, int_number, bridge): igor@24: """ igor@24: Reconnect the interface with the number <int_number> igor@24: of the domain <domain> from the bridge to which igor@24: it is connected to the bridge <bridge> igor@24: """ igor@38: if bridge in real_bridges: igor@38: print "Bridge %s is a real bridge" % (bridge) igor@38: return -1 igor@38: igor@24: int_disconnect(domain, int_number) igor@24: int_connect(domain, int_number, bridge) igor@24: igor@24: def show_int(domain, int_number): igor@25: """ igor@25: Show information about the interface <int_nuber> igor@25: of the domain <domain> igor@25: """ igor@26: return vbridges_table[domain][int_number] igor@24: igor@28: igor@28: def dump_start(bridge, filter=""): igor@38: if bridge in real_bridges: igor@38: print "Bridge %s is a real bridge" % (bridge) igor@38: return -1 igor@32: try: igor@32: print "Writing dump... (press Ctrl-C to stop)" igor@32: run_command("sudo tcpdump -w xentaur.dump -i %s %s > /dev/null 2>&1 " % (bridge,filter)) igor@32: except: igor@32: print "Done.\n Dump is written to xentaur.dump" igor@28: return 0 igor@28: igor@28: def dump_stop(): igor@28: return 0 igor@33: igor@33: igor@33: #----------------------------------------------------------------------- igor@33: # CONFIGURATION TEMPLATES igor@33: igor@33: igor@33: def configure_ip_addresses(doms=domains): igor@40: igor@40: cisco_set_ip_on_int=""" igor@40: \n\n\n igor@40: int fa%s/0 igor@66: duplex full igor@40: no ip address igor@40: ip address %s 255.255.255.0 igor@40: no shutdown igor@40: exit igor@40: """ igor@40: igor@40: quagga_set_ip_on_int=""" igor@40: int eth%s igor@40: no ip address igor@40: ip address %s/24 igor@40: no shutdown igor@40: exit igor@40: """ igor@40: igor@63: linux_set_ip_on_int=""" igor@63: ifconfig eth%s %s netmask 255.255.255.0 igor@63: """ igor@63: igor@40: for dom in doms: igor@40: i=domains.index(dom)+1 igor@40: if domain_types[domains.index(dom)] == 'quagga': igor@40: command = quagga_set_ip_on_int igor@40: write_to(i,"\nconf t\n") igor@40: j=0 igor@40: for br in vbridges_table[dom]: igor@40: write_to(i,command % (j, "192.168.%s.%s"%(bridges.index(br)+1,i))) igor@40: j+=1 igor@40: write_to(i,"\nend\n") igor@63: elif domain_types[domains.index(dom)] == 'linux': igor@63: command = linux_set_ip_on_int igor@63: j=0 igor@63: for br in vbridges_table[dom]: igor@63: #write_to(i, command % (j, "192.168.%s.%s"%(bridges.index(br)+1,i)) ) igor@63: print i, command % (j, "192.168.%s.%s"%(bridges.index(br)+1,i)) igor@63: j+=1 igor@40: else: igor@40: command = cisco_set_ip_on_int igor@40: write_to(i,"\nena\nconf t\n") igor@40: j=0 igor@40: for br in vbridges_table[dom]: igor@40: write_to(i,command % (j, "192.168.%s.%s"%(bridges.index(br)+1,i))) igor@40: j+=1 igor@40: write_to(i,"\nend\n") igor@40: return 0 igor@40: igor@40: def configure_no_ip_addresses(doms=domains): igor@40: igor@40: cisco_set_ip_on_int=""" igor@40: \n\n\n igor@40: int fa%s/0 igor@40: no ip address %s 255.255.255.0 igor@40: exit igor@40: """ igor@40: igor@40: quagga_set_ip_on_int=""" igor@40: int eth%s igor@40: no ip address %s/24 igor@40: exit igor@40: """ igor@40: igor@40: for dom in doms: igor@40: i=domains.index(dom)+1 igor@40: if domain_types[domains.index(dom)] == 'quagga': igor@40: command = quagga_set_ip_on_int igor@40: write_to(i,"\nconf t\n") igor@40: j=0 igor@40: for br in vbridges_table[dom]: igor@40: write_to(i,command % (j, "192.168.%s.%s"%(bridges.index(br)+1,i))) igor@40: j+=1 igor@40: write_to(i,"\nend\n") igor@40: else: igor@40: command = cisco_set_ip_on_int igor@40: write_to(i,"\nena\nconf t\n") igor@40: j=0 igor@40: for br in vbridges_table[dom]: igor@40: write_to(i,command % (j, "192.168.%s.%s"%(bridges.index(br)+1,i))) igor@40: j+=1 igor@40: write_to(i,"\nend\n") igor@33: return 0 igor@33: igor@33: def configure_ospf(doms=domains): igor@40: for dom in doms: igor@40: if domain_types[domains.index(dom)] == 'quagga': igor@40: write_to(dom,"\n\nconf t\nrouter ospf\nnetwork 192.168.0.0/16 area 0\nend\n") igor@40: else: igor@40: 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: return 0 igor@33: igor@49: def configure_hostname(doms=domains): igor@49: for dom in doms: igor@49: if domain_types[domains.index(dom)] == 'quagga': igor@49: write_to(dom,"\n\nconf t\nhostname %s\nend\n" % dom) igor@49: else: igor@49: write_to(dom,"\n\nena\nconf t\nhostname %s\nend\n" % dom) igor@49: return 0 igor@49: igor@49: def configure_logging_synchronous(doms=domains): igor@49: for dom in domains: igor@49: if domain_types[domains.index(dom)] == 'quagga': igor@49: 0 igor@49: else: igor@49: write_to(dom,"\n\nena\nconf t\nline console 0\nlogging synchronous\nend\n") igor@49: return 0 igor@49: igor@51: def configure_exec_timeout_0(doms=domains): igor@51: for dom in domains: igor@51: if domain_types[domains.index(dom)] == 'quagga': igor@51: 0 igor@51: else: igor@52: write_to(dom,"\n\nena\nconf t\nline console 0\nexec-timeout 0\nend\n") igor@51: return 0 igor@51: igor@55: def configure_no_cdp_log_mismatch_duplex(doms=domains): igor@66: for dom in filter_by_type(domains,'dynamips'): igor@55: write_to(dom,"\n\nena\nconf t\nno cdp log mismatch duplex\nend\n") igor@55: igor@33: def configure_save(doms=domains): igor@33: write_to(doms,"\nwr\n") igor@33: igor@49: def configure_root(doms=domains): igor@49: write_to(doms,"root\n") igor@49: igor@0: #----------------------------------------------------------------------- igor@0: igor@0: igor@0: def add_domain(name,type): igor@0: domains.append(name) igor@0: domain_types.append(type) igor@0: igor@0: def brake_link(domain,bridge): igor@0: broken_links.append([domain,bridge]) igor@0: igor@4: wt_timeout=0.5 igor@8: def write_to(screen,string,return_to_screen=""): igor@2: """ igor@2: write_to(screen,string): igor@2: igor@8: Type *string* to the specified screen(s). igor@8: Screen may be specified with the number *screen*, igor@8: with array of numbers, igor@8: with array of names. igor@2: igor@2: """ igor@5: screen_numbers=[] # number of the screens to write to igor@5: if type(screen) == list: igor@5: screen_numbers=map(lambda x: domains.index(x)+1, screen) igor@5: elif type(screen) == int: igor@5: screen_numbers=[screen] igor@5: else: igor@5: screen_numbers=[domains.index(screen)+1] igor@5: igor@5: for screen_number in screen_numbers: igor@5: run_command("screen -X select "+str(screen_number)) igor@5: time.sleep(wt_timeout) igor@5: for line in string.splitlines(): igor@5: f=open('/tmp/xentaurbuf', 'w') igor@5: f.write(line+"\n") igor@5: f.close() igor@5: run_command("screen -X readreg p /tmp/xentaurbuf") igor@5: time.sleep(wt_timeout) igor@5: run_command("nohup screen -X paste p >& /dev/null") igor@5: time.sleep(wt_timeout) igor@5: igor@8: if return_to_screen != "": igor@8: run_command("screen -X select %s" % (return_to_screen)) igor@8: time.sleep(wt_timeout) igor@0: igor@49: def filter_by_type(doms,type): igor@49: """ igor@49: filter_by_type(doms,type) igor@49: igor@49: Return only domains of *doms* that have specified *type* igor@49: """ igor@49: return filter(lambda x: domain_types[domains.index(x)]==type,domains) igor@49: igor@0: #----------------------------------------------------------------------- igor@0: igor@5: cisco_fa01_up=""" igor@5: ena igor@5: conf t igor@5: int fa0/0 igor@49: duplex half igor@5: no shutdown igor@5: exit igor@5: int fa1/0 igor@49: duplex half igor@5: no shutdown igor@5: exit igor@5: exit igor@5: exit igor@5: """ igor@5: igor@5: cisco_set_ip_on_int=""" igor@5: interface fa%s/0 igor@5: no ip address igor@5: ip address %s 255.255.255.0 igor@5: exit igor@5: """ igor@5: igor@0: nodes=domains igor@0: igor@56: create_objects() igor@56: igor@56: igor@49: if len(sys.argv) == 2: igor@49: if sys.argv[1] == 'start-all': igor@0: start_all() igor@49: elif sys.argv[1] == 'start-domains': igor@49: start_domains() igor@49: elif sys.argv[1] == 'start-bridges': igor@49: start_bridges() igor@49: elif sys.argv[1] == 'stop-all': igor@0: stop_all() igor@49: elif sys.argv[1] == 'stop-domains': igor@49: stop_domains() igor@49: elif sys.argv[1] == 'stop-bridges': igor@49: stop_bridges() igor@49: elif sys.argv[1] == 'restart-all': igor@49: restart_all() igor@0: elif sys.argv[1] == 'screen': igor@50: screen() igor@0: elif sys.argv[1] == 'graph': igor@0: graph() igor@0: elif sys.argv[1] == 'shell': igor@0: shell() igor@60: elif sys.argv[1] == 'info': igor@60: info() igor@49: else: igor@49: show_usage() igor@49: sys.exit(1) igor@49: elif len(sys.argv) == 3: igor@49: if sys.argv[1] == 'start': igor@49: start_domain(sys.argv[2]) igor@49: elif sys.argv[1] == 'stop': igor@49: stop_domain(sys.argv[2]) igor@49: elif sys.argv[1] == 'restart': igor@49: stop_domain(sys.argv[2]) igor@49: start_domain(sys.argv[2]) igor@49: else: igor@49: show_usage() igor@49: sys.exit(1) igor@0: else: igor@0: show_usage() igor@0: sys.exit(1) igor@0: igor@0: sys.exit(0) igor@0: igor@0: