Sha256: 7dda9ac58ba577498df500dde92af9596cf9b97b37564054572ebfcee266e2d8
Contents?: true
Size: 1.19 KB
Versions: 11
Compression:
Stored size: 1.19 KB
Contents
module AwsEc2::Cleaner class Ami < AwsEc2::Base include AwsEc2::AwsService def clean query = @options[:query] keep = @options[:keep] || 2 puts "Cleaning out old AMIs with base name: #{@options[:query]}" return if ENV['TEST'] images = search_ami(query) images = images.sort_by { |i| i.name }.reverse delete_list = images[keep..-1] || [] puts "Deleting #{delete_list.size} images." delete_list.each do |i| delete(i) end end private def delete(image) message = "Deleting image: #{image.image_id} #{image.name}" if @options[:noop] puts "NOOP: #{message}" else puts message ec2.deregister_image(image_id: image.image_id) end rescue Aws::EC2::Errors::InvalidAMIIDUnavailable # happens when image was just deleted but its still # showing up as available when calling describe_images puts "WARN: #{e.message}" end def search_ami(query, owners=["self"]) ec2.describe_images( owners: owners, filters: [ {name: "name", values: [query]}, {name: "state", values: ["available"]} ] ).images end end end
Version data entries
11 entries across 11 versions & 1 rubygems