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
# 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])
  rescue Aws::Waiters::Errors::TooManyAttemptsError
    # When waiting for the AMI takes longer than the default (10 minutes),
    # then assume it will eventually succeed and just continue.
  end

  new(response.image_id, instance)
end

Instance Method Details

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



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

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

#deleteObject



52
53
54
55
56
57
58
59
60
# File 'lib/capistrano/asg/rolling/ami.rb', line 52

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)


48
49
50
# File 'lib/capistrano/asg/rolling/ami.rb', line 48

def exists?
  aws_ec2_image.exists?
end

#hashObject



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

def hash
  id.hash
end

#snapshotsObject



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

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)


72
73
74
# File 'lib/capistrano/asg/rolling/ami.rb', line 72

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

#tagsObject



68
69
70
# File 'lib/capistrano/asg/rolling/ami.rb', line 68

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