aws_ami_cleanup 0.3 → 1.1

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: 25ac02ebfcda946944410be7a4044e5a01949152337baa037f3fa9ea9be8b9a2
4
- data.tar.gz: 91e0c6ceb77364a9ba929b693b7f33a0ff2e742ce889eeb33cfb1bf29368b007
3
+ metadata.gz: fdb3d28f7c0eb4b2c6acd19468ff42990802239afed6d39d966bdf9e314af913
4
+ data.tar.gz: aec4c2d2bea2f67e295df587023c300075602037c093063d726e3a9dffe797b0
5
5
  SHA512:
6
- metadata.gz: 797cdde4f267868ae2287ae5fc46ac8c30dc711dee230dda7563ee498dc0d523eee165543808f7474c6ec00ee135f08dd9037dfc4a627eec6c5c605c815fd756
7
- data.tar.gz: 4dc150e871dece040998b2e87ef6fe851e636869c4768578b7b629b226bd4e2aa01606ae2097bf12ffc209615495edcc650d2d0b4ebd1ffa9420786dfbae7559
6
+ metadata.gz: 00bc9351ee496669abbc4e5c0bb66c6ece7c7ad4f71ea3e1607785fa9f4a2890066d9d94cffc10c5b998cb99afa17ad0bf2535cc305e7655303f40151a668e2e
7
+ data.tar.gz: 3c2741cb1ae825e8ee0e6433914635839415c8365f2fdb5244e8c6a7c5d9fcfb99c8aa83f95a2936ef79a6c058e580d726dd1e36f88efc51441279eaedad374f
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- ruby-2.6.6
1
+ ruby-3.1.2
data/README.md CHANGED
@@ -8,6 +8,7 @@ This gem provides the `cleanup_amis` script that allows deregistering unused AMI
8
8
  - ec2:DeregisterImage
9
9
  - ec2:DeleteSnapshot
10
10
  - autoscaling:DescribeAutoScalingGroups
11
+ - autoscaling:DescribeLaunchConfigurations
11
12
 
12
13
  Script should be invoked as follows:
13
14
 
@@ -26,5 +26,5 @@ Gem::Specification.new do |s|
26
26
  s.add_runtime_dependency "aws-sdk-ec2", "~> 1"
27
27
  s.add_runtime_dependency "aws-sdk-autoscaling", "~> 1"
28
28
  s.add_runtime_dependency "thor", "~> 1"
29
- s.add_development_dependency("byebug", "~> 11")
29
+ s.add_development_dependency("debug")
30
30
  end
@@ -9,7 +9,7 @@ module AwsAmiCleanup
9
9
 
10
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, skip_image_under_use_verification)
12
+ def initialize(region, number_of_amis_to_keep, skip_image_under_use_verification:, assume_role_for_querying_state:, aws_role_for_querying_state:)
13
13
  @region = region
14
14
 
15
15
  @number_of_amis_to_keep = number_of_amis_to_keep || DEFAULT_NUMBER_OF_AMIS_TO_KEEP
@@ -18,6 +18,16 @@ module AwsAmiCleanup
18
18
  end
19
19
 
20
20
  @skip_image_under_use_verification = skip_image_under_use_verification
21
+ @assume_role_for_querying_state = assume_role_for_querying_state
22
+
23
+ if @skip_image_under_use_verification && @assume_role_for_querying_state
24
+ raise 'Cannot include skip image under use verification and assume role for querying state options'
25
+ end
26
+
27
+ if @assume_role_for_querying_state
28
+ raise "Must include the IAM role's ARN to assume if using a different role for querying state" if aws_role_for_querying_state.nil?
29
+ @aws_role_for_querying_state = aws_role_for_querying_state
30
+ end
21
31
  end
22
32
 
23
33
  def execute!(ami_name:, ami_owner:, dry_run:)
@@ -45,7 +55,7 @@ module AwsAmiCleanup
45
55
  puts "Deregistering #{ami.image_id}"
46
56
 
47
57
  begin
48
- ec2.deregister_image(image_id: ami.image_id, dry_run: dry_run)
58
+ ec2_client_for_cleanup.deregister_image(image_id: ami.image_id, dry_run: dry_run)
49
59
  rescue Aws::EC2::Errors::DryRunOperation
50
60
  # When running in dry mode, EC2 raises this exception if operation would have succeeded, we catch them so the full process can run
51
61
  end
@@ -64,12 +74,40 @@ module AwsAmiCleanup
64
74
 
65
75
  protected
66
76
 
67
- def ec2
77
+ def ec2_client_for_query_state
78
+ return @__ec2_client_for_query_state if defined?(@__ec2_client_for_query_state)
79
+
80
+ @__ec2_client_for_query_state =
81
+ if @assume_role_for_querying_state
82
+ role_credentials = Aws::AssumeRoleCredentials.new(
83
+ role_arn: @aws_role_for_querying_state,
84
+ role_session_name: 'ami-cleanup',
85
+ region: region
86
+ )
87
+ Aws::EC2::Client.new(credentials: role_credentials, region: region)
88
+ else
89
+ ec2_client_for_cleanup
90
+ end
91
+ end
92
+
93
+ def ec2_client_for_cleanup
68
94
  @__ec2 ||= Aws::EC2::Client.new(region: region)
69
95
  end
70
96
 
71
97
  def auto_scaling
72
- @__auto_scaling ||= Aws::AutoScaling::Client.new(region: region)
98
+ return @__auto_scaling if defined?(@__auto_scaling)
99
+
100
+ @__auto_scaling =
101
+ if @aws_role_for_querying_state
102
+ role_credentials = Aws::AssumeRoleCredentials.new(
103
+ role_arn: @aws_role_for_querying_state,
104
+ role_session_name: 'ami-cleanup',
105
+ region: region
106
+ )
107
+ Aws::AutoScaling::Client.new(credentials: role_credentials, region: region)
108
+ else
109
+ Aws::AutoScaling::Client.new(region: region)
110
+ end
73
111
  end
74
112
 
75
113
  def amis(ami_name, ami_owner)
@@ -77,7 +115,7 @@ module AwsAmiCleanup
77
115
 
78
116
  # Cannot lookup by Name tag because that's only available from the owner account.
79
117
  describe_images_params = { owners: [ ami_owner ] }
80
- all_images_from_owner = ec2.describe_images(describe_images_params).images
118
+ all_images_from_owner = ec2_client_for_query_state.describe_images(describe_images_params).images
81
119
  name_matching_images = all_images_from_owner.filter {|i| i.name.match?(ami_name) }
82
120
 
83
121
  @__amis = sort_by_created_at(name_matching_images)
@@ -97,11 +135,11 @@ module AwsAmiCleanup
97
135
  launch_template_ids = autoscaling_groups.reject {|asg| asg.launch_template.nil? }
98
136
  .collect {|asg| asg.launch_template.launch_template_id }
99
137
  launch_template_ids.each do |launch_template_id|
100
- image_ids << ec2.describe_launch_template_versions(launch_template_id: launch_template_id, max_results: 1)
101
- .launch_template_versions
102
- .first
103
- .launch_template_data
104
- .image_id
138
+ image_ids << ec2_client_for_query_state.describe_launch_template_versions(launch_template_id: launch_template_id, max_results: 1)
139
+ .launch_template_versions
140
+ .first
141
+ .launch_template_data
142
+ .image_id
105
143
  end
106
144
 
107
145
  # Find AMIs used by auto scaling groups with launch configurations
@@ -111,7 +149,7 @@ module AwsAmiCleanup
111
149
  image_ids += launch_configurations.map(&:image_id)
112
150
 
113
151
  # Finally, find AMIs used by instances not belonging to auto scaling groups
114
- ec2_reservations = ec2.describe_instances
152
+ ec2_reservations = ec2_client_for_query_state.describe_instances
115
153
  image_ids += ec2_reservations.reservations.collect {|res| res.instances.map(&:image_id) }.flatten
116
154
 
117
155
  image_ids.flatten
@@ -125,7 +163,7 @@ module AwsAmiCleanup
125
163
  snapshot_id = ebs_mapping.ebs.snapshot_id
126
164
  puts "Deleting snapshot #{snapshot_id}"
127
165
  begin
128
- ec2.delete_snapshot(snapshot_id: snapshot_id, dry_run: dry_run)
166
+ ec2_client_for_cleanup.delete_snapshot(snapshot_id: snapshot_id, dry_run: dry_run)
129
167
  rescue Aws::EC2::Errors::DryRunOperation
130
168
  # When running in dry mode, EC2 raises this exception if operation would have succeeded, we catch them so the full process can run
131
169
  end
@@ -5,15 +5,19 @@ module AwsAmiCleanup
5
5
  desc "clean_amis", "delete unused AMIs owned by ami_owner with ami_name name"
6
6
  option :ami_name, required: true
7
7
  option :ami_owner, required: true
8
- option :number_of_amis_to_keep, required: false
8
+ option :number_of_amis_to_keep, required: true
9
+ option :assume_role_for_querying_state, type: :boolean, required: false
10
+ option :aws_role_for_querying_state, required: false
11
+ option :dry_run, type: :boolean, required: false
9
12
  option :region, required: false
10
13
  option :skip_verification, type: :boolean, required: false
11
- option :dry_run, type: :boolean, required: false
12
14
  def clean_amis
13
15
  cleanup_amis = AwsAmiCleanup::CleanupAmis.new(
14
16
  region,
15
17
  options[:number_of_amis_to_keep]&.to_i,
16
- options[:skip_verification]
18
+ skip_image_under_use_verification: options[:skip_verification],
19
+ assume_role_for_querying_state: options[:assume_role_for_querying_state],
20
+ aws_role_for_querying_state: options[:aws_role_for_querying_state]
17
21
  )
18
22
 
19
23
  cleanup_amis.execute!(ami_name: options[:ami_name],
@@ -23,8 +27,8 @@ module AwsAmiCleanup
23
27
 
24
28
  desc "console", "interactive session"
25
29
  def console
26
- require 'byebug'
27
- byebug
30
+ require 'debug'
31
+ debugger
28
32
  end
29
33
 
30
34
  protected
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AwsAmiCleanup
4
- VERSION = "0.3"
4
+ VERSION = "1.1"
5
5
  end
6
6
 
metadata CHANGED
@@ -1,11 +1,11 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aws_ami_cleanup
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.3'
4
+ version: '1.1'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Diego Marcet
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
  date: 2021-02-19 00:00:00.000000000 Z
@@ -53,20 +53,20 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '1'
55
55
  - !ruby/object:Gem::Dependency
56
- name: byebug
56
+ name: debug
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - "~>"
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: '11'
61
+ version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - "~>"
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
- version: '11'
69
- description:
68
+ version: '0'
69
+ description:
70
70
  email: diego@controlshiftlabs.com
71
71
  executables:
72
72
  - cleanup_amis
@@ -88,7 +88,7 @@ homepage: http://github.com/controlshift/aws_ami_cleanup
88
88
  licenses:
89
89
  - MIT
90
90
  metadata: {}
91
- post_install_message:
91
+ post_install_message:
92
92
  rdoc_options: []
93
93
  require_paths:
94
94
  - lib
@@ -103,8 +103,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
103
103
  - !ruby/object:Gem::Version
104
104
  version: '0'
105
105
  requirements: []
106
- rubygems_version: 3.0.8
107
- signing_key:
106
+ rubygems_version: 3.3.19
107
+ signing_key:
108
108
  specification_version: 4
109
109
  summary: Script for deleting obsolete AMIs
110
110
  test_files: []