extras/list-stock-amis
#!/usr/local/ruby-current/bin/ruby
# Copyright:: Copyright (c) 2019 eGlobalTech, Inc., all rights reserved
#
# Licensed under the BSD-3 license (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License in the root of the project or at
#
# http://egt-labs.com/mu/LICENSE.html
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require 'optimist'
require 'json'
require 'yaml'
require File.realpath(File.expand_path(File.dirname(__FILE__)+"/../bin/mu-load-config.rb"))
require 'mu'
credentials = if ARGV[0] and !ARGV[0].empty?
ARGV[0]
else
nil
end
filters = [
{
name: "owner-id",
values: [MU::Cloud::AWS.credToAcct(credentials)]
}
]
platforms = {}
MU::Cloud::AWS.listRegions.each { | r|
images = MU::Cloud::AWS.ec2(region: r, credentials: credentials).describe_images(
filters: filters + [{ "name" => "state", "values" => ["available"]}]
).images
images.each { |ami|
if (DateTime.now.to_time - DateTime.parse(ami.creation_date).to_time) < 15552000 and ami.name.match(/^MU-PROD-\d{10}-[A-Z]{2}-(.*)/)
platform = Regexp.last_match[1].downcase
next if !platform
platforms[platform] ||= {}
if !platforms[platform][r] or
DateTime.parse(ami.creation_date).to_time > platforms[platform][r]['date']
platforms[platform][r] = {
"date" => DateTime.parse(ami.creation_date).to_time,
"ami" => ami.image_id,
"name" => ami.name
}
end
end
}
}
platforms.each_pair { |p, r_data|
r_data.each_pair { |r, data|
r_data[r] = data["ami"]
}
}
puts platforms.to_yaml