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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: db5bf4d59de51127ee6dab8a547cce124a734dd1c628096e59509ea21dd572d4
4
- data.tar.gz: 5a6aa3840422b64a71be65ce11ac2a290e4e46a2e5f8730865537ce8676c3368
3
+ metadata.gz: 8f995865e05bc638aa49921b3a41e44490162333f63cf48498c90253c03c7998
4
+ data.tar.gz: 15f352aef72ad76110950ea2b0a2f6e0da48c22d6bd0e5ac3ae71f81d2d94eaf
5
5
  SHA512:
6
- metadata.gz: 8f6000074b50a25f566478022a78a341b676a0fa10eb8cc7945043889035bf5b0b080bac0eede9748ce1886db13c0f92745f0fe2f0abe254acc2a80d93f95f16
7
- data.tar.gz: 212429abd100adba8cf1be8cbda5a322237a5114ffba2114b3266bce54e1be56d5f1a1dad184f1d2a5245b9713f039063ab2c33f6359a3d506f1d0b3769fadf3
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) and `region` for the AWS region (default is `us-east-1`).
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
- snapshot = Aws::EC2::Snapshot.new(snapshot_id)
111
- snapshot.delete
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], ami_owner: options[:ami_owner])
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"
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AwsAmiCleanup
4
- VERSION = "0.1"
4
+ VERSION = "0.2"
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.1'
4
+ version: '0.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Diego Marcet