xentaur

diff xentaur.py @ 0:ab37f0ad70e5

Initial revision
author igor
date Fri Jul 13 16:57:33 2007 +0300 (2007-07-13)
parents
children e34dd9ee49e5
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xentaur.py	Fri Jul 13 16:57:33 2007 +0300
     1.3 @@ -0,0 +1,211 @@
     1.4 +#!/usr/bin/python
     1.5 +
     1.6 +
     1.7 +import sys
     1.8 +import xenomips_vars
     1.9 +xenomips_vars.N='1'
    1.10 +sys.path.append('/etc/xen')
    1.11 +from xenomipsN import vbridges_table, hidden_bridges, domains, broken_links, temporary_links, domain_types
    1.12 +from IPython.Shell import IPShellEmbed
    1.13 +
    1.14 +def create_bridges_script():
    1.15 +    unbound_bridges=bridges
    1.16 +    create_unbound_bridges="\n".join(map(lambda x: "brctl show | awk '{print $1}' | grep -q "+x+" || brctl addbr "+x, unbound_bridges))
    1.17 +    create_unbound_bridges+="\n"+"\n".join(map(lambda x: "ip link set "+x+" up", unbound_bridges))
    1.18 +
    1.19 +    print """#!/bin/sh
    1.20 +# create unbound bridges
    1.21 +%(create_unbound_bridges)s
    1.22 +""" % {'create_unbound_bridges' : create_unbound_bridges}
    1.23 +
    1.24 +
    1.25 +def create_domains_script():
    1.26 +    for N in range(len(domains)):
    1.27 +        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"
    1.28 +
    1.29 +def destroy_domains_script():
    1.30 +    for N in range(len(domains)):
    1.31 +        print "xm shutdown "+domains[N]
    1.32 +
    1.33 +def create_screens_script():
    1.34 +
    1.35 +    N=0
    1.36 +    screens=[]
    1.37 +    for domain in domains:
    1.38 +        ip="192.168.80."+str(200+N)
    1.39 +        screens.append("screen -t "+domain+"   "+str(N)+" sh -c 'while true; do ssh root@"+ip+" ; done'")
    1.40 +        N+=1
    1.41 +    screenlist="\n".join(screens)
    1.42 +
    1.43 +    print """
    1.44 +cat <<SCREENRC > screenrc
    1.45 +screen -t console 0 bash
    1.46 +%(screenlist)s
    1.47 +SCREENRC
    1.48 +""" % { 'screenlist' : screenlist }
    1.49 +
    1.50 +def graph_node(node):
    1.51 +    i=0
    1.52 +    domain_type={}
    1.53 +    for domain in domains:
    1.54 +        domain_type[domain]=domain_types[i]
    1.55 +        i+=1
    1.56 +    return node+" [label=\"  "+node+"\",shapefile=\"shapes/all/"+domain_type[node]+".png\",fontcolor=navy,fontsize=14]"
    1.57 +
    1.58 +def graph():
    1.59 +    
    1.60 +    nodelist=""
    1.61 +    bridgelist=""
    1.62 +    linklist=""
    1.63 +    physicallist=""
    1.64 +    networklist=""
    1.65 +
    1.66 +    nodelist=";\n    ".join(map(graph_node,nodes))
    1.67 +    if nodelist: nodelist += ";"
    1.68 +
    1.69 +    bridgelist=";\n    ".join(bridges-set(hidden_bridges))
    1.70 +    if bridgelist: bridgelist += ";"
    1.71 +
    1.72 +    links=[]
    1.73 +    for host, bridges_raw in vbridges_table.iteritems():
    1.74 +        i=0
    1.75 +        for this_bridge in bridges_raw:
    1.76 +            if this_bridge in hidden_bridges:
    1.77 +                continue 
    1.78 +            if [ host, this_bridge ] in broken_links:
    1.79 +                links.append(host+" -- "+this_bridge+" [taillabel=\"fa"+str(i)+"/0\",style=dashed]")
    1.80 +            else:
    1.81 +                links.append(host+" -- "+this_bridge+" [taillabel=\"fa"+str(i)+"/0\"]")
    1.82 +            i+=1
    1.83 +
    1.84 +    for link in temporary_links:
    1.85 +        links.append(link[0]+" -- "+link[1]+" [color=blue,len=10,w=5,weight=5]")
    1.86 +
    1.87 +    linklist=";\n    ".join(links)
    1.88 +
    1.89 +    graph_dot = {
    1.90 +            'nodelist'      : nodelist,
    1.91 +            'bridgelist'    : bridgelist,
    1.92 +            'linklist'      : linklist,
    1.93 +            'physicallist'  : physicallist,
    1.94 +            'networklist'   : networklist,
    1.95 +          }
    1.96 +
    1.97 +    print """
    1.98 +    cat <<'GRAPH' > xenomips.dot
    1.99 +graph G {
   1.100 +    edge [len=1.25];
   1.101 +    splines=true;
   1.102 +// nodes
   1.103 +
   1.104 +    node [shape=plaintext,color=white,shapefile="shapes/cisco.bmp/router.png"];
   1.105 +    %(nodelist)s
   1.106 +
   1.107 +// bridges
   1.108 +
   1.109 +    node [shape=none,shapefile="shapes/cisco.bmp/switch.png"];
   1.110 +    %(bridgelist)s
   1.111 +
   1.112 +// physical
   1.113 +
   1.114 +    node [shape=rectangle,color=blue];
   1.115 +    %(physicallist)s
   1.116 +
   1.117 +// networks (not bridges, not physical)
   1.118 +    node [shape=rectangle,color=green];
   1.119 +    %(networklist)s
   1.120 +
   1.121 +// links (between nodes and bridges)
   1.122 +    %(linklist)s
   1.123 +
   1.124 +};
   1.125 +GRAPH
   1.126 +    neato -Tpng -o xenomips.png xenomips.dot 
   1.127 +""" % graph_dot
   1.128 +
   1.129 +def start_all():
   1.130 +        create_bridges_script()
   1.131 +        create_screens_script()
   1.132 +        create_domains_script()
   1.133 +        graph()
   1.134 +        print """
   1.135 +        cat <<NOTE_FOR_USER
   1.136 +# To view virtual network map, run:
   1.137 +    gqview xenomips.png
   1.138 +# To attach to VM consoles, run:
   1.139 +    screen -c screenrc
   1.140 +NOTE_FOR_USER
   1.141 +"""
   1.142 +
   1.143 +def shell():
   1.144 +    ipshell = IPShellEmbed()
   1.145 +    ipshell()
   1.146 +
   1.147 +def stop_all():
   1.148 +        destroy_domains_script(domains)
   1.149 +
   1.150 +def show_usage():
   1.151 +    print """Usage:
   1.152 +    xentaur {start|stop|start-bridges|start-domains|stop-domains|screen|graph}
   1.153 +"""
   1.154 +
   1.155 +#-----------------------------------------------------------------------
   1.156 +
   1.157 +def run(program, *args):
   1.158 +    pid = os.fork()
   1.159 +    if not pid:
   1.160 +        os.execvp(program, (program,) +  args)
   1.161 +    return os.wait()[0]
   1.162 +
   1.163 +def run_command(line)
   1.164 +    cmds=line.split()
   1.165 +    run(cmds[0],cmds[1:])
   1.166 +
   1.167 +def add_domain(name,type):
   1.168 +    domains.append(name)
   1.169 +    domain_types.append(type)
   1.170 +
   1.171 +def brake_link(domain,bridge):
   1.172 +    broken_links.append([domain,bridge])
   1.173 +
   1.174 +def type_to(screen,string)
   1.175 +    run_command("screen -X readreg p /tmp/xentaurbuf")
   1.176 +    run_command("screen -X select 1 ")
   1.177 +    run_command("screen -X paste p ")
   1.178 +    run_command("sleep 1")
   1.179 +    run_command("screen -X select 0")
   1.180 +
   1.181 +#-----------------------------------------------------------------------
   1.182 +
   1.183 +bridges=[]
   1.184 +for domain in vbridges_table.keys():
   1.185 +    bridges += set(vbridges_table[domain])
   1.186 +bridges=set(bridges)
   1.187 +
   1.188 +nodes=domains
   1.189 +
   1.190 +if len(sys.argv) > 1:
   1.191 +    if sys.argv[1] == 'start':
   1.192 +        start_all()
   1.193 +    if sys.argv[1] == 'stop':
   1.194 +        stop_all()
   1.195 +    if sys.argv[1] == 'start-bridges':
   1.196 +        create_bridges_script(bridges)
   1.197 +    if sys.argv[1] == 'start-domains':
   1.198 +        create_domains_script(domains)
   1.199 +    if sys.argv[1] == 'stop-domains':
   1.200 +        destroy_domains_script(domains)
   1.201 +    elif sys.argv[1] == 'screen':
   1.202 +        create_screens_script(domains)
   1.203 +    elif sys.argv[1] == 'graph':
   1.204 +        graph()
   1.205 +    elif sys.argv[1] == 'shell':
   1.206 +        shell()
   1.207 +
   1.208 +else:
   1.209 +    show_usage()
   1.210 +    sys.exit(1)
   1.211 +
   1.212 +sys.exit(0)
   1.213 +
   1.214 +