Module: Kitchen::Driver::Mixins::DedicatedHosts
- Included in:
- Ec2
- Defined in:
- lib/kitchen/driver/aws/dedicated_hosts.rb
Instance Method Summary collapse
-
#allocate_host ⇒ Object
allocate new dedicated host for requested instance type.
-
#allow_allocate_host? ⇒ Boolean
check config, if host allocation is enabled.
-
#allow_deallocate_host? ⇒ Boolean
check config, if host deallocation is enabled.
-
#deallocate_host(host_id) ⇒ Object
deallocate a dedicated host.
-
#host_available? ⇒ Boolean
check if a suitable dedicated host is available.
-
#host_for_id(host_id) ⇒ Object
get host data for host id.
-
#host_unused?(host) ⇒ Boolean
check if host has no instances running.
-
#hosts_managed ⇒ Object
get dedicated hosts managed by Test Kitchen.
-
#hosts_with_capacity ⇒ Object
get dedicated host with capacity for instance type.
-
#instance_family_from_type(instance_type) ⇒ Object
return instance family from type.
-
#instance_size_from_type(instance_type) ⇒ Object
return instance size from type.
Instance Method Details
#allocate_host ⇒ Object
allocate new dedicated host for requested instance type
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/kitchen/driver/aws/dedicated_hosts.rb', line 54 def allocate_host unless allow_allocate_host? warn "ERROR: Attempted to allocate dedicated host but need environment variable TK_ALLOCATE_DEDICATED_HOST to be set" exit! end unless config[:availability_zone] warn "Attempted to allocate dedicated host but option 'availability_zone' is not set" exit! end info("Allocating dedicated host for #{config[:instance_type]} instances. This will incur additional cost") request = { availability_zone: config[:availability_zone], quantity: 1, auto_placement: "on", tag_specifications: [ { resource_type: "dedicated-host", tags: [ { key: "ManagedBy", value: "Test Kitchen" }, ], }, ], } # ".metal" is a 1:1 association, everything else has multi-instance capability if instance_size_from_type(config[:instance_type]) == "metal" request[:instance_type] = config[:instance_type] else request[:instance_family] = instance_family_from_type(config[:instance_type]) end response = ec2.client.allocate_hosts(request) response.host_ids.first end |
#allow_allocate_host? ⇒ Boolean
check config, if host allocation is enabled
123 124 125 |
# File 'lib/kitchen/driver/aws/dedicated_hosts.rb', line 123 def allow_allocate_host? config[:allocate_dedicated_host] end |
#allow_deallocate_host? ⇒ Boolean
check config, if host deallocation is enabled
129 130 131 |
# File 'lib/kitchen/driver/aws/dedicated_hosts.rb', line 129 def allow_deallocate_host? config[:deallocate_dedicated_host] end |
#deallocate_host(host_id) ⇒ Object
deallocate a dedicated host
97 98 99 100 101 102 103 104 105 |
# File 'lib/kitchen/driver/aws/dedicated_hosts.rb', line 97 def deallocate_host(host_id) info("Deallocating dedicated host #{host_id}") response = ec2.client.release_hosts({ host_ids: [host_id] }) unless response.unsuccessful.empty? warn "ERROR: Could not release dedicated host #{host_id}. Host may remain allocated and incur cost" exit! end end |
#host_available? ⇒ Boolean
check if a suitable dedicated host is available
7 8 9 |
# File 'lib/kitchen/driver/aws/dedicated_hosts.rb', line 7 def host_available? !hosts_with_capacity.empty? end |
#host_for_id(host_id) ⇒ Object
get host data for host id
36 37 38 |
# File 'lib/kitchen/driver/aws/dedicated_hosts.rb', line 36 def host_for_id(host_id) ec2.client.describe_hosts(host_ids: [host_id])&.first end |
#host_unused?(host) ⇒ Boolean
check if host has no instances running
29 30 31 |
# File 'lib/kitchen/driver/aws/dedicated_hosts.rb', line 29 def host_unused?(host) host.instances.empty? end |
#hosts_managed ⇒ Object
get dedicated hosts managed by Test Kitchen
42 43 44 45 46 47 48 49 50 |
# File 'lib/kitchen/driver/aws/dedicated_hosts.rb', line 42 def hosts_managed response = ec2.client.describe_hosts( filter: [ { name: "tag:ManagedBy", values: ["Test Kitchen"] }, ] ) response.hosts.select { |host| host.state == "available" } end |
#hosts_with_capacity ⇒ Object
get dedicated host with capacity for instance type
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/kitchen/driver/aws/dedicated_hosts.rb', line 13 def hosts_with_capacity hosts_managed.select do |host| # T-instance hosts do not report available capacity and can be overprovisioned if host.available_capacity.nil? true else instance_capacity = host.available_capacity.available_instance_capacity capacity_for_type = instance_capacity.detect { |cap| cap.instance_type == config[:instance_type] } capacity_for_type.available_capacity > 0 end end end |
#instance_family_from_type(instance_type) ⇒ Object
return instance family from type
110 111 112 |
# File 'lib/kitchen/driver/aws/dedicated_hosts.rb', line 110 def instance_family_from_type(instance_type) instance_type.split(".").first end |
#instance_size_from_type(instance_type) ⇒ Object
return instance size from type
117 118 119 |
# File 'lib/kitchen/driver/aws/dedicated_hosts.rb', line 117 def instance_size_from_type(instance_type) instance_type.split(".").last end |