igor@0: #!/usr/bin/python igor@0: igor@0: igor@2: import sys,os,time igor@35: igor@35: xentaur_path=os.environ['HOME']+"/xentaur" igor@35: igor@0: sys.path.append('/etc/xen') igor@35: sys.path.append(xentaur_path) igor@11: igor@41: network='snrs_ipsec_rsa_1' igor@40: domain='qua1' igor@35: from xendomain import * igor@35: igor@29: bridges_turned_down=[] igor@29: igor@0: from IPython.Shell import IPShellEmbed igor@0: igor@2: igor@2: screenrc=os.environ['HOME']+"/.screenrc_xentaur" igor@2: 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@41: 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: create_unbound_bridges+="\n"+"\n".join(map(lambda x: "sudo brctl stp "+x+" off", unbound_bridges)) igor@41: create_unbound_bridges+="\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@0: %(create_unbound_bridges)s igor@0: """ % {'create_unbound_bridges' : create_unbound_bridges} igor@0: igor@49: def start_domain(domain): igor@49: 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: 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@49: start_bridges() igor@49: start_domains() igor@49: 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@49: def screen(): igor@7: N=1 igor@0: screens=[] igor@0: for domain in domains: igor@11: 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: N+=1 igor@0: screenlist="\n".join(screens) igor@0: igor@11: # igor@11: # Previous terminal acccess commands: igor@11: # ip="192.168.80."+str(200+N) igor@11: # screens.append("screen -t "+domain+" "+str(N)+" sh -c 'while true; do ssh root@"+ip+" ; done'") igor@11: # igor@11: 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: igor@0: screen -t console 0 bash igor@10: %s igor@49: """ % (hardstatus,screenlist)) igor@10: f.close() igor@49: print "# GNU Screen config file is written to: %s" % screenrc igor@0: igor@0: def graph_node(node): igor@0: i=0 igor@0: domain_type={} igor@0: for domain in domains: igor@0: domain_type[domain]=domain_types[i] igor@0: i+=1 nata@31: return node+" [label=\" "+node+"\",shapefile=\"shapes/all/"+domain_type[node]+".png\",fontcolor=black,fontsize=16]" igor@0: igor@29: def graph_bridge(bridge): igor@29: if bridge in hidden_bridges: igor@29: return "" igor@38: if bridge in real_bridges: igor@38: return "%s [shape=none,shapefile=\"shapes/all/real_switch.png\"]" % (bridge) igor@38: elif bridge in bridges_turned_down: igor@29: return "%s [shape=none,shapefile=\"shapes/all/switch_turned_down.png\"]" % (bridge) igor@29: else: igor@29: return "%s [shape=none,shapefile=\"shapes/all/switch.png\"]" % (bridge) igor@29: igor@29: igor@0: def graph(): igor@0: nodelist="" igor@0: bridgelist="" igor@0: linklist="" igor@0: physicallist="" igor@0: networklist="" igor@0: igor@0: nodelist=";\n ".join(map(graph_node,nodes)) igor@0: if nodelist: nodelist += ";" igor@0: igor@29: bridgelist=";\n ".join(map(graph_bridge,bridges)) igor@0: if bridgelist: bridgelist += ";" igor@0: igor@0: links=[] igor@0: for host, bridges_raw in vbridges_table.iteritems(): igor@0: i=0 igor@0: for this_bridge in bridges_raw: igor@27: if this_bridge in hidden_bridges or not this_bridge: igor@0: continue igor@28: if not [ host, bridges_raw.index(this_bridge), this_bridge ] in temporary_links: igor@28: links.append(host+" -- "+this_bridge+" [taillabel=\"fa"+str(bridges_raw.index(this_bridge))+"/0\"]") igor@27: i+=1 igor@0: igor@0: for link in temporary_links: igor@28: links.append(link[0]+" -- "+link[2]+" [taillabel=\"fa"+str(link[1])+"/0\",color=blue,len=10,w=5,weight=5]") igor@0: igor@27: for link in broken_links: igor@28: links.append(link[0]+" -- "+link[2]+" [taillabel=\"fa"+str(link[1])+"/0\",style=dashed]") igor@27: igor@39: igor@39: # bridge-bridge links igor@39: for host, bridges_raw in bridge_bridge_table.iteritems(): igor@39: i=0 igor@39: for this_bridge in bridges_raw: igor@39: if this_bridge in hidden_bridges or not this_bridge: igor@39: continue igor@39: if not [ host, bridges_raw.index(this_bridge), this_bridge ] in temporary_links: igor@39: links.append(host+" -- "+this_bridge) igor@39: i+=1 igor@39: igor@39: igor@39: igor@0: linklist=";\n ".join(links) igor@0: igor@0: graph_dot = { igor@0: 'nodelist' : nodelist, igor@0: 'bridgelist' : bridgelist, igor@0: 'linklist' : linklist, igor@0: 'physicallist' : physicallist, igor@0: 'networklist' : networklist, igor@0: } igor@0: igor@46: f = open(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@0: igor@0: node [shape=plaintext,color=white,shapefile="shapes/cisco.bmp/router.png"]; igor@0: %(nodelist)s igor@0: igor@0: // bridges igor@0: igor@6: node [shape=none,shapefile="shapes/all/switch.png"]; igor@0: %(bridgelist)s igor@0: igor@0: // physical igor@0: igor@0: node [shape=rectangle,color=blue]; igor@0: %(physicallist)s igor@0: igor@0: // networks (not bridges, not physical) igor@0: node [shape=rectangle,color=green]; igor@0: %(networklist)s igor@0: igor@0: // links (between nodes and bridges) igor@0: %(linklist)s igor@0: igor@0: }; igor@10: """ % graph_dot) igor@10: f.close() igor@46: run_command("neato -Tpng -o %s.png %s.dot "%(network,network)) igor@46: run_command("neato -Tjpg -o %s.jpg %s.dot "%(network,network)) igor@46: run_command("neato -Tsvg -o %s.svg %s.dot "%(network,network)) igor@49: print "# Network map is written to files: %s.{png,svg,jpg,dot}" % network igor@0: igor@27: def autoredraw(): igor@27: graph() igor@27: igor@0: def shell(): nata@31: autoredraw() igor@0: ipshell = IPShellEmbed() igor@0: ipshell() igor@0: igor@0: igor@0: def show_usage(): igor@0: print """Usage: igor@49: xentaur [] 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 -- start the igor@49: stop -- stop the igor@49: igor@49: graph -- generate network scheme (result is in .{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@33: igor@38: class Domain: igor@38: def __init__ (self,name): igor@38: self.name=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 graphviz(self): igor@38: return "" igor@38: def get_domain_id(self): igor@38: return get_domain_id(self.name) 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 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 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 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 igor@23: of the 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 igor@24: of the domain to the 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 igor@24: of the domain from the bridge to which igor@24: it is connected to the 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 igor@25: of the 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@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@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@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@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@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@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: