cwa 0.2.6 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/cwa.rb +1 -18
- data/lib/cwa/alarms.rb +10 -10
- data/lib/cwa/cli.rb +73 -70
- data/lib/cwa/client.rb +22 -2
- data/lib/cwa/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 18c398f2c91a08ef355f725b361937608fa500b37ab27b6a610b9668fc3ae92a
|
4
|
+
data.tar.gz: 90aa7ff8fa4231e29effd83e4093803e862afd4e23c535f31232a59448e5a415
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b0400884cd212804b16584a58f982ce397b787ea469444f6e936da9a180b89b96b605e9049fe94d5e5c1dba3fcfcdc23de043ab8df9516f089415f054ea46543
|
7
|
+
data.tar.gz: 6447715b1e308ebd2801748447f7984c49046c526f4cd42244edc43cefb4d75cf2e6c6f1459d68bfe0645d7b9e81741b4016397e2505e0ce45fed45ce6ab9d02
|
data/lib/cwa.rb
CHANGED
@@ -12,25 +12,8 @@ module CWA
|
|
12
12
|
class Error < StandardError; end
|
13
13
|
|
14
14
|
class << self
|
15
|
-
def configure(**opts)
|
16
|
-
@aws_opts = opts
|
17
|
-
end
|
18
|
-
|
19
15
|
def get(opts = {})
|
20
|
-
|
21
|
-
@aws_opts[:profile] = opts[:profile] if opts[:profile]
|
22
|
-
@aws_opts[:region] = opts[:region] if opts[:region]
|
23
|
-
|
24
|
-
Client.new(@aws_opts)
|
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
|
16
|
+
Client.new(opts)
|
34
17
|
end
|
35
18
|
end
|
36
19
|
end
|
data/lib/cwa/alarms.rb
CHANGED
@@ -10,28 +10,28 @@ module CWA
|
|
10
10
|
}.freeze
|
11
11
|
|
12
12
|
def initialize(client, **opts)
|
13
|
-
@
|
14
|
-
@client = client
|
13
|
+
@client = client
|
15
14
|
end
|
16
15
|
|
17
16
|
def filter(query)
|
18
|
-
alms = alarms
|
17
|
+
@alms = alarms
|
19
18
|
|
20
19
|
# querys
|
21
20
|
name_query = ->(alm) { alm.alarm_name == query[:name] }
|
22
21
|
regexp_query = ->(alm) { alm.alarm_name =~ /#{query[:regexp]}/ }
|
23
22
|
namespace_query = ->(alm) { alm.namespace == query[:namespace] }
|
24
23
|
|
25
|
-
alms = alms.select(&name_query) if query[:name ]
|
26
|
-
alms = alms.select(®exp_query) if query[:regexp ]
|
27
|
-
alms = alms.select(&namespace_query) if query[:namespace ]
|
28
|
-
alms = dimension?(alms, query[:dimensions]) if query[:dimensions]
|
29
|
-
|
24
|
+
@alms = alms.select(&name_query) if query[:name ]
|
25
|
+
@alms = alms.select(®exp_query) if query[:regexp ]
|
26
|
+
@alms = alms.select(&namespace_query) if query[:namespace ]
|
27
|
+
@alms = dimension?(alms, query[:dimensions]) if query[:dimensions]
|
28
|
+
|
29
|
+
@alms
|
30
30
|
end
|
31
31
|
|
32
|
-
def refresh
|
32
|
+
def refresh(query = nil)
|
33
33
|
@alms = nil
|
34
|
-
alarms
|
34
|
+
query ? filter(query) : alarms
|
35
35
|
end
|
36
36
|
|
37
37
|
private
|
data/lib/cwa/cli.rb
CHANGED
@@ -12,24 +12,24 @@ ASSUME_DIR = File.join(Dir.home, '.config', 'cwa').freeze
|
|
12
12
|
ASSUME_FILE = File.join(ASSUME_DIR, 'assume.yml').freeze
|
13
13
|
|
14
14
|
OUTPUT_KEYS = %i[
|
15
|
-
|
16
|
-
|
17
|
-
|
15
|
+
namespace
|
16
|
+
alarm_name
|
17
|
+
actions_enabled
|
18
18
|
].freeze
|
19
19
|
|
20
20
|
OUTPUT_KEYS_DETAIL = %i[
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
21
|
+
namespace
|
22
|
+
alarm_name
|
23
|
+
actions_enabled
|
24
|
+
dimensions
|
25
|
+
alarm_arn
|
26
|
+
alarm_description
|
27
27
|
].freeze
|
28
28
|
|
29
29
|
|
30
30
|
AWS_OPTIONS = %i[
|
31
|
-
|
32
|
-
|
31
|
+
profile
|
32
|
+
region
|
33
33
|
].freeze
|
34
34
|
|
35
35
|
OPTIONS = '--name ALARMNAME --regexp ALARMNAME --namespae NAMESPACE --dimensions KEY:VALUE'.freeze
|
@@ -37,7 +37,6 @@ OPTIONS = '--name ALARMNAME --regexp ALARMNAME --namespae NAMESPACE --dimensions
|
|
37
37
|
module CWA
|
38
38
|
# cli class
|
39
39
|
class Cli < Thor
|
40
|
-
begin
|
41
40
|
class_option :verbose, type: :boolean
|
42
41
|
class_option :profile, type: :string
|
43
42
|
class_option :region, type: :string
|
@@ -50,22 +49,32 @@ module CWA
|
|
50
49
|
option :regexp, type: :string, aliases: 'r'
|
51
50
|
option :dimensions, type: :hash, aliases: 'd'
|
52
51
|
def alarms
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
52
|
+
begin
|
53
|
+
cwa = CWA.get(aws_opts)
|
54
|
+
alms = cwa.alarms(options)
|
55
|
+
keys = OUTPUT_KEYS
|
56
|
+
keys = OUTPUT_KEYS_DETAIL if options[:verbose]
|
57
|
+
|
58
|
+
alms = alms.map do |alm|
|
59
|
+
keys.reduce({}) { |h, key| h.merge!(key => alm.method(key).call) }
|
60
|
+
end
|
61
|
+
|
62
|
+
raise 'not alarms' if alms.empty?
|
63
|
+
|
64
|
+
case options[:output]
|
65
|
+
when 'json'
|
66
|
+
puts JSON.dump(alms)
|
67
|
+
when 'yaml'
|
68
|
+
puts YAML.dump(alms)
|
69
|
+
else
|
70
|
+
head = alms.first.keys
|
71
|
+
rows = alms.map{|alm| alm.values }
|
72
|
+
table = Terminal::Table.new :headings => head, :rows => rows
|
73
|
+
puts table
|
74
|
+
end
|
75
|
+
rescue StandardError => e
|
76
|
+
puts "error => #{e}".colorize(:red)
|
77
|
+
exit 1
|
69
78
|
end
|
70
79
|
end
|
71
80
|
|
@@ -76,9 +85,7 @@ module CWA
|
|
76
85
|
option :dimensions, type: :hash, aliases: 'd'
|
77
86
|
def enable
|
78
87
|
begin
|
79
|
-
|
80
|
-
|
81
|
-
cwa = CWA.get(options)
|
88
|
+
cwa = CWA.get(aws_opts)
|
82
89
|
alms = cwa.alarms(options)
|
83
90
|
alms = check_alm(alms, :enable)
|
84
91
|
|
@@ -105,9 +112,7 @@ module CWA
|
|
105
112
|
option :dimensions, type: :hash, aliases: 'd'
|
106
113
|
def disable
|
107
114
|
begin
|
108
|
-
|
109
|
-
|
110
|
-
cwa = CWA.get(options)
|
115
|
+
cwa = CWA.get(aws_opts)
|
111
116
|
alms = cwa.alarms(options)
|
112
117
|
alms = check_alm(alms, :disable)
|
113
118
|
|
@@ -129,53 +134,51 @@ module CWA
|
|
129
134
|
|
130
135
|
desc 'configure', 'create config files'
|
131
136
|
def configure
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
137
|
+
begin
|
138
|
+
configs = %w[assume_role]
|
139
|
+
|
140
|
+
puts configs
|
141
|
+
print "create type? : "
|
142
|
+
type = $stdin.gets.strip
|
143
|
+
case type
|
144
|
+
when 'assume_role'
|
145
|
+
print "name? : "
|
146
|
+
name = $stdin.gets.strip
|
147
|
+
print "arn? : "
|
148
|
+
arn = $stdin.gets.strip
|
149
|
+
print "session_name? : "
|
150
|
+
session = $stdin.gets.strip
|
151
|
+
|
152
|
+
assume = {name => { arn: arn, session_name: session}}
|
153
|
+
|
154
|
+
FileUtils.mkdir_p(ASSUME_DIR) unless Dir.exist?(ASSUME_DIR)
|
155
|
+
assume.merge!(YAML.load_file(ASSUME_FILE)) if File.exist?(ASSUME_FILE)
|
156
|
+
file = open(ASSUME_FILE, "w")
|
157
|
+
YAML.dump(assume, file)
|
158
|
+
|
159
|
+
puts "create => #{ASSUME_FILE.colorize(:yellow)}"
|
160
|
+
end
|
153
161
|
rescue StandardError => e
|
154
162
|
puts "error => #{e}".colorize(:red)
|
155
163
|
exit 1
|
156
164
|
end
|
165
|
+
end
|
157
166
|
|
158
167
|
private
|
159
|
-
def
|
160
|
-
|
161
|
-
alms = cwa.alarms(options)
|
162
|
-
keys = OUTPUT_KEYS
|
163
|
-
keys = OUTPUT_KEYS_DETAIL if options[:verbose]
|
168
|
+
def aws_opts
|
169
|
+
opts = {}
|
164
170
|
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
end
|
170
|
-
v
|
171
|
-
end
|
171
|
+
opts[:region] = options[:region]
|
172
|
+
opts[:profile] = options[:profile]
|
173
|
+
opts[:assume_role] = assume
|
174
|
+
opts.compact
|
172
175
|
end
|
173
176
|
|
174
|
-
def
|
177
|
+
def assume
|
178
|
+
return nil unless options[:assume_role]
|
175
179
|
raise 'not config file, pls "cwa configure"' unless File.exist?(ASSUME_FILE)
|
176
180
|
|
177
|
-
|
178
|
-
CWA.assume_role(assume)
|
181
|
+
YAML.load_file(ASSUME_FILE)[options[:assume_role]]
|
179
182
|
end
|
180
183
|
|
181
184
|
def check_alm(alms, mode)
|
data/lib/cwa/client.rb
CHANGED
@@ -9,6 +9,11 @@ module CWA
|
|
9
9
|
class Error < StandardError; end
|
10
10
|
|
11
11
|
def initialize(opts)
|
12
|
+
if opts[:assume_role]
|
13
|
+
role = opts.delete(:assume_role)
|
14
|
+
opts[:credentials] = assume_role(role)
|
15
|
+
end
|
16
|
+
|
12
17
|
@client = Aws::CloudWatch::Client.new(opts)
|
13
18
|
end
|
14
19
|
|
@@ -16,11 +21,17 @@ module CWA
|
|
16
21
|
@alarms ||= Alarms.new(@client)
|
17
22
|
alms = @alarms.filter(query)
|
18
23
|
alms.each { |alm| yield alm } if block_given?
|
24
|
+
|
25
|
+
@query_cache = query
|
19
26
|
alms
|
20
27
|
end
|
21
28
|
|
22
|
-
def
|
23
|
-
|
29
|
+
def update(cache: true)
|
30
|
+
if cache
|
31
|
+
@alarms.refresh(@query_cache)
|
32
|
+
else
|
33
|
+
@alarms.refresh
|
34
|
+
end
|
24
35
|
end
|
25
36
|
|
26
37
|
def enable(alm)
|
@@ -32,5 +43,14 @@ module CWA
|
|
32
43
|
alm = alm[:alarm_name]
|
33
44
|
@client.disable_alarm_actions({ alarm_names: [alm] })
|
34
45
|
end
|
46
|
+
|
47
|
+
private
|
48
|
+
def assume_role(opts)
|
49
|
+
Aws::AssumeRoleCredentials.new(
|
50
|
+
client: Aws::STS::Client.new,
|
51
|
+
role_arn: opts[:arn],
|
52
|
+
role_session_name: opts[:session_name]
|
53
|
+
)
|
54
|
+
end
|
35
55
|
end
|
36
56
|
end
|
data/lib/cwa/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cwa
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.1
|
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-
|
11
|
+
date: 2020-10-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: aws-sdk-core
|