xentaur

annotate xentaur.py @ 3:2c6cfa822bd8

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