Class: EC2Bootstrap::AMI

Inherits:
Object
  • Object
show all
Defined in:
lib/ec2_bootstrap/ami.rb

Constant Summary collapse

DEFAULT_AWS_REGION =
'us-east-1'
DEFAULT_IMAGE_FILTERS =
[
		{name: 'image-type', values: ['machine']},
		{name: 'state', values: ['available']}
]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, logger) ⇒ AMI

Returns a new instance of AMI.



14
15
16
17
18
# File 'lib/ec2_bootstrap/ami.rb', line 14

def initialize(config, logger)
	@region = config.delete('region') || DEFAULT_AWS_REGION
	@search_options = config
	@logger = logger
end

Class Method Details

.from_config(config, logger) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/ec2_bootstrap/ami.rb', line 20

def self.from_config(config, logger)
	if config['filters']
		config['filters'] = config['filters'].to_a.map {|filter| {name: filter.first, values: filter.last}}
		config['filters'] += DEFAULT_IMAGE_FILTERS
	end
	return self.new(config, logger)
end

Instance Method Details

#fetch_eligible_imagesObject



40
41
42
43
# File 'lib/ec2_bootstrap/ami.rb', line 40

def fetch_eligible_images
	ENV['AWS_REGION'] = @region
	return Aws::EC2::Resource.new.images(@search_options)
end

#find_newest_image_idObject



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ec2_bootstrap/ami.rb', line 28

def find_newest_image_id
	images = self.fetch_eligible_images

	newest_image = images.max_by{|i| Time.parse(i.creation_date)}
	newest_image_id = newest_image ? newest_image.id : nil

	@logger.error("Couldn't find any AMIs matching your specifications. Can't set a default AMI.") unless newest_image_id
	@logger.info("Using #{newest_image_id} as the default AMI.") if newest_image_id

	return newest_image_id
end