xentaur

annotate xentaur.py @ 12:54c51dea92a2

rerunning dynamips and vtysh after exit
author igor
date Fri Sep 14 12:04:44 2007 +0300 (2007-09-14)
parents f7fa09a390ed
children 3f704d1f0f85
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@11 8
igor@11 9 xen_config_name='fabergeN'
igor@11 10 from fabergeN import bridges,vbridges_table, hidden_bridges, domains, broken_links, temporary_links, domain_types
igor@11 11
igor@0 12 from IPython.Shell import IPShellEmbed
igor@0 13
igor@2 14
igor@2 15 screenrc=os.environ['HOME']+"/.screenrc_xentaur"
igor@2 16
igor@10 17 def run(program, *args):
igor@10 18 pid = os.fork()
igor@10 19 if not pid:
igor@10 20 os.execvp(program, (program,) + args)
igor@10 21 return os.wait()[0]
igor@10 22
igor@10 23 def run_command(line):
igor@10 24 #cmds=line.split()
igor@10 25 #run(cmds[0],*cmds[1:])
igor@10 26 run("/bin/sh", "-c", line)
igor@10 27
igor@0 28 def create_bridges_script():
igor@0 29 unbound_bridges=bridges
igor@4 30 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 31 create_unbound_bridges+="\n"+"\n".join(map(lambda x: "sudo /bin/ip link set "+x+" up", unbound_bridges))
igor@0 32
igor@0 33 print """#!/bin/sh
igor@0 34 # create unbound bridges
igor@0 35 %(create_unbound_bridges)s
igor@0 36 """ % {'create_unbound_bridges' : create_unbound_bridges}
igor@0 37
igor@0 38
igor@0 39 def create_domains_script():
igor@0 40 for N in range(len(domains)):
igor@11 41 print "sudo /usr/sbin/xm create "+xen_config_name+" 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 42
igor@0 43 def destroy_domains_script():
igor@0 44 for N in range(len(domains)):
igor@4 45 print "sudo /usr/sbin/xm shutdown "+domains[N]
igor@0 46
igor@0 47 def create_screens_script():
igor@0 48
igor@7 49 N=1
igor@0 50 screens=[]
igor@0 51 for domain in domains:
igor@11 52 screens.append("screen -t "+domain+" "+str(N)+" sh -c 'while true; do sudo xm console "+domain+" ; echo Retrying in 5 seconds...; sleep 5; done'")
igor@0 53 N+=1
igor@0 54 screenlist="\n".join(screens)
igor@0 55
igor@11 56 #
igor@11 57 # Previous terminal acccess commands:
igor@11 58 # ip="192.168.80."+str(200+N)
igor@11 59 # screens.append("screen -t "+domain+" "+str(N)+" sh -c 'while true; do ssh root@"+ip+" ; done'")
igor@11 60 #
igor@11 61
igor@4 62 #hardstatus string "\%{gk}\%c \%{yk}\%M\%d \%{wk}\%?\%-Lw\%?\%{bw}\%n*\%f\%t\%?(\%u)\%?\%{wk}\%?\%+Lw\%?"
igor@10 63
igor@11 64 f=open(screenrc, "w");
igor@10 65 f.write("""
igor@2 66 hardstatus on
igor@2 67 hardstatus alwayslastline
igor@2 68
igor@0 69 screen -t console 0 bash
igor@10 70 %s
igor@10 71 """ % (screenlist))
igor@10 72 f.close()
igor@0 73
igor@0 74 def graph_node(node):
igor@0 75 i=0
igor@0 76 domain_type={}
igor@0 77 for domain in domains:
igor@0 78 domain_type[domain]=domain_types[i]
igor@0 79 i+=1
igor@0 80 return node+" [label=\" "+node+"\",shapefile=\"shapes/all/"+domain_type[node]+".png\",fontcolor=navy,fontsize=14]"
igor@0 81
igor@0 82 def graph():
igor@0 83 nodelist=""
igor@0 84 bridgelist=""
igor@0 85 linklist=""
igor@0 86 physicallist=""
igor@0 87 networklist=""
igor@0 88
igor@0 89 nodelist=";\n ".join(map(graph_node,nodes))
igor@0 90 if nodelist: nodelist += ";"
igor@0 91
igor@5 92 bridgelist=";\n ".join(set(bridges)-set(hidden_bridges))
igor@0 93 if bridgelist: bridgelist += ";"
igor@0 94
igor@0 95 links=[]
igor@0 96 for host, bridges_raw in vbridges_table.iteritems():
igor@0 97 i=0
igor@0 98 for this_bridge in bridges_raw:
igor@0 99 if this_bridge in hidden_bridges:
igor@0 100 continue
igor@0 101 if [ host, this_bridge ] in broken_links:
igor@0 102 links.append(host+" -- "+this_bridge+" [taillabel=\"fa"+str(i)+"/0\",style=dashed]")
igor@0 103 else:
igor@0 104 links.append(host+" -- "+this_bridge+" [taillabel=\"fa"+str(i)+"/0\"]")
igor@0 105 i+=1
igor@0 106
igor@0 107 for link in temporary_links:
igor@0 108 links.append(link[0]+" -- "+link[1]+" [color=blue,len=10,w=5,weight=5]")
igor@0 109
igor@0 110 linklist=";\n ".join(links)
igor@0 111
igor@0 112 graph_dot = {
igor@0 113 'nodelist' : nodelist,
igor@0 114 'bridgelist' : bridgelist,
igor@0 115 'linklist' : linklist,
igor@0 116 'physicallist' : physicallist,
igor@0 117 'networklist' : networklist,
igor@0 118 }
igor@0 119
igor@10 120 f = open("xenomips.dot", "w");
igor@10 121 f.write ("""
igor@0 122 graph G {
igor@0 123 edge [len=1.25];
igor@0 124 splines=true;
igor@0 125 // nodes
igor@0 126
igor@0 127 node [shape=plaintext,color=white,shapefile="shapes/cisco.bmp/router.png"];
igor@0 128 %(nodelist)s
igor@0 129
igor@0 130 // bridges
igor@0 131
igor@6 132 node [shape=none,shapefile="shapes/all/switch.png"];
igor@0 133 %(bridgelist)s
igor@0 134
igor@0 135 // physical
igor@0 136
igor@0 137 node [shape=rectangle,color=blue];
igor@0 138 %(physicallist)s
igor@0 139
igor@0 140 // networks (not bridges, not physical)
igor@0 141 node [shape=rectangle,color=green];
igor@0 142 %(networklist)s
igor@0 143
igor@0 144 // links (between nodes and bridges)
igor@0 145 %(linklist)s
igor@0 146
igor@0 147 };
igor@10 148 """ % graph_dot)
igor@10 149 f.close()
igor@10 150 run_command("neato -Tpng -o xenomips.png xenomips.dot ")
igor@0 151
igor@0 152 def start_all():
igor@0 153 create_bridges_script()
igor@0 154 create_screens_script()
igor@0 155 create_domains_script()
igor@0 156 graph()
igor@0 157 print """
igor@0 158 cat <<NOTE_FOR_USER
igor@0 159 # To view virtual network map, run:
igor@0 160 gqview xenomips.png
igor@0 161 # To attach to VM consoles, run:
igor@0 162 screen -c screenrc
igor@0 163 NOTE_FOR_USER
igor@0 164 """
igor@0 165
igor@0 166 def shell():
igor@0 167 ipshell = IPShellEmbed()
igor@0 168 ipshell()
igor@0 169
igor@0 170 def stop_all():
igor@4 171 destroy_domains_script()
igor@0 172
igor@0 173 def show_usage():
igor@0 174 print """Usage:
igor@0 175 xentaur {start|stop|start-bridges|start-domains|stop-domains|screen|graph}
igor@0 176 """
igor@0 177
igor@0 178 #-----------------------------------------------------------------------
igor@0 179
igor@0 180
igor@0 181 def add_domain(name,type):
igor@0 182 domains.append(name)
igor@0 183 domain_types.append(type)
igor@0 184
igor@0 185 def brake_link(domain,bridge):
igor@0 186 broken_links.append([domain,bridge])
igor@0 187
igor@4 188 wt_timeout=0.5
igor@8 189 def write_to(screen,string,return_to_screen=""):
igor@2 190 """
igor@2 191 write_to(screen,string):
igor@2 192
igor@8 193 Type *string* to the specified screen(s).
igor@8 194 Screen may be specified with the number *screen*,
igor@8 195 with array of numbers,
igor@8 196 with array of names.
igor@2 197
igor@2 198 """
igor@5 199 screen_numbers=[] # number of the screens to write to
igor@5 200 if type(screen) == list:
igor@5 201 screen_numbers=map(lambda x: domains.index(x)+1, screen)
igor@5 202 elif type(screen) == int:
igor@5 203 screen_numbers=[screen]
igor@5 204 else:
igor@5 205 screen_numbers=[domains.index(screen)+1]
igor@5 206
igor@5 207 for screen_number in screen_numbers:
igor@5 208 run_command("screen -X select "+str(screen_number))
igor@5 209 time.sleep(wt_timeout)
igor@5 210 for line in string.splitlines():
igor@5 211 f=open('/tmp/xentaurbuf', 'w')
igor@5 212 f.write(line+"\n")
igor@5 213 f.close()
igor@5 214 run_command("screen -X readreg p /tmp/xentaurbuf")
igor@5 215 time.sleep(wt_timeout)
igor@5 216 run_command("nohup screen -X paste p >& /dev/null")
igor@5 217 time.sleep(wt_timeout)
igor@5 218
igor@8 219 if return_to_screen != "":
igor@8 220 run_command("screen -X select %s" % (return_to_screen))
igor@8 221 time.sleep(wt_timeout)
igor@0 222
igor@0 223 #-----------------------------------------------------------------------
igor@0 224
igor@5 225 cisco_fa01_up="""
igor@5 226 ena
igor@5 227 conf t
igor@5 228 int fa0/0
igor@5 229 no shutdown
igor@5 230 exit
igor@5 231 int fa1/0
igor@5 232 no shutdown
igor@5 233 exit
igor@5 234 exit
igor@5 235 exit
igor@5 236 """
igor@5 237
igor@5 238 cisco_set_ip_on_int="""
igor@5 239 interface fa%s/0
igor@5 240 no ip address
igor@5 241 ip address %s 255.255.255.0
igor@5 242 exit
igor@5 243 """
igor@5 244
igor@5 245 #-----------------------------------------------------------------------
igor@0 246
igor@0 247 nodes=domains
igor@0 248
igor@0 249 if len(sys.argv) > 1:
igor@0 250 if sys.argv[1] == 'start':
igor@0 251 start_all()
igor@0 252 if sys.argv[1] == 'stop':
igor@0 253 stop_all()
igor@0 254 if sys.argv[1] == 'start-bridges':
igor@3 255 create_bridges_script()
igor@0 256 if sys.argv[1] == 'start-domains':
igor@3 257 create_domains_script()
igor@0 258 if sys.argv[1] == 'stop-domains':
igor@3 259 destroy_domains_script()
igor@0 260 elif sys.argv[1] == 'screen':
igor@3 261 create_screens_script()
igor@0 262 elif sys.argv[1] == 'graph':
igor@0 263 graph()
igor@0 264 elif sys.argv[1] == 'shell':
igor@0 265 shell()
igor@0 266
igor@0 267 else:
igor@0 268 show_usage()
igor@0 269 sys.exit(1)
igor@0 270
igor@0 271 sys.exit(0)
igor@0 272
igor@0 273