Class: Kitchen::Driver::Aws::InstanceGenerator
- Inherits:
-
Object
- Object
- Kitchen::Driver::Aws::InstanceGenerator
- Defined in:
- lib/kitchen/driver/aws/instance_generator.rb
Overview
A class for encapsulating the instance payload logic
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#ec2 ⇒ Object
readonly
Returns the value of attribute ec2.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Instance Method Summary collapse
-
#ec2_instance_data ⇒ Hash
Transform the provided kitchen config into the hash we’ll use to create the aws instance can be passed in null, others need to be ommitted if they are null Some fields can be passed in null, others need to be ommitted if they are null.
-
#initialize(config, ec2, logger) ⇒ InstanceGenerator
constructor
A new instance of InstanceGenerator.
- #prepared_user_data ⇒ Object
Constructor Details
#initialize(config, ec2, logger) ⇒ InstanceGenerator
Returns a new instance of InstanceGenerator.
35 36 37 38 39 |
# File 'lib/kitchen/driver/aws/instance_generator.rb', line 35 def initialize(config, ec2, logger) @config = config @ec2 = ec2 @logger = logger end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
33 34 35 |
# File 'lib/kitchen/driver/aws/instance_generator.rb', line 33 def config @config end |
#ec2 ⇒ Object (readonly)
Returns the value of attribute ec2.
33 34 35 |
# File 'lib/kitchen/driver/aws/instance_generator.rb', line 33 def ec2 @ec2 end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
33 34 35 |
# File 'lib/kitchen/driver/aws/instance_generator.rb', line 33 def logger @logger end |
Instance Method Details
#ec2_instance_data ⇒ Hash
Transform the provided kitchen config into the hash we’ll use to create the aws instance can be passed in null, others need to be ommitted if they are null Some fields can be passed in null, others need to be ommitted if they are null
45 46 47 48 49 50 51 52 53 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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/kitchen/driver/aws/instance_generator.rb', line 45 def ec2_instance_data # rubocop:disable Metrics/MethodLength, Metrics/AbcSize # Support for looking up security group id and subnet id using tags. vpc_id = nil client = ::Aws::EC2::Client.new(region: config[:region]) if config[:subnet_id].nil? && config[:subnet_filter] filters = [config[:subnet_filter]].flatten r = { filters: [] } filters.each do |subnet_filter| r[:filters] << { name: "tag:#{subnet_filter[:tag]}", values: [subnet_filter[:value]], } end subnets = client.describe_subnets(r).subnets raise "Subnets with tags '#{filters}' not found during security group creation" if subnets.empty? # => Select the least-populated subnet if we have multiple matches subnet = subnets.max_by { |s| s[:available_ip_address_count] } vpc_id = subnet.vpc_id config[:subnet_id] = subnet.subnet_id end if config[:security_group_ids].nil? && config[:security_group_filter] # => Grab the VPC in the case a Subnet ID rather than Filter was set vpc_id ||= client.describe_subnets(subnet_ids: [config[:subnet_id]]).subnets[0].vpc_id security_groups = [] filters = [config[:security_group_filter]].flatten filters.each do |sg_filter| r = {} if sg_filter[:name] r[:filters] = [ { name: "group-name", values: [sg_filter[:name]], }, { name: "vpc-id", values: [vpc_id], }, ] end if sg_filter[:tag] r[:filters] = [ { name: "tag:#{sg_filter[:tag]}", values: [sg_filter[:value]], }, { name: "vpc-id", values: [vpc_id], }, ] end security_group = client.describe_security_groups(r).security_groups if security_group.any? security_group.each { |sg| security_groups.push(sg.group_id) } else raise "A Security Group matching the following filter could not be found:\n#{sg_filter}" end end config[:security_group_ids] = security_groups end i = { instance_type: config[:instance_type], ebs_optimized: config[:ebs_optimized], image_id: config[:image_id], key_name: config[:aws_ssh_key_id], subnet_id: config[:subnet_id], private_ip_address: config[:private_ip_address], min_count: 1, max_count: 1, } if config[:tags] && !config[:tags].empty? = 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 } end instance_tag_spec = { resource_type: "instance", tags: } volume_tag_spec = { resource_type: "volume", tags: } i[:tag_specifications] = [instance_tag_spec, volume_tag_spec] end availability_zone = config[:availability_zone] if availability_zone if /^[a-z]$/i.match?(availability_zone) availability_zone = "#{config[:region]}#{availability_zone}" end i[:placement] = { availability_zone: availability_zone.downcase } end tenancy = config[:tenancy] if tenancy if i.key?(:placement) i[:placement][:tenancy] = tenancy else i[:placement] = { tenancy: tenancy } end end unless config[:block_device_mappings].nil? || config[:block_device_mappings].empty? i[:block_device_mappings] = config[:block_device_mappings] end i[:security_group_ids] = Array(config[:security_group_ids]) if config[:security_group_ids] i[:metadata_options] = config[:metadata_options] if config[:metadata_options] i[:user_data] = prepared_user_data if prepared_user_data if config[:iam_profile_name] i[:iam_instance_profile] = { name: config[:iam_profile_name] } end unless config.fetch(:associate_public_ip, nil).nil? i[:network_interfaces] = [{ device_index: 0, associate_public_ip_address: config[:associate_public_ip], delete_on_termination: true, }] # If specifying `:network_interfaces` in the request, you must specify # network specific configs in the network_interfaces block and not at # the top level if config[:subnet_id] i[:network_interfaces][0][:subnet_id] = i.delete(:subnet_id) end if config[:private_ip_address] i[:network_interfaces][0][:private_ip_address] = i.delete(:private_ip_address) end if config[:security_group_ids] i[:network_interfaces][0][:groups] = i.delete(:security_group_ids) end end availability_zone = config[:availability_zone] if availability_zone if /^[a-z]$/i.match?(availability_zone) availability_zone = "#{config[:region]}#{availability_zone}" end i[:placement] = { availability_zone: availability_zone.downcase } end tenancy = config[:tenancy] if tenancy if i.key?(:placement) i[:placement][:tenancy] = tenancy else i[:placement] = { tenancy: tenancy } end end unless config[:instance_initiated_shutdown_behavior].nil? || config[:instance_initiated_shutdown_behavior].empty? i[:instance_initiated_shutdown_behavior] = config[:instance_initiated_shutdown_behavior] end i end |
#prepared_user_data ⇒ Object
203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/kitchen/driver/aws/instance_generator.rb', line 203 def prepared_user_data # If user_data is a file reference, lets read it as such return nil if config[:user_data].nil? return @user_data if @user_data raw_user_data = config.fetch(:user_data) if !raw_user_data.include?("\0") && File.file?(raw_user_data) raw_user_data = File.read(raw_user_data) end @user_data = Base64.encode64(raw_user_data) end |