xentaur

annotate xentaur.py @ 22:c4a092e32ee1

bridge up and down
author igor
date Fri Sep 21 13:16:28 2007 +0300 (2007-09-21)
parents 3f704d1f0f85
children 6bb2a29e91be
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@21 10 exec 'from '+xen_config_name+' 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@22 178
igor@22 179 #-----------------------------------------------------------------------
igor@22 180 # BRIDGES and IFACES
igor@22 181
igor@22 182 def bridge_down(bridge):
igor@22 183 """
igor@22 184 Turn the bridge <bridge> down
igor@22 185 """
igor@22 186 run_command("sudo ip link set %s down" % bridge)
igor@22 187
igor@22 188 def bridge_up(bridge):
igor@22 189 """
igor@22 190 Turn the bridge <bridge> up
igor@22 191 """
igor@22 192 run_command("sudo ip link set %s up" % bridge)
igor@22 193
igor@22 194 def show_bridge(bridge):
igor@22 195 """
igor@22 196 Show the state of the bridge <bridge>
igor@22 197 """
igor@22 198 run_command("sudo ip link show %s" % bridge)
igor@22 199
igor@0 200 #-----------------------------------------------------------------------
igor@0 201
igor@0 202
igor@0 203 def add_domain(name,type):
igor@0 204 domains.append(name)
igor@0 205 domain_types.append(type)
igor@0 206
igor@0 207 def brake_link(domain,bridge):
igor@0 208 broken_links.append([domain,bridge])
igor@0 209
igor@4 210 wt_timeout=0.5
igor@8 211 def write_to(screen,string,return_to_screen=""):
igor@2 212 """
igor@2 213 write_to(screen,string):
igor@2 214
igor@8 215 Type *string* to the specified screen(s).
igor@8 216 Screen may be specified with the number *screen*,
igor@8 217 with array of numbers,
igor@8 218 with array of names.
igor@2 219
igor@2 220 """
igor@5 221 screen_numbers=[] # number of the screens to write to
igor@5 222 if type(screen) == list:
igor@5 223 screen_numbers=map(lambda x: domains.index(x)+1, screen)
igor@5 224 elif type(screen) == int:
igor@5 225 screen_numbers=[screen]
igor@5 226 else:
igor@5 227 screen_numbers=[domains.index(screen)+1]
igor@5 228
igor@5 229 for screen_number in screen_numbers:
igor@5 230 run_command("screen -X select "+str(screen_number))
igor@5 231 time.sleep(wt_timeout)
igor@5 232 for line in string.splitlines():
igor@5 233 f=open('/tmp/xentaurbuf', 'w')
igor@5 234 f.write(line+"\n")
igor@5 235 f.close()
igor@5 236 run_command("screen -X readreg p /tmp/xentaurbuf")
igor@5 237 time.sleep(wt_timeout)
igor@5 238 run_command("nohup screen -X paste p >& /dev/null")
igor@5 239 time.sleep(wt_timeout)
igor@5 240
igor@8 241 if return_to_screen != "":
igor@8 242 run_command("screen -X select %s" % (return_to_screen))
igor@8 243 time.sleep(wt_timeout)
igor@0 244
igor@0 245 #-----------------------------------------------------------------------
igor@0 246
igor@5 247 cisco_fa01_up="""
igor@5 248 ena
igor@5 249 conf t
igor@5 250 int fa0/0
igor@5 251 no shutdown
igor@5 252 exit
igor@5 253 int fa1/0
igor@5 254 no shutdown
igor@5 255 exit
igor@5 256 exit
igor@5 257 exit
igor@5 258 """
igor@5 259
igor@5 260 cisco_set_ip_on_int="""
igor@5 261 interface fa%s/0
igor@5 262 no ip address
igor@5 263 ip address %s 255.255.255.0
igor@5 264 exit
igor@5 265 """
igor@5 266
igor@5 267 #-----------------------------------------------------------------------
igor@0 268
igor@0 269 nodes=domains
igor@0 270
igor@0 271 if len(sys.argv) > 1:
igor@0 272 if sys.argv[1] == 'start':
igor@0 273 start_all()
igor@0 274 if sys.argv[1] == 'stop':
igor@0 275 stop_all()
igor@0 276 if sys.argv[1] == 'start-bridges':
igor@3 277 create_bridges_script()
igor@0 278 if sys.argv[1] == 'start-domains':
igor@3 279 create_domains_script()
igor@0 280 if sys.argv[1] == 'stop-domains':
igor@3 281 destroy_domains_script()
igor@0 282 elif sys.argv[1] == 'screen':
igor@3 283 create_screens_script()
igor@0 284 elif sys.argv[1] == 'graph':
igor@0 285 graph()
igor@0 286 elif sys.argv[1] == 'shell':
igor@0 287 shell()
igor@0 288
igor@0 289 else:
igor@0 290 show_usage()
igor@0 291 sys.exit(1)
igor@0 292
igor@0 293 sys.exit(0)
igor@0 294
igor@0 295