igor@0: #!/usr/bin/python igor@0: igor@0: igor@2: import sys,os,time igor@0: import xenomips_vars igor@0: xenomips_vars.N='1' igor@0: sys.path.append('/etc/xen') igor@11: igor@11: xen_config_name='fabergeN' igor@21: exec 'from '+xen_config_name+' import bridges,vbridges_table, hidden_bridges, domains, broken_links, temporary_links, domain_types' igor@11: 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@0: def create_bridges_script(): igor@0: unbound_bridges=bridges igor@4: 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: create_unbound_bridges+="\n"+"\n".join(map(lambda x: "sudo /bin/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@0: igor@0: def create_domains_script(): igor@0: for N in range(len(domains)): igor@11: print "sudo /usr/sbin/xm create "+xen_config_name+" N="+str(N)+" && sleep 1 && sudo /usr/sbin/xm sched-credit -d $(sudo /usr/sbin/xm list | grep "+domains[N]+" | awk '{print $2}') -c 10 && sleep 1" igor@0: igor@0: def destroy_domains_script(): igor@0: for N in range(len(domains)): igor@4: print "sudo /usr/sbin/xm shutdown "+domains[N] igor@0: igor@0: def create_screens_script(): igor@0: 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@4: #hardstatus string "\%{gk}\%c \%{yk}\%M\%d \%{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@2: igor@0: screen -t console 0 bash igor@10: %s igor@10: """ % (screenlist)) igor@10: f.close() 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 igor@0: return node+" [label=\" "+node+"\",shapefile=\"shapes/all/"+domain_type[node]+".png\",fontcolor=navy,fontsize=14]" igor@0: 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@5: bridgelist=";\n ".join(set(bridges)-set(hidden_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@0: if this_bridge in hidden_bridges: igor@0: continue igor@0: if [ host, this_bridge ] in broken_links: igor@0: links.append(host+" -- "+this_bridge+" [taillabel=\"fa"+str(i)+"/0\",style=dashed]") igor@0: else: igor@0: links.append(host+" -- "+this_bridge+" [taillabel=\"fa"+str(i)+"/0\"]") igor@0: i+=1 igor@0: igor@0: for link in temporary_links: igor@0: links.append(link[0]+" -- "+link[1]+" [color=blue,len=10,w=5,weight=5]") igor@0: 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@10: f = open("xenomips.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@10: run_command("neato -Tpng -o xenomips.png xenomips.dot ") igor@0: igor@0: def start_all(): igor@0: create_bridges_script() igor@0: create_screens_script() igor@0: create_domains_script() igor@0: graph() igor@0: print """ igor@0: cat < down igor@22: """ igor@22: run_command("sudo ip link set %s down" % bridge) igor@22: igor@22: def bridge_up(bridge): igor@22: """ igor@22: Turn the bridge up igor@22: """ igor@22: run_command("sudo ip link set %s up" % bridge) igor@22: igor@22: def show_bridge(bridge): igor@22: """ igor@22: Show the state of the bridge igor@22: """ 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@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@23: dom_id=get_domain_id(domain) igor@23: 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@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@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@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@0: #----------------------------------------------------------------------- igor@0: igor@5: cisco_fa01_up=""" igor@5: ena igor@5: conf t igor@5: int fa0/0 igor@5: no shutdown igor@5: exit igor@5: int fa1/0 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@5: #----------------------------------------------------------------------- igor@0: igor@0: nodes=domains igor@0: igor@0: if len(sys.argv) > 1: igor@0: if sys.argv[1] == 'start': igor@0: start_all() igor@0: if sys.argv[1] == 'stop': igor@0: stop_all() igor@0: if sys.argv[1] == 'start-bridges': igor@3: create_bridges_script() igor@0: if sys.argv[1] == 'start-domains': igor@3: create_domains_script() igor@0: if sys.argv[1] == 'stop-domains': igor@3: destroy_domains_script() igor@0: elif sys.argv[1] == 'screen': igor@3: create_screens_script() igor@0: elif sys.argv[1] == 'graph': igor@0: graph() igor@0: elif sys.argv[1] == 'shell': igor@0: shell() igor@0: igor@0: else: igor@0: show_usage() igor@0: sys.exit(1) igor@0: igor@0: sys.exit(0) igor@0: igor@0: