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@5: from xenomipsN import bridges,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@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@4: print "sudo /usr/sbin/xm create xenomipsN 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@7: screens.append("screen -t "+domain+" "+str(N)+" xm console "+domain) igor@7: # ip="192.168.80."+str(200+N) igor@7: # 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@4: #hardstatus string "\%{gk}\%c \%{yk}\%M\%d \%{wk}\%?\%-Lw\%?\%{bw}\%n*\%f\%t\%?(\%u)\%?\%{wk}\%?\%+Lw\%?" igor@0: print """ igor@4: igor@0: cat < screenrc igor@2: hardstatus on igor@2: hardstatus alwayslastline 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@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@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@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@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@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: