awful 0.0.152 → 0.0.153

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: 7e9cf294dcdf1b576e46d1737c8a8d209b317208
4
- data.tar.gz: 76d39a1f0288bbc5727ba0d481d707d633ee44c4
3
+ metadata.gz: 96d3e0155175c85b636ee238c2b5eb481643187e
4
+ data.tar.gz: '0294c90dfe5e0b1ec0273f2f26052870eb241e61'
5
5
  SHA512:
6
- metadata.gz: c88e205ba51cb5365e1224e8ab73b6a0893702c60720821ff346a77dc440acc0193eaf2e688c25894196d2f3b7b6bd26ea7a0b37465168911efd0c2e2ff3a2a6
7
- data.tar.gz: 1c0e24efd8f3787b345233bb9f7d725e51a893626812d719029e514b9433e5bf4afb2f70e11106fdaa631d3bcbb98c905b14fe1f9d69ae18ddf125ee72e102eb
6
+ metadata.gz: 1fda7ef50c77ce9a36403d60341c0c458532a98cc1bd0714425737d341c9b23910b8d5c6e546695fc34ba007d56e2303e90f71943d9ce6cdfd36666be0d06dfd
7
+ data.tar.gz: 752f8cc8f91cadad15d2a8615978c13e0e00c01283cce9098facb97d14a3e82723a741280e13135ff913b51589fc565b8a6cc3e77b5bd2c29c8a6e9c20297a66
data/lib/awful.rb CHANGED
@@ -164,6 +164,19 @@ module Awful
164
164
  end
165
165
  end
166
166
 
167
+ ## universal paginator for aws-sdk calls
168
+ def paginate(thing)
169
+ token = nil
170
+ things = []
171
+ loop do
172
+ resp = yield(token)
173
+ things += resp.send(thing)
174
+ ## some apis use marker, some use token
175
+ token = resp.respond_to?(:next_marker) ? resp.next_marker : resp.next_token
176
+ break if token.nil?
177
+ end
178
+ things
179
+ end
167
180
  end
168
181
  end
169
182
  end
@@ -1,3 +1,5 @@
1
+ require 'time'
2
+
1
3
  module Awful
2
4
  module Short
3
5
  def cwlogs(*args)
@@ -10,28 +12,29 @@ module Awful
10
12
  def logs
11
13
  @logs ||= Aws::CloudWatchLogs::Client.new
12
14
  end
15
+
16
+ ## human-readable timestamp
17
+ def human_time(timestamp)
18
+ if timestamp.nil?
19
+ '-'
20
+ else
21
+ Time.at(timestamp.to_i/1000)
22
+ end
23
+ end
13
24
  end
14
25
 
15
26
  desc 'ls [PREFIX]', 'list log groups'
16
27
  method_option :long, aliases: '-l', default: false, desc: 'Long listing'
17
28
  def ls(prefix = nil)
18
- next_token = nil
19
- log_groups = []
20
- loop do
21
- response = logs.describe_log_groups(log_group_name_prefix: prefix, next_token: next_token)
22
- log_groups = log_groups + response.log_groups
23
- next_token = response.next_token
24
- break if next_token.nil?
25
- end
26
-
27
- ## return and output groups
28
- log_groups.output do |groups|
29
+ paginate(:log_groups) do |token|
30
+ logs.describe_log_groups(log_group_name_prefix: prefix, next_token: token)
31
+ end.output do |groups|
29
32
  if options[:long]
30
33
  print_table groups.map { |group|
31
34
  [
32
35
  group.log_group_name,
33
36
  group.retention_in_days,
34
- Time.at(group.creation_time.to_i/1000),
37
+ human_time(group.creation_time),
35
38
  group.stored_bytes,
36
39
  ]
37
40
  }
@@ -52,30 +55,23 @@ module Awful
52
55
  end
53
56
 
54
57
  desc 'streams GROUP [PREFIX]', 'list log streams for GROUP'
55
- method_option :long, aliases: '-l', default: false, desc: 'Long listing'
56
- method_option :limit, aliases: '-n', default: 50, desc: 'Count to limit returned results'
57
- method_option :alpha, aliases: '-a', default: false, desc: 'Order by name'
58
+ method_option :long, aliases: '-l', default: false, desc: 'long listing'
59
+ method_option :limit, aliases: '-n', default: 50, desc: 'limit number of results per page call'
60
+ method_option :alpha, aliases: '-a', default: false, desc: 'order by name'
58
61
  def streams(group, prefix = nil)
59
- next_token = nil
60
- log_streams = []
61
- loop do
62
- response = logs.describe_log_streams(
62
+ paginate(:log_streams) do |token|
63
+ logs.describe_log_streams(
63
64
  log_group_name: group,
64
65
  log_stream_name_prefix: prefix,
65
66
  order_by: options[:alpha] ? 'LogStreamName' : 'LastEventTime',
66
67
  descending: (not options[:alpha]), # want desc order if by time for most recent first
67
68
  limit: options[:limit],
68
- next_token: next_token
69
+ next_token: token,
69
70
  )
70
- log_streams = log_streams + response.log_streams
71
- next_token = response.next_token
72
- break if next_token.nil?
73
- break if log_streams.count >= options[:limit].to_i
74
- end
75
- log_streams.output do |streams|
71
+ end.output do |streams|
76
72
  if options[:long]
77
73
  print_table streams.map { |s|
78
- [ s.log_stream_name, Time.at(s.creation_time.to_i/1000), Time.at(s.last_event_timestamp.to_i/1000) ]
74
+ [ s.log_stream_name, human_time(s.creation_time), human_time(s.last_event_timestamp) ]
79
75
  }
80
76
  else
81
77
  puts streams.map(&:log_stream_name)
@@ -145,12 +141,69 @@ module Awful
145
141
 
146
142
  events.output do |ev|
147
143
  if options[:long]
148
- print_table ev.map { |e| [Time.at(e.timestamp.to_i/1000), e.message] }
144
+ print_table ev.map { |e| [human_time(e.timestamp), e.message] }
149
145
  else
150
146
  puts ev.map(&:message)
151
147
  end
152
148
  end
153
149
  end
154
150
 
151
+ desc 'filter GROUP [STREAMS]', 'filter'
152
+ method_option :pattern, aliases: '-p', type: :string, default: nil, desc: 'pattern to filter logs'
153
+ method_option :start, aliases: '-s', type: :string, default: nil, desc: 'start time'
154
+ method_option :end, aliases: '-e', type: :string, default: nil, desc: 'end time'
155
+ def filter(group, *streams)
156
+ start_time = options[:start] ? Time.parse(options[:start]).to_i*1000 : nil
157
+ end_time = options[:end] ? Time.parse(options[:end]).to_i*1000 : nil
158
+ token = nil
159
+ loop do
160
+ resp = logs.filter_log_events(
161
+ log_group_name: group,
162
+ log_stream_names: streams.empty? ? nil : streams,
163
+ next_token: token,
164
+ start_time: start_time,
165
+ end_time: end_time,
166
+ filter_pattern: options[:pattern],
167
+ )
168
+ resp.events.each do |e|
169
+ time = set_color(human_time(e.timestamp).utc, :green)
170
+ stream = set_color(e.log_stream_name, :blue)
171
+ puts("#{time} #{stream} #{e.message}")
172
+ end
173
+ token = resp.next_token
174
+ break unless token
175
+ end
176
+ end
177
+
178
+ desc 'tail GROUP [STREAMS]', 'tail log group streams'
179
+ method_option :numlines, aliases: '-n', type: :numeric, default: 10, desc: 'number of lines to show'
180
+ method_option :follow, aliases: '-f', type: :boolean, default: false, desc: 'follow log output'
181
+ method_option :sleep, aliases: '-s', type: :numeric, default: 3, desc: 'seconds to sleep between poll for new data'
182
+ method_option :timestamp, type: :boolean, default: true, desc: 'show timestamp for each line'
183
+ def tail(group, stream)
184
+ trap('SIGINT', 'EXIT') # expect to exit with ctrl-c
185
+
186
+ ## how to print each line
187
+ out = if options[:timestamp]
188
+ ->(e) { puts("#{set_color(human_time(e.timestamp).utc, :green)} #{e.message}") }
189
+ else
190
+ ->(e) { puts e.message }
191
+ end
192
+
193
+ token = nil
194
+ loop do
195
+ resp = logs.get_log_events(
196
+ log_group_name: group,
197
+ log_stream_name: stream,
198
+ limit: options[:numlines],
199
+ next_token: token,
200
+ )
201
+ resp.events.each do |e|
202
+ out.call(e)
203
+ end
204
+ token = resp.next_forward_token
205
+ options[:follow] ? sleep(options[:sleep]) : break
206
+ end
207
+ end
155
208
  end
156
209
  end
data/lib/awful/kms.rb CHANGED
@@ -22,17 +22,17 @@ module Awful
22
22
  set_color(string, COLORS.fetch(string.to_sym, :yellow))
23
23
  end
24
24
 
25
- def paginate(thing)
26
- next_marker = nil
27
- things = []
28
- loop do
29
- response = yield(next_marker)
30
- things += response.send(thing)
31
- next_marker = response.next_marker
32
- break unless next_marker
33
- end
34
- things
35
- end
25
+ # def paginate(thing)
26
+ # next_marker = nil
27
+ # things = []
28
+ # loop do
29
+ # response = yield(next_marker)
30
+ # things += response.send(thing)
31
+ # next_marker = response.next_marker
32
+ # break unless next_marker
33
+ # end
34
+ # things
35
+ # end
36
36
 
37
37
  def aliases
38
38
  paginate(:aliases) do |marker|
data/lib/awful/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Awful
2
- VERSION = '0.0.152'
2
+ VERSION = '0.0.153'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: awful
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.152
4
+ version: 0.0.153
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ric Lister
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-28 00:00:00.000000000 Z
11
+ date: 2016-12-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -224,7 +224,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
224
224
  version: '0'
225
225
  requirements: []
226
226
  rubyforge_project:
227
- rubygems_version: 2.5.1
227
+ rubygems_version: 2.5.2
228
228
  signing_key:
229
229
  specification_version: 4
230
230
  summary: Simple AWS command-line tool.