Module: Enscalator::Plugins::AmazonLinux

Defined in:
lib/enscalator/plugins/amazon_linux.rb

Overview

Amazon Linux appliance

Constant Summary collapse

STORAGE =

Supported storage types in AWS

[:ebs, :'instance-store']
EBS_VOLUME_TYPES =

Supported EBS volume types

[:standard, :gp2]
ARCH =

Supported Debian image architectures

[:x86_64, :i386]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_ami(region:, release: '2015.09.1', storage: :ebs, arch: :x86_64, ebs_type: :gp2, filters: {}) ⇒ String

Returns first ami-id found for the query.

Parameters:

  • region (Symbol, String)

    name

  • release (Symbol, String) (defaults to: '2015.09.1')

    a codename or version number

  • storage (Symbol) (defaults to: :ebs)

    storage kind

  • arch (Symbol) (defaults to: :x86_64)

    architecture

  • ebs_type (Symbol) (defaults to: :gp2)

    (:standard or :gp2)

  • filters (Hash) (defaults to: {})

    search filters

Returns:

  • (String)

    first ami-id found for the query

Raises:

  • (ArgumentError)

    if storage is nil, empty or not one of supported values

  • (ArgumentError)

    if arch is nil, empty or not one of supported values



24
25
26
27
28
29
30
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
# File 'lib/enscalator/plugins/amazon_linux.rb', line 24

def get_ami(region:, release: '2015.09.1', storage: :ebs, arch: :x86_64, ebs_type: :gp2, filters: {})
  fail ArgumentError, "storage can only be one of #{STORAGE}" unless STORAGE.include? storage
  fail ArgumentError, "arch can only be one of #{ARCH}" unless ARCH.include? arch
  fail ArgumentError, "ebs_type can only be one of #{EBS_VOLUME_TYPES}" unless EBS_VOLUME_TYPES.include? ebs_type

  client = Aws::EC2::Client.new(region: region)

  resp = client.describe_images(
    owners: ['amazon'],
    filters: [
      {
        name: 'name',
        values: %W(amzn-ami-hvm-#{release}* amzn-ami-pv-#{release}*)
      },
      {
        name: 'root-device-type',
        values: [storage.to_s]
      },
      {
        name: 'architecture',
        values: [arch.to_s]
      }
    ] + filters_map_to_array(filters)
  )

  err_msg = format('Could not find any Linux Amazon Ami that fits the criteria: %s, %s, %s, %s, %s, %s',
                   region, release, storage, arch, ebs_type, filters)
  fail StandardError, err_msg unless resp.images

  images = resp.images.sort_by(&:creation_date).reverse
  images = images.select { |i| i.block_device_mappings.first.ebs.volume_type == ebs_type.to_s } if storage == :ebs
  images.first.image_id
end

Instance Method Details

#amazon_linux_init(region: self.region, release: '2015.09.1', storage: :ebs, arch: :x86_64, ebs_type: :gp2, filters: {}) ⇒ Object

Create AMI id parameter for an Amazon linux instance

Parameters:

  • region (Symbol, String) (defaults to: self.region)

    name

  • release (Symbol, String) (defaults to: '2015.09.1')

    a codename or version number

  • storage (Symbol) (defaults to: :ebs)

    storage kind (ebs or instance_store)

  • arch (String) (defaults to: :x86_64)

    architecture (x86_64 or i386)

  • ebs_type (Symbol) (defaults to: :gp2)

    (:standard or :gp2)

  • filters (Hash) (defaults to: {})

    filters for the search



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/enscalator/plugins/amazon_linux.rb', line 78

def amazon_linux_init(region: self.region,
                      release: '2015.09.1',
                      storage: :ebs,
                      arch: :x86_64,
                      ebs_type: :gp2,
                      filters: {})
  parameter_ami 'AmazonLinux', AmazonLinux.get_ami(region: region,
                                                   release: release,
                                                   storage: storage,
                                                   arch: arch,
                                                   ebs_type: ebs_type,
                                                   filters: filters)
end