lib/chef/knife/ec2_server_create.rb in knife-ec2-1.0.11 vs lib/chef/knife/ec2_server_create.rb in knife-ec2-1.0.12

- old
+ new

@@ -359,10 +359,13 @@ retry end attach_nics if config[:network_interfaces] + # Re-fetch the latest server data after assigning vpc or network interfaces + @server = fetch_ec2_instance(instance_id) if reload_server_data_required? + if vpc_mode? msg_pair("Subnet ID", server.subnet_id) msg_pair("Tenancy", server.tenancy) if config[:associate_public_ip] msg_pair("Public DNS Name", server.public_dns_name) @@ -854,27 +857,29 @@ min_count: 1, placement: { availability_zone: config_value(:availability_zone), }, } + network_attrs = {} - if primary_eni = config_value(:primary_eni) - network_attrs[:network_interface_id] = primary_eni - network_attrs[:device_index] = 0 - else - attributes[:security_group_ids] = config_value(:security_group_ids) - network_attrs[:subnet_id] = config_value(:subnet_id) if vpc_mode? + if !!config_value(:primary_eni) + network_attrs[:network_interface_id] = config_value(:primary_eni) + elsif vpc_mode? + network_attrs[:subnet_id] = config_value(:subnet_id) end if vpc_mode? - network_attrs[:groups] = attributes[:security_group_ids] + network_attrs[:groups] = config_value(:security_group_ids) if !!config_value(:security_group_ids) network_attrs[:private_ip_address] = config_value(:private_ip_address) - network_attrs[:associate_public_ip_address] = config_value(:associate_public_ip) if config[:associate_public_ip] + network_attrs[:associate_public_ip_address] = config_value(:associate_public_ip) end if network_attrs.length > 0 + network_attrs[:device_index] = 0 attributes[:network_interfaces] = [network_attrs] + else + attributes[:security_group_ids] = config_value(:security_group_ids) end attributes[:placement][:group_name] = config_value(:placement_group) attributes[:placement][:tenancy] = "dedicated" if vpc_mode? && config_value(:dedicated_instance) attributes[:iam_instance_profile] = {} @@ -1118,27 +1123,34 @@ subnet = fetch_subnet(server.subnet_id) subnet.map_public_ip_on_launch end + # Assign connection host based on attribute passed + # @return [String] def connection_host - unless @connection_host - if config[:server_connect_attribute] - connect_attribute = config[:server_connect_attribute] - server.send(config[:server_connect_attribute]) + @connection_host ||= server.send(connect_attribute) + + puts "\nSSH Target Address: #{@connection_host}(#{connect_attribute})" + @connection_host + end + + # Identify connection attribute if: + # Option --server-connect-attribute is set. + # For VPC mode check if public IP or elastic IP has been requested. + # Otherwise assign public DNS or public IP. + # @return [String] + def connect_attribute + @connect_attribute ||= begin + if !!config[:server_connect_attribute] + config[:server_connect_attribute] elsif vpc_mode? && !(subnet_public_ip_on_launch? || config[:associate_public_ip] || config[:associate_eip]) - connect_attribute = "private_ip_address" - server.private_ip_address + "private_ip_address" else - connect_attribute = server.public_dns_name ? "public_dns_name" : "public_ip_address" - server.send(connect_attribute) + server.public_dns_name ? "public_dns_name" : "public_ip_address" end - @connection_host = server.send(connect_attribute) end - - puts "\nSSH Target Address: #{@connection_host}(#{connect_attribute})" - @connection_host end def create_tags(hashed_tags) request_tags = [] hashed_tags.each_pair do |key, val| @@ -1163,11 +1175,10 @@ def associate_address(elastic_ip) ec2_connection.associate_address({ allocation_id: elastic_ip.allocation_id, instance_id: server.id, - public_ip: elastic_ip.public_ip, }) end def validate_nics! params = {} @@ -1423,9 +1434,13 @@ if config[:cpu_credits] == "unlimited" "Enabled" else "Disabled" end + end + + def reload_server_data_required? + !!config[:associate_eip] || !!config[:classic_link_vpc_id] || !!config[:network_interfaces] end end end end