Class: Capistrano::Autoscale::AWS::AMI

Inherits:
Base
  • Object
show all
Includes:
Taggable
Defined in:
lib/capistrano/autoscale/aws/ami.rb

Constant Summary collapse

DEPLOY_GROUP_TAG =
'Autoscale-Deploy-group'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Taggable

#tag, #tags

Methods inherited from Base

#autoscaling_client, #aws_access_key_id, #aws_autoscale_ami_tags, #aws_autoscale_snapshot_tags, #aws_credentials, #aws_options, #aws_region, #aws_secret_access_key, #ec2_client

Constructor Details

#initialize(id, block_device_mappings = []) ⇒ AMI

Returns a new instance of AMI.



13
14
15
16
17
18
19
20
# File 'lib/capistrano/autoscale/aws/ami.rb', line 13

def initialize(id, block_device_mappings = [])
  @id = id
  @aws_counterpart = ::Aws::EC2::Image.new id, client: ec2_client

  @snapshots = block_device_mappings.map do |mapping|
    Capistrano::Autoscale::AWS::Snapshot.new mapping&.ebs&.snapshot_id
  end
end

Instance Attribute Details

#aws_counterpartObject (readonly)

Returns the value of attribute aws_counterpart.



11
12
13
# File 'lib/capistrano/autoscale/aws/ami.rb', line 11

def aws_counterpart
  @aws_counterpart
end

#idObject (readonly)

Returns the value of attribute id.



11
12
13
# File 'lib/capistrano/autoscale/aws/ami.rb', line 11

def id
  @id
end

#snapshotsObject (readonly)

Returns the value of attribute snapshots.



11
12
13
# File 'lib/capistrano/autoscale/aws/ami.rb', line 11

def snapshots
  @snapshots
end

Class Method Details

.create(instance, prefix: 'autoscale', no_reboot: false) ⇒ Object



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
# File 'lib/capistrano/autoscale/aws/ami.rb', line 31

def self.create(instance, prefix: 'autoscale', no_reboot: false)
  name = "#{prefix}-#{Time.now.to_i}"

  image = instance.aws_counterpart.create_image(
    name: name,
    instance_id: instance.id,
    no_reboot: no_reboot
  )
  image = image.wait_until_exists

  block_device_mappings = nil
  # Wait until block_device_mappings/snapshots are available.
  loop do
    block_device_mappings = image.block_device_mappings
    snapshot_ids = block_device_mappings.map { |mapping| mapping.ebs&.snapshot_id }.compact
    break if !snapshot_ids.empty? || image.state == 'failed'

    sleep 1
    image.load
  end

  raise Capistrano::Autoscale::Errors::CreateImageFailed, image if image.state == 'failed'

  ami = new image.id, block_device_mappings
  ami.tag 'Name', name
  ami.snapshots.each { |snapshot| snapshot.tag 'Name', name }
  ami
end

Instance Method Details

#create_tags(deploy_group) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/capistrano/autoscale/aws/ami.rb', line 60

def create_tags(deploy_group)
  tag(DEPLOY_GROUP_TAG, deploy_group)

  aws_autoscale_ami_tags.each { |key, value| tag(key, value) }

  snapshots.each(&:create_tags)
end

#deleteObject



26
27
28
29
# File 'lib/capistrano/autoscale/aws/ami.rb', line 26

def delete
  ec2_client.deregister_image image_id: id
  snapshots.each(&:delete)
end

#deploy_groupObject



22
23
24
# File 'lib/capistrano/autoscale/aws/ami.rb', line 22

def deploy_group
  tags[DEPLOY_GROUP_TAG]
end