aws_log_cleaner 0.1.7 → 0.1.8

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
  SHA1:
3
- metadata.gz: 01b45eb13e994162c2b137d83a39fb5aea229324
4
- data.tar.gz: 8611794db35d12cd38b166613e5ec1a3f022f192
3
+ metadata.gz: fd1ee81183128c4954349661dfceb8ec7c1e1cf4
4
+ data.tar.gz: 3b689c2c7a60e27d589fe0042f66fc8c7796d491
5
5
  SHA512:
6
- metadata.gz: 94ba3d272f3611bd54d9255204c10f56a879f9adb693a06c0b48bbd4e0cebabed1d6f623f9897dfeaa0474de48efe0d78cb3ebfc27d69021a354630bc96247e6
7
- data.tar.gz: be81383cc1e304308ed6915e749698dbf655bf771e5374e54d9a68c2eeab6c533bae379060e0471482659d25ab9e3dabab8df25a298c7dc5706507b0e9ed2e63
6
+ metadata.gz: cd34d68fef24670b8cb6a6cd2b336fa3b4b38fd7e0cdbcd1fbe13c3c38086f6617855fbc27bb0ff1d4fa24b09a2ab30a629081778b1db7e02c83f1e5b1629de1
7
+ data.tar.gz: d80fa1bf6e02a4df64b84f2402896da4ddfb4df1c1b19b2d469902b2a355190df50051cfd023f619d16244ed8b8107c347cc92fa9d0c4afa853c972be8e8b576
data/.appveyor.yml ADDED
@@ -0,0 +1,14 @@
1
+ install:
2
+ - set PATH=C:\Ruby21-x64\bin;%PATH%
3
+ - bundle install
4
+ - gem install bundler -v 1.14.6
5
+
6
+ build: off
7
+
8
+ before_test:
9
+ - ruby -v
10
+ - gem -v
11
+ - bundle -v
12
+
13
+ test_script:
14
+ - bundle exec rake
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- [![Build Status](https://travis-ci.org/daniebker/aws_log_cleaner.svg?branch=master)](https://travis-ci.org/daniebker/aws_log_cleaner)
1
+ [![Build Status](https://travis-ci.org/daniebker/aws_log_cleaner.svg?branch=master)](https://travis-ci.org/daniebker/aws_log_cleaner) [![Build status](https://ci.appveyor.com/api/projects/status/0rn4dqa7wyr3cqh9?svg=true)](https://ci.appveyor.com/project/daniebker/aws-log-cleaner) [![Gem Version](https://badge.fury.io/rb/aws_log_cleaner.svg)](https://badge.fury.io/rb/aws_log_cleaner)
2
2
 
3
3
  # AwsLogCleaner
4
4
 
@@ -26,33 +26,39 @@ Or install it yourself as:
26
26
 
27
27
  * `AWS_SECRET_KEY` & `AWS_ACCESS_KEY` env variables must be set.
28
28
  OR
29
- * A credentials file must be present in `~\.aws`
29
+ * A credentials file must be present in `~\.aws`
30
30
 
31
31
  The application defaults to `eu-west-1` but this can be overridden using `AWS_REGION` environment variable, or by passing -r [--region] on the commandline.
32
32
 
33
33
  ### From the command line
34
34
 
35
- * Get help by using:
35
+ Get help by using:
36
36
 
37
- `aws_log_cleaner -h`
37
+ $ aws_log_cleaner -h
38
38
 
39
- * Run the plan command in eu-west-1 where prefix like some_text.
39
+ Run the plan command in eu-west-1 where prefix like some_text.
40
40
 
41
- `aws_log_cleaner -p -l 'some_text' -r 'eu-west-1' -k [ACCESS_KEY] -s [SECRET]`
41
+ $ aws_log_cleaner -p -l 'some_text' -r 'eu-west-1' -k [ACCESS_KEY] -s [SECRET]
42
42
 
43
43
  ## Code
44
44
 
45
45
  Initialise a new LogCleaner
46
46
 
47
- `log_cleaner = AwsLogCleaner::AwsLogCleaner.new`
47
+ ```ruby
48
+ log_cleaner = AwsLogCleaner::AwsLogCleaner.new
49
+ ```
48
50
 
49
51
  Run a plan
50
52
 
51
- `log_cleaner.plan('some_text')`
53
+ ```ruby
54
+ log_cleaner.plan('some_text')
55
+ ```
52
56
 
53
57
  Or run a delete
54
58
 
55
- `log_cleaner.delete('some_text')`
59
+ ```ruby
60
+ log_cleaner.delete('some_text')
61
+ ```
56
62
 
57
63
  ## Development
58
64
 
data/bin/aws_log_cleaner CHANGED
@@ -5,7 +5,7 @@ require 'colorize'
5
5
 
6
6
  USAGE = <<ENDUSAGE.freeze
7
7
  Usage:
8
- aws_log_cleaner [-h] ([-p] or [-a]) [-r region] [-l like]
8
+ aws_log_cleaner [-h] ([-p] or [-a]) [-o] [-r region] [-l like]
9
9
  ENDUSAGE
10
10
 
11
11
  HELP = <<ENDHELP.freeze
@@ -17,6 +17,8 @@ HELP = <<ENDHELP.freeze
17
17
  -k, --access_key The access key to use for AWS.
18
18
  -s, --secret The secret to use for AWS.
19
19
  -P, --profile The profile to use for AWS Credentials.
20
+ -o, --orphan Clean orphan logs.
21
+ -v, --version Print version.
20
22
  ENDHELP
21
23
 
22
24
  args = { region: nil, access_key: nil, secret: nil }
@@ -27,6 +29,8 @@ ARGV.each do |arg|
27
29
  when '-h', '--help' then args[:help] = true
28
30
  when '-p', '--plan' then args[:plan] = true
29
31
  when '-a', '--apply' then args[:delete] = true
32
+ when '-o', '--orphan' then args[:orphan] = true
33
+ when '-v', '--version' then args[:version] = true
30
34
  when '-l', '--like' then next_arg = :like
31
35
  when '-r', '--region' then next_arg = :region
32
36
  when '-k', '--access_key' then next_arg = :access_key
@@ -41,15 +45,18 @@ ARGV.each do |arg|
41
45
  end
42
46
  end
43
47
 
48
+ if args[:version]
49
+ puts AwsLogCleaner::VERSION
50
+ exit
51
+ end
52
+
44
53
  if args[:help] || (!args[:plan] && !args[:delete])
45
54
  puts USAGE
46
55
  puts HELP
47
56
  exit
48
57
  end
49
58
 
50
- like = args[:like].nil? ? '' : args[:like].downcase
51
-
52
- credentials_args =
59
+ credentials_args =
53
60
  if args[:access_key] && args[:secret]
54
61
  {
55
62
  access_key: args[:access_key],
@@ -59,26 +66,20 @@ credentials_args =
59
66
  {
60
67
  profile: args[:profile]
61
68
  }
62
- else
63
- nil
64
69
  end
65
70
 
66
- credentials_args[:region] = args[:region] unless args[:region].nil?
71
+ credentials_args[:region] = args[:region] unless args[:region].nil?
67
72
 
68
73
  log_cleaner = \
69
- AwsLogCleaner::AwsLogCleaner.new(credentials_args)
74
+ AwsLogCleaner::AwsLogCleaner.new(credentials_args, args[:like], args[:orphan])
70
75
 
71
76
  if args[:plan]
72
77
  puts "== PLAN MODE ==\n\n"
73
-
74
- result = log_cleaner.plan(like)
75
-
78
+ result = log_cleaner.plan
76
79
  puts "Found #{result.count} item(s) to remove\n\n"
77
80
  elsif args[:delete] && !args[:plan]
78
81
  puts "== DELETE MODE ==\n\n"
79
-
80
- result = log_cleaner.delete(like)
81
-
82
+ result = log_cleaner.delete
82
83
  puts "Deleted #{result.count} item(s)\n\n"
83
84
  end
84
85
 
@@ -1,18 +1,14 @@
1
1
  require_relative 'aws_log_cleaner/cloud_watch_logs'
2
- require_relative 'aws_log_cleaner/log_group_cleaner_commands'
2
+ require_relative 'aws_log_cleaner/log_group_cleaner'
3
3
  require_relative 'aws_log_cleaner/api_gateway'
4
4
  require_relative 'aws_log_cleaner/api_gateway_retriever'
5
5
  require_relative 'aws_log_cleaner/log_group_filterer'
6
6
  require_relative 'aws_log_cleaner/aws_credentials'
7
7
  require_relative 'aws_log_cleaner/version'
8
8
 
9
-
10
9
  module AwsLogCleaner
11
-
12
- # Application wrapper
13
10
  class AwsLogCleaner
14
-
15
- def initialize(credentials_args)
11
+ def initialize(credentials_args, like, clean_orphans)
16
12
  credentials = AwsCredentials.new(credentials_args)
17
13
  cloud_watch_logs = CloudWatchLogs.new(credentials)
18
14
  api_gateway = ApiGateway.new(credentials)
@@ -20,20 +16,21 @@ module AwsLogCleaner
20
16
  api_retriever = ApiGatewayRetriever.new(api_gateway)
21
17
  log_group_filterer = LogGroupFilterer.new(cloud_watch_logs)
22
18
 
23
- @log_cleaner = LogGroupCleanerCommands.new(
19
+ @log_cleaner = LogGroupCleaner.new(
24
20
  cloud_watch_logs,
25
21
  api_retriever,
26
- log_group_filterer
22
+ log_group_filterer,
23
+ like,
24
+ clean_orphans
27
25
  )
28
26
  end
29
27
 
30
- def plan(like)
31
- @log_cleaner.plan(like)
28
+ def plan
29
+ @log_cleaner.plan
32
30
  end
33
31
 
34
- def delete(like)
35
- @log_cleaner.delete(like)
32
+ def delete
33
+ @log_cleaner.delete
36
34
  end
37
-
38
35
  end
39
- end
36
+ end
@@ -3,11 +3,8 @@ require 'aws-sdk'
3
3
  Aws.use_bundled_cert!
4
4
 
5
5
  module AwsLogCleaner
6
-
7
- # Class responsible for interacting with the Aws
8
- # ApiGatewayClient
6
+ # Class responsible for interacting with the AWS ApiGatewayClient
9
7
  class ApiGateway
10
-
11
8
  def initialize(credentials)
12
9
  @api_client = Aws::APIGateway::Client.new(
13
10
  region: credentials.region,
@@ -16,6 +13,13 @@ module AwsLogCleaner
16
13
  end
17
14
 
18
15
  def list_all_apis
16
+ @rest_apis = get_rest_apis if @rest_apis.nil?
17
+ @rest_apis
18
+ end
19
+
20
+ private
21
+
22
+ def get_rest_apis
19
23
  Enumerator.new do |enum|
20
24
  request = { limit: 25 }
21
25
  loop do
@@ -28,6 +32,5 @@ module AwsLogCleaner
28
32
  end
29
33
  end
30
34
  end
31
-
32
35
  end
33
36
  end
@@ -1,17 +1,17 @@
1
-
2
1
  module AwsLogCleaner
3
-
4
2
  # Responsible for retrieving API gateway instances filtered by a given text
5
3
  class ApiGatewayRetriever
6
-
7
4
  def initialize(api_gateway)
8
5
  @api_gateway = api_gateway
9
6
  end
10
7
 
11
8
  def retrieve(text)
12
9
  apis = @api_gateway.list_all_apis
13
- apis.select{ |item| item.name.to_s.downcase.include?(text) }
10
+ apis.select { |item| item.name.to_s.downcase.include?(text) }
14
11
  end
15
12
 
13
+ def retrieve_all
14
+ @api_gateway.list_all_apis
15
+ end
16
16
  end
17
17
  end
@@ -1,10 +1,8 @@
1
1
  require 'aws-sdk'
2
2
 
3
3
  module AwsLogCleaner
4
-
5
4
  # Class responsible for interacting with AWS Cloudwatch
6
5
  class CloudWatchLogs
7
-
8
6
  # Required for Windows users.
9
7
  Aws.use_bundled_cert!
10
8
 
@@ -15,18 +13,9 @@ module AwsLogCleaner
15
13
  )
16
14
  end
17
15
 
18
- def list_all_log_groups(token = nil)
19
- log_groups = []
20
- resp = describe_log_groups(token)
21
- log_groups.concat(resp.log_groups)
22
-
23
- if resp.next_token.to_s.empty?
24
- log_groups
25
- else
26
- log_groups.concat(
27
- list_all_log_groups(resp.next_token)
28
- )
29
- end
16
+ def list_all_log_groups
17
+ @log_groups = describe_log_groups if @log_groups.nil?
18
+ @log_groups
30
19
  end
31
20
 
32
21
  def delete_log_groups(log_group_names)
@@ -39,15 +28,15 @@ module AwsLogCleaner
39
28
 
40
29
  private
41
30
 
42
- def describe_log_groups(token)
43
- if token.nil?
44
- @cloud_watch_logs.describe_log_groups
45
- else
46
- @cloud_watch_logs.describe_log_groups(
47
- next_token: token
48
- )
31
+ def describe_log_groups
32
+ log_groups = []
33
+ token = nil
34
+ loop do
35
+ resp = @cloud_watch_logs.describe_log_groups(next_token: token)
36
+ log_groups.concat(resp.log_groups)
37
+ token = resp.next_token
38
+ return log_groups if token.to_s.empty?
49
39
  end
50
40
  end
51
-
52
41
  end
53
42
  end
@@ -0,0 +1,80 @@
1
+ require_relative 'cloud_watch_logs'
2
+
3
+ module AwsLogCleaner
4
+ class LogGroupCleaner
5
+ API_GATEWAY_LOG_PREFIX = 'API-Gateway-Execution-Logs_'.freeze
6
+
7
+ def initialize(
8
+ cloud_watch_logs,
9
+ api_retriever,
10
+ log_group_filterer,
11
+ like,
12
+ clean_orphans
13
+ )
14
+ @cloud_watch_logs = cloud_watch_logs
15
+ @api_retriever = api_retriever
16
+ @log_group_filterer = log_group_filterer
17
+ @like = like.downcase unless like.nil?
18
+ @clean_orphans = clean_orphans
19
+ end
20
+
21
+ def plan
22
+ result(log_groups_to_delete)
23
+ end
24
+
25
+ def delete
26
+ log_groups = log_groups_to_delete
27
+ @cloud_watch_logs.delete_log_groups(log_groups)
28
+ result(log_groups)
29
+ end
30
+
31
+ private
32
+
33
+ def log_groups_to_delete
34
+ log_groups = []
35
+ log_groups.concat(log_group_names_containing(@like)) if @like
36
+ log_groups.concat(log_group_names_orphan) if @clean_orphans
37
+ log_groups.uniq
38
+ end
39
+
40
+ def log_group_names_containing(text)
41
+ log_groups = @log_group_filterer.filter_by_name_includes(text)
42
+
43
+ apis = @api_retriever.retrieve(text)
44
+ apis.each do |api|
45
+ log_groups.concat(
46
+ @log_group_filterer.filter_by_name_includes(
47
+ "#{API_GATEWAY_LOG_PREFIX}#{api.id}"
48
+ )
49
+ )
50
+ end
51
+
52
+ log_groups.map(&:log_group_name)
53
+ end
54
+
55
+ def log_group_names_orphan
56
+ groups = @cloud_watch_logs.list_all_log_groups
57
+ groups = groups.select { |log_group| orphan?(log_group) }
58
+ groups.map(&:log_group_name)
59
+ end
60
+
61
+ def orphan?(log_group)
62
+ apis = @api_retriever.retrieve_all
63
+ api_id = extract_api_id(log_group.log_group_name)
64
+ api_id.nil? ? false : apis.none? { |api| api.id == api_id }
65
+ end
66
+
67
+ def extract_api_id(log_group_name)
68
+ match = log_group_name.match(%r{#{API_GATEWAY_LOG_PREFIX}(\w+)/\w+})
69
+ match.captures[0] unless match.nil?
70
+ end
71
+
72
+ def result(log_groups)
73
+ to_delete = []
74
+ log_groups.each do |name|
75
+ to_delete.push("(-) #{name}")
76
+ end
77
+ to_delete
78
+ end
79
+ end
80
+ end
@@ -1,17 +1,14 @@
1
-
2
1
  module AwsLogCleaner
3
-
4
2
  class LogGroupFilterer
5
-
6
3
  def initialize(cloud_watch_logs)
7
4
  @cloud_watch_logs = cloud_watch_logs
8
5
  end
9
6
 
10
7
  def filter_by_name_includes(text)
11
8
  log_groups = @cloud_watch_logs.list_all_log_groups
12
- log_groups.select{ |item| item.log_group_name.to_s.downcase.include?(text) }
9
+ log_groups.select do |item|
10
+ item.log_group_name.to_s.downcase.include?(text)
11
+ end
13
12
  end
14
-
15
13
  end
16
-
17
14
  end
@@ -1,3 +1,3 @@
1
1
  module AwsLogCleaner
2
- VERSION = '0.1.7'
2
+ VERSION = '0.1.8'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aws_log_cleaner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - dbaker
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-08-08 00:00:00.000000000 Z
11
+ date: 2017-08-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -90,6 +90,7 @@ executables:
90
90
  extensions: []
91
91
  extra_rdoc_files: []
92
92
  files:
93
+ - ".appveyor.yml"
93
94
  - ".gitignore"
94
95
  - ".rspec"
95
96
  - ".travis.yml"
@@ -106,7 +107,7 @@ files:
106
107
  - lib/aws_log_cleaner/api_gateway_retriever.rb
107
108
  - lib/aws_log_cleaner/aws_credentials.rb
108
109
  - lib/aws_log_cleaner/cloud_watch_logs.rb
109
- - lib/aws_log_cleaner/log_group_cleaner_commands.rb
110
+ - lib/aws_log_cleaner/log_group_cleaner.rb
110
111
  - lib/aws_log_cleaner/log_group_filterer.rb
111
112
  - lib/aws_log_cleaner/version.rb
112
113
  homepage: https://github.com/daniebker/aws_log_cleaner
@@ -1,56 +0,0 @@
1
- require_relative 'cloud_watch_logs'
2
-
3
- module AwsLogCleaner
4
-
5
- # Responsible for cleaning log groups in AWS.
6
- class LogGroupCleanerCommands
7
-
8
- def initialize(cloud_watch_logs, api_retriever, log_group_filterer)
9
- @cloud_watch_logs = cloud_watch_logs
10
- @api_retriever = api_retriever
11
- @log_group_filterer = log_group_filterer
12
- end
13
-
14
- def plan(text)
15
- result(text)
16
- end
17
-
18
- def delete(text)
19
- @cloud_watch_logs.delete_log_groups(log_group_names_containing(text))
20
- result(text)
21
- end
22
-
23
- private
24
-
25
- def log_group_names_containing(text)
26
- if @log_group_names.nil?
27
- @log_group_names = []
28
- log_groups =
29
- @log_group_filterer.filter_by_name_includes(text)
30
-
31
- apis = @api_retriever.retrieve(text)
32
- apis.each do |api|
33
- log_groups.concat(
34
- @log_group_filterer.filter_by_name_includes(api.id.to_s)
35
- )
36
- end
37
-
38
- log_groups.each do |log_group|
39
- @log_group_names.push(log_group.log_group_name)
40
- end
41
- end
42
-
43
- @log_group_names
44
- end
45
-
46
- def result(text)
47
- to_delete = []
48
- log_group_names_containing(text).each do |name|
49
- to_delete.push("(-) #{name}")
50
- end
51
- to_delete
52
- end
53
-
54
- end
55
-
56
- end