xentaur

diff xentaur.py @ 67:6c145935ece5

Fixed path scripts and configuration templates moved to a single file.
author Igor Chubin <igor@chub.in>
date Mon Jan 11 13:01:35 2010 +0200 (2010-01-11)
parents aaf034af3a35
children f652fab38c7a
line diff
     1.1 --- a/xentaur.py	Sat Jan 09 20:20:08 2010 +0200
     1.2 +++ b/xentaur.py	Mon Jan 11 13:01:35 2010 +0200
     1.3 @@ -20,14 +20,21 @@
     1.4  sys.path.append(path_network)
     1.5  sys.path.append('.')
     1.6  
     1.7 +
     1.8  node_object={}
     1.9  link_object={}
    1.10  bridge_object={}
    1.11  ec2_node={}
    1.12  
    1.13 +# ec2 settings
    1.14 +ec2_dns_domain='ec2.xgu.ru'
    1.15 +ec2_zone="us-east-1a"
    1.16 +
    1.17  domain='dyn1'
    1.18  from xendomain import *
    1.19  
    1.20 +execfile(path_xentaur+"/configuration_templates.py")
    1.21 +
    1.22  bridges_turned_down=[]
    1.23  
    1.24  def run(program, *args):
    1.25 @@ -82,14 +89,12 @@
    1.26  def ec2_assign_nodes_to_instances():
    1.27      domain_number=0
    1.28      for dom in domains:
    1.29 -        node_name=network+"-node%s"%(domain_number/2)
    1.30 -        ec2_node[dom]=node_name
    1.31 +        node_name=network+"-node%s"%(domain_number/3)
    1.32 +        ec2_node[dom]=node_name+"."+ec2_dns_domain
    1.33          domain_number+=1
    1.34  
    1.35  def make_start_emulators():
    1.36      s=""
    1.37 -    #s+="cat screenrc_template > screenrc_$NETWORK\n"
    1.38 -    #s+="echo \"screen -t console 0 sh -c 'cd ~/xentaur; ./xentaur.py shell'\" >> screenrc_$NETWORK\n"
    1.39      ios="/mnt/ios/C7200-AD.BIN"
    1.40      ec2_assign_nodes_to_instances()
    1.41      for dom in domains:
    1.42 @@ -108,22 +113,38 @@
    1.43              i+=1
    1.44          s += "ssh %s \"mkdir -p /mnt/%s; cd /mnt/%s; screen -S %s- -d -m %s\"\n"%(node_name,dom,dom,dom,line)
    1.45          s += "ssh %s \"%s\"\n" % (node_name, line2)
    1.46 -        #s += "echo \"screen %s ssh -t %s 'screen -r %s-'\" >> screenrc_%s\n" % (domain_number, node_name, dom, network)
    1.47 -        #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.48      return s
    1.49  
    1.50  def make_start_instances_config(net=network):
    1.51      f = open(path_network+"/start-instances-"+network, "w");
    1.52 -    f.write("INSTANCES_NUMBER=%s\n"%((len(domains)+1)/2))
    1.53 +    f.write("""
    1.54 +NETWORK=%(network)s
    1.55 +INSTANCES_NUMBER=%(instances_number)s
    1.56 +INSTANCE_AMI=ami-b21ff8db
    1.57 +INSTANCE_AMI=ami-7cfd1a15
    1.58 +VOLUME_NAME=vol-28d13141
    1.59 +SSH_SECRET_KEY=~/.ec2/id_rsa-pstam-keypair
    1.60 +SSH_KEYPAIR=pstam-keypair
    1.61 +EC2_ZONE=%(ec2_zone)s
    1.62 +DOMAIN=%(ec2_dns_domain)s
    1.63 +SCRIPTS_PATH=%(path_scripts)s
    1.64 +""" % {
    1.65 +        "network"           : network,
    1.66 +        "instances_number"  : len(set(ec2_node.values())),
    1.67 +        "path_scripts"      : path_scripts,
    1.68 +
    1.69 +        "ec2_dns_domain"    : ec2_dns_domain,
    1.70 +        "ec2_zone"          : ec2_zone,
    1.71 +    })
    1.72      f.write("start_emulators()\n{\n%s\n}\n"%make_start_emulators())
    1.73      f.close()
    1.74  
    1.75  def start_network(net=network):
    1.76      make_start_instances_config()
    1.77 -    #run_command("cd %s; env NETWORK=%s sh %s instances start" % (path_network, network, path_scrips+"/ec2-instances"))
    1.78 +    run_command("cd %s; env NETWORK=%s sh %s start" % (path_network, network, path_scripts+"/ec2-instances"))
    1.79  
    1.80  def stop_network(net=network):
    1.81 -    run_command("cd %s; env NETWORK=%s sh instances stop" % (path_network, network))
    1.82 +    run_command("cd %s; env NETWORK=%s sh %s stop" % (path_network, network, path_scripts+"/ec2-instances"))
    1.83  
    1.84  ## Stop
    1.85  
    1.86 @@ -223,7 +244,6 @@
    1.87      screenlist="\n".join(screens)
    1.88  
    1.89      hardstatus='hardstatus string "%{rk}Xentaur%{bk}@%H %{gk}%c %{yk}%d.%m %{wk}%?%-Lw%?%{bw}%n*%f%t%?(%u)%?%{wk}%?%+Lw%?"'
    1.90 -   
    1.91      f=open(screenrc, "w");
    1.92      f.write("""
    1.93  hardstatus on
    1.94 @@ -578,7 +598,6 @@
    1.95      """
    1.96      return vbridges_table[domain][int_number]
    1.97  
    1.98 -
    1.99  def dump_start(bridge, filter=""):
   1.100      if bridge in real_bridges:
   1.101          print "Bridge %s is a real bridge" % (bridge)
   1.102 @@ -595,148 +614,17 @@
   1.103  
   1.104  
   1.105  #-----------------------------------------------------------------------
   1.106 -# CONFIGURATION TEMPLATES
   1.107 -
   1.108 -
   1.109 -def configure_ip_addresses(doms=domains):
   1.110 -
   1.111 -    cisco_set_ip_on_int="""
   1.112 -\n\n\n
   1.113 -int fa%s/0
   1.114 -duplex full
   1.115 -no ip address
   1.116 -ip address %s 255.255.255.0
   1.117 -no shutdown
   1.118 -exit
   1.119 -"""
   1.120 -
   1.121 -    quagga_set_ip_on_int="""
   1.122 -int eth%s
   1.123 -no ip address
   1.124 -ip address %s/24
   1.125 -no shutdown
   1.126 -exit
   1.127 -"""
   1.128 -
   1.129 -    linux_set_ip_on_int="""
   1.130 -ifconfig eth%s %s netmask 255.255.255.0
   1.131 -"""
   1.132 -
   1.133 -    for dom in doms:
   1.134 -        i=domains.index(dom)+1
   1.135 -        if domain_types[domains.index(dom)] == 'quagga':
   1.136 -            command = quagga_set_ip_on_int
   1.137 -            write_to(i,"\nconf t\n")
   1.138 -            j=0
   1.139 -            for br in  vbridges_table[dom]:
   1.140 -                write_to(i,command % (j, "192.168.%s.%s"%(bridges.index(br)+1,i)))
   1.141 -                j+=1
   1.142 -            write_to(i,"\nend\n")
   1.143 -        elif domain_types[domains.index(dom)] == 'linux':
   1.144 -            command = linux_set_ip_on_int
   1.145 -            j=0
   1.146 -            for br in  vbridges_table[dom]:
   1.147 -                #write_to(i, command % (j, "192.168.%s.%s"%(bridges.index(br)+1,i)) )
   1.148 -                print i, command % (j, "192.168.%s.%s"%(bridges.index(br)+1,i)) 
   1.149 -                j+=1
   1.150 -        else:
   1.151 -            command = cisco_set_ip_on_int
   1.152 -            write_to(i,"\nena\nconf t\n")
   1.153 -            j=0
   1.154 -            for br in  vbridges_table[dom]:
   1.155 -                write_to(i,command % (j, "192.168.%s.%s"%(bridges.index(br)+1,i)))
   1.156 -                j+=1
   1.157 -            write_to(i,"\nend\n")
   1.158 -    return 0
   1.159 -
   1.160 -def configure_no_ip_addresses(doms=domains):
   1.161 -
   1.162 -    cisco_set_ip_on_int="""
   1.163 -\n\n\n
   1.164 -int fa%s/0
   1.165 -no ip address %s 255.255.255.0
   1.166 -exit
   1.167 -"""
   1.168 -
   1.169 -    quagga_set_ip_on_int="""
   1.170 -int eth%s
   1.171 -no ip address %s/24
   1.172 -exit
   1.173 -"""
   1.174 -
   1.175 -    for dom in doms:
   1.176 -        i=domains.index(dom)+1
   1.177 -        if domain_types[domains.index(dom)] == 'quagga':
   1.178 -            command = quagga_set_ip_on_int
   1.179 -            write_to(i,"\nconf t\n")
   1.180 -            j=0
   1.181 -            for br in  vbridges_table[dom]:
   1.182 -                write_to(i,command % (j, "192.168.%s.%s"%(bridges.index(br)+1,i)))
   1.183 -                j+=1
   1.184 -            write_to(i,"\nend\n")
   1.185 -        else:
   1.186 -            command = cisco_set_ip_on_int
   1.187 -            write_to(i,"\nena\nconf t\n")
   1.188 -            j=0
   1.189 -            for br in  vbridges_table[dom]:
   1.190 -                write_to(i,command % (j, "192.168.%s.%s"%(bridges.index(br)+1,i)))
   1.191 -                j+=1
   1.192 -            write_to(i,"\nend\n")
   1.193 -    return 0
   1.194 -
   1.195 -def configure_ospf(doms=domains):
   1.196 -    for dom in doms:
   1.197 -        if domain_types[domains.index(dom)] == 'quagga':
   1.198 -            write_to(dom,"\n\nconf t\nrouter ospf\nnetwork 192.168.0.0/16 area 0\nend\n")
   1.199 -        else:
   1.200 -            write_to(dom,"\n\nena\nconf t\nrouter ospf 1\nnetwork 192.168.0.0 0.0.255.255 area 0\nend\n")
   1.201 -    return 0
   1.202 -
   1.203 -def configure_hostname(doms=domains):
   1.204 -    for dom in doms:
   1.205 -        if domain_types[domains.index(dom)] == 'quagga':
   1.206 -            write_to(dom,"\n\nconf t\nhostname %s\nend\n" % dom)
   1.207 -        else:
   1.208 -            write_to(dom,"\n\nena\nconf t\nhostname %s\nend\n" % dom)
   1.209 -    return 0
   1.210 -
   1.211 -def configure_logging_synchronous(doms=domains):
   1.212 -    for dom in domains:
   1.213 -        if domain_types[domains.index(dom)] == 'quagga':
   1.214 -            0
   1.215 -        else:
   1.216 -            write_to(dom,"\n\nena\nconf t\nline console 0\nlogging synchronous\nend\n")
   1.217 -    return 0
   1.218 -
   1.219 -def configure_exec_timeout_0(doms=domains):
   1.220 -    for dom in domains:
   1.221 -        if domain_types[domains.index(dom)] == 'quagga':
   1.222 -            0
   1.223 -        else:
   1.224 -            write_to(dom,"\n\nena\nconf t\nline console 0\nexec-timeout 0\nend\n")
   1.225 -    return 0
   1.226 -
   1.227 -def configure_no_cdp_log_mismatch_duplex(doms=domains):
   1.228 -    for dom in filter_by_type(domains,'dynamips'):
   1.229 -        write_to(dom,"\n\nena\nconf t\nno cdp log mismatch duplex\nend\n")
   1.230 -
   1.231 -def configure_save(doms=domains):
   1.232 -    write_to(doms,"\nwr\n")
   1.233 -
   1.234 -def configure_root(doms=domains):
   1.235 -    write_to(doms,"root\n")
   1.236 -
   1.237 -#-----------------------------------------------------------------------
   1.238 -
   1.239  
   1.240  def add_domain(name,type):
   1.241      domains.append(name)
   1.242      domain_types.append(type)
   1.243  
   1.244 -def brake_link(domain,bridge):
   1.245 +def break_link(domain,bridge):
   1.246      broken_links.append([domain,bridge])
   1.247  
   1.248 -wt_timeout=0.5
   1.249 +#-----------------------------------------------------------------------
   1.250 +
   1.251 +wt_timeout=1
   1.252  def write_to(screen,string,return_to_screen=""):
   1.253      """
   1.254      write_to(screen,string):
   1.255 @@ -754,7 +642,7 @@
   1.256          screen_numbers=[screen]
   1.257      else:
   1.258          screen_numbers=[domains.index(screen)+1]
   1.259 -    
   1.260 +
   1.261      for screen_number in screen_numbers:
   1.262          run_command("screen -X select "+str(screen_number))
   1.263          time.sleep(wt_timeout)
   1.264 @@ -766,7 +654,7 @@
   1.265              time.sleep(wt_timeout)
   1.266              run_command("nohup screen -X paste p >& /dev/null")
   1.267              time.sleep(wt_timeout)
   1.268 -    
   1.269 +
   1.270          if return_to_screen != "":
   1.271              run_command("screen -X select %s" % (return_to_screen))
   1.272              time.sleep(wt_timeout)
   1.273 @@ -781,33 +669,9 @@
   1.274  
   1.275  #-----------------------------------------------------------------------
   1.276  
   1.277 -cisco_fa01_up="""
   1.278 -ena
   1.279 -conf t
   1.280 -int fa0/0
   1.281 -duplex half
   1.282 -no shutdown
   1.283 -exit
   1.284 -int fa1/0
   1.285 -duplex half
   1.286 -no shutdown
   1.287 -exit
   1.288 -exit
   1.289 -exit
   1.290 -"""
   1.291 -
   1.292 -cisco_set_ip_on_int="""
   1.293 -interface fa%s/0
   1.294 -no ip address
   1.295 -ip address %s 255.255.255.0
   1.296 -exit
   1.297 -"""
   1.298 -
   1.299  nodes=domains
   1.300 -
   1.301  create_objects()
   1.302  
   1.303 -
   1.304  if len(sys.argv) == 2:
   1.305      if sys.argv[1] == 'start-all':
   1.306          start_all()