Class: Capistrano::ASG::Rolling::AMI

Inherits:
Object
  • Object
show all
Includes:
AWS
Defined in:
lib/capistrano/asg/rolling/ami.rb

Overview

AWS EC2 Machine Image.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AWS

#aws_autoscaling_client, #aws_ec2_client

Constructor Details

#initialize(id, instance = nil) ⇒ AMI

Returns a new instance of AMI.



12
13
14
15
# File 'lib/capistrano/asg/rolling/ami.rb', line 12

def initialize(id, instance = nil)
  @id = id
  @instance = instance
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



10
11
12
# File 'lib/capistrano/asg/rolling/ami.rb', line 10

def id
  @id
end

#instanceObject (readonly)

Returns the value of attribute instance.



10
11
12
# File 'lib/capistrano/asg/rolling/ami.rb', line 10

def instance
  @instance
end

Class Method Details

.create(instance:, name:, description: nil, tags: nil) ⇒ Object

Create an AMI from an instance and wait until the AMI is available.



18
19
20
21
22
23
24
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
# File 'lib/capistrano/asg/rolling/ami.rb', line 18

def self.create(instance:, name:, description: nil, tags: nil)
  aws_ec2_client = instance.aws_ec2_client

  options = {
    instance_id: instance.id,
    name: name,
    description: description
  }

  if tags
    tag_specifications = tags.map { |key, value| { key: key, value: value } }

    options[:tag_specifications] = [
      { resource_type: 'image', tags: tag_specifications },
      { resource_type: 'snapshot', tags: tag_specifications }
    ]
  end

  response = aws_ec2_client.create_image(options)

  begin
    aws_ec2_client.wait_until(:image_available, image_ids: [response.image_id]) do |waiter|
      waiter.delay = Configuration.ami_wait_delay
      waiter.max_attempts = Configuration.ami_wait_max_attempts
    end
  rescue Aws::Waiters::Errors::TooManyAttemptsError => e
    # When waiting for the AMI takes longer than the default (10 minutes),
    # then assume it will eventually succeed and just continue. Surface a
    # warning so operators can see that the wait did not complete in time
    # (for example after increasing the root volume size of the source
    # instance) before the subsequent Instance Refresh is started.
    Kernel.warn("WARNING: timed out waiting for AMI #{response.image_id} to become available: #{e.message}")
  end

  new(response.image_id, instance)
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



83
84
85
# File 'lib/capistrano/asg/rolling/ami.rb', line 83

def ==(other)
  id == other.id
end

#deleteObject



59
60
61
62
63
64
65
66
67
# File 'lib/capistrano/asg/rolling/ami.rb', line 59

def delete
  # Retrieve the snapshots first because we can't call #describe_images anymore
  # after deregistering the image.
  image_snapshots = snapshots

  aws_ec2_client.deregister_image(image_id: id)

  image_snapshots.each(&:delete)
end

#exists?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/capistrano/asg/rolling/ami.rb', line 55

def exists?
  aws_ec2_image.exists?
end

#hashObject



89
90
91
# File 'lib/capistrano/asg/rolling/ami.rb', line 89

def hash
  id.hash
end

#snapshotsObject



69
70
71
72
73
# File 'lib/capistrano/asg/rolling/ami.rb', line 69

def snapshots
  @snapshots ||= aws_ec2_image.block_device_mappings.filter_map do |mapping|
    Snapshot.new(mapping.ebs.snapshot_id) if mapping.ebs
  end
end

#tag?(key) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/capistrano/asg/rolling/ami.rb', line 79

def tag?(key)
  tags.key?(key)
end

#tagsObject



75
76
77
# File 'lib/capistrano/asg/rolling/ami.rb', line 75

def tags
  @tags ||= aws_ec2_image.tags.to_h { |tag| [tag.key, tag.value] }
end