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
-
#block_device_mappings ⇒ Object
Transforms the provided config into the appropriate hash for creating a BDM in AWS.
-
#debug_if_root_device(bdms) ⇒ Object
If the provided bdms match the root device in the AMI, emit log that states this.
-
#ec2_instance_data ⇒ Object
Transform the provided config into the hash to send to AWS.
-
#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.
34 35 36 37 38 |
# File 'lib/kitchen/driver/aws/instance_generator.rb', line 34 def initialize(config, ec2, logger) @config = config @ec2 = ec2 @logger = logger end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
32 33 34 |
# File 'lib/kitchen/driver/aws/instance_generator.rb', line 32 def config @config end |
#ec2 ⇒ Object (readonly)
Returns the value of attribute ec2.
32 33 34 |
# File 'lib/kitchen/driver/aws/instance_generator.rb', line 32 def ec2 @ec2 end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
32 33 34 |
# File 'lib/kitchen/driver/aws/instance_generator.rb', line 32 def logger @logger end |
Instance Method Details
#block_device_mappings ⇒ Object
Transforms the provided config into the appropriate hash for creating a BDM in AWS
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 |
# File 'lib/kitchen/driver/aws/instance_generator.rb', line 85 def block_device_mappings # rubocop:disable all return @bdms if @bdms bdms = config[:block_device_mappings] || [] if bdms.empty? if config[:ebs_volume_size] || config.fetch(:ebs_delete_on_termination, nil) || config[:ebs_device_name] || config[:ebs_volume_type] # If the user didn't supply block_device_mappings but did supply # the old configs, copy them into the block_device_mappings array correctly # TODO: remove this logic when we remove the deprecated values bdms << { :ebs_volume_size => config[:ebs_volume_size] || 8, :ebs_delete_on_termination => config.fetch(:ebs_delete_on_termination, true), :ebs_device_name => config[:ebs_device_name] || "/dev/sda1", :ebs_volume_type => config[:ebs_volume_type] || "standard" } end end # Convert the provided keys to what AWS expects bdms = bdms.map do |bdm| b = { :ebs => { :volume_size => bdm[:ebs_volume_size], :delete_on_termination => bdm[:ebs_delete_on_termination] }, :device_name => bdm[:ebs_device_name] } b[:ebs][:volume_type] = bdm[:ebs_volume_type] if bdm[:ebs_volume_type] b[:ebs][:iops] = bdm[:ebs_iops] if bdm[:ebs_iops] b[:ebs][:snapshot_id] = bdm[:ebs_snapshot_id] if bdm[:ebs_snapshot_id] b[:virtual_name] = bdm[:ebs_virtual_name] if bdm[:ebs_virtual_name] b end debug_if_root_device(bdms) @bdms = bdms end |
#debug_if_root_device(bdms) ⇒ Object
If the provided bdms match the root device in the AMI, emit log that states this
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/kitchen/driver/aws/instance_generator.rb', line 126 def debug_if_root_device(bdms) return if bdms.nil? || bdms.empty? image_id = config[:image_id] image = ec2.resource.image(image_id) begin root_device_name = image.root_device_name rescue ::Aws::EC2::Errors::InvalidAMIIDNotFound # Not raising here because AWS will give a more meaningful message # when we try to create the instance return end bdms.find { |bdm| if bdm[:device_name] == root_device_name logger.info("Overriding root device [#{root_device_name}] from image [#{image_id}]") end } end |
#ec2_instance_data ⇒ Object
Transform the provided config into the hash to send to AWS. Some fields can be passed in null, others need to be ommitted if they are null
42 43 44 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 |
# File 'lib/kitchen/driver/aws/instance_generator.rb', line 42 def ec2_instance_data # rubocop:disable Metrics/MethodLength, Metrics/AbcSize i = { :placement => { :availability_zone => config[:availability_zone] }, :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] } i[:block_device_mappings] = block_device_mappings unless block_device_mappings.empty? i[:security_group_ids] = Array(config[:security_group_ids]) if config[:security_group_ids] 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 if !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 i end |
#prepared_user_data ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/kitchen/driver/aws/instance_generator.rb', line 144 def prepared_user_data # If user_data is a file reference, lets read it as such return nil if config[:user_data].nil? @user_data ||= begin if File.file?(config[:user_data]) @user_data = File.read(config[:user_data]) else @user_data = config[:user_data] end @user_data = Base64.encode64(@user_data) end end |