class Elbas::AWS::AMI

Constants

DEPLOY_GROUP_TAG
DEPLOY_ID_TAG

Attributes

id[R]
snapshots[R]

Public Class Methods

create(instance, no_reboot: true) click to toggle source
# File lib/elbas/aws/ami.rb, line 41
def self.create(instance, no_reboot: true)
  ami = instance.aws_counterpart.create_image({
    name: "ELBAS-ami-#{Time.now.to_i}",
    instance_id: instance.id,
    no_reboot: no_reboot
  })

  new ami.id
end
new(id, snapshots = []) click to toggle source
# File lib/elbas/aws/ami.rb, line 11
def initialize(id, snapshots = [])
  @id = id
  @aws_counterpart = ::Aws::EC2::Image.new id, client: aws_client

  @snapshots = snapshots.map do |snapshot|
    Elbas::AWS::Snapshot.new snapshot&.ebs&.snapshot_id
  end
end

Public Instance Methods

ancestors() click to toggle source
# File lib/elbas/aws/ami.rb, line 28
def ancestors
  aws_amis_in_deploy_group.select { |aws_ami|
    deploy_id_from_aws_tags(aws_ami.tags) != deploy_id
  }.map { |aws_ami|
    self.class.new aws_ami.image_id, aws_ami.block_device_mappings
  }
end
delete() click to toggle source
# File lib/elbas/aws/ami.rb, line 36
def delete
  aws_client.deregister_image image_id: id
  snapshots.each(&:delete)
end
deploy_group() click to toggle source
# File lib/elbas/aws/ami.rb, line 24
def deploy_group
  tags[DEPLOY_GROUP_TAG]
end
deploy_id() click to toggle source
# File lib/elbas/aws/ami.rb, line 20
def deploy_id
  tags[DEPLOY_ID_TAG]
end

Private Instance Methods

aws_amis_in_deploy_group() click to toggle source
# File lib/elbas/aws/ami.rb, line 56
def aws_amis_in_deploy_group
  aws_client.describe_images({
    owners: ['self'],
    filters: [{
      name: "tag:#{DEPLOY_GROUP_TAG}",
      values: [deploy_group],
    }]
  }).images
end
aws_namespace() click to toggle source
# File lib/elbas/aws/ami.rb, line 52
def aws_namespace
  ::Aws::EC2
end
deploy_id_from_aws_tags(tags) click to toggle source
# File lib/elbas/aws/ami.rb, line 66
def deploy_id_from_aws_tags(tags)
  tags.detect { |tag| tag.key == DEPLOY_ID_TAG }&.value
end