cwa 0.1.1 → 0.2.5

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: e21a07d7fe8b8c5f28bdc354c35a44065e0a755ce05ef34ad75eceeca73d53b8
4
- data.tar.gz: 7361cab2144cac541b90609f8321b5722d9cc33ed5ac9d3300382c096f033641
3
+ metadata.gz: e2829f3d40b0966152d43b204bf2b269d56055ffeda7561ae80fa05b5aae137b
4
+ data.tar.gz: a1d09ffb821c75d33f710ef5b96034c2a809e698ca4f9e9bd5c7bcf4abd61a62
5
5
  SHA512:
6
- metadata.gz: e5abc10648836931a6bea3c21221459a6b0fbfac65c1beed03d83adb77de7296095133abee6fcf931697006bca2238f8c6ddcf3772e2bd23b937425b331cdad0
7
- data.tar.gz: 3de701a4724fc7afe878057c71c441f248a8c441915b93116621f6ee6db118f4c8da5f35a25414a4d63d0a93721649e473dcfa8c079c5a446ffe992595309ddc
6
+ metadata.gz: e965333fb74efe2744e5d631733028cc209c95e059ffa5c2bcfca3fe97771a80b0443d57e31e81f56fd7517657e2687747a54f8a8976211d48508eeb94e7055b
7
+ data.tar.gz: 01571ed454488177e3051d807b0f65e515eb602d6cbfd59f38fea251e3a569078bfc86ea328ec3567236dfbb290d8c9f545224f5daafbfe9cb851129fa0ac387
@@ -0,0 +1 @@
1
+ 2.6.5
@@ -0,0 +1,16 @@
1
+ ---
2
+ include:
3
+ - "**/*.rb"
4
+ exclude:
5
+ - spec/**/*
6
+ - test/**/*
7
+ - vendor/**/*
8
+ - ".bundle/**/*"
9
+ require: []
10
+ domains: []
11
+ reporters:
12
+ - rubocop
13
+ - require_not_found
14
+ require_paths: []
15
+ plugins: []
16
+ max_files: 5000
@@ -0,0 +1,5 @@
1
+ {
2
+ "solargraph": {
3
+ "cmd": ["bundle", "exec", "solargraph", "stdio"]
4
+ }
5
+ }
@@ -32,6 +32,7 @@ Gem::Specification.new do |spec|
32
32
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
33
33
  spec.require_paths = ["lib"]
34
34
 
35
+ spec.add_runtime_dependency"aws-sdk-core", "~> 3"
35
36
  spec.add_runtime_dependency"aws-sdk-cloudwatch", "~> 1"
36
37
  spec.add_runtime_dependency"thor", "~> 1"
37
38
  spec.add_runtime_dependency"terminal-table", "~> 1.8"
@@ -39,4 +40,5 @@ Gem::Specification.new do |spec|
39
40
 
40
41
  spec.add_development_dependency "bundler"
41
42
  spec.add_development_dependency "rake"
43
+ spec.add_development_dependency "solargraph"
42
44
  end
@@ -0,0 +1,4 @@
1
+ ---
2
+ <name>:
3
+ :arn: <arn>
4
+ :session_name: <session_name>
data/lib/cwa.rb CHANGED
@@ -1,6 +1,13 @@
1
- require "cwa/client"
2
- require "cwa/version"
1
+ # frozen_string_literal: true
3
2
 
3
+ require 'aws-sdk-core'
4
+ require 'cwa/client'
5
+ require 'cwa/version'
6
+
7
+ #--
8
+ # Copyright (c) 2021 Ito Toshifumi
9
+ # cloudwatch alarm cli
10
+ #++
4
11
  module CWA
5
12
  class Error < StandardError; end
6
13
 
@@ -9,9 +16,21 @@ module CWA
9
16
  @aws_opts = opts
10
17
  end
11
18
 
12
- def get
13
- @aws_opts = {} unless @aws_opts
19
+ def get(opts = {})
20
+ @aws_opts ||= {}
21
+ @aws_opts[:profile] = opts[:profile] if opts[:profile]
22
+ @aws_opts[:region] = opts[:region] if opts[:region]
23
+
14
24
  Client.new(@aws_opts)
15
25
  end
26
+
27
+ def assume_role(opts)
28
+ role_credentials = Aws::AssumeRoleCredentials.new(
29
+ client: Aws::STS::Client.new(opts),
30
+ role_arn: opts[:arn],
31
+ role_session_name: opts[:session_name]
32
+ )
33
+ @aws_opts[:credentials] = role_credentials
34
+ end
16
35
  end
17
36
  end
@@ -1,39 +1,62 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module CWA
2
4
  class Alarms
3
5
  class Error < StandardError; end
4
6
 
7
+ DEFAULT_OPTS = {
8
+ alarm_name_prefix: '*',
9
+ max_records: 100
10
+ }.freeze
11
+
5
12
  def initialize(client, **opts)
6
13
  @opts = opts
7
14
  @client = client
8
15
  end
9
16
 
10
17
  def filter(query)
11
- alms = _alarms.metric_alarms
18
+ alms = alarms
12
19
 
13
20
  # querys
14
- name_query = ->(alm) { alm.alarm_name == query[:name] }
15
- regexp_query = ->(alm) { alm.alarm_name =~ /#{query[:regexp]}/ }
16
- namespace_query = ->(alm) { alm.namespace == query[:namespace] }
17
-
18
- alms = alms.select(&name_query) if query[:name ]
19
- alms = alms.select(&regexp_query) if query[:regexp ]
20
- alms = alms.select(&namespace_query) if query[:namespace ]
21
- alms = _dimension?(alms, query[:dimensions]) if query[:dimensions]
21
+ name_query = ->(alm) { alm.alarm_name == query[:name] }
22
+ regexp_query = ->(alm) { alm.alarm_name =~ /#{query[:regexp]}/ }
23
+ namespace_query = ->(alm) { alm.namespace == query[:namespace] }
24
+
25
+ alms = alms.select(&name_query) if query[:name ]
26
+ alms = alms.select(&regexp_query) if query[:regexp ]
27
+ alms = alms.select(&namespace_query) if query[:namespace ]
28
+ alms = dimension?(alms, query[:dimensions]) if query[:dimensions]
22
29
  alms
23
30
  end
24
31
 
25
32
  def refresh
26
33
  @alms = nil
27
- _alarms
34
+ alarms
28
35
  end
29
36
 
30
37
  private
31
- def _alarms(**opts)
32
- opts[:alarm_name_prefix] = "*" unless opts
33
- @alms ||= @client.describe_alarms(opts)
38
+ def alarms(**opts)
39
+ opts = DEFAULT_OPTS unless opts
40
+
41
+ unless @alms
42
+ @alms = Array.new
43
+ alms = @client.describe_alarms(opts)
44
+ next_token = alms.next_token
45
+ @alms << alms
46
+
47
+ while next_token
48
+ opts[:next_token] = next_token
49
+ alms = @client.describe_alarms(opts)
50
+ next_token = alms.next_token
51
+
52
+ @alms << alms
53
+ end
54
+ end
55
+
56
+ @alms.map {|e| e.metric_alarms }.flatten
34
57
  end
35
58
 
36
- def _dimension?(alms, dimensions)
59
+ def dimension?(alms, dimensions)
37
60
  alms.select do |alm|
38
61
  alm.dimensions.any? do |dims|
39
62
  dimensions.keys.any?(dims.name) && dimensions.values.any?(dims.value)
@@ -1,58 +1,75 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'cwa'
2
4
  require 'thor'
3
5
  require 'terminal-table'
4
6
  require 'colorize'
7
+ require 'yaml'
8
+ require 'fileutils'
9
+
10
+ ASSUME_DIR = File.join(Dir.home, '.config', 'cwa').freeze
11
+ ASSUME_FILE = File.join(ASSUME_DIR, 'assume.yml').freeze
12
+
13
+ OUTPUT_KEYS = %i[
14
+ namespace
15
+ alarm_name
16
+ actions_enabled
17
+ dimensions
18
+ alarm_arn
19
+ alarm_description
20
+ ].freeze
5
21
 
6
- OUTPUT_KEYS = [
7
- :namespace,
8
- :alarm_name,
9
- :actions_enabled,
10
- :dimensions,
11
- :alarm_arn,
12
- :alarm_description,
13
- ]
14
22
 
15
- OPTIONS = "--name ALARMNAME --regexp ALARMNAME --namespae NAMESPACE --dimensions KEY:VALUE"
23
+ AWS_OPTIONS = %i[
24
+ profile
25
+ region
26
+ ].freeze
27
+
28
+ OPTIONS = '--name ALARMNAME --regexp ALARMNAME --namespae NAMESPACE --dimensions KEY:VALUE'.freeze
16
29
 
17
30
  module CWA
31
+ # cli class
18
32
  class Cli < Thor
19
- class_option :verbose, :type => :boolean
20
-
21
- desc "alarms #{OPTIONS}", "show cloudwatch alms"
22
- option :name, type: :string, aliases: "n"
23
- option :namespace, type: :string, aliases: "s"
24
- option :regexp, type: :string, aliases: "r"
25
- option :dimensions, type: :hash, aliases: "d"
26
- #option ambiguous: :string
33
+ begin
34
+ class_option :verbose, type: :boolean
35
+ class_option :profile, type: :string
36
+ class_option :region, type: :string
37
+ class_option :assume_role, type: :string
38
+
39
+ desc "alarms #{OPTIONS}", 'show cloudwatch alms'
40
+ option :name, type: :string, aliases: 'n'
41
+ option :namespace, type: :string, aliases: 's'
42
+ option :regexp, type: :string, aliases: 'r'
43
+ option :dimensions, type: :hash, aliases: 'd'
27
44
  def alarms
28
- begin
29
- alms = _output_alms
30
- raise "not alarms" if alms.empty?
45
+ enable_assume if options[:assume_role]
31
46
 
32
- head = alms.first.keys
33
- rows = alms.map{|alm| alm.values }
34
- table = Terminal::Table.new :headings => head, :rows => rows
47
+ alms = output_alms
48
+ raise 'not alarms' if alms.empty?
35
49
 
36
- puts table
37
- rescue => err
38
- puts "error => #{err}".colorize(:red)
39
- exit 1
40
- end
50
+ head = alms.first.keys
51
+ rows = alms.map{|alm| alm.values }
52
+ table = Terminal::Table.new :headings => head, :rows => rows
53
+
54
+ puts table
41
55
  end
42
56
 
43
- desc "enable #{OPTIONS}", "enable cloudwatch alms"
44
- option :name, type: :string, aliases: "n"
45
- option :namespace, type: :string, aliases: "s"
46
- option :regexp, type: :string, aliases: "r"
47
- option :dimensions, type: :hash, aliases: "d"
57
+ desc "enable #{OPTIONS}", 'enable cloudwatch alms'
58
+ option :name, type: :string, aliases: 'n'
59
+ option :namespace, type: :string, aliases: 's'
60
+ option :regexp, type: :string, aliases: 'r'
61
+ option :dimensions, type: :hash, aliases: 'd'
48
62
  def enable
49
63
  begin
50
- cwa = CWA.get
64
+ enable_assume if options[:assume_role]
65
+
66
+ cwa = CWA.get(options)
51
67
  alms = cwa.alarms(options)
52
- alms = _check_alm(alms, :enable)
68
+ alms = check_alm(alms, :enable)
69
+
70
+ raise 'not alarms' if alms.empty?
53
71
 
54
- raise "not alarms" if alms.empty?
55
- _confirm("cloudwatch alarm enable?")
72
+ confirm('cloudwatch alarm enable?')
56
73
 
57
74
  alms.each do |alm|
58
75
  cwa.enable(alm)
@@ -60,25 +77,28 @@ module CWA
60
77
  end
61
78
  puts
62
79
  alarms
63
- rescue => err
64
- puts "error => #{err}".colorize(:red)
80
+ rescue StandardError => e
81
+ puts "error => #{e}".colorize(:red)
65
82
  exit 1
66
83
  end
67
84
  end
68
85
 
69
- desc "disable #{OPTIONS}", "disable cloudwatch alms"
70
- option :name, type: :string, aliases: "n"
71
- option :namespace, type: :string, aliases: "s"
72
- option :regexp, type: :string, aliases: "r"
73
- option :dimensions, type: :hash, aliases: "d"
86
+ desc "disable #{OPTIONS}", 'disable cloudwatch alms'
87
+ option :name, type: :string, aliases: 'n'
88
+ option :namespace, type: :string, aliases: 's'
89
+ option :regexp, type: :string, aliases: 'r'
90
+ option :dimensions, type: :hash, aliases: 'd'
74
91
  def disable
75
92
  begin
76
- cwa = CWA.get
93
+ _enable_assume if options[:assume_role]
94
+
95
+ cwa = CWA.get(options)
77
96
  alms = cwa.alarms(options)
78
- alms = _check_alm(alms, :disable)
97
+ alms = check_alm(alms, :disable)
79
98
 
80
- raise "not alarms" if alms.empty?
81
- _confirm("cloudwatch alarm disable?")
99
+ raise 'not alarms' if alms.empty?
100
+
101
+ confirm('cloudwatch alarm disable?')
82
102
 
83
103
  alms.each do |alm|
84
104
  cwa.disable(alm)
@@ -92,9 +112,37 @@ module CWA
92
112
  end
93
113
  end
94
114
 
115
+ desc 'configure', 'create config files'
116
+ def configure
117
+ configs = %w[assume_role]
118
+
119
+ puts configs
120
+ print "create type? : "
121
+ type = $stdin.gets.strip
122
+ case type
123
+ when 'assume_role'
124
+ print "name? : "
125
+ name = $stdin.gets.strip
126
+ print "arn? : "
127
+ arn = $stdin.gets.strip
128
+ print "session_name? : "
129
+ session = $stdin.gets.strip
130
+
131
+ assume = {name => { arn: arn, session_name: session}}
132
+ FileUtils.mkdir_p(ASSUME_DIR) unless Dir.exist?(ASSUME_DIR)
133
+ file = open(ASSUME_FILE, "w")
134
+ YAML.dump(assume, file)
135
+ puts "create => #{ASSUME_FILE.colorize(:yellow)}"
136
+ end
137
+ end
138
+ rescue StandardError => e
139
+ puts "error => #{e}".colorize(:red)
140
+ exit 1
141
+ end
142
+
95
143
  private
96
- def _output_alms
97
- cwa = CWA.get
144
+ def output_alms
145
+ cwa = CWA.get(options)
98
146
  alms = cwa.alarms(options)
99
147
 
100
148
  alms.map do |alm|
@@ -106,33 +154,40 @@ module CWA
106
154
  end
107
155
  end
108
156
 
109
- def _check_alm(alms, mode)
157
+ def enable_assume
158
+ raise 'not config file, pls "cwa configure"' unless File.exist?(ASSUME_FILE)
159
+
160
+ assume = YAML.load_file(ASSUME_FILE)[options[:assume_role]]
161
+ CWA.assume_role(assume)
162
+ end
163
+
164
+ def check_alm(alms, mode)
110
165
  check = false if mode == :enable
111
166
  check = true if mode == :disable
112
167
  alms.map do |alm|
113
- puts "-" * 50
114
- puts "namespace : #{alm[:namespace ]}"
115
- puts "alarm_name : #{alm[:alarm_name ]}"
116
- puts "dimensions : #{alm[:dimensions ]}"
168
+ puts '-' * 50
169
+ puts "namespace : #{alm[:namespace]}"
170
+ puts "alarm_name : #{alm[:alarm_name]}"
171
+ puts "dimensions : #{alm[:dimensions]}"
117
172
  puts "actions_enabled : #{alm[:actions_enabled]}"
118
173
  unless alm[:actions_enabled] == check
119
174
  puts "=> #{'skip'.colorize(:yellow)}"
120
- puts "-" * 50
175
+ puts '-' * 50
121
176
  next
122
177
  end
123
- puts "-" * 50
178
+ puts '-' * 50
124
179
  alm
125
180
  end.compact
126
181
  end
127
182
 
128
- def _confirm(check_word, **opt)
129
- true_word = ( opt[:true] || /yes|y/ )
130
- false_word = ( opt[:false] || /no/ )
183
+ def confirm(check_word, **_opt)
184
+ true_word = /yes|y/
185
+ false_word = /no/
131
186
 
132
187
  while true
133
- print "#{check_word} (#{true_word.inspect.delete("/")}/#{false_word.inspect.delete("/")}) : "
134
- case anser = $stdin.gets.strip
135
- when true_word then return true
188
+ print "#{check_word} (#{true_word.inspect.delete('/')}/#{false_word.inspect.delete('/')}) : "
189
+ case $stdin.gets.strip
190
+ when true_word then return true
136
191
  when false_word then return false
137
192
  end
138
193
  end
@@ -1,7 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'aws-sdk-cloudwatch'
2
- require "cwa/alarms"
4
+ require 'cwa/alarms'
3
5
 
4
6
  module CWA
7
+ # AWS client class
5
8
  class Client
6
9
  class Error < StandardError; end
7
10
 
@@ -12,9 +15,7 @@ module CWA
12
15
  def alarms(query)
13
16
  @alarms ||= Alarms.new(@client)
14
17
  alms = @alarms.filter(query)
15
- if block_given?
16
- alms.each{|alm| yield alm }
17
- end
18
+ alms.each { |alm| yield alm } if block_given?
18
19
  alms
19
20
  end
20
21
 
@@ -24,12 +25,12 @@ module CWA
24
25
 
25
26
  def enable(alm)
26
27
  alm = alm[:alarm_name]
27
- @client.enable_alarm_actions({alarm_names: [alm] })
28
+ @client.enable_alarm_actions({ alarm_names: [alm] })
28
29
  end
29
30
 
30
31
  def disable(alm)
31
32
  alm = alm[:alarm_name]
32
- @client.disable_alarm_actions({alarm_names: [alm] })
33
+ @client.disable_alarm_actions({ alarm_names: [alm] })
33
34
  end
34
35
  end
35
36
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module CWA
2
- VERSION = "0.1.1"
4
+ VERSION = "0.2.5"
3
5
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cwa
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - gen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-10-03 00:00:00.000000000 Z
11
+ date: 2020-10-10 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aws-sdk-core
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: aws-sdk-cloudwatch
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -94,6 +108,20 @@ dependencies:
94
108
  - - ">="
95
109
  - !ruby/object:Gem::Version
96
110
  version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: solargraph
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
97
125
  description: cloudwatch alarm client
98
126
  email:
99
127
  - ymdtshm@gmail.com
@@ -103,6 +131,9 @@ extensions: []
103
131
  extra_rdoc_files: []
104
132
  files:
105
133
  - ".gitignore"
134
+ - ".ruby-version"
135
+ - ".solargraph.yml"
136
+ - ".vim-lsp-settings/settings.json"
106
137
  - Gemfile
107
138
  - README.md
108
139
  - Rakefile
@@ -110,6 +141,7 @@ files:
110
141
  - bin/setup
111
142
  - cwa.gemspec
112
143
  - exe/cwa
144
+ - exsample/assume.yml
113
145
  - lib/cwa.rb
114
146
  - lib/cwa/alarms.rb
115
147
  - lib/cwa/cli.rb
@@ -135,7 +167,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
135
167
  - !ruby/object:Gem::Version
136
168
  version: '0'
137
169
  requirements: []
138
- rubygems_version: 3.1.2
170
+ rubygems_version: 3.0.3
139
171
  signing_key:
140
172
  specification_version: 4
141
173
  summary: cloudwatch alarm client