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