Class: AwsUtils::Ec2LatestImage

Inherits:
Object
  • Object
show all
Defined in:
lib/awsutils/ec2latestimage.rb

Instance Method Summary collapse

Instance Method Details

#releasesObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/awsutils/ec2latestimage.rb', line 8

def releases
  @releases ||= begin
    parsed_releases =
      if opts[:ownedbyme]
        raise 'AWS_OWNER_ID not defined' unless ENV['AWS_OWNER_ID']

        require 'aws-sdk-ec2'

        ubuntu_images =
          connection.describe_images(owners: [ENV['AWS_OWNER_ID']]).images.select do |image|
            image.name =~ %r{^ubuntu\/}
          end

        ubuntu_images.map do |image|
          {
            ami: image.image_id,
            distro_version: image.name.split('/')[3].split('-')[2],
            release: image.name.split('/')[3].split('-')[5..-1].join('-'),
            type: image.name.split('/')[2] + ':' + image.root_device_type,
            arch: image.architecture,
            region: opts[:region] # overriding this because images API doesn't list a region
          }
        end
      else
        resp = JSON.parse(
          Net::HTTP.get(
            URI("http://cloud-images.ubuntu.com/locator/ec2/releasesTable?_=#{(Time.now.to_f * 1000).to_i}")
          ).sub(/\],\n\]/, "]\n]")
        )
        parse_releases_array(resp['aaData'])
      end

    parsed_releases.select do |rel|
      rel[:region] == opts[:region] &&
        rel[:distro_version] == opts[:release].to_s &&
        %w[amd64 x86_64].include?(rel[:arch])
    end
  end
end

#runObject



48
49
50
# File 'lib/awsutils/ec2latestimage.rb', line 48

def run
  print_releases
end