Class: Bosh::AwsCloud::Cloud

Inherits:
Cloud
  • Object
show all
Includes:
Helpers
Defined in:
lib/cloud/aws/cloud.rb

Constant Summary collapse

DEFAULT_MAX_RETRIES =

default maximum number of times to retry an AWS API call

2
METADATA_TIMEOUT =

in seconds

5
DEVICE_POLL_TIMEOUT =

in seconds

60

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#cloud_error, #default_ephemeral_disk_mapping, #ebs_ephemeral_disk_mapping, #extract_security_groups

Constructor Details

#initialize(options) ⇒ Cloud

Initialize BOSH AWS CPI. The contents of sub-hashes are defined in the README

Parameters:

  • options (Hash)

    CPI options

Options Hash (options):

  • aws (Hash)

    AWS specific options

  • agent (Hash)

    agent options

  • registry (Hash)

    agent options



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
# File 'lib/cloud/aws/cloud.rb', line 25

def initialize(options)
  @options = options.dup.freeze
  validate_options
  validate_credentials_source

  @logger = Bosh::Clouds::Config.logger
  aws_logger = @logger

  @aws_params = {
    credentials_source: aws_properties['credentials_source'] || 'static',
    region:            aws_properties['region'],
    ec2_endpoint:      aws_properties['ec2_endpoint'] || default_ec2_endpoint,
    elb_endpoint:      aws_properties['elb_endpoint'] || default_elb_endpoint,
    max_retries:       aws_properties['max_retries']  || DEFAULT_MAX_RETRIES,
    logger:            aws_logger
  }

  %w(
    http_read_timeout
    http_wire_trace
    proxy_uri
    ssl_verify_peer
    ssl_ca_file
    ssl_ca_path
  ).each do |k|
    @aws_params[k.to_sym] = aws_properties[k] unless aws_properties[k].nil?
  end

  # credentials_source could be static (default) or env_or_profile
  # static credentials must be included in aws_properties
  # env_or_profile credentials will use the AWS DefaultCredentialsProvider
  # to find AWS credentials in environment variables or EC2 instance profiles

  if @aws_params[:credentials_source] == 'static'
    @aws_params[:access_key_id] = aws_properties['access_key_id']
    @aws_params[:secret_access_key] = aws_properties['secret_access_key']
  end

  # AWS Ruby SDK is threadsafe but Ruby autoload isn't,
  # so we need to trigger eager autoload while constructing CPI
  AWS.eager_autoload!

  initialize_aws
  initialize_registry

  elb = AWS::ELB.new(@aws_params)

  @instance_manager = InstanceManager.new(region, registry, elb, az_selector, @logger)

  @metadata_lock = Mutex.new
end

Instance Attribute Details

#ec2Object (readonly)

Returns the value of attribute ec2.



14
15
16
# File 'lib/cloud/aws/cloud.rb', line 14

def ec2
  @ec2
end

#loggerObject

Returns the value of attribute logger.



17
18
19
# File 'lib/cloud/aws/cloud.rb', line 17

def logger
  @logger
end

#optionsObject (readonly)

Returns the value of attribute options.



16
17
18
# File 'lib/cloud/aws/cloud.rb', line 16

def options
  @options
end

#registryObject (readonly)

Returns the value of attribute registry.



15
16
17
# File 'lib/cloud/aws/cloud.rb', line 15

def registry
  @registry
end

Instance Method Details

#attach_disk(instance_id, disk_id) ⇒ Object

Attach an EBS volume to an EC2 instance

Parameters:

  • instance_id (String)

    EC2 instance id of the virtual machine to attach the disk to

  • disk_id (String)

    EBS volume id of the disk to attach



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/cloud/aws/cloud.rb', line 306

def attach_disk(instance_id, disk_id)
  with_thread_name("attach_disk(#{instance_id}, #{disk_id})") do
    instance = @ec2.instances[instance_id]
    volume = @ec2.volumes[disk_id]

    device_name = attach_ebs_volume(instance, volume)

    update_agent_settings(instance) do |settings|
      settings["disks"] ||= {}
      settings["disks"]["persistent"] ||= {}
      settings["disks"]["persistent"][disk_id] = device_name
    end
    logger.info("Attached `#{disk_id}' to `#{instance_id}'")
  end
end

#compare_private_ip_addresses(instance, specified_ip_address) ⇒ void

This method returns an undefined value.

Compares actual instance private IP addresses with the IP address specified at the network spec

change the IP address of a running server, so we need to send the InstanceUpdater a request to do it for us

Parameters:

  • instance (AWS::EC2::Instance)

    EC2 instance

  • specified_ip_address (String)

    IP address specified at the network spec (if Manual Network)

Raises:

  • (Bosh::Clouds:NotSupported)

    If the IP address change, we need to recreate the VM as you can’t



450
451
452
453
454
455
456
457
458
# File 'lib/cloud/aws/cloud.rb', line 450

def compare_private_ip_addresses(instance, specified_ip_address)
  actual_ip_address = instance.private_ip_address

  unless specified_ip_address.nil? || actual_ip_address == specified_ip_address
    raise Bosh::Clouds::NotSupported,
          "IP address change requires VM recreation: %s to %s" %
          [actual_ip_address, specified_ip_address]
  end
end

#compare_security_groups(instance, network_spec) ⇒ Object

If the security groups change, we need to recreate the VM as you can’t change the security group of a running instance, we need to send the InstanceUpdater a request to do it for us



426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# File 'lib/cloud/aws/cloud.rb', line 426

def compare_security_groups(instance, network_spec)
  actual_group_names = instance.security_groups.collect { |sg| sg.name }
  specified_group_names = extract_security_groups(network_spec)
  if specified_group_names.empty?
    new_group_names = Array(aws_properties["default_security_groups"])
  else
    new_group_names = specified_group_names
  end

  unless actual_group_names.sort == new_group_names.sort
    raise Bosh::Clouds::NotSupported,
          "security groups change requires VM recreation: %s to %s" %
              [actual_group_names.join(", "), new_group_names.join(", ")]
  end
end

#configure_networks(instance_id, network_spec) ⇒ Object

Configure network for an EC2 instance

Parameters:

  • instance_id (String)

    EC2 instance id

  • network_spec (Hash)

    network properties

Raises:

  • (Bosh::Clouds:NotSupported)

    if there’s a network change that requires the recreation of the VM



403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# File 'lib/cloud/aws/cloud.rb', line 403

def configure_networks(instance_id, network_spec)
  with_thread_name("configure_networks(#{instance_id}, ...)") do
    logger.info("Configuring '#{instance_id}' to use new network settings: #{network_spec.pretty_inspect}")

    instance = @ec2.instances[instance_id]

    network_configurator = NetworkConfigurator.new(network_spec)

    compare_security_groups(instance, network_spec)

    compare_private_ip_addresses(instance, network_configurator.private_ip)

    network_configurator.configure(@ec2, instance)

    update_agent_settings(instance) do |settings|
      settings["networks"] = agent_network_spec(network_spec)
    end
  end
end

#create_disk(size, cloud_properties, instance_id = nil) ⇒ String

Creates a new EBS volume

Parameters:

  • size (Integer)

    disk size in MiB

  • instance_id (optional, String) (defaults to: nil)

    EC2 instance id of the VM that this disk will be attached to

Returns:

  • (String)

    created EBS volume id



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/cloud/aws/cloud.rb', line 202

def create_disk(size, cloud_properties, instance_id = nil)
  with_thread_name("create_disk(#{size}, #{instance_id})") do
    type = validate_disk_type(cloud_properties.fetch('type', 'standard'))
    validate_disk_size(type, size)

    # if the disk is created for an instance, use the same availability zone as they must match
    volume = @ec2.volumes.create(
      size: (size / 1024.0).ceil,
      availability_zone: @az_selector.select_availability_zone(instance_id),
      volume_type: type,
      encrypted: cloud_properties.fetch('encrypted', false)
    )

    logger.info("Creating volume '#{volume.id}'")
    ResourceWait.for_volume(volume: volume, state: :available)

    volume.id
  end
end

#create_stemcell(image_path, stemcell_properties) ⇒ String

Creates a new EC2 AMI using stemcell image. This method can only be run on an EC2 instance, as image creation involves creating and mounting new EBS volume as local block device.

Parameters:

  • image_path (String)

    local filesystem path to a stemcell image

  • cloud_properties (Hash)

    AWS-specific stemcell properties

Returns:

  • (String)

    EC2 AMI name of the stemcell



476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
# File 'lib/cloud/aws/cloud.rb', line 476

def create_stemcell(image_path, stemcell_properties)
  with_thread_name("create_stemcell(#{image_path}...)") do
    creator = StemcellCreator.new(region, stemcell_properties)

    return creator.fake.id if creator.fake?

    begin
      # These variables are used in 'ensure' clause
      instance = nil
      volume = nil

      # 1. Create and mount new EBS volume (2GB default)
      disk_size = stemcell_properties["disk"] || 2048
      volume_id = create_disk(disk_size, {}, current_vm_id)
      volume = @ec2.volumes[volume_id]
      instance = @ec2.instances[current_vm_id]

      sd_name = attach_ebs_volume(instance, volume)
      ebs_volume = find_ebs_device(sd_name)

      logger.info("Creating stemcell with: '#{volume.id}' and '#{stemcell_properties.inspect}'")
      creator.create(volume, ebs_volume, image_path).id
    rescue => e
      logger.error(e)
      raise e
    ensure
      if instance && volume
        detach_ebs_volume(instance, volume, true)
        delete_disk(volume.id)
      end
    end
  end
end

#create_vm(agent_id, stemcell_id, resource_pool, network_spec, disk_locality = nil, environment = nil) ⇒ String

Create an EC2 instance and wait until it’s in running state

Parameters:

  • agent_id (String)

    agent id associated with new VM

  • stemcell_id (String)

    AMI id of the stemcell used to create the new instance

  • resource_pool (Hash)

    resource pool specification

  • network_spec (Hash)

    network specification, if it contains security groups they must already exist

  • disk_locality (optional, Array) (defaults to: nil)

    list of disks that might be attached to this instance in the future, can be used as a placement hint (i.e. instance will only be created if resource pool availability zone is the same as disk availability zone)

  • environment (optional, Hash) (defaults to: nil)

    data to be merged into agent settings

Returns:

  • (String)

    EC2 instance id of the new virtual machine



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
# File 'lib/cloud/aws/cloud.rb', line 121

def create_vm(agent_id, stemcell_id, resource_pool, network_spec, disk_locality = nil, environment = nil)
  with_thread_name("create_vm(#{agent_id}, ...)") do
    # do this early to fail fast
    stemcell = StemcellFinder.find_by_region_and_id(region, stemcell_id)

    begin
      instance, block_device_agent_info = @instance_manager.create(
        agent_id,
        stemcell.image_id,
        resource_pool,
        network_spec,
        (disk_locality || []),
        environment,
        options,
      )

      logger.info("Creating new instance '#{instance.id}'")

      NetworkConfigurator.new(network_spec).configure(region, instance)

      registry_settings = initial_agent_settings(
        agent_id,
        network_spec,
        environment,
        stemcell.root_device_name,
        block_device_agent_info
      )
      registry.update_settings(instance.id, registry_settings)

      instance.id
    rescue => e # is this rescuing too much?
      logger.error(%Q[Failed to create instance: #{e.message}\n#{e.backtrace.join("\n")}])
      instance.terminate(fast_path_delete?) if instance
      raise e
    end
  end
end

#current_vm_idObject

Reads current instance id from EC2 metadata. We are assuming instance id cannot change while current process is running and thus memoizing it.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/cloud/aws/cloud.rb', line 81

def current_vm_id
  @metadata_lock.synchronize do
    return @current_vm_id if @current_vm_id

    client = HTTPClient.new
    client.connect_timeout = METADATA_TIMEOUT
    # Using 169.254.169.254 is an EC2 convention for getting
    # instance metadata
    uri = "http://169.254.169.254/latest/meta-data/instance-id/"

    response = client.get(uri)
    unless response.status == 200
      cloud_error("Instance metadata endpoint returned " \
                  "HTTP #{response.status}")
    end

    @current_vm_id = response.body
  end

rescue HTTPClient::TimeoutError
  cloud_error("Timed out reading instance metadata, " \
              "please make sure CPI is running on EC2 instance")
end

#default_ec2_endpointObject



159
160
161
# File 'lib/cloud/aws/cloud.rb', line 159

def default_ec2_endpoint
  ['ec2', aws_region, 'amazonaws.com'].compact.join('.')
end

#default_elb_endpointObject



163
164
165
# File 'lib/cloud/aws/cloud.rb', line 163

def default_elb_endpoint
  ['elasticloadbalancing', aws_region, 'amazonaws.com'].compact.join('.')
end

#delete_disk(disk_id) ⇒ Object

Delete EBS volume

Parameters:

  • disk_id (String)

    EBS volume id

Raises:

  • (Bosh::Clouds::CloudError)

    if disk is not in available state



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/cloud/aws/cloud.rb', line 256

def delete_disk(disk_id)
  with_thread_name("delete_disk(#{disk_id})") do
    volume = @ec2.volumes[disk_id]

    logger.info("Deleting volume `#{volume.id}'")

    # Retry 1, 6, 11, 15, 15, 15.. seconds. The total time is ~10 min.
    # VolumeInUse can be returned by AWS if disk was attached to VM
    # that was recently removed.
    tries = ResourceWait::DEFAULT_WAIT_ATTEMPTS
    sleep_cb = ResourceWait.sleep_callback(
      "Waiting for volume `#{volume.id}' to be deleted",
      { interval: 5, total: tries }
    )
    ensure_cb = Proc.new do |retries|
      cloud_error("Timed out waiting to delete volume `#{volume.id}'") if retries == tries
    end
    errors = [AWS::EC2::Errors::VolumeInUse, AWS::EC2::Errors::RequestLimitExceeded]

    Bosh::Common.retryable(tries: tries, sleep: sleep_cb, on: errors, ensure: ensure_cb) do
      begin
        volume.delete
      rescue AWS::EC2::Errors::InvalidVolume::NotFound => e
        logger.warn("Failed to delete disk '#{disk_id}' because it was not found: #{e.inspect}")
        raise Bosh::Clouds::DiskNotFound.new(false), "Disk '#{disk_id}' not found"
      end

      true # return true to only retry on Exceptions
    end

    if fast_path_delete?
      begin
        TagManager.tag(volume, "Name", "to be deleted")
        logger.info("Volume `#{disk_id}' has been marked for deletion")
      rescue AWS::EC2::Errors::InvalidVolume::NotFound
        # Once in a blue moon AWS if actually fast enough that the volume is already gone
        # when we get here, and if it is, our work here is done!
      end
      return
    end

    ResourceWait.for_volume(volume: volume, state: :deleted)

    logger.info("Volume `#{disk_id}' has been deleted")
  end
end

#delete_snapshot(snapshot_id) ⇒ Object

Delete a disk snapshot

Parameters:

  • snapshot_id (String)

    snapshot id to delete



386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/cloud/aws/cloud.rb', line 386

def delete_snapshot(snapshot_id)
  with_thread_name("delete_snapshot(#{snapshot_id})") do
    snapshot = @ec2.snapshots[snapshot_id]

    if snapshot.status == :in_use
      raise Bosh::Clouds::CloudError, "snapshot '#{snapshot.id}' can not be deleted as it is in use"
    end

    snapshot.delete
    logger.info("snapshot '#{snapshot_id}' deleted")
  end
end

#delete_stemcell(stemcell_id) ⇒ Object

Delete a stemcell and the accompanying snapshots

Parameters:

  • stemcell_id (String)

    EC2 AMI name of the stemcell to be deleted



512
513
514
515
516
517
# File 'lib/cloud/aws/cloud.rb', line 512

def delete_stemcell(stemcell_id)
  with_thread_name("delete_stemcell(#{stemcell_id})") do
    stemcell = StemcellFinder.find_by_region_and_id(region, stemcell_id)
    stemcell.delete
  end
end

#delete_vm(instance_id) ⇒ Object

Delete EC2 instance (“terminate” in AWS language) and wait until it reports as terminated

Parameters:

  • instance_id (String)

    EC2 instance id



171
172
173
174
175
176
# File 'lib/cloud/aws/cloud.rb', line 171

def delete_vm(instance_id)
  with_thread_name("delete_vm(#{instance_id})") do
    logger.info("Deleting instance '#{instance_id}'")
    @instance_manager.find(instance_id).terminate(fast_path_delete?)
  end
end

#detach_disk(instance_id, disk_id) ⇒ Object

Detach an EBS volume from an EC2 instance

Parameters:

  • instance_id (String)

    EC2 instance id of the virtual machine to detach the disk from

  • disk_id (String)

    EBS volume id of the disk to detach



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/cloud/aws/cloud.rb', line 325

def detach_disk(instance_id, disk_id)
  with_thread_name("detach_disk(#{instance_id}, #{disk_id})") do
    instance = @ec2.instances[instance_id]
    volume = @ec2.volumes[disk_id]

    if volume.exists?
      detach_ebs_volume(instance, volume)
    else
      @logger.info("Disk `#{disk_id}' not found while trying to detach it from vm `#{instance_id}'...")
    end

    update_agent_settings(instance) do |settings|
      settings["disks"] ||= {}
      settings["disks"]["persistent"] ||= {}
      settings["disks"]["persistent"].delete(disk_id)
    end

    logger.info("Detached `#{disk_id}' from `#{instance_id}'")
  end
end

#find_ebs_device(sd_name) ⇒ Object



546
547
548
549
550
551
552
553
554
555
556
557
558
559
# File 'lib/cloud/aws/cloud.rb', line 546

def find_ebs_device(sd_name)
  xvd_name = sd_name.gsub(/^\/dev\/sd/, "/dev/xvd")

  DEVICE_POLL_TIMEOUT.times do
    if File.blockdev?(sd_name)
      return sd_name
    elsif File.blockdev?(xvd_name)
      return xvd_name
    end
    sleep(1)
  end

  cloud_error("Cannot find EBS volume on current instance")
end

#get_disks(vm_id) ⇒ Object



346
347
348
349
350
351
352
353
354
# File 'lib/cloud/aws/cloud.rb', line 346

def get_disks(vm_id)
  disks = []
  @ec2.instances[vm_id].block_devices.each do |block_device|
    if block_device[:ebs]
      disks << block_device[:ebs][:volume_id]
    end
  end
  disks
end

#has_disk?(disk_id) ⇒ bool

Check whether an OpenStack volume exists or not

Parameters:

  • disk_id (String)

    OpenStack volume UUID

Returns:

  • (bool)

    whether the specific disk is there or not



245
246
247
248
249
250
# File 'lib/cloud/aws/cloud.rb', line 245

def has_disk?(disk_id)
  with_thread_name("has_disk?(#{disk_id})") do
    @logger.info("Check the presence of disk with id `#{disk_id}'...")
    @ec2.volumes[disk_id].exists?
  end
end

#has_vm?(instance_id) ⇒ Boolean

Has EC2 instance

Parameters:

  • instance_id (String)

    EC2 instance id

Returns:

  • (Boolean)


190
191
192
193
194
# File 'lib/cloud/aws/cloud.rb', line 190

def has_vm?(instance_id)
  with_thread_name("has_vm?(#{instance_id})") do
    @instance_manager.find(instance_id).exists?
  end
end

#reboot_vm(instance_id) ⇒ Object

Reboot EC2 instance

Parameters:

  • instance_id (String)

    EC2 instance id



181
182
183
184
185
# File 'lib/cloud/aws/cloud.rb', line 181

def reboot_vm(instance_id)
  with_thread_name("reboot_vm(#{instance_id})") do
    @instance_manager.find(instance_id).reboot
  end
end

#set_vm_metadata(vm, metadata) ⇒ void

This method returns an undefined value.

Add tags to an instance. In addition to the suplied tags, it adds a ‘Name’ tag as it is shown in the AWS console.

Parameters:

  • vm (String)

    vm id that was once returned by #create_vm

  • metadata (Hash)

    metadata key/value pairs



524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
# File 'lib/cloud/aws/cloud.rb', line 524

def (vm, )
   = Hash[.map{|key,value| [key.to_s, value] }]

  instance = @ec2.instances[vm]

  .each_pair do |key, value|
    TagManager.tag(instance, key, value)
  end

  job = ['job']
  index = ['index']

  if job && index
    name = "#{job}/#{index}"
  elsif ['compiling']
    name = "compiling/#{['compiling']}"
  end
  TagManager.tag(instance, "Name", name) if name
rescue AWS::EC2::Errors::TagLimitExceeded => e
  logger.error("could not tag #{instance.id}: #{e.message}")
end

#snapshot_disk(disk_id, metadata) ⇒ String

Take snapshot of disk

Parameters:

  • disk_id (String)

    disk id of the disk to take the snapshot of

Returns:

  • (String)

    snapshot id



359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/cloud/aws/cloud.rb', line 359

def snapshot_disk(disk_id, )
   = Hash[.map{|key,value| [key.to_s, value] }]

  with_thread_name("snapshot_disk(#{disk_id})") do
    volume = @ec2.volumes[disk_id]
    devices = []
    volume.attachments.each {|attachment| devices << attachment.device}

    name = ['deployment', 'job', 'index'].collect { |key| [key] }
    name << devices.first.split('/').last unless devices.empty?

    snapshot = volume.create_snapshot(name.join('/'))
    logger.info("snapshot '#{snapshot.id}' of volume '#{disk_id}' created")

    ['agent_id', 'instance_id', 'director_name', 'director_uuid'].each do |key|
      TagManager.tag(snapshot, key, [key])
    end
    TagManager.tag(snapshot, 'device', devices.first) unless devices.empty?
    TagManager.tag(snapshot, 'Name', name.join('/'))

    ResourceWait.for_snapshot(snapshot: snapshot, state: :completed)
    snapshot.id
  end
end

#validate_disk_size(type, size) ⇒ Object

Raises:

  • (ArgumentError)


222
223
224
225
226
227
228
229
230
231
# File 'lib/cloud/aws/cloud.rb', line 222

def validate_disk_size(type, size)
  raise ArgumentError, 'disk size needs to be an integer' unless size.kind_of?(Integer)

  cloud_error('AWS CPI minimum disk size is 1 GiB') if size < 1024
  if type == 'standard'
    cloud_error('AWS CPI maximum disk size is 1 TiB') if size > 1024 * 1000
  else
    cloud_error('AWS CPI maximum disk size is 16 TiB') if size > 1024 * 16000
  end
end

#validate_disk_type(type) ⇒ Object



233
234
235
236
237
238
# File 'lib/cloud/aws/cloud.rb', line 233

def validate_disk_type(type)
  unless %w[gp2 standard io1].include?(type)
    cloud_error('AWS CPI supports only gp2, io1, or standard disk type')
  end
  type
end