xentaur

annotate xentaur.py @ 0:ab37f0ad70e5

Initial revision
author igor
date Fri Jul 13 16:57:33 2007 +0300 (2007-07-13)
parents
children e34dd9ee49e5
rev   line source
igor@0 1 #!/usr/bin/python
igor@0 2
igor@0 3
igor@0 4 import sys
igor@0 5 import xenomips_vars
igor@0 6 xenomips_vars.N='1'
igor@0 7 sys.path.append('/etc/xen')
igor@0 8 from xenomipsN import vbridges_table, hidden_bridges, domains, broken_links, temporary_links, domain_types
igor@0 9 from IPython.Shell import IPShellEmbed
igor@0 10
igor@0 11 def create_bridges_script():
igor@0 12 unbound_bridges=bridges
igor@0 13 create_unbound_bridges="\n".join(map(lambda x: "brctl show | awk '{print $1}' | grep -q "+x+" || brctl addbr "+x, unbound_bridges))
igor@0 14 create_unbound_bridges+="\n"+"\n".join(map(lambda x: "ip link set "+x+" up", unbound_bridges))
igor@0 15
igor@0 16 print """#!/bin/sh
igor@0 17 # create unbound bridges
igor@0 18 %(create_unbound_bridges)s
igor@0 19 """ % {'create_unbound_bridges' : create_unbound_bridges}
igor@0 20
igor@0 21
igor@0 22 def create_domains_script():
igor@0 23 for N in range(len(domains)):
igor@0 24 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 25
igor@0 26 def destroy_domains_script():
igor@0 27 for N in range(len(domains)):
igor@0 28 print "xm shutdown "+domains[N]
igor@0 29
igor@0 30 def create_screens_script():
igor@0 31
igor@0 32 N=0
igor@0 33 screens=[]
igor@0 34 for domain in domains:
igor@0 35 ip="192.168.80."+str(200+N)
igor@0 36 screens.append("screen -t "+domain+" "+str(N)+" sh -c 'while true; do ssh root@"+ip+" ; done'")
igor@0 37 N+=1
igor@0 38 screenlist="\n".join(screens)
igor@0 39
igor@0 40 print """
igor@0 41 cat <<SCREENRC > screenrc
igor@0 42 screen -t console 0 bash
igor@0 43 %(screenlist)s
igor@0 44 SCREENRC
igor@0 45 """ % { 'screenlist' : screenlist }
igor@0 46
igor@0 47 def graph_node(node):
igor@0 48 i=0
igor@0 49 domain_type={}
igor@0 50 for domain in domains:
igor@0 51 domain_type[domain]=domain_types[i]
igor@0 52 i+=1
igor@0 53 return node+" [label=\" "+node+"\",shapefile=\"shapes/all/"+domain_type[node]+".png\",fontcolor=navy,fontsize=14]"
igor@0 54
igor@0 55 def graph():
igor@0 56
igor@0 57 nodelist=""
igor@0 58 bridgelist=""
igor@0 59 linklist=""
igor@0 60 physicallist=""
igor@0 61 networklist=""
igor@0 62
igor@0 63 nodelist=";\n ".join(map(graph_node,nodes))
igor@0 64 if nodelist: nodelist += ";"
igor@0 65
igor@0 66 bridgelist=";\n ".join(bridges-set(hidden_bridges))
igor@0 67 if bridgelist: bridgelist += ";"
igor@0 68
igor@0 69 links=[]
igor@0 70 for host, bridges_raw in vbridges_table.iteritems():
igor@0 71 i=0
igor@0 72 for this_bridge in bridges_raw:
igor@0 73 if this_bridge in hidden_bridges:
igor@0 74 continue
igor@0 75 if [ host, this_bridge ] in broken_links:
igor@0 76 links.append(host+" -- "+this_bridge+" [taillabel=\"fa"+str(i)+"/0\",style=dashed]")
igor@0 77 else:
igor@0 78 links.append(host+" -- "+this_bridge+" [taillabel=\"fa"+str(i)+"/0\"]")
igor@0 79 i+=1
igor@0 80
igor@0 81 for link in temporary_links:
igor@0 82 links.append(link[0]+" -- "+link[1]+" [color=blue,len=10,w=5,weight=5]")
igor@0 83
igor@0 84 linklist=";\n ".join(links)
igor@0 85
igor@0 86 graph_dot = {
igor@0 87 'nodelist' : nodelist,
igor@0 88 'bridgelist' : bridgelist,
igor@0 89 'linklist' : linklist,
igor@0 90 'physicallist' : physicallist,
igor@0 91 'networklist' : networklist,
igor@0 92 }
igor@0 93
igor@0 94 print """
igor@0 95 cat <<'GRAPH' > xenomips.dot
igor@0 96 graph G {
igor@0 97 edge [len=1.25];
igor@0 98 splines=true;
igor@0 99 // nodes
igor@0 100
igor@0 101 node [shape=plaintext,color=white,shapefile="shapes/cisco.bmp/router.png"];
igor@0 102 %(nodelist)s
igor@0 103
igor@0 104 // bridges
igor@0 105
igor@0 106 node [shape=none,shapefile="shapes/cisco.bmp/switch.png"];
igor@0 107 %(bridgelist)s
igor@0 108
igor@0 109 // physical
igor@0 110
igor@0 111 node [shape=rectangle,color=blue];
igor@0 112 %(physicallist)s
igor@0 113
igor@0 114 // networks (not bridges, not physical)
igor@0 115 node [shape=rectangle,color=green];
igor@0 116 %(networklist)s
igor@0 117
igor@0 118 // links (between nodes and bridges)
igor@0 119 %(linklist)s
igor@0 120
igor@0 121 };
igor@0 122 GRAPH
igor@0 123 neato -Tpng -o xenomips.png xenomips.dot
igor@0 124 """ % graph_dot
igor@0 125
igor@0 126 def start_all():
igor@0 127 create_bridges_script()
igor@0 128 create_screens_script()
igor@0 129 create_domains_script()
igor@0 130 graph()
igor@0 131 print """
igor@0 132 cat <<NOTE_FOR_USER
igor@0 133 # To view virtual network map, run:
igor@0 134 gqview xenomips.png
igor@0 135 # To attach to VM consoles, run:
igor@0 136 screen -c screenrc
igor@0 137 NOTE_FOR_USER
igor@0 138 """
igor@0 139
igor@0 140 def shell():
igor@0 141 ipshell = IPShellEmbed()
igor@0 142 ipshell()
igor@0 143
igor@0 144 def stop_all():
igor@0 145 destroy_domains_script(domains)
igor@0 146
igor@0 147 def show_usage():
igor@0 148 print """Usage:
igor@0 149 xentaur {start|stop|start-bridges|start-domains|stop-domains|screen|graph}
igor@0 150 """
igor@0 151
igor@0 152 #-----------------------------------------------------------------------
igor@0 153
igor@0 154 def run(program, *args):
igor@0 155 pid = os.fork()
igor@0 156 if not pid:
igor@0 157 os.execvp(program, (program,) + args)
igor@0 158 return os.wait()[0]
igor@0 159
igor@0 160 def run_command(line)
igor@0 161 cmds=line.split()
igor@0 162 run(cmds[0],cmds[1:])
igor@0 163
igor@0 164 def add_domain(name,type):
igor@0 165 domains.append(name)
igor@0 166 domain_types.append(type)
igor@0 167
igor@0 168 def brake_link(domain,bridge):
igor@0 169 broken_links.append([domain,bridge])
igor@0 170
igor@0 171 def type_to(screen,string)
igor@0 172 run_command("screen -X readreg p /tmp/xentaurbuf")
igor@0 173 run_command("screen -X select 1 ")
igor@0 174 run_command("screen -X paste p ")
igor@0 175 run_command("sleep 1")
igor@0 176 run_command("screen -X select 0")
igor@0 177
igor@0 178 #-----------------------------------------------------------------------
igor@0 179
igor@0 180 bridges=[]
igor@0 181 for domain in vbridges_table.keys():
igor@0 182 bridges += set(vbridges_table[domain])
igor@0 183 bridges=set(bridges)
igor@0 184
igor@0 185 nodes=domains
igor@0 186
igor@0 187 if len(sys.argv) > 1:
igor@0 188 if sys.argv[1] == 'start':
igor@0 189 start_all()
igor@0 190 if sys.argv[1] == 'stop':
igor@0 191 stop_all()
igor@0 192 if sys.argv[1] == 'start-bridges':
igor@0 193 create_bridges_script(bridges)
igor@0 194 if sys.argv[1] == 'start-domains':
igor@0 195 create_domains_script(domains)
igor@0 196 if sys.argv[1] == 'stop-domains':
igor@0 197 destroy_domains_script(domains)
igor@0 198 elif sys.argv[1] == 'screen':
igor@0 199 create_screens_script(domains)
igor@0 200 elif sys.argv[1] == 'graph':
igor@0 201 graph()
igor@0 202 elif sys.argv[1] == 'shell':
igor@0 203 shell()
igor@0 204
igor@0 205 else:
igor@0 206 show_usage()
igor@0 207 sys.exit(1)
igor@0 208
igor@0 209 sys.exit(0)
igor@0 210
igor@0 211