xentaur

annotate xentaur.py @ 5:83a41d802a52

function write_to has improved.

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