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@0: from xenomipsN import vbridges_table, hidden_bridges, domains, broken_links, temporary_links, domain_types igor@0: from IPython.Shell import IPShellEmbed igor@0: igor@2: igor@2: screenrc=os.environ['HOME']+"/.screenrc_xentaur" igor@2: igor@0: def create_bridges_script(): igor@0: unbound_bridges=bridges igor@0: create_unbound_bridges="\n".join(map(lambda x: "brctl show | awk '{print $1}' | grep -q "+x+" || brctl addbr "+x, unbound_bridges)) igor@0: create_unbound_bridges+="\n"+"\n".join(map(lambda x: "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@0: print "xm create xenomipsN N="+str(N)+" && sleep 1 && xm sched-credit -d $(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@0: print "xm shutdown "+domains[N] igor@0: igor@0: def create_screens_script(): igor@0: igor@0: N=0 igor@0: screens=[] igor@0: for domain in domains: igor@0: ip="192.168.80."+str(200+N) igor@0: screens.append("screen -t "+domain+" "+str(N)+" sh -c 'while true; do ssh root@"+ip+" ; done'") igor@0: N+=1 igor@0: screenlist="\n".join(screens) igor@0: igor@0: print """ igor@0: cat < screenrc igor@2: hardstatus on igor@2: hardstatus alwayslastline igor@2: hardstatus string "%{gk}%c %{yk}%M%d %{wk}%?%-Lw%?%{bw}%n*%f%t%?(%u)%?%{wk}%?%+Lw%?" igor@2: igor@0: screen -t console 0 bash igor@0: %(screenlist)s igor@0: SCREENRC igor@0: """ % { 'screenlist' : screenlist } 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@0: bridgelist=";\n ".join(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@0: print """ igor@0: cat <<'GRAPH' > xenomips.dot 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@0: node [shape=none,shapefile="shapes/cisco.bmp/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@0: GRAPH igor@0: neato -Tpng -o xenomips.png xenomips.dot igor@0: """ % graph_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 <& /dev/null") igor@2: time.sleep(0.05) igor@0: run_command("screen -X select 0") igor@0: igor@0: #----------------------------------------------------------------------- igor@0: igor@0: bridges=[] igor@0: for domain in vbridges_table.keys(): igor@0: bridges += set(vbridges_table[domain]) igor@0: bridges=set(bridges) 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@0: create_bridges_script(bridges) igor@0: if sys.argv[1] == 'start-domains': igor@0: create_domains_script(domains) igor@0: if sys.argv[1] == 'stop-domains': igor@0: destroy_domains_script(domains) igor@0: elif sys.argv[1] == 'screen': igor@0: create_screens_script(domains) 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: