lib/kitchen/driver/ec2.rb in kitchen-ec2-2.5.0 vs lib/kitchen/driver/ec2.rb in kitchen-ec2-3.0.0.preview

- old
+ new

@@ -31,18 +31,17 @@ require_relative "aws/standard_platform/rhel" require_relative "aws/standard_platform/fedora" require_relative "aws/standard_platform/freebsd" require_relative "aws/standard_platform/ubuntu" require_relative "aws/standard_platform/windows" +require "aws-sdk-ec2" require "aws-sdk-core/waiters/errors" require "retryable" require "time" require "etc" require "socket" -Aws.eager_autoload! - module Kitchen module Driver # Amazon EC2 driver for Test Kitchen. @@ -53,11 +52,11 @@ kitchen_driver_api_version 2 plugin_version Kitchen::Driver::EC2_VERSION default_config :region, ENV["AWS_REGION"] || "us-east-1" - default_config :shared_credentials_profile, nil + default_config :shared_credentials_profile, ENV["AWS_PROFILE"] default_config :availability_zone, nil default_config :instance_type do |driver| driver.default_instance_type end default_config :ebs_optimized, false @@ -94,13 +93,10 @@ default_config :ssl_verify_peer, true default_config :skip_cost_warning, false def initialize(*args, &block) super - # AWS Ruby SDK loading isn't thread safe, so as soon as we know we're - # going to use EC2, autoload it. Seems to have been fixed in Ruby 2.3+ - ::Aws.eager_autoload! unless RUBY_VERSION.to_f >= 2.3 end def self.validation_warn(driver, old_key, new_key) driver.warn "WARN: The driver[#{driver.class.name}] config key `#{old_key}` " \ "is deprecated, please use `#{new_key}`" @@ -250,19 +246,19 @@ # Waiting can also fail, so we have to also retry on that. If it means we re-tag the # instance, so be it. # Tagging an instance is possible before volumes are attached. Tagging the volumes after # instance creation is consistent. Retryable.retryable( - :tries => 10, - :sleep => lambda { |n| [2**n, 30].min }, - :on => ::Aws::EC2::Errors::InvalidInstanceIDNotFound + tries: 10, + sleep: lambda { |n| [2**n, 30].min }, + on: ::Aws::EC2::Errors::InvalidInstanceIDNotFound ) do |r, _| info("Attempting to tag the instance, #{r} retries") tag_server(server) # Get information about the AMI (image) used to create the image. - image_data = ec2.client.describe_images({ :image_ids => [server.image_id] })[0][0] + image_data = ec2.client.describe_images({ image_ids: [server.image_id] })[0][0] state[:server_id] = server.id info("EC2 instance <#{state[:server_id]}> created.") # instance-store backed images do not have attached volumes, so only @@ -301,11 +297,11 @@ server.terminate end if state[:spot_request_id] debug("Deleting spot request <#{state[:server_id]}>") ec2.client.cancel_spot_instance_requests( - :spot_instance_request_ids => [state[:spot_request_id]] + spot_instance_request_ids: [state[:spot_request_id]] ) state.delete(:spot_request_id) end # If we are going to clean up an automatic security group, we need # to wait for the instance to shut down. This slightly breaks the @@ -431,11 +427,11 @@ # deleting the instance cancels the request, but deleting the request # does not affect the instance state[:spot_request_id] = spot_request_id ec2.client.wait_until( :spot_instance_request_fulfilled, - :spot_instance_request_ids => [spot_request_id] + spot_instance_request_ids: [spot_request_id] ) do |w| w.max_attempts = config[:retryable_tries] w.delay = config[:retryable_sleep] w.before_attempt do |attempts| c = attempts * config[:retryable_sleep] @@ -447,13 +443,13 @@ end def create_spot_request request_duration = config[:retryable_tries] * config[:retryable_sleep] request_data = { - :spot_price => config[:spot_price].to_s, - :launch_specification => instance_generator.ec2_instance_data, - :valid_until => Time.now + request_duration, + spot_price: config[:spot_price].to_s, + launch_specification: instance_generator.ec2_instance_data, + valid_until: Time.now + request_duration, } if config[:block_duration_minutes] request_data[:block_duration_minutes] = config[:block_duration_minutes] end @@ -465,23 +461,23 @@ if config[:tags] && !config[:tags].empty? tags = config[:tags].map do |k, v| # we convert the value to a string because # nils should be passed as an empty String # and Integers need to be represented as Strings - { :key => k, :value => v.to_s } + { key: k, value: v.to_s } end - server.create_tags(:tags => tags) + server.create_tags(tags: tags) end end def tag_volumes(server) if config[:tags] && !config[:tags].empty? tags = config[:tags].map do |k, v| - { :key => k, :value => v.to_s } + { key: k, value: v.to_s } end server.volumes.each do |volume| - volume.create_tags(:tags => tags) + volume.create_tags(tags: tags) end end end # Compares the requested volume count vs what has actually been set to be @@ -491,12 +487,12 @@ def wait_until_volumes_ready(server, state) wait_with_destroy(server, state, "volumes to be ready") do |aws_instance| described_volume_count = 0 ready_volume_count = 0 if aws_instance.exists? - described_volume_count = ec2.client.describe_volumes(:filters => [ - { :name => "attachment.instance-id", :values => ["#{state[:server_id]}"] }] + described_volume_count = ec2.client.describe_volumes(filters: [ + { name: "attachment.instance-id", values: ["#{state[:server_id]}"] }] ).volumes.length aws_instance.volumes.each { ready_volume_count += 1 } end (described_volume_count > 0) && (described_volume_count == ready_volume_count) end @@ -540,13 +536,13 @@ info "Waited #{c}/#{t}s for instance <#{state[:server_id]}> #{status_msg}." end begin with_request_limit_backoff(state) do server.wait_until( - :max_attempts => config[:retryable_tries], - :delay => config[:retryable_sleep], - :before_attempt => wait_log, + max_attempts: config[:retryable_tries], + delay: config[:retryable_sleep], + before_attempt: wait_log, &block ) end rescue ::Aws::Waiters::Errors::WaiterFailed error("Ran out of time waiting for the server with id [#{state[:server_id]}]" \ @@ -557,11 +553,11 @@ end def fetch_windows_admin_password(server, state) wait_with_destroy(server, state, "to fetch windows admin password") do |aws_instance| enc = server.client.get_password_data( - :instance_id => state[:server_id] + instance_id: state[:server_id] ).password_data # Password data is blank until password is available !enc.nil? && !enc.empty? end pass = with_request_limit_backoff(state) do @@ -593,11 +589,11 @@ { "dns" => "public_dns_name", "public" => "public_ip_address", "private" => "private_ip_address", "private_dns" => "private_dns_name", - } + }.freeze # # Lookup hostname of provided server. If interface_type is provided use # that interface to lookup hostname. Otherwise, try ordered list of # options. @@ -666,11 +662,11 @@ & winrm.cmd set winrm/config/winrs '@{MaxShellsPerUser="50"}' >> $logfile & winrm.cmd set winrm/config/winrs '@{MaxMemoryPerShellMB="1024"}' >> $logfile #Firewall Config & netsh advfirewall firewall set rule name="Windows Remote Management (HTTP-In)" profile=public protocol=tcp localport=5985 remoteip=localsubnet new remoteip=any >> $logfile Set-ItemProperty -Name LocalAccountTokenFilterPolicy -Path HKLM:\\software\\Microsoft\\Windows\\CurrentVersion\\Policies\\system -Value 1 - EOH + EOH # Preparing custom static admin user if we defined something other than Administrator custom_admin_script = "" if !(instance.transport[:username] =~ /administrator/i) && instance.transport[:password] custom_admin_script = Kitchen::Util.outdent!(<<-EOH) @@ -710,11 +706,11 @@ debug("No platform detected for #{image.name}.") end end def image_info(image) - root_device = image.block_device_mappings. - find { |b| b.device_name == image.root_device_name } + root_device = image.block_device_mappings + .find { |b| b.device_name == image.root_device_name } volume_type = " #{root_device.ebs.volume_type}" if root_device && root_device.ebs " Architecture: #{image.architecture}," \ " Virtualization: #{image.virtualization_type}," \ " Storage: #{image.root_device_type}#{volume_type}," \