seiton 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,99 @@
1
+ module Seiton
2
+ module Ec2Check
3
+ def generator_ec2_instances_check(delete_resources, ignore_resources = nil)
4
+ template = <<-'EOF'
5
+ <% delete_resources.each do |resource| %>
6
+ describe ec2("<%= resource.instance_id %>") do
7
+ it { should_not exist }
8
+ end
9
+ <%- end -%>
10
+ <% if ignore_resources %>
11
+ <% ignore_resources.each do |resource| %>
12
+ describe 'Do not delete this resource ( <%= resource.instance_id %> ).' do
13
+ describe ec2("<%= resource.instance_id %>") do
14
+ it { should exist }
15
+ end
16
+ end
17
+ <%- end -%>
18
+ <% end %>
19
+ EOF
20
+ File.open("spec/" + "ec2_instance_spec.rb", "w") do |file|
21
+ file.puts "require 'spec_helper'"
22
+ file.puts ERB.new(template, nil, "-").result(binding).gsub(/^\n/, "")
23
+ end
24
+ end
25
+
26
+ def generator_ec2_volumes_check(delete_resources)
27
+ template = <<-'EOF'
28
+ <% delete_resources.each do |resource| %>
29
+ describe ebs("<%= resource.volume_id %>") do
30
+ it { should_not exist }
31
+ end
32
+ <% end %>
33
+ EOF
34
+ File.open("spec/" + "ec2_volumes_spec.rb", "w") do |file|
35
+ file.puts "require 'spec_helper'"
36
+ file.puts ERB.new(template, nil, "-").result(binding).gsub(/^\n/, "")
37
+ end
38
+ end
39
+
40
+ def generator_ec2_images_check(delete_resources, ignore_resources = nil)
41
+ template = <<-'EOF'
42
+ <% delete_resources.each do |resource| %>
43
+ describe ami("<%= resource.image_id %>") do
44
+ it { should_not exist }
45
+ end
46
+ <%- end -%>
47
+ <% if ignore_resources %>
48
+ <% ignore_resources.each do |resource| %>
49
+ describe 'Do not delete this resource ( <%= resource.name %> ).' do
50
+ describe ami("<%= resource.image_id %>") do
51
+ it { should exist }
52
+ end
53
+ end
54
+ <%- end -%>
55
+ <% end %>
56
+ EOF
57
+ File.open("spec/" + "ec2_images_spec.rb", "w") do |file|
58
+ file.puts "require 'spec_helper'"
59
+ file.puts ERB.new(template, nil, "-").result(binding).gsub(/^\n/, "")
60
+ end
61
+ end
62
+
63
+ def generator_ec2_snapshots_check(delete_resources)
64
+ template = <<-'EOF'
65
+ <% delete_resources.each do |resource| %>
66
+ <%= resource.snapshot_id %>
67
+ <% end %>
68
+ EOF
69
+ FileUtils.mkdir_p("check") unless FileTest.exist?("check")
70
+ File.open("check/" + "ec2_snapshots_list.txt", "w") do |file|
71
+ file.puts ERB.new(template, nil, "-").result(binding).gsub(/^\n/, "")
72
+ end
73
+ end
74
+
75
+ def generator_ec2_eips_check(delete_resources, ignore_resources = nil)
76
+ template = <<-'EOF'
77
+ <% delete_resources.each do |resource| %>
78
+ describe eip("<%= resource.public_ip %>") do
79
+ it { should_not exist }
80
+ end
81
+ <% end %>
82
+ <% if ignore_resources %>
83
+ <% ignore_resources.each do |resource| %>
84
+ describe 'Do not delete this resource ( <%= resource %> ).' do
85
+ describe eip("<%= resource %>") do
86
+ it { should exist }
87
+ end
88
+ end
89
+ <%- end -%>
90
+ <% end %>
91
+ EOF
92
+ File.open("spec/" + "ec2_eips_spec.rb", "w") do |file|
93
+ file.puts "require 'spec_helper'"
94
+ file.puts ERB.new(template, nil, "-").result(binding).gsub(/^\n/, "")
95
+ end
96
+ end
97
+
98
+ end
99
+ end
@@ -0,0 +1,7 @@
1
+ class String
2
+ def queue_name
3
+ res = self.split('/')
4
+ return '' if res.empty?
5
+ res.last
6
+ end
7
+ end
@@ -0,0 +1,52 @@
1
+ module Seiton
2
+ class Rds < Client
3
+
4
+ include Seiton::Helper
5
+ include Seiton::RdsCheck
6
+
7
+ def rds_snapshot(check = false, dt, ignores)
8
+ if check
9
+ log.info('List up the resources to be removed.')
10
+ else
11
+ log.info('Start deleting.')
12
+ end
13
+ res = rds_client.describe_db_cluster_snapshots({})
14
+ delete_db_snapshots = []
15
+ res.db_cluster_snapshots.each do |delete_db_snapshot|
16
+ if datetime_parse(delete_db_snapshot.snapshot_create_time.to_s) < datetime_parse(dt)
17
+ delete_db_snapshots << delete_db_snapshot
18
+ end
19
+ end
20
+
21
+ unless ignores.nil?
22
+ ignore_resouces = []
23
+ ignores.each do |ignore|
24
+ ignore_resouces << delete_db_snapshots.select { |delete_db_snapshot| delete_db_snapshot.db_cluster_snapshot_identifier == ignore }.last
25
+ delete_db_snapshots.delete_if { |delete_db_snapshot| delete_db_snapshot.db_cluster_snapshot_identifier == ignore }
26
+ end
27
+ end
28
+
29
+ if delete_db_snapshots.empty?
30
+ log.info('The resource to be deleted does not exist.')
31
+ exit 0
32
+ end
33
+
34
+ puts display_db_snapshot_resources(delete_db_snapshots)
35
+ generator_db_snapshots_check(delete_db_snapshots, ignore_resouces)
36
+ exit 0 if check
37
+
38
+ if process_ok?
39
+ begin
40
+ delete_db_snapshots.each do |delete_db_snapshot|
41
+ delete_db_snapshot_action(delete_db_snapshot.db_cluster_snapshot_identifier)
42
+ end
43
+ rescue StandardError => e
44
+ log.error(e)
45
+ exit 1
46
+ end
47
+ else
48
+ exit 0
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,20 @@
1
+ module Seiton
2
+ module RdsCheck
3
+ def generator_db_snapshots_check(delete_resouces, ignore_resouces = nil)
4
+ template = <<-'EOF'
5
+ <% delete_resouces.each do |resouce| %>
6
+ <%= resouce.db_cluster_snapshot_identifier %>
7
+ <%- end -%>
8
+ <% if ignore_resouces %>
9
+ <% ignore_resouces.each do |resouce| %>
10
+ ignore|<%= resouce.db_cluster_snapshot_identifier %>
11
+ <%- end -%>
12
+ <% end %>
13
+ EOF
14
+ FileUtils.mkdir_p("check") unless FileTest.exist?("check")
15
+ File.open("check/" + "db_snapshots_list.txt", "w") do |file|
16
+ file.puts ERB.new(template, nil, "-").result(binding).gsub(/^\n/, "")
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,54 @@
1
+ module Seiton
2
+ class Sqs < Client
3
+
4
+ include Seiton::Helper
5
+ include Seiton::SqsCheck
6
+
7
+ def sqs_queue(check = false, ignores)
8
+ if check
9
+ log.info('List up the resources to be removed.')
10
+ else
11
+ log.info('Start deleting.')
12
+ end
13
+
14
+ delete_resources = []
15
+ loop do
16
+ res = sqs_client.list_queues({})
17
+ res.queue_urls.each do |r|
18
+ delete_resources << r
19
+ end
20
+ break if res.next_token.nil?
21
+ end
22
+
23
+ unless ignores.nil?
24
+ ignore_resouces = []
25
+ ignores.each do |ignore|
26
+ ignore_resouces << delete_resources.select { |delete_resource| delete_resource.include?(ignore) }.last
27
+ delete_resources.delete_if { |delete_resource| delete_resource.include?(ignore) }
28
+ end
29
+ end
30
+
31
+ if delete_resources.empty?
32
+ log.info('The resource to be deleted does not exist.')
33
+ exit 0
34
+ end
35
+
36
+ puts display_sqs_queue_resources(delete_resources)
37
+ generator_sqs_queue_check(delete_resources, ignore_resouces)
38
+ exit 0 if check
39
+
40
+ if process_ok?
41
+ begin
42
+ delete_resources.each do |delete_resource|
43
+ delete_sqs_queue_action(delete_resource)
44
+ end
45
+ rescue StandardError => e
46
+ log.error(e)
47
+ exit 1
48
+ end
49
+ else
50
+ exit 0
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,26 @@
1
+ module Seiton
2
+ module SqsCheck
3
+ def generator_sqs_queue_check(delete_resouces, ignore_resources = nil)
4
+ template = <<-'EOF'
5
+ <% delete_resouces.each do |resource| %>
6
+ describe sqs("<%= resource.queue_name %>") do
7
+ it { should_not exist }
8
+ end
9
+ <%- end -%>
10
+ <% if ignore_resources %>
11
+ <% ignore_resources.each do |resource| %>
12
+ describe 'Do not delete this resource ( <%= resource.queue_name %> ).' do
13
+ describe sqs("<%= resource.queue_name %>") do
14
+ it { should exist }
15
+ end
16
+ end
17
+ <%- end -%>
18
+ <% end %>
19
+ EOF
20
+ File.open("spec/" + "sqs_queue_spec.rb", "w") do |file|
21
+ file.puts "require 'spec_helper'"
22
+ file.puts ERB.new(template, nil, "-").result(binding).gsub(/^\n/, "")
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ module Seiton
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'seiton/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "seiton"
8
+ spec.version = Seiton::VERSION
9
+ spec.authors = ["inokappa"]
10
+ spec.email = ["inokara@gmail.com"]
11
+
12
+ spec.summary = %q{seiton is a command line tool that allows you to remove unused AWS resources.}
13
+ spec.description = %q{seiton is a command line tool that allows you to remove unused AWS resources.}
14
+ spec.homepage = "https://github.com/inokappa/seiton"
15
+
16
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
17
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|check)/}) }
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 2.1.4"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_dependency 'thor'
26
+ spec.add_dependency 'aws-sdk'
27
+ spec.add_dependency 'terminal-table'
28
+ spec.add_dependency 'awspec'
29
+ spec.add_dependency 'highline'
30
+ end
metadata ADDED
@@ -0,0 +1,161 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: seiton
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - inokappa
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-07-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.1.4
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.1.4
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: thor
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: aws-sdk
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: terminal-table
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: awspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: highline
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: seiton is a command line tool that allows you to remove unused AWS resources.
112
+ email:
113
+ - inokara@gmail.com
114
+ executables:
115
+ - seiton
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - ".gitignore"
120
+ - Gemfile
121
+ - README.md
122
+ - Rakefile
123
+ - bin/console
124
+ - bin/setup
125
+ - exe/seiton
126
+ - lib/seiton.rb
127
+ - lib/seiton/cli.rb
128
+ - lib/seiton/client.rb
129
+ - lib/seiton/common.rb
130
+ - lib/seiton/ec2.rb
131
+ - lib/seiton/ec2_check.rb
132
+ - lib/seiton/ext.rb
133
+ - lib/seiton/rds.rb
134
+ - lib/seiton/rds_check.rb
135
+ - lib/seiton/sqs.rb
136
+ - lib/seiton/sqs_check.rb
137
+ - lib/seiton/version.rb
138
+ - seiton.gemspec
139
+ homepage: https://github.com/inokappa/seiton
140
+ licenses: []
141
+ metadata: {}
142
+ post_install_message:
143
+ rdoc_options: []
144
+ require_paths:
145
+ - lib
146
+ required_ruby_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ required_rubygems_version: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ requirements: []
157
+ rubygems_version: 3.0.3
158
+ signing_key:
159
+ specification_version: 4
160
+ summary: seiton is a command line tool that allows you to remove unused AWS resources.
161
+ test_files: []