aws_ami_cleanup 0.1 → 0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/aws_ami_cleanup/cleanup_amis.rb +12 -6
- data/lib/aws_ami_cleanup/commands.rb +4 -1
- 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: 8f995865e05bc638aa49921b3a41e44490162333f63cf48498c90253c03c7998
|
4
|
+
data.tar.gz: 15f352aef72ad76110950ea2b0a2f6e0da48c22d6bd0e5ac3ae71f81d2d94eaf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5cbb4cdf40523ace6141d987c089f20a500119c47316e5f45c9ec802dac099caef79b7b4b05f5b07da1b24b824274fafb261ae8f4fe283bb43c40dde11b07bde
|
7
|
+
data.tar.gz: 36ca22c54797eef7984e64a36b190e83af58365d6bf277ebd7ebc0cf5dad1e0e118b8ea2ac1321f39833b295d710b15c4afdae4c916dcfdb6e415b7719a99f74
|
data/README.md
CHANGED
@@ -17,4 +17,4 @@ 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)
|
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.
|
@@ -18,7 +18,9 @@ module AwsAmiCleanup
|
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
|
-
def execute!(ami_name:, ami_owner:)
|
21
|
+
def execute!(ami_name:, ami_owner:, dry_run:)
|
22
|
+
puts "RUNNING IN DRY MODE." if dry_run
|
23
|
+
|
22
24
|
potential_amis_to_remove = amis(ami_name, ami_owner)
|
23
25
|
ami_ids = potential_amis_to_remove.collect(&:image_id)
|
24
26
|
ami_ids_to_remove = ami_ids - amis_in_use
|
@@ -29,11 +31,12 @@ module AwsAmiCleanup
|
|
29
31
|
amis_to_keep = potential_amis_to_remove[0..(number_of_amis_to_keep-1)]
|
30
32
|
|
31
33
|
puts "Deregistering old AMIs..."
|
34
|
+
|
32
35
|
amis_to_remove.each do |ami|
|
33
36
|
ebs_mappings = ami.block_device_mappings
|
34
37
|
puts "Deregistering #{ami.image_id}"
|
35
|
-
ami.deregister
|
36
|
-
delete_ami_snapshots(ebs_mappings)
|
38
|
+
ami.deregister unless dry_run
|
39
|
+
delete_ami_snapshots(ebs_mappings, dry_run)
|
37
40
|
end
|
38
41
|
|
39
42
|
puts "Currently active AMIs..."
|
@@ -100,15 +103,18 @@ module AwsAmiCleanup
|
|
100
103
|
image_ids.flatten
|
101
104
|
end
|
102
105
|
|
103
|
-
def delete_ami_snapshots(ebs_mappings)
|
106
|
+
def delete_ami_snapshots(ebs_mappings, dry_run)
|
104
107
|
ebs_mappings.each do |ebs_mapping|
|
105
108
|
# Skip ephimeral block devices
|
106
109
|
next if ebs_mapping.ebs.nil? || ebs_mapping.ebs.snapshot_id.nil?
|
107
110
|
|
108
111
|
snapshot_id = ebs_mapping.ebs.snapshot_id
|
109
112
|
puts "Deleting snapshot #{snapshot_id}"
|
110
|
-
|
111
|
-
|
113
|
+
|
114
|
+
unless dry_run
|
115
|
+
snapshot = Aws::EC2::Snapshot.new(snapshot_id)
|
116
|
+
snapshot.delete
|
117
|
+
end
|
112
118
|
end
|
113
119
|
end
|
114
120
|
end
|
@@ -7,10 +7,13 @@ 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
11
|
def clean_amis
|
11
12
|
cleanup_amis = AwsAmiCleanup::CleanupAmis.new(region, options[:number_of_amis_to_keep]&.to_i)
|
12
13
|
|
13
|
-
cleanup_amis.execute!(ami_name: options[:ami_name],
|
14
|
+
cleanup_amis.execute!(ami_name: options[:ami_name],
|
15
|
+
ami_owner: options[:ami_owner],
|
16
|
+
dry_run: options[:dry_run]&.casecmp?('true'))
|
14
17
|
end
|
15
18
|
|
16
19
|
desc "console", "interactive session"
|