aws_ami_cleanup 0.2 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8f995865e05bc638aa49921b3a41e44490162333f63cf48498c90253c03c7998
4
- data.tar.gz: 15f352aef72ad76110950ea2b0a2f6e0da48c22d6bd0e5ac3ae71f81d2d94eaf
3
+ metadata.gz: 25ac02ebfcda946944410be7a4044e5a01949152337baa037f3fa9ea9be8b9a2
4
+ data.tar.gz: 91e0c6ceb77364a9ba929b693b7f33a0ff2e742ce889eeb33cfb1bf29368b007
5
5
  SHA512:
6
- metadata.gz: 5cbb4cdf40523ace6141d987c089f20a500119c47316e5f45c9ec802dac099caef79b7b4b05f5b07da1b24b824274fafb261ae8f4fe283bb43c40dde11b07bde
7
- data.tar.gz: 36ca22c54797eef7984e64a36b190e83af58365d6bf277ebd7ebc0cf5dad1e0e118b8ea2ac1321f39833b295d710b15c4afdae4c916dcfdb6e415b7719a99f74
6
+ metadata.gz: 797cdde4f267868ae2287ae5fc46ac8c30dc711dee230dda7563ee498dc0d523eee165543808f7474c6ec00ee135f08dd9037dfc4a627eec6c5c605c815fd756
7
+ data.tar.gz: 4dc150e871dece040998b2e87ef6fe851e636869c4768578b7b629b226bd4e2aa01606ae2097bf12ffc209615495edcc650d2d0b4ebd1ffa9420786dfbae7559
data/README.md CHANGED
@@ -17,4 +17,9 @@ cleanup_amis clean_amis --ami_name 'my-ami' --ami_owner 'self'
17
17
 
18
18
  Where `ami_owner` can be a combination of AWS account IDs, `self`, `amazon`, and `aws-marketplace`.
19
19
 
20
- Additionally you can provide the `number_of_amis_to_keep` argument to specify how many AMIs to keep (default is 3), `region` for the AWS region (default is `us-east-1`) and `dry_run` for running without deleting any resources in AWS.
20
+ Additionally you can provide the following arguments:
21
+
22
+ - `number_of_amis_to_keep` to specify how many AMIs to keep (default is 3).
23
+ - `region` for the AWS region (default is `us-east-1`).
24
+ - `dry_run` for running without deleting any resources in AWS.
25
+ - `skip_image_under_use_verification` for skipping verification on whether the AMI is being used by any auto scaling group or reserved EC2 instance.
@@ -7,15 +7,17 @@ module AwsAmiCleanup
7
7
  class CleanupAmis
8
8
  DEFAULT_NUMBER_OF_AMIS_TO_KEEP = 3
9
9
 
10
- attr_accessor :region, :number_of_amis_to_keep
10
+ attr_accessor :region, :number_of_amis_to_keep, :skip_image_under_use_verification
11
11
 
12
- def initialize(region, number_of_amis_to_keep)
12
+ def initialize(region, number_of_amis_to_keep, skip_image_under_use_verification)
13
13
  @region = region
14
14
 
15
15
  @number_of_amis_to_keep = number_of_amis_to_keep || DEFAULT_NUMBER_OF_AMIS_TO_KEEP
16
16
  if number_of_amis_to_keep <= 0
17
17
  raise 'Number of AMIs to keep must be higher than 0.'
18
18
  end
19
+
20
+ @skip_image_under_use_verification = skip_image_under_use_verification
19
21
  end
20
22
 
21
23
  def execute!(ami_name:, ami_owner:, dry_run:)
@@ -23,7 +25,13 @@ module AwsAmiCleanup
23
25
 
24
26
  potential_amis_to_remove = amis(ami_name, ami_owner)
25
27
  ami_ids = potential_amis_to_remove.collect(&:image_id)
26
- ami_ids_to_remove = ami_ids - amis_in_use
28
+
29
+ if skip_image_under_use_verification
30
+ ami_ids_to_remove = ami_ids
31
+ else
32
+ ami_ids_to_remove = ami_ids - amis_in_use
33
+ end
34
+
27
35
  potential_amis_to_remove.keep_if {|a| ami_ids_to_remove.include?(a.image_id) }
28
36
 
29
37
  if potential_amis_to_remove.count > number_of_amis_to_keep
@@ -35,7 +43,13 @@ module AwsAmiCleanup
35
43
  amis_to_remove.each do |ami|
36
44
  ebs_mappings = ami.block_device_mappings
37
45
  puts "Deregistering #{ami.image_id}"
38
- ami.deregister unless dry_run
46
+
47
+ begin
48
+ ec2.deregister_image(image_id: ami.image_id, dry_run: dry_run)
49
+ rescue Aws::EC2::Errors::DryRunOperation
50
+ # When running in dry mode, EC2 raises this exception if operation would have succeeded, we catch them so the full process can run
51
+ end
52
+
39
53
  delete_ami_snapshots(ebs_mappings, dry_run)
40
54
  end
41
55
 
@@ -110,10 +124,10 @@ module AwsAmiCleanup
110
124
 
111
125
  snapshot_id = ebs_mapping.ebs.snapshot_id
112
126
  puts "Deleting snapshot #{snapshot_id}"
113
-
114
- unless dry_run
115
- snapshot = Aws::EC2::Snapshot.new(snapshot_id)
116
- snapshot.delete
127
+ begin
128
+ ec2.delete_snapshot(snapshot_id: snapshot_id, dry_run: dry_run)
129
+ rescue Aws::EC2::Errors::DryRunOperation
130
+ # When running in dry mode, EC2 raises this exception if operation would have succeeded, we catch them so the full process can run
117
131
  end
118
132
  end
119
133
  end
@@ -7,13 +7,18 @@ module AwsAmiCleanup
7
7
  option :ami_owner, required: true
8
8
  option :number_of_amis_to_keep, required: false
9
9
  option :region, required: false
10
- option :dry_run, required: false
10
+ option :skip_verification, type: :boolean, required: false
11
+ option :dry_run, type: :boolean, required: false
11
12
  def clean_amis
12
- cleanup_amis = AwsAmiCleanup::CleanupAmis.new(region, options[:number_of_amis_to_keep]&.to_i)
13
+ cleanup_amis = AwsAmiCleanup::CleanupAmis.new(
14
+ region,
15
+ options[:number_of_amis_to_keep]&.to_i,
16
+ options[:skip_verification]
17
+ )
13
18
 
14
19
  cleanup_amis.execute!(ami_name: options[:ami_name],
15
20
  ami_owner: options[:ami_owner],
16
- dry_run: options[:dry_run]&.casecmp?('true'))
21
+ dry_run: options[:dry_run])
17
22
  end
18
23
 
19
24
  desc "console", "interactive session"
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AwsAmiCleanup
4
- VERSION = "0.2"
4
+ VERSION = "0.3"
5
5
  end
6
6
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aws_ami_cleanup
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.2'
4
+ version: '0.3'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Diego Marcet