lib/kitchen/driver/ec2.rb in kitchen-ec2-3.13.0 vs lib/kitchen/driver/ec2.rb in kitchen-ec2-3.14.0
- old
+ new
@@ -19,10 +19,11 @@
require "benchmark" unless defined?(Benchmark)
require "json" unless defined?(JSON)
require "kitchen"
require_relative "ec2_version"
require_relative "aws/client"
+require_relative "aws/dedicated_hosts"
require_relative "aws/instance_generator"
require_relative "aws/standard_platform"
require_relative "aws/standard_platform/amazon"
require_relative "aws/standard_platform/amazon2"
require_relative "aws/standard_platform/centos"
@@ -87,11 +88,15 @@
default_config :retry_limit, 3
default_config :tenancy, "default"
default_config :instance_initiated_shutdown_behavior, nil
default_config :ssl_verify_peer, true
default_config :skip_cost_warning, false
+ default_config :allocate_dedicated_host, false
+ default_config :deallocate_dedicated_host, false
+ include Kitchen::Driver::Mixins::DedicatedHosts
+
def initialize(*args, &block)
super
end
def self.validation_warn(driver, old_key, new_key)
@@ -223,10 +228,22 @@
info("Disabling AWS-managed SSH key pairs for this EC2 instance.")
info("The key pairs for the kitchen transport config and the AMI must match.")
config[:aws_ssh_key_id] = nil
end
+ # Allocate new dedicated hosts if needed and allowed
+ if config[:tenancy] == "host"
+ unless host_available? || allow_allocate_host?
+ warn "ERROR: tenancy `host` requested but no suitable host and allocation not allowed (set `allocate_dedicated_host` setting)"
+ exit!
+ end
+
+ allocate_host unless host_available?
+
+ info("Auto placement on one dedicated host out of: #{hosts_with_capacity.map(&:host_id).join(", ")}")
+ end
+
if config[:spot_price]
# Spot instance when a price is set
server = with_request_limit_backoff(state) { submit_spots }
else
# On-demand instance
@@ -294,9 +311,15 @@
end
# Clean up any auto-created security groups or keys.
delete_security_group(state)
delete_key(state)
+
+ # Clean up dedicated hosts matching instance_type and unused (if allowed)
+ if config[:tenancy] == "host" && allow_deallocate_host?
+ empty_hosts = hosts_with_capacity.select { |host| host_unused?(host) }
+ empty_hosts.each { |host| deallocate_host(host.host_id) }
+ end
end
def image
return @image if defined?(@image)