aws_ami_cleanup 0.2 → 0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +6 -1
- data/lib/aws_ami_cleanup/cleanup_amis.rb +22 -8
- data/lib/aws_ami_cleanup/commands.rb +8 -3
- data/lib/aws_ami_cleanup/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 25ac02ebfcda946944410be7a4044e5a01949152337baa037f3fa9ea9be8b9a2
|
4
|
+
data.tar.gz: 91e0c6ceb77364a9ba929b693b7f33a0ff2e742ce889eeb33cfb1bf29368b007
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
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
|
-
|
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
|
-
|
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
|
-
|
115
|
-
|
116
|
-
|
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 :
|
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(
|
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]
|
21
|
+
dry_run: options[:dry_run])
|
17
22
|
end
|
18
23
|
|
19
24
|
desc "console", "interactive session"
|