bosh_aws_cpi 1.2579.0 → 2.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,90 @@
1
+ module Bosh::AwsCloud
2
+ class Instance
3
+ def initialize(aws_instance, registry, elb, logger)
4
+ @aws_instance = aws_instance
5
+ @registry = registry
6
+ @elb = elb
7
+ @logger = logger
8
+ end
9
+
10
+ def id
11
+ @aws_instance.id
12
+ end
13
+
14
+ def elastic_ip
15
+ @aws_instance.elastic_ip
16
+ end
17
+
18
+ def associate_elastic_ip(elastic_ip)
19
+ @aws_instance.associate_elastic_ip(elastic_ip)
20
+ end
21
+
22
+ def disassociate_elastic_ip
23
+ @aws_instance.disassociate_elastic_ip
24
+ end
25
+
26
+ def wait_for_running
27
+ # If we time out, it is because the instance never gets from state running to started,
28
+ # so we signal the director that it is ok to retry the operation. At the moment this
29
+ # forever (until the operation is cancelled by the user).
30
+ begin
31
+ @logger.info("Waiting for instance to be ready...")
32
+ ResourceWait.for_instance(instance: @aws_instance, state: :running)
33
+ rescue Bosh::Common::RetryCountExceeded
34
+ @logger.warn("Timed out waiting for instance '#{aws_instance.id}' to be running")
35
+ raise Bosh::Clouds::VMCreationFailed.new(true)
36
+ end
37
+ end
38
+
39
+ # Soft reboots EC2 instance
40
+ def reboot
41
+ # There is no trackable status change for the instance being
42
+ # rebooted, so it's up to CPI client to keep track of agent
43
+ # being ready after reboot.
44
+ # Due to this, we can't deregister the instance from any load
45
+ # balancers it might be attached to, and reattach once the
46
+ # reboot is complete, so we just have to let the load balancers
47
+ # take the instance out of rotation, and put it back in once it
48
+ # is back up again.
49
+ @aws_instance.reboot
50
+ end
51
+
52
+ def terminate(fast=false)
53
+ begin
54
+ @aws_instance.terminate
55
+ rescue AWS::EC2::Errors::InvalidInstanceID::NotFound => e
56
+ @logger.warn("Failed to terminate instance '#{@aws_instance.id}' because it was not found: #{e.inspect}")
57
+ raise Bosh::Clouds::VMNotFound, "VM `#{@aws_instance.id}' not found"
58
+ ensure
59
+ @logger.info("Deleting instance settings for '#{@aws_instance.id}'")
60
+ @registry.delete_settings(@aws_instance.id)
61
+ end
62
+
63
+ if fast
64
+ TagManager.tag(@aws_instance, "Name", "to be deleted")
65
+ @logger.info("Instance #{@aws_instance.id} marked to deletion")
66
+ return
67
+ end
68
+
69
+ begin
70
+ @logger.info("Deleting instance '#{@aws_instance.id}'")
71
+ ResourceWait.for_instance(instance: @aws_instance, state: :terminated)
72
+ rescue AWS::EC2::Errors::InvalidInstanceID::NotFound => e
73
+ @logger.debug("Failed to find terminated instance '#{@aws_instance.id}' after deletion: #{e.inspect}")
74
+ # It's OK, just means that instance has already been deleted
75
+ end
76
+ end
77
+
78
+ # Determines if the instance exists.
79
+ def exists?
80
+ @aws_instance.exists? && @aws_instance.status != :terminated
81
+ end
82
+
83
+ def attach_to_load_balancers(load_balancer_ids)
84
+ load_balancer_ids.each do |load_balancer_id|
85
+ lb = @elb.load_balancers[load_balancer_id]
86
+ lb.instances.register(@aws_instance)
87
+ end
88
+ end
89
+ end
90
+ end
@@ -5,240 +5,339 @@ module Bosh::AwsCloud
5
5
  class InstanceManager
6
6
  include Helpers
7
7
 
8
- attr_reader :instance
9
- attr_reader :instance_params
10
- attr_reader :elbs
8
+ class DiskInfo
9
+ attr_reader :size, :count
11
10
 
12
- def initialize(region, registry, az_selector=nil)
11
+ def initialize(size, count)
12
+ @size = size
13
+ @count = count
14
+ end
15
+ end
16
+
17
+ InstanceStorageMap = {
18
+ # previous generation
19
+ 'm1.small' => DiskInfo.new(160, 1),
20
+ 'm1.medium' => DiskInfo.new(410, 1),
21
+ 'm1.large' => DiskInfo.new(420, 2),
22
+ 'm1.xlarge' => DiskInfo.new(420, 4),
23
+
24
+ 'c1.medium' => DiskInfo.new(350, 1),
25
+ 'c1.xlarge' => DiskInfo.new(420, 4),
26
+
27
+ 'cc2.8xlarge' => DiskInfo.new(840, 4),
28
+
29
+ 'cg1.4xlarge' => DiskInfo.new(840, 2),
30
+
31
+ 'm2.xlarge' => DiskInfo.new(420, 1),
32
+ 'm2.2xlarge' => DiskInfo.new(850, 1),
33
+ 'm2.4xlarge' => DiskInfo.new(840, 2),
34
+
35
+ 'cr1.8xlarge' => DiskInfo.new(120, 2),
36
+
37
+ 'hi1.4xlarge' => DiskInfo.new(1024, 2),
38
+
39
+ 'hs1.8xlarge' => DiskInfo.new(2000, 24),
40
+
41
+ # current generation
42
+ 'm3.medium' => DiskInfo.new(4, 1),
43
+ 'm3.large' => DiskInfo.new(32, 1),
44
+ 'm3.xlarge' => DiskInfo.new(40, 2),
45
+ 'm3.2xlarge' => DiskInfo.new(80, 2),
46
+
47
+ 'c3.large' => DiskInfo.new(16, 2),
48
+ 'c3.xlarge' => DiskInfo.new(40, 2),
49
+ 'c3.2xlarge' => DiskInfo.new(80, 2),
50
+ 'c3.4xlarge' => DiskInfo.new(160, 2),
51
+ 'c3.8xlarge' => DiskInfo.new(320, 2),
52
+
53
+ 'r3.large' => DiskInfo.new(32, 1),
54
+ 'r3.xlarge' => DiskInfo.new(80, 1),
55
+ 'r3.2xlarge' => DiskInfo.new(160, 1),
56
+ 'r3.4xlarge' => DiskInfo.new(320, 1),
57
+ 'r3.8xlarge' => DiskInfo.new(320, 2),
58
+
59
+ 'g2.2xlarge' => DiskInfo.new(60, 1),
60
+ 'g2.8xlarge' => DiskInfo.new(120, 2),
61
+
62
+ 'i2.xlarge' => DiskInfo.new(800, 1),
63
+ 'i2.2xlarge' => DiskInfo.new(800, 2),
64
+ 'i2.4xlarge' => DiskInfo.new(800, 4),
65
+ 'i2.8xlarge' => DiskInfo.new(800, 8),
66
+
67
+ 'd2.xlarge' => DiskInfo.new(2000, 3),
68
+ 'd2.2xlarge' => DiskInfo.new(2000, 6),
69
+ 'd2.4xlarge' => DiskInfo.new(2000, 12),
70
+ 'd2.8xlarge' => DiskInfo.new(2000, 24)
71
+ }
72
+
73
+ def initialize(region, registry, elb, az_selector, logger)
13
74
  @region = region
14
75
  @registry = registry
15
- @logger = Bosh::Clouds::Config.logger
76
+ @elb = elb
16
77
  @az_selector = az_selector
17
- @instance_params = {count: 1}
78
+ @logger = logger
18
79
  end
19
80
 
20
81
  def create(agent_id, stemcell_id, resource_pool, networks_spec, disk_locality, environment, options)
21
- @instance_params[:image_id] = stemcell_id
22
- @instance_params[:instance_type] = resource_pool["instance_type"]
23
- set_user_data_parameter(networks_spec)
24
- set_key_name_parameter(resource_pool["key_name"], options["aws"]["default_key_name"])
25
- set_security_groups_parameter(networks_spec, options["aws"]["default_security_groups"])
26
- set_vpc_parameters(networks_spec)
27
- set_availability_zone_parameter(
28
- (disk_locality || []).map { |volume_id| @region.volumes[volume_id].availability_zone.to_s },
29
- resource_pool["availability_zone"],
30
- (@instance_params[:subnet].availability_zone_name if @instance_params[:subnet])
31
- )
32
-
82
+ instance_params, block_device_info = build_instance_params(stemcell_id, resource_pool, networks_spec, disk_locality, options)
83
+
33
84
  @logger.info("Creating new instance with: #{instance_params.inspect}")
34
-
35
- if resource_pool["spot_bid_price"]
36
- @logger.info("Launching spot instance...")
37
- security_group_ids = []
38
- @region.security_groups.each do |group|
39
- security_group_ids << group.security_group_id if @instance_params[:security_groups].include?(group.name)
40
- end
41
- spot_request_spec = create_spot_request_spec(instance_params, security_group_ids, resource_pool["spot_bid_price"])
42
- @logger.debug("Requesting spot instance with: #{spot_request_spec.inspect}")
43
- spot_instance_requests = @region.client.request_spot_instances(spot_request_spec)
44
- @logger.debug("Got spot instance requests: #{spot_instance_requests.inspect}")
45
-
46
- wait_for_spot_instance_request_to_be_active spot_instance_requests
47
- else
48
- # Retry the create instance operation a couple of times if we are told that the IP
49
- # address is in use - it can happen when the director recreates a VM and AWS
50
- # is too slow to update its state when we have released the IP address and want to
51
- # realocate it again.
52
- errors = [AWS::EC2::Errors::InvalidIPAddress::InUse]
53
- Bosh::Common.retryable(sleep: instance_create_wait_time, tries: 10, on: errors) do |tries, error|
54
- @logger.info("Launching on demand instance...")
55
- @logger.warn("IP address was in use: #{error}") if tries > 0
56
- @instance = @region.instances.create(instance_params)
57
- end
58
- end
59
85
 
60
- # We need to wait here for the instance to be running, as if we are going to
61
- # attach to a load balancer, the instance must be running.
62
- # If we time out, it is because the instance never gets from state running to started,
63
- # so we signal the director that it is ok to retry the operation. At the moment this
64
- # forever (until the operation is cancelled by the user).
86
+ aws_instance = create_aws_instance(instance_params, resource_pool)
87
+
88
+ instance = Instance.new(aws_instance, @registry, @elb, @logger)
89
+
65
90
  begin
66
- @logger.info("Waiting for instance to be ready...")
67
- ResourceWait.for_instance(instance: instance, state: :running)
68
- rescue Bosh::Common::RetryCountExceeded => e
69
- @logger.warn("timed out waiting for #{instance.id} to be running")
70
- raise Bosh::Clouds::VMCreationFailed.new(true)
91
+ # We need to wait here for the instance to be running, as if we are going to
92
+ # attach to a load balancer, the instance must be running.
93
+ instance.wait_for_running
94
+ instance.attach_to_load_balancers(resource_pool['elbs'] || [])
95
+ rescue => e
96
+ @logger.warn("Failed to configure instance '#{instance.id}': #{e.inspect}")
97
+ begin
98
+ instance.terminate
99
+ rescue => e
100
+ @logger.error("Failed to terminate mis-configured instance '#{instance.id}': #{e.inspect}")
101
+ end
102
+ raise
71
103
  end
72
104
 
73
- @elbs = resource_pool['elbs']
74
- attach_to_load_balancers if elbs
75
-
76
- instance
77
- end
78
-
79
- def create_spot_request_spec(instance_params, security_group_ids, spot_price) {
80
- spot_price: "#{spot_price}",
81
- instance_count: 1,
82
- valid_until: "#{(Time.now + 20*60).utc.iso8601}",
83
- launch_specification: {
84
- image_id: instance_params[:image_id],
85
- key_name: instance_params[:key_name],
86
- instance_type: instance_params[:instance_type],
87
- user_data: Base64.encode64(instance_params[:user_data]),
88
- placement: {
89
- availability_zone: instance_params[:availability_zone]
90
- },
91
- network_interfaces: [
92
- {
93
- subnet_id: instance_params[:subnet].subnet_id,
94
- groups: security_group_ids,
95
- device_index: 0,
96
- private_ip_address: instance_params[:private_ip_address]
97
- }
98
- ]
99
- }
100
- }
105
+ block_device_agent_info = block_device_info
106
+ .group_by { |v| v[:bosh_type] }
107
+ .map { |type, devices| {type => devices.map { |device| {"path" => device[:device_name]} }} }
108
+ .inject({}) { |a, b| a.merge(b) }
109
+
110
+ return instance, block_device_agent_info
101
111
  end
102
112
 
103
- def wait_for_spot_instance_request_to_be_active(spot_instance_requests)
104
- # Query the spot request state until it becomes "active".
105
- # This can result in the errors listed below; this is normally because AWS has
106
- # been slow to update its state so the correct response is to wait a bit and try again.
107
- errors = [AWS::EC2::Errors::InvalidSpotInstanceRequestID::NotFound]
108
- Bosh::Common.retryable(sleep: instance_create_wait_time*2, tries: 20, on: errors) do |tries, error|
109
- @logger.warn("Retrying after expected error: #{error}") if error
110
- @logger.debug("Checking state of spot instance requests...")
111
- spot_instance_request_ids = spot_instance_requests[:spot_instance_request_set].map { |r| r[:spot_instance_request_id] }
112
- response = @region.client.describe_spot_instance_requests(:spot_instance_request_ids => spot_instance_request_ids)
113
- statuses = response[:spot_instance_request_set].map { |rr| rr[:state] }
114
- @logger.debug("Spot instance request states: #{statuses.inspect}")
115
- if statuses.all? { |s| s == 'active' }
116
- @logger.info("Spot request instances fulfilled: #{response.inspect}")
117
- instance_id = response[:spot_instance_request_set].map { |rr| rr[:instance_id] }[0]
118
- @instance = @region.instances[instance_id]
119
- end
120
- end
113
+ # @param [String] instance_id EC2 instance id
114
+ def find(instance_id)
115
+ Instance.new(@region.instances[instance_id], @registry, @elb, @logger)
121
116
  end
122
117
 
123
- def terminate(instance_id, fast=false)
124
- @instance = @region.instances[instance_id]
118
+ private
119
+
120
+ def build_instance_params(stemcell_id, resource_pool, networks_spec, disk_locality, options)
121
+ virtualization_type = @region.images[stemcell_id].virtualization_type
122
+ block_device_info = block_device_mapping(virtualization_type, resource_pool)
125
123
 
126
- remove_from_load_balancers
124
+ instance_params = {count: 1}
125
+ instance_params[:image_id] = stemcell_id
126
+ instance_params[:instance_type] = resource_pool["instance_type"]
127
+ instance_params[:block_device_mappings] = block_device_info.map { |entry| entry.reject { |k| k == :bosh_type } }
128
+ instance_params[:placement_group] = resource_pool["placement_group"] if resource_pool["placement_group"]
129
+ instance_params[:dedicated_tenancy] = true if resource_pool["tenancy"] == "dedicated"
127
130
 
128
- begin
129
- instance.terminate
130
- rescue AWS::EC2::Errors::InvalidInstanceID::NotFound => e
131
- @logger.info("Failed to terminate instance because it was not found: #{e.inspect}")
132
- raise Bosh::Clouds::VMNotFound, "VM `#{instance_id}' not found"
133
- ensure
134
- @logger.info("Deleting instance settings for '#{instance_id}'")
135
- @registry.delete_settings(instance_id)
136
- end
131
+ set_user_data_parameter(instance_params, networks_spec)
132
+ set_key_name_parameter(instance_params, resource_pool["key_name"], options["aws"]["default_key_name"])
133
+ set_security_groups_parameter(instance_params, networks_spec, options["aws"]["default_security_groups"])
134
+ set_vpc_parameters(instance_params, networks_spec)
135
+ set_iam_instance_profile_parameter(instance_params, resource_pool["iam_instance_profile"], options["aws"]["default_iam_instance_profile"])
136
+ set_availability_zone_parameter(
137
+ instance_params,
138
+ (disk_locality || []).map { |volume_id| @region.volumes[volume_id].availability_zone.to_s },
139
+ resource_pool["availability_zone"],
140
+ (instance_params[:subnet].availability_zone_name if instance_params[:subnet])
141
+ )
137
142
 
138
- if fast
139
- TagManager.tag(instance, "Name", "to be deleted")
140
- @logger.info("Instance #{instance_id} marked to deletion")
141
- return
143
+ return instance_params, block_device_info
144
+ end
145
+
146
+ def create_aws_spot_instance(instance_params, spot_bid_price)
147
+ @logger.info("Launching spot instance...")
148
+ spot_manager = Bosh::AwsCloud::SpotManager.new(@region)
149
+
150
+ spot_manager.create(instance_params, spot_bid_price)
151
+ end
152
+
153
+ def create_aws_instance(instance_params, resource_pool)
154
+ if resource_pool["spot_bid_price"]
155
+ begin
156
+ return create_aws_spot_instance instance_params, resource_pool["spot_bid_price"]
157
+ rescue Bosh::Clouds::VMCreationFailed => e
158
+ raise unless resource_pool["spot_ondemand_fallback"]
159
+ end
142
160
  end
143
161
 
144
- begin
145
- @logger.info("Deleting instance '#{instance.id}'")
146
- ResourceWait.for_instance(instance: instance, state: :terminated)
147
- rescue AWS::EC2::Errors::InvalidInstanceID::NotFound
148
- # It's OK, just means that instance has already been deleted
162
+ # Retry the create instance operation a couple of times if we are told that the IP
163
+ # address is in use - it can happen when the director recreates a VM and AWS
164
+ # is too slow to update its state when we have released the IP address and want to
165
+ # realocate it again.
166
+ errors = [AWS::EC2::Errors::InvalidIPAddress::InUse, AWS::EC2::Errors::RequestLimitExceeded]
167
+ Bosh::Common.retryable(sleep: instance_create_wait_time, tries: 20, on: errors) do |tries, error|
168
+ @logger.info("Launching on demand instance...")
169
+ if error.class == AWS::EC2::Errors::InvalidIPAddress::InUse
170
+ @logger.warn("IP address was in use: #{error}")
171
+ end
172
+ @region.instances.create(instance_params)
149
173
  end
150
174
  end
151
175
 
152
- # Soft reboots EC2 instance
153
- # @param [String] instance_id EC2 instance id
154
- def reboot(instance_id)
155
- instance = @region.instances[instance_id]
156
-
157
- # There is no trackable status change for the instance being
158
- # rebooted, so it's up to CPI client to keep track of agent
159
- # being ready after reboot.
160
- # Due to this, we can't deregister the instance from any load
161
- # balancers it might be attached to, and reattach once the
162
- # reboot is complete, so we just have to let the load balancers
163
- # take the instance out of rotation, and put it back in once it
164
- # is back up again.
165
- instance.reboot
166
- end
167
-
168
- def attach_to_load_balancers
169
- elb = AWS::ELB.new
170
-
171
- elbs.each do |load_balancer|
172
- lb = elb.load_balancers[load_balancer]
173
- lb.instances.register(instance)
174
- end
176
+ def instance_create_wait_time
177
+ 30
175
178
  end
176
179
 
177
- # Determines if the instance exists.
178
- # @param [String] instance_id EC2 instance id
179
- def has_instance?(instance_id)
180
- instance = @region.instances[instance_id]
180
+ def block_device_mapping(virtualization_type, resource_pool)
181
+ ephemeral_disk_options = resource_pool.fetch("ephemeral_disk", {})
181
182
 
182
- instance.exists? && instance.status != :terminated
183
- end
183
+ requested_size = ephemeral_disk_options['size'] || 0
184
+ actual_size = ephemeral_disk_options['size'] || 10 * 1024
184
185
 
185
- def remove_from_load_balancers
186
- elb = AWS::ELB.new
186
+ ephemeral_volume_properties = VolumeProperties.new(
187
+ size: actual_size,
188
+ type: ephemeral_disk_options['type'],
189
+ iops: ephemeral_disk_options['iops'],
190
+ )
187
191
 
188
- elb.load_balancers.each do |load_balancer|
189
- begin
190
- load_balancer.instances.deregister(instance)
191
- rescue AWS::ELB::Errors::InvalidInstance
192
- # ignore this, as it just means it wasn't registered
192
+ ephemeral_volume_properties.validate!
193
+
194
+ instance_type = resource_pool.fetch('instance_type', 'unspecified')
195
+ raw_instance_storage = resource_pool.fetch('raw_instance_storage', false)
196
+
197
+ local_disk_info = InstanceManager::InstanceStorageMap[instance_type]
198
+ if raw_instance_storage && local_disk_info.nil?
199
+ raise Bosh::Clouds::CloudError, "raw_instance_storage requested for instance type '#{instance_type}' that does not have instance storage"
200
+ end
201
+
202
+ if raw_instance_storage || local_disk_info.nil? || local_disk_info.size < (requested_size / 1024.0).ceil
203
+ @logger.debug('Use EBS storage to create the virtual machine')
204
+ block_device_mapping_param = InstancesCreatePresenter.new(ephemeral_volume_properties).present
205
+ else
206
+ @logger.debug('Use instance storage to create the virtual machine')
207
+ block_device_mapping_param = default_ephemeral_disk_mapping
208
+ end
209
+
210
+ block_device_mapping_param[0][:bosh_type] = 'ephemeral'
211
+
212
+ if raw_instance_storage
213
+ next_device = first_raw_ephemeral_device(virtualization_type)
214
+ for i in 0..local_disk_info.count - 1 do
215
+ block_device_mapping_param << {
216
+ virtual_name: "ephemeral#{i}",
217
+ device_name: next_device,
218
+ bosh_type: "raw_ephemeral",
219
+ }
220
+ next_device = next_device.next
221
+ end
222
+ end
223
+
224
+ if (resource_pool.has_key?('root_disk'))
225
+ root_disk_size_in_mb = resource_pool['root_disk']['size']
226
+ root_disk_type = resource_pool['root_disk'].fetch('type', 'standard')
227
+ root_disk_iops = resource_pool['root_disk']['iops']
228
+ root_disk_volume_properties = VolumeProperties.new(
229
+ size: root_disk_size_in_mb,
230
+ type: root_disk_type,
231
+ iops: root_disk_iops
232
+ )
233
+ root_disk_volume_properties.validate!
234
+
235
+ root_device = {
236
+ :volume_size => (root_disk_size_in_mb / 1024.0).ceil,
237
+ :volume_type => root_disk_type,
238
+ :delete_on_termination => true,
239
+ }
240
+
241
+ if root_disk_type == 'io1' && root_disk_iops > 0
242
+ root_device[:iops] = root_disk_iops
243
+ end
244
+
245
+ if virtualization_type == :hvm
246
+ block_device_mapping_param << {
247
+ device_name: "/dev/xvda",
248
+ ebs: root_device
249
+ }
250
+ else
251
+ block_device_mapping_param << {
252
+ device_name: "/dev/sda",
253
+ ebs: root_device
254
+ }
193
255
  end
194
256
  end
257
+
258
+
259
+ block_device_mapping_param
195
260
  end
196
261
 
197
- def set_key_name_parameter(resource_pool_key_name, default_aws_key_name)
262
+ def first_raw_ephemeral_device(virtualization_type)
263
+ case virtualization_type
264
+ when :hvm
265
+ '/dev/xvdba'
266
+ when :paravirtual
267
+ '/dev/sdc'
268
+ else
269
+ raise Bosh::Clouds::CloudError, "unknown virtualization type #{virtualization_type}"
270
+ end
271
+ end
272
+
273
+ def set_key_name_parameter(instance_params, resource_pool_key_name, default_aws_key_name)
198
274
  key_name = resource_pool_key_name || default_aws_key_name
199
275
  instance_params[:key_name] = key_name unless key_name.nil?
200
276
  end
201
277
 
202
- def set_security_groups_parameter(networks_spec, default_security_groups)
203
- security_group_names = extract_security_group_names(networks_spec)
204
- if security_group_names.empty?
205
- instance_params[:security_groups] = default_security_groups
278
+ def set_security_groups_parameter(instance_params, networks_spec, default_security_groups)
279
+ security_groups = extract_security_groups(networks_spec)
280
+ if security_groups.empty?
281
+ validate_and_prepare_security_groups_parameter(instance_params, default_security_groups)
206
282
  else
207
- instance_params[:security_groups] = security_group_names
283
+ validate_and_prepare_security_groups_parameter(instance_params, security_groups)
208
284
  end
209
285
  end
210
286
 
211
- def set_vpc_parameters(network_spec)
287
+ def set_vpc_parameters(instance_params, network_spec)
212
288
  manual_network_spec = network_spec.values.select { |spec| ["manual", nil].include? spec["type"] }.first
213
289
  if manual_network_spec
214
290
  instance_params[:private_ip_address] = manual_network_spec["ip"]
215
291
  end
216
-
217
- subnet_network_spec = network_spec.values.select { |spec|
218
- ["manual", nil, "dynamic"].include?(spec["type"]) &&
219
- spec.fetch("cloud_properties", {}).has_key?("subnet")
292
+
293
+ subnet_network_spec = network_spec.values.select { |spec|
294
+ ["manual", nil, "dynamic"].include?(spec["type"]) &&
295
+ spec.fetch("cloud_properties", {}).has_key?("subnet")
220
296
  }.first
221
297
  if subnet_network_spec
222
- instance_params[:subnet] = @region.subnets[subnet_network_spec["cloud_properties"]["subnet"]]
223
- end
298
+ instance_params[:subnet] = @region.subnets[subnet_network_spec["cloud_properties"]["subnet"]]
299
+ end
224
300
  end
225
301
 
226
- def set_availability_zone_parameter(volume_zones, resource_pool_zone, subnet_zone)
302
+ def set_availability_zone_parameter(instance_params, volume_zones, resource_pool_zone, subnet_zone)
227
303
  availability_zone = @az_selector.common_availability_zone(volume_zones, resource_pool_zone, subnet_zone)
228
304
  instance_params[:availability_zone] = availability_zone if availability_zone
229
305
  end
230
306
 
231
- def set_user_data_parameter(networks_spec)
307
+ def set_user_data_parameter(instance_params, networks_spec)
232
308
  user_data = {registry: {endpoint: @registry.endpoint}}
233
309
 
234
310
  spec_with_dns = networks_spec.values.select { |spec| spec.has_key? "dns" }.first
235
311
  user_data[:dns] = {nameserver: spec_with_dns["dns"]} if spec_with_dns
236
312
 
237
- @instance_params[:user_data] = Yajl::Encoder.encode(user_data)
313
+ instance_params[:user_data] = Yajl::Encoder.encode(user_data)
238
314
  end
239
315
 
240
- private
316
+ def set_iam_instance_profile_parameter(instance_params, resource_pool_iam_instance_profile, default_aws_iam_instance_profile)
317
+ iam_instance_profile = resource_pool_iam_instance_profile || default_aws_iam_instance_profile
318
+ instance_params[:iam_instance_profile] = iam_instance_profile unless iam_instance_profile.nil?
319
+ end
320
+
321
+ def validate_and_prepare_security_groups_parameter(instance_params, security_groups)
322
+ return if security_groups.nil? || security_groups.empty?
323
+
324
+ is_id = is_security_group_id?(security_groups.first)
325
+
326
+ security_groups.drop(1).each do |security_group|
327
+ unless is_security_group_id?(security_group) == is_id
328
+ raise Bosh::Clouds::CloudError, 'security group names and ids can not be used together in security groups'
329
+ end
330
+ end
241
331
 
242
- def instance_create_wait_time; 30; end
332
+ if is_id
333
+ instance_params[:security_group_ids] = security_groups
334
+ else
335
+ instance_params[:security_groups] = security_groups
336
+ end
337
+ end
338
+
339
+ def is_security_group_id?(security_group)
340
+ security_group.start_with?('sg-') && security_group.size == 11
341
+ end
243
342
  end
244
- end
343
+ end
@@ -0,0 +1,29 @@
1
+ module Bosh
2
+ module AwsCloud
3
+ class InstancesCreatePresenter
4
+ attr_reader :volume_properties
5
+ def initialize(volume_properties)
6
+ @volume_properties = volume_properties
7
+ end
8
+
9
+ def present
10
+ ebs = {
11
+ volume_size: volume_size_in_gb,
12
+ volume_type: volume_properties.type,
13
+ delete_on_termination: true,
14
+ }
15
+
16
+ ebs[:iops] = volume_properties.iops if volume_properties.iops
17
+
18
+ [{device_name: '/dev/sdb', ebs: ebs}]
19
+ end
20
+
21
+ private
22
+
23
+ def volume_size_in_gb
24
+ (volume_properties.size / 1024.0).ceil
25
+ end
26
+ end
27
+ end
28
+ end
29
+