xentaur

diff xentaur.py @ 66:aaf034af3a35

Merge of Xgurulla into Xentaur code. Not completed yet!!!

Now Xentaur can work with Amazon EC2,
but only with. Local domains management
is switched off temporarily.
author Igor Chubin <igor@chub.in>
date Sat Jan 09 20:20:08 2010 +0200 (2010-01-09)
parents cf823d27b029
children 6c145935ece5
line diff
     1.1 --- a/xentaur.py	Mon Oct 12 00:12:11 2009 +0300
     1.2 +++ b/xentaur.py	Sat Jan 09 20:20:08 2010 +0200
     1.3 @@ -2,32 +2,34 @@
     1.4  # vim: set fileencoding=utf-8 :
     1.5  
     1.6  import sys,os,time
     1.7 +from IPython.Shell import IPShellEmbed
     1.8  
     1.9 -xentaur_path="/xentaur/xentaur"
    1.10 +path_xentaur="%s/hg/xentaur" % os.environ['HOME']
    1.11 +path_shapes=path_xentaur+'/shapes'
    1.12 +path_scripts=path_xentaur+'/files'
    1.13 +
    1.14 +network='net1'
    1.15 +
    1.16 +path_network=os.environ['HOME']+"/.xentaur/"+network
    1.17 +if not os.path.exists(path_network):
    1.18 +    os.makedirs(path_network)
    1.19 +screenrc=path_network+"/.screenrc_xentaur"
    1.20  
    1.21  sys.path.append('/etc/xen')
    1.22 -sys.path.append(xentaur_path)
    1.23 +sys.path.append(path_xentaur)
    1.24 +sys.path.append(path_network)
    1.25  sys.path.append('.')
    1.26  
    1.27  node_object={}
    1.28  link_object={}
    1.29  bridge_object={}
    1.30 +ec2_node={}
    1.31  
    1.32 -network='multicast'
    1.33  domain='dyn1'
    1.34 -
    1.35 -#network='snrs'
    1.36 -#domain='dyn1'
    1.37  from xendomain import *
    1.38  
    1.39  bridges_turned_down=[]
    1.40  
    1.41 -from IPython.Shell import IPShellEmbed
    1.42 -
    1.43 -
    1.44 -screenrc=".screenrc_xentaur_"+network
    1.45 -path_shapes='/xentaur/xentaur/shapes'
    1.46 -
    1.47  def run(program, *args):
    1.48      pid = os.fork()
    1.49      if not pid:
    1.50 @@ -63,7 +65,7 @@
    1.51  """ % (script)
    1.52  
    1.53  def start_domain(domain):
    1.54 -    print "sudo xm create "+xentaur_path+"/xendomain.py "+" domain="+domain+" network="+network+" && sleep 1 && sudo xm sched-credit -d $(sudo xm list | grep "+domain+" | awk '{print $2}') -c 10 && sleep 1"
    1.55 +    print "sudo xm create "+path_xentaur+"/xendomain.py "+" domain="+domain+" network="+network+" && sleep 1 && sudo xm sched-credit -d $(sudo xm list | grep "+domain+" | awk '{print $2}') -c 10 && sleep 1"
    1.56  
    1.57  def start_domains(doms=domains):
    1.58      for domain in doms:
    1.59 @@ -73,9 +75,56 @@
    1.60  def start_all():
    1.61      graph()
    1.62      screen()
    1.63 +    html()
    1.64      start_bridges()
    1.65      start_domains()
    1.66  
    1.67 +def ec2_assign_nodes_to_instances():
    1.68 +    domain_number=0
    1.69 +    for dom in domains:
    1.70 +        node_name=network+"-node%s"%(domain_number/2)
    1.71 +        ec2_node[dom]=node_name
    1.72 +        domain_number+=1
    1.73 +
    1.74 +def make_start_emulators():
    1.75 +    s=""
    1.76 +    #s+="cat screenrc_template > screenrc_$NETWORK\n"
    1.77 +    #s+="echo \"screen -t console 0 sh -c 'cd ~/xentaur; ./xentaur.py shell'\" >> screenrc_$NETWORK\n"
    1.78 +    ios="/mnt/ios/C7200-AD.BIN"
    1.79 +    ec2_assign_nodes_to_instances()
    1.80 +    for dom in domains:
    1.81 +        node_name=ec2_node[dom]
    1.82 +        i=0
    1.83 +        line='dynamips '+ios
    1.84 +        line2=''
    1.85 +        for iface in vbridges_table[dom]:
    1.86 +            bridge=vbridges_table[dom][i]
    1.87 +            if i>0:
    1.88 +                line += " -p %s:PA-FE-TX "%i
    1.89 +            line += " -s %s:0:tap:%s_%s "% (i,dom,iface)
    1.90 +            line2 += "sleep 2; brctl addif %s %s; "%(bridge, dom+"_"+iface)
    1.91 +            line2 +='ifconfig %s up; ifconfig %s promisc ;' %(bridge, bridge)
    1.92 +            line2 += "ifconfig %s_%s up; "%(dom, iface)
    1.93 +            i+=1
    1.94 +        s += "ssh %s \"mkdir -p /mnt/%s; cd /mnt/%s; screen -S %s- -d -m %s\"\n"%(node_name,dom,dom,dom,line)
    1.95 +        s += "ssh %s \"%s\"\n" % (node_name, line2)
    1.96 +        #s += "echo \"screen %s ssh -t %s 'screen -r %s-'\" >> screenrc_%s\n" % (domain_number, node_name, dom, network)
    1.97 +        #s += "echo \"screen -t %s %s connect-ec2-session INSTANCE EC2_NODE %s %s %s\" >> screenrc_$NETWORK\n" % (dom, domain_number+1, node_name, network, dom)
    1.98 +    return s
    1.99 +
   1.100 +def make_start_instances_config(net=network):
   1.101 +    f = open(path_network+"/start-instances-"+network, "w");
   1.102 +    f.write("INSTANCES_NUMBER=%s\n"%((len(domains)+1)/2))
   1.103 +    f.write("start_emulators()\n{\n%s\n}\n"%make_start_emulators())
   1.104 +    f.close()
   1.105 +
   1.106 +def start_network(net=network):
   1.107 +    make_start_instances_config()
   1.108 +    #run_command("cd %s; env NETWORK=%s sh %s instances start" % (path_network, network, path_scrips+"/ec2-instances"))
   1.109 +
   1.110 +def stop_network(net=network):
   1.111 +    run_command("cd %s; env NETWORK=%s sh instances stop" % (path_network, network))
   1.112 +
   1.113  ## Stop
   1.114  
   1.115  def stop_domain(domain,wait=0):
   1.116 @@ -165,8 +214,12 @@
   1.117      wait_seconds=1
   1.118      screens=[]
   1.119      for domain in domains:
   1.120 -        screens.append("screen -t %s %s sh -c 'while true; do %s ; echo Retrying in %s secods...; sleep %s ; clear; done'" %
   1.121 -                                 (domain,domains.index(domain)+1,node_object[domain].console_string(),wait_seconds,wait_seconds))
   1.122 +        screens.append("screen -t %(domain)s %(domain_number)s %(console_string)s" % {
   1.123 +            'domain' : domain,
   1.124 +            'domain_number' : domains.index(domain)+1,
   1.125 +            'console_string': node_object[domain].console_string(), 
   1.126 +            'wait_interval' : wait_seconds } )
   1.127 +
   1.128      screenlist="\n".join(screens)
   1.129  
   1.130      hardstatus='hardstatus string "%{rk}Xentaur%{bk}@%H %{gk}%c %{yk}%d.%m %{wk}%?%-Lw%?%{bw}%n*%f%t%?(%u)%?%{wk}%?%+Lw%?"'
   1.131 @@ -179,7 +232,7 @@
   1.132  
   1.133  screen -t console 0 sh -c 'while true; do %s/xentaur.py shell ; echo Retrying in %s secods...; sleep %s ; clear; done'
   1.134  %s
   1.135 -""" % (hardstatus,xentaur_path,wait_seconds,wait_seconds,screenlist))
   1.136 +""" % (hardstatus,path_xentaur,wait_seconds,wait_seconds,screenlist))
   1.137      f.close()
   1.138      print "# GNU Screen config file is written to: %s" % screenrc
   1.139  
   1.140 @@ -197,7 +250,7 @@
   1.141      linklist=";\n    ".join(map(lambda link: link_object[link].graphviz_string(),link_object.keys()))
   1.142      if linklist: linklist += ";"
   1.143  
   1.144 -    f = open(network+".dot", "w");
   1.145 +    f = open(path_network+"/"+network+".dot", "w");
   1.146      f.write ("""
   1.147  graph G {
   1.148      edge [len=1.25];
   1.149 @@ -222,18 +275,40 @@
   1.150  };
   1.151  """ % (nodelist, bridgelist, physicallist, networklist, linklist))
   1.152      f.close()
   1.153 -    run_command("neato -Tpng -o %s.png %s.dot "%(network,network))
   1.154 -    run_command("neato -Tjpg -o %s.jpg %s.dot "%(network,network))
   1.155 -    run_command("neato -Tsvg -o %s.svg %s.dot "%(network,network))
   1.156 -    run_command("neato -Tcmapx -o %s.cmapx -NURL=http://google.com %s.dot "%(network,network))
   1.157 -    print "# Network map is written to files: %s.{png,svg,jpg,dot}" % network
   1.158 +    run_command("neato -Tpng -o %s.png %s.dot "%(path_network+"/"+network,path_network+"/"+network))
   1.159 +    run_command("neato -Tjpg -o %s.jpg %s.dot "%(path_network+"/"+network,path_network+"/"+network))
   1.160 +    run_command("neato -Tsvg -o %s.svg %s.dot "%(path_network+"/"+network,path_network+"/"+network))
   1.161 +    #run_command("neato -Tcmapx -o %s.cmapx -NURL=http://google.com %s.dot "%(path_network+"/"+network,path_network+"/"+network))
   1.162 +    print "# Network map is written to files: %s.{png,svg,jpg,dot}" % (path_network+"/"+network)
   1.163 +
   1.164 +def html():
   1.165 +    f = open(path_network+"/index.html", "w");
   1.166 +    f.write ("""
   1.167 +<html>
   1.168 +<head>
   1.169 +<title>Network %s map</network>
   1.170 +<body>
   1.171 +<img src="%s.png" /><br/>
   1.172 +<pre>
   1.173 +nodes=%s
   1.174 +bridges=%s
   1.175 +vbridges_table=%s
   1.176 +</pre>
   1.177 +</body>
   1.178 +</head>
   1.179 +</html>
   1.180 +""" % (network, network, domains, bridges, vbridges_table))
   1.181 +    f.close()
   1.182 +    #run_command("cp %s.html /var/www/ec2/network/%s/index.html"%(network,network))
   1.183  
   1.184  def autoredraw():
   1.185      graph()
   1.186 +    html()
   1.187 +    screen()
   1.188  
   1.189  def shell():
   1.190      autoredraw()
   1.191 -    ipshell = IPShellEmbed()
   1.192 +    ipshell = IPShellEmbed(['-noconfirm_exit'])
   1.193      ipshell()
   1.194  
   1.195  def version():
   1.196 @@ -247,21 +322,6 @@
   1.197    ' / \     |
   1.198  
   1.199      """
   1.200 -#    print "Xentaur 0.1-PRE"
   1.201 -#    print "(Godzilla-mutant)  _"
   1.202 -#    print "                 / * \\"
   1.203 -#    print "                /   .-"
   1.204 -#    print "               /   |"
   1.205 -#    print "              | \\ \\\\ \\"
   1.206 -#    print "     _ -------|  \\ \\\\ \\"
   1.207 -#    print " / /              \\_\\ -"
   1.208 -#    print "/ |\\           |   |"
   1.209 -#    print "| |  \\ .-----. | \\ |"
   1.210 -#    print "  |  /        \\ \\ \\ \\"
   1.211 -#    print "  \\/|.\\        \\ \\ \\ \\"
   1.212 -#    print "   \\| - .       \\_\\ \\_\\"
   1.213 -#    print "-----------------------------------------------"
   1.214 -
   1.215  
   1.216  def info():
   1.217      version()
   1.218 @@ -364,6 +424,11 @@
   1.219          return self.name+" [color=white,shape=plaintext,label=\"  "+self.name+"\",shapefile=\""+path_shapes+"/all/"+\
   1.220          domain_types[domains.index(self.name)]+".png\",fontcolor=black,fontsize=16,target=\"http://google.com\"]"
   1.221      def console_string(self):
   1.222 +        ec2=True # FIXME
   1.223 +        ec2_assign_nodes_to_instances()
   1.224 +        if ec2:
   1.225 +            return path_scripts+"/node-terminal-session INSTANCE EC2_NODE %s %s %s"%(ec2_node[self.name], network, self.name)
   1.226 +        
   1.227          if self.type in [ 'quagga', 'dynamips', 'freebsd', 'linux' ]:
   1.228              return "sudo xm console "+self.name
   1.229          elif self.name in real_bridges or self.name in real_nodes:
   1.230 @@ -396,7 +461,7 @@
   1.231              return self.node+" -- "+self.bridge+" [taillabel=\"fa"+str(self.interface)+"/0\",style=dashed]"
   1.232  
   1.233          ip="\\n.%s.%s" % (bridges.index(self.bridge)+1, domains.index(self.node)+1) 
   1.234 -        if domain_types[domains.index(self.node)] == 'xenomips':
   1.235 +        if domain_types[domains.index(self.node)] == 'dynamips':
   1.236              int_name="fa"+str(self.interface)+"/0"
   1.237          else:
   1.238              int_name="eth"+str(self.interface)
   1.239 @@ -538,6 +603,7 @@
   1.240      cisco_set_ip_on_int="""
   1.241  \n\n\n
   1.242  int fa%s/0
   1.243 +duplex full
   1.244  no ip address
   1.245  ip address %s 255.255.255.0
   1.246  no shutdown
   1.247 @@ -651,7 +717,7 @@
   1.248      return 0
   1.249  
   1.250  def configure_no_cdp_log_mismatch_duplex(doms=domains):
   1.251 -    for dom in filter_by_type(domains,'xenomips'):
   1.252 +    for dom in filter_by_type(domains,'dynamips'):
   1.253          write_to(dom,"\n\nena\nconf t\nno cdp log mismatch duplex\nend\n")
   1.254  
   1.255  def configure_save(doms=domains):