lib/rubber/recipes/rubber/instances.rb in axtro-rubber-1.2.3 vs lib/rubber/recipes/rubber/instances.rb in axtro-rubber-1.5.7

- old
+ new

@@ -33,13 +33,11 @@ end # Add in roles that the given set of roles depends on ir = Rubber::Configuration::RoleItem.expand_role_dependencies(ir, get_role_dependencies) - user_data = env.cloud_providers[env.cloud_provider]["user_data"] - - create_instance(instance_alias, ir, create_spot_instance, user_data) + create_instance(instance_alias, ir, create_spot_instance) end desc <<-DESC Refresh the host data for a EC2 instance with the given ALIAS. This is useful to run when rubber:create fails after instance creation @@ -156,11 +154,11 @@ set :print_ip_command, "ifconfig eth0 | awk 'NR==2 {print $2}' | awk -F: '{print $2}'" # Creates a new ec2 instance with the given alias and roles # Configures aliases (/etc/hosts) on local and remote machines - def create_instance(instance_alias, instance_roles, create_spot_instance=false, user_data = nil) + def create_instance(instance_alias, instance_roles, create_spot_instance=false) fatal "Instance already exists: #{instance_alias}" if rubber_instances[instance_alias] role_names = instance_roles.collect{|x| x.name} env = rubber_cfg.environment.bind(role_names, instance_alias) @@ -201,11 +199,11 @@ print "\n" end if !create_spot_instance || (create_spot_instance && max_wait_time < 0) logger.info "Creating instance #{ami}/#{ami_type}/#{security_groups.join(',') rescue 'Default'}/#{availability_zone || 'Default'}" - instance_id = cloud.create_instance(ami, ami_type, security_groups, availability_zone, user_data) + instance_id = cloud.create_instance(ami, ami_type, security_groups, availability_zone) end logger.info "Instance #{instance_id} created" instance_item = Rubber::Configuration::InstanceItem.new(instance_alias, env.domain, instance_roles, instance_id, security_groups) @@ -216,63 +214,13 @@ print "Waiting for instance to start" while true do print "." sleep 2 - instance = cloud.describe_instances(instance_id).first - if instance[:state] == "running" - print "\n" - logger.info "Instance running, fetching hostname/ip data" - instance_item.external_host = instance[:external_host] - instance_item.external_ip = instance[:external_ip] - instance_item.internal_host = instance[:internal_host] - instance_item.zone = instance[:zone] - instance_item.platform = instance[:platform] - rubber_instances.save() + break if refresh_instance(instance_alias) - # setup amazon elastic ips if configured to do so - setup_static_ips - - # Need to setup aliases so ssh doesn't give us errors when we - # later try? to connect to same ip but using alias - setup_local_aliases - - # re-load the roles since we may have just defined new ones - load_roles() unless env.disable_auto_roles - - # Connect to newly created instance and grab its internal ip - # so that we can update all aliases - - task :_get_ip, :hosts => instance_item.external_ip do - # There's no good way to get the internal IP for a Windows host, so just set it to the external - # and let the router handle mapping to the internal network. - if instance_item.windows? - instance_item.internal_ip = instance_item.external_ip - else - instance_item.internal_ip = capture(print_ip_command).strip - end - - rubber_instances.save() - end - - # even though instance is running, sometimes ssh hasn't started yet, - # so retry on connect failure - begin - _get_ip - rescue ConnectionError - sleep 2 - logger.info "Failed to connect to #{instance_alias} (#{instance_item.external_ip}), retrying" - retry - end - - # Add the aliases for this instance to all other hosts - setup_remote_aliases - setup_dns_aliases - - break - end end end # Refreshes a ec2 instance with the given alias # Configures aliases (/etc/hosts) on local and remote machines @@ -280,20 +228,26 @@ instance_item = rubber_instances[instance_alias] fatal "Instance does not exist: #{instance_alias}" if ! instance_item env = rubber_cfg.environment.bind(instance_item.role_names, instance_alias) - - instance = cloud.describe_instances(instance_item.instance_id).first + instance = cloud.describe_instances(instance_item.instance_id).first rescue {} + if instance[:state] == "running" - logger.info "\nInstance running, fetching hostname/ip data" + print "\n" + logger.info "Instance running, fetching hostname/ip data" instance_item.external_host = instance[:external_host] instance_item.external_ip = instance[:external_ip] instance_item.internal_host = instance[:internal_host] instance_item.zone = instance[:zone] + instance_item.platform = instance[:platform] + rubber_instances.save() + # turn back on root ssh access if we are using root as the capistrano user for connecting + enable_root_ssh(instance_item.external_ip, fetch(:initial_ssh_user, 'ubuntu')) if (user == 'root' && ! instance_item.windows?) + # setup amazon elastic ips if configured to do so setup_static_ips # Need to setup aliases so ssh doesn't give us errors when we # later try to connect to same ip but using alias @@ -303,11 +257,18 @@ load_roles() unless env.disable_auto_roles # Connect to newly created instance and grab its internal ip # so that we can update all aliases task :_get_ip, :hosts => instance_item.external_ip do - instance_item.internal_ip = capture(print_ip_command).strip + # There's no good way to get the internal IP for a Windows host, so just set it to the external + # and let the router handle mapping to the internal network. + if instance_item.windows? + instance_item.internal_ip = instance_item.external_ip + else + instance_item.internal_ip = capture(print_ip_command).strip + end + rubber_instances.save() end # even though instance is running, sometimes ssh hasn't started yet, # so retry on connect failure @@ -321,11 +282,14 @@ # Add the aliases for this instance to all other hosts setup_remote_aliases setup_dns_aliases + + return true end + return false end # Destroys the given ec2 instance def destroy_instance(instance_alias) @@ -335,13 +299,13 @@ env = rubber_cfg.environment.bind(instance_item.role_names, instance_item.name) value = Capistrano::CLI.ui.ask("About to DESTROY #{instance_alias} (#{instance_item.instance_id}) in mode #{RUBBER_ENV}. Are you SURE [yes/NO]?: ") fatal("Exiting", 0) if value != "yes" -# if instance_item.static_ip -# value = Capistrano::CLI.ui.ask("Instance has a static ip, do you want to release it? [y/N]?: ") -# destroy_static_ip(instance_item.static_ip) if value =~ /^y/ -# end + if instance_item.static_ip + value = Capistrano::CLI.ui.ask("Instance has a static ip, do you want to release it? [y/N]?: ") + destroy_static_ip(instance_item.static_ip) if value =~ /^y/ + end if instance_item.volumes value = Capistrano::CLI.ui.ask("Instance has persistent volumes, do you want to destroy them? [y/N]?: ") if value =~ /^y/ instance_item.volumes.clone.each do |volume_id|