seiton 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d5fa7db4dbca098385daad98c7f1811f8f7949161d0769bfb1a4f80ef22d5dc2
4
+ data.tar.gz: 818a821063394c85f225a5106c59fa07ce2dce1e12f1571b8002ba913c82f7e4
5
+ SHA512:
6
+ metadata.gz: e81ea92a72a7069c200c3f29998291ebd44f4f48ffd6d7fa75c9dace87f31bec2ffd7bbcb61fdf8933456e7da75f832e73f2a804717368be56cf588b3e4ea828
7
+ data.tar.gz: 4e11b1f5ec9b446fdab3273b9a70ddc7749a4eb72e8eb616efc85243fd992986fae27557a9e4c3f44062ca748fc2ecd42d8d0eb97f37763fa2369d4bf5d2fff3
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/
9
+ /tmp/
10
+ /vendor/
11
+ .envrc
12
+ .ruby-version
13
+ /result_html/
14
+ /check/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in seiton.gemspec
4
+ gemspec
@@ -0,0 +1,295 @@
1
+ # seiton
2
+
3
+ The seiton (整頓) tidies up your AWS Resources.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'seiton'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```sh
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+
21
+ ```sh
22
+ $ gem install seiton
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ ### help
28
+
29
+ ```sh
30
+ $ bundle exec seiton --help
31
+ Commands:
32
+ seiton ec2_snapshot # Delete the EC2 Snapshot.
33
+ seiton eip # Delete the Elastic IP.
34
+ seiton help [COMMAND] # Describe available commands or one specific command
35
+ seiton instance # Delete the EC2 Instance.
36
+ seiton rds_snapshot # Delete the RDS Snapshot.
37
+ seiton sqs_queue # Delete the SQS Queue.
38
+ seiton version # Print the version number.
39
+ ```
40
+
41
+ ### Delete EC2 AMI
42
+
43
+ #### help
44
+
45
+ ```sh
46
+ $ bundle exec seiton --help ami
47
+ Usage:
48
+ seiton ami
49
+
50
+ Options:
51
+ -b, [--before-datetime=BEFORE_DATETIME] # Specify the date and time for deletion (delete resources before the specified date and time.)
52
+ -i, [--ignores=one two three] # Specify resources to be undeleted (you can specify multiple resources).
53
+ -c, [--check], [--no-check] # Check the resources to be deleted.
54
+
55
+ Delete the EC2 AMI.
56
+ ```
57
+
58
+ #### check
59
+
60
+ ```sh
61
+ bundle exec seiton ami --before-datetime=2020/01/01 --check
62
+ ```
63
+
64
+ #### delete
65
+
66
+ ```sh
67
+ bundle exec seiton ami --before-datetime=2020/01/01
68
+ ```
69
+
70
+ #### ignore
71
+
72
+ ```sh
73
+ bundle exec seiton ami --before-datetime=2020/01/01 --ignores=xxxx yyyy zzzz
74
+ ```
75
+
76
+ #### Testing for delete complete
77
+
78
+ ```sh
79
+ bundle exec rake check:ec2_images # AMI
80
+ bundle exec rake check:ec2_snapshots # Snapshot
81
+ ```
82
+
83
+ ### Delete EIP
84
+
85
+ #### help
86
+
87
+ ```sh
88
+ $ bundle exec seiton --help eip
89
+ Usage:
90
+ seiton eip
91
+
92
+ Options:
93
+ -i, [--ignores=one two three] # Specify resources to be undeleted (you can specify multiple resources).
94
+ -c, [--check], [--no-check] # Check the resources to be deleted.
95
+
96
+ Delete the Elastic IP.
97
+ ```
98
+
99
+ #### check
100
+
101
+ ```sh
102
+ bundle exec seiton eip --check
103
+ ```
104
+
105
+ #### delete
106
+
107
+ ```sh
108
+ bundle exec seiton eip
109
+ ```
110
+
111
+ #### ignore
112
+
113
+ ```sh
114
+ bundle exec seiton eip --ignores=xxx.xxx.xxx.xxx yyy.yyy.yyy.yyy
115
+ ```
116
+
117
+ #### Testing for delete complete
118
+
119
+ ```sh
120
+ bundle exec rake check:ec2_eips      # Elastic IP
121
+ ```
122
+
123
+ ### Delete EC2 Instance
124
+
125
+ #### help
126
+
127
+ ```sh
128
+ $ bundle exec seiton --help instance
129
+ Usage:
130
+ seiton instance
131
+
132
+ Options:
133
+ -b, [--before-datetime=BEFORE_DATETIME] # Specify the date and time for deletion (delete resources before the specified date and time.)
134
+ -i, [--ignores=one two three] # Specify resources to be undeleted (you can specify multiple resources).
135
+ -c, [--check], [--no-check] # Check the resources to be deleted.
136
+
137
+ Delete the EC2 Instance.
138
+ ```
139
+
140
+ #### check
141
+
142
+ ```sh
143
+ bundle exec seiton instance --before-datetime=2020/01/01 --check
144
+ ```
145
+
146
+ #### delete
147
+
148
+ ```sh
149
+ bundle exec seiton instance --before-datetime=2020/01/01
150
+ ```
151
+
152
+ #### ignore
153
+
154
+ ```sh
155
+ bundle exec seiton instance --before-datetime=2020/01/01 --ignores=tag_name instance_id
156
+ ```
157
+
158
+ #### Testing for delete complete
159
+
160
+ ```sh
161
+ bundle exec rake check:ec2_instances # EC2 Instance
162
+ bundle exec rake check:ec2_volumes # EC2 Volume
163
+ ```
164
+
165
+ ### Delete RDS Snapshot
166
+
167
+ #### help
168
+
169
+ ```sh
170
+ $ bundle exec seiton --help rds_snapshot
171
+ Usage:
172
+ seiton rds_snapshot
173
+
174
+ Options:
175
+ -b, [--before-datetime=BEFORE_DATETIME] # Specify the date and time for deletion (delete resources before the specified date and time.)
176
+ -i, [--ignores=one two three] # Specify resources to be undeleted (you can specify multiple resources).
177
+ -c, [--check], [--no-check] # Check the resources to be deleted.
178
+
179
+ Delete the RDS Snapshot.
180
+ ```
181
+
182
+ #### check
183
+
184
+ ```sh
185
+ bundle exec seiton rds_snapshot --before-datetime=2020/01/01 --check
186
+ ```
187
+
188
+ #### delete
189
+
190
+ ```sh
191
+ bundle exec seiton rds_snapshot --before-datetime=2020/01/01
192
+ ```
193
+
194
+ #### ignore
195
+
196
+ ```sh
197
+ bundle exec seiton rds_snapshot --before-datetime=2020/01/01 --ignores=xxxx yyyy zzzz
198
+ ```
199
+
200
+ #### Testing for delete complete
201
+
202
+ ```sh
203
+ bundle exec rake check:db_snapshots # RDS Snapshot
204
+ ```
205
+
206
+ ### Delete EC2 Snapshot
207
+
208
+ #### help
209
+
210
+ ```sh
211
+ $ bundle exec seiton --help ec2_snapshot
212
+ Usage:
213
+ seiton ec2_snapshot
214
+
215
+ Options:
216
+ -b, [--before-datetime=BEFORE_DATETIME] # Specify the date and time for deletion (delete resources before the specified date and time.)
217
+ -i, [--ignores=one two three] # Specify resources to be undeleted (you can specify multiple resources).
218
+ -c, [--check], [--no-check] # Check the resources to be deleted.
219
+
220
+ Delete the EC2 Snapshot.
221
+ ```
222
+
223
+ #### check
224
+
225
+ ```sh
226
+ bundle exec seiton ec2_snapshot --before-datetime=2020/01/01 --check
227
+ ```
228
+
229
+ #### delete
230
+
231
+ ```sh
232
+ bundle exec seiton ec2_snapshot --before-datetime=2020/01/01
233
+ ```
234
+
235
+ #### ignore
236
+
237
+ ```sh
238
+ bundle exec seiton ec2_snapshot --before-datetime=2020/01/01 --ignores=xxxx yyyy zzzz
239
+ ```
240
+
241
+ #### Testing for delete complete
242
+
243
+ ```sh
244
+ bundle exec rake check:ec2_snapshots # EC2 Snapshot
245
+ ```
246
+
247
+ ### Delete SQS Queue
248
+
249
+ #### help
250
+
251
+ ```sh
252
+ $ bundle exec seiton help sqs_queue
253
+ Usage:
254
+ seiton sqs_queue
255
+
256
+ Options:
257
+ -i, [--ignores=one two three] # Specify resources to be undeleted (you can specify multiple resources).
258
+ -c, [--check], [--no-check] # Check the resources to be deleted.
259
+
260
+ Delete the SQS Queue.
261
+ ```
262
+
263
+ #### check
264
+
265
+ ```sh
266
+ bundle exec seiton sqs_queue --check
267
+ ```
268
+
269
+ #### delete
270
+
271
+ ```sh
272
+ bundle exec seiton sqs_queue
273
+ ```
274
+
275
+ #### ignore
276
+
277
+ ```sh
278
+ bundle exec seiton sqs_queue --ignores=xxxx yyyy zzzz
279
+ ```
280
+
281
+ #### Testing for delete complete
282
+
283
+ ```sh
284
+ bundle exec rake check:sqs_queue # SQS Queue
285
+ ```
286
+
287
+ ## Development
288
+
289
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment. Run `bundle exec seiton` to use the gem in this directory, ignoring other installed copies of this gem.
290
+
291
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
292
+
293
+ ## Contributing
294
+
295
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/seiton.
@@ -0,0 +1,112 @@
1
+ require 'rspec/core/rake_task'
2
+ require 'bundler/gem_tasks'
3
+ require 'aws-sdk'
4
+ require 'highline'
5
+ require 'logger'
6
+
7
+ RSpec::Core::RakeTask.new('spec')
8
+ task :default => :spec
9
+
10
+ namespace :check do
11
+ targets1 = []
12
+ targets2 = []
13
+ Dir.glob(['./spec/*', './check/*']).each do |file|
14
+ target = File.basename(file)
15
+ if target.include?('_spec.rb') then
16
+ targets1 << File.basename(target, '_spec.rb')
17
+ elsif target.include?('_list.txt') then
18
+ targets2 << File.basename(target, '_list.txt')
19
+ end
20
+ end
21
+
22
+ targets1.each do |target|
23
+ desc "Test for #{target} deleted"
24
+
25
+ RSpec::Core::RakeTask.new(target.to_sym) do |t|
26
+ # t.rspec_opts = ["--format documentation", "--format html", "--out ./result_html/#{target}_result.html"]
27
+ t.rspec_opts = ["--format documentation", "--format html", "--out ./result_html/#{target}_result.html"]
28
+ t.pattern = "spec/#{target}_spec.rb"
29
+ t.verbose = true
30
+ end if target != 'ec2_snapshot'
31
+ end
32
+
33
+ targets2.each do |target|
34
+ desc "Test for #{target} deleted"
35
+ task target.to_sym do
36
+ log.info('Check that ' + target + ' has been deleted.')
37
+ resouces = []
38
+ File.open('./check/' + target + '_list.txt', "r") do |f|
39
+ f.each_line do |line|
40
+ resouces << line.chomp
41
+ end
42
+ end
43
+ eval "check_#{target}(resouces)"
44
+ end
45
+ end
46
+ end
47
+
48
+ def ec2_client
49
+ @ec2_client ||= Aws::EC2::Client.new
50
+ end
51
+
52
+ def rds_client
53
+ @rds_client ||= Aws::RDS::Client.new
54
+ end
55
+
56
+ def hl
57
+ @hl ||= HighLine.new
58
+ end
59
+
60
+ def log
61
+ @log ||= Logger.new(STDOUT)
62
+ end
63
+
64
+ def ignore_resource?(resource)
65
+ if resource.include?('ignore|')
66
+ r = resource.split('|').last
67
+ log.info('[ ' + hl.color("!", :yellow) + ' ] ' + r + ' is not for deletion.')
68
+ not_deleted = '[ ' + hl.color("\u2714".encode('utf-8'), :green) + ' ] ' + r + ' exists.'
69
+ deleted = '[ ' + hl.color("\u2715".encode('utf-8'), :red) + ' ] ' + r + ' has been removed.'
70
+ else
71
+ r = resource
72
+ deleted = '[ ' + hl.color("\u2714".encode('utf-8'), :green) + ' ] ' + r + ' has been removed..'
73
+ not_deleted = '[ ' + hl.color("\u2715".encode('utf-8'), :red) + ' ] ' + r + ' exists.'
74
+ end
75
+ return r, deleted, not_deleted
76
+ end
77
+
78
+ def check_ec2_snapshots(resouces)
79
+ resouces.each do |resouce|
80
+ r, deleted, not_deleted = ignore_resource?(resouce)
81
+ begin
82
+ ec2_client.describe_snapshots({ snapshot_ids: [ r ] })
83
+ log.info(not_deleted)
84
+ rescue
85
+ log.info(deleted)
86
+ end
87
+ end
88
+ end
89
+
90
+ def check_ec2_eips(resouces)
91
+ resouces.each do |resouce|
92
+ r, deleted, not_deleted = ignore_resource?(resouce)
93
+ res = ec2_client.describe_addresses({ filters: [{ name: 'public-ip', values: [r] }] })
94
+ if res.addresses.empty?
95
+ log.info(deleted)
96
+ else
97
+ log.info(not_deleted)
98
+ end
99
+ end
100
+ end
101
+
102
+ def check_db_snapshots(resouces)
103
+ resouces.each do |resouce|
104
+ r, deleted, not_deleted = ignore_resource?(resouce)
105
+ res = rds_client.describe_db_cluster_snapshots(db_cluster_snapshot_identifier: r)
106
+ if res.db_cluster_snapshots.empty?
107
+ log.info(deleted)
108
+ else
109
+ log.info(not_deleted)
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "seiton"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start