Class: OpenStax::Aws::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/openstax/aws/image.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id: nil, region: nil, aws_image: nil) ⇒ Image

Returns a new instance of Image.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/openstax/aws/image.rb', line 6

def initialize(id: nil, region: nil, aws_image: nil)
  if aws_image
    @aws_image = aws_image
  else
    if id.nil? || region.nil?
      raise ArgumentError, "`id` and `region` must be provided"
    end

    @aws_image = Aws::EC2::Image.new(id, region: region)
  end
end

Instance Attribute Details

#aws_imageObject (readonly)

Returns the value of attribute aws_image.



4
5
6
# File 'lib/openstax/aws/image.rb', line 4

def aws_image
  @aws_image
end

Class Method Details

.find_by_sha(sha:, region:, deployment_sha: nil) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/openstax/aws/image.rb', line 28

def self.find_by_sha(sha:, region:, deployment_sha: nil)
  filters = [{name: "tag:sha", values: [sha]}]
  filters << {name: "tag:deployment_sha", values: [deployment_sha]} unless deployment_sha.nil?

  Aws::EC2::Client.new(region: region).describe_images({
    owners: ['self'], filters: filters
  }).images.map{|aws_image| new(aws_image: aws_image)}
end

Instance Method Details

#get_tag(key:) ⇒ Object



18
19
20
21
22
# File 'lib/openstax/aws/image.rb', line 18

def get_tag(key:)
  tag = aws_image.tags.find{|tag| tag.key == key}
  raise "No tag with key #{key} on AMI #{aws_image.image_id}" if tag.nil?
  tag.value
end

#shaObject



24
25
26
# File 'lib/openstax/aws/image.rb', line 24

def sha
  get_tag(key: "sha")
end