papertrail 0.10.0 → 0.11.1

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
- SHA1:
3
- metadata.gz: ea790fef3b0c4239577138618f98b3aa12d4c366
4
- data.tar.gz: bde482d33ce1b144314af54f01fe4f334924b59a
2
+ SHA256:
3
+ metadata.gz: ffe54b3afcb866c1d80ccadf5deff840bbc2d8e9fb851ec2054f0869160eb565
4
+ data.tar.gz: 13e987776d80aedee04422b2490a09123e2045bdc0a220069931a2047e354009
5
5
  SHA512:
6
- metadata.gz: 17ed6f10ebd3ba1e8cfb4b4d1fee258314ac62293cbc9d8c31ba71e079724ce61188b22dd64ef5046ffe2756e3db385b4c05cca58f77636de60d47e3ffe5a7b2
7
- data.tar.gz: d33c5d61a76d488afb1404aaa30740a082184c40ba6b75e6beeaa00db2ccadcff3302702c4f79986c2383f14eb946e6819364fc9fdaf0f8fdd123a940fb37237
6
+ metadata.gz: 13122c148a5417fe71ab68baf78c689ed52178d7c5176b7d194684167f0d1a95ada468666bef37d8a5bf3dccedacbaab93e5c40d44642af2f670b1f7580ddd8c
7
+ data.tar.gz: e5afab539b26cc93d45d3d44da399efa4d3547f2071451f5c765cdff093e4a212338b20c26ae402cee521e7158f27792cd26558dd07a3305b5f5bb59ea532bc3
data/README.md CHANGED
@@ -298,7 +298,7 @@ to your enhancement.
298
298
  [Papertrail]: http://papertrailapp.com/
299
299
  [Connection]: https://github.com/papertrail/papertrail-cli/blob/master/lib/papertrail/connection.rb
300
300
  [HTTP API]: http://help.papertrailapp.com/kb/how-it-works/http-api
301
- [User Profile]: https://papertrailapp.com/user/edit
301
+ [User Profile]: https://papertrailapp.com/account/profile
302
302
  [RubyGems]: https://rubygems.org/gems/papertrail-cli
303
303
  [lnav]: http://lnav.org/
304
304
  [escape characters]: http://en.wikipedia.org/wiki/ANSI_escape_code#Colors
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ require 'papertrail'
2
3
  require 'papertrail/cli_add_group'
3
4
 
4
5
  begin
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ require 'papertrail'
2
3
  require 'papertrail/cli_add_system'
3
4
 
4
5
  begin
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ require 'papertrail'
2
3
  require 'papertrail/cli_join_group'
3
4
 
4
5
  begin
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ require 'papertrail'
2
3
  require 'papertrail/cli_leave_group'
3
4
 
4
5
  begin
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ require 'papertrail'
2
3
  require 'papertrail/cli_remove_system'
3
4
 
4
5
  begin
data/lib/papertrail.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Papertrail
2
- VERSION = '0.10.0'
2
+ VERSION = '0.11.1'
3
3
  end
4
4
 
5
5
  require 'papertrail/connection'
@@ -95,6 +95,9 @@ module Papertrail
95
95
 
96
96
  @connection = Papertrail::Connection.new(options)
97
97
 
98
+ # Use HTTP Keep-Alive unless delay is too long
99
+ connection.start if options[:delay] < 10
100
+
98
101
  if options[:system]
99
102
  query_options[:system_id] = connection.find_id_for_source(options[:system])
100
103
  unless query_options[:system_id]
@@ -166,7 +169,7 @@ module Papertrail
166
169
 
167
170
  idx = attribs.hash % 5
168
171
  color = COLORS[idx]
169
- pre = "#{event.received_at.strftime('%b %e %X')} #{event.data['hostname']} #{event.data['program']}:"
172
+ pre = "#{event.received_at.strftime('%b %d %X')} #{event.data['hostname']} #{event.data['program']}:"
170
173
  post = " #{event.data['message']}"
171
174
  pre.ansi(color) + post
172
175
  end
@@ -46,15 +46,16 @@ module Papertrail
46
46
 
47
47
  raise OptionParser::MissingArgument, 'group' if options[:group].nil?
48
48
 
49
- connection = Papertrail::Connection.new(options)
50
-
51
- # Bail if group already exists
52
- if connection.show_group(options[:group])
53
- exit 0
54
- end
49
+ Papertrail::Connection.new(options).start do |connection|
50
+ # Bail if group already exists
51
+ existing = connection.show_group(options[:group])
52
+ if existing && existing['name'].upcase == options[:group].upcase
53
+ exit 0
54
+ end
55
55
 
56
- if connection.create_group(options[:group], options[:wildcard])
57
- exit 0
56
+ if connection.create_group(options[:group], options[:wildcard])
57
+ exit 0
58
+ end
58
59
  end
59
60
 
60
61
  exit 1
@@ -84,19 +84,20 @@ module Papertrail
84
84
  error 'Either --ip-address or --destination-port most be provided'
85
85
  end
86
86
 
87
- connection = Papertrail::Connection.new(options)
88
-
89
- # Bail if system already exists
90
- if connection.show_source(options[:system])
91
- exit 0
92
- end
87
+ Papertrail::Connection.new(options).start do |connection|
88
+ # Bail if system already exists
89
+ existing = connection.show_source(options[:system])
90
+ if existing && existing['name'].upcase == options[:system].upcase
91
+ exit 0
92
+ end
93
93
 
94
- if options[:destination_port] && !options[:hostname]
95
- options[:hostname] = options[:system]
96
- end
94
+ if options[:destination_port] && !options[:hostname]
95
+ options[:hostname] = options[:system]
96
+ end
97
97
 
98
- if connection.register_source(options[:system], options)
99
- exit 0
98
+ if connection.register_source(options[:system], options)
99
+ exit 0
100
+ end
100
101
  end
101
102
 
102
103
  exit 1
@@ -38,19 +38,32 @@ module Papertrail
38
38
  end
39
39
  end
40
40
 
41
+ def start
42
+ if block_given?
43
+ @connection.start { yield self }
44
+ else
45
+ @connection.start
46
+ self
47
+ end
48
+ end
49
+
50
+ def finish
51
+ @connection.finish
52
+ end
53
+
41
54
  def find_id_for_source(name)
42
- response = @connection.get('systems.json')
55
+ response = @connection.get('systems.json', :system_name => name)
43
56
 
44
57
  find_id_for_item(response.body, name)
45
58
  end
46
59
 
47
60
  def find_id_for_group(name)
48
- response = @connection.get('groups.json')
61
+ response = @connection.get('groups.json', :group_name => name)
49
62
  find_id_for_item(response.body, name)
50
63
  end
51
64
 
52
65
  def find_search(name, group_id = nil)
53
- response = @connection.get('searches.json')
66
+ response = @connection.get('searches.json', :search_name => name)
54
67
 
55
68
  candidates = find_items_by_name(response.body, name)
56
69
  return nil if candidates.empty?
@@ -13,7 +13,7 @@ module Papertrail
13
13
  end
14
14
 
15
15
  def to_s
16
- "#{received_at.strftime('%b %e %X')} #{data['hostname']} #{data['program']}: #{data['message']}"
16
+ "#{received_at.strftime('%b %d %X')} #{data['hostname']} #{data['program']}: #{data['message']}"
17
17
  end
18
18
  end
19
19
  end
@@ -37,6 +37,19 @@ module Papertrail
37
37
  @headers = {}
38
38
  end
39
39
 
40
+ def start
41
+ if block_given?
42
+ https.start { yield self }
43
+ else
44
+ https.start
45
+ self
46
+ end
47
+ end
48
+
49
+ def finish
50
+ https.finish
51
+ end
52
+
40
53
  def basic_auth(login, pass)
41
54
  @headers['Authorization'] = 'Basic ' + ["#{login}:#{pass}"].pack('m').delete("\r\n")
42
55
  end
@@ -46,73 +59,62 @@ module Papertrail
46
59
  end
47
60
 
48
61
  def get(path, params = {})
49
- if params.size > 0
50
- path = "#{path}?cli_version=#{Papertrail::VERSION}&#{build_nested_query(params)}"
51
- end
52
- attempts = 0
53
- begin
54
- on_complete(https.get(request_uri(path), @headers))
55
- rescue SystemCallError, Net::HTTPFatalError => e
56
- sleep 5.0
57
- attempts += 1
58
- retry if (attempts < 3)
59
- raise e
60
- end
62
+ request(:get, path, params)
61
63
  end
62
64
 
63
65
  def put(path, params)
64
- attempts = 0
65
- begin
66
- on_complete(https.put(request_uri(path), build_nested_query(params), @headers))
67
- rescue SystemCallError, Net::HTTPFatalError => e
68
- attempts += 1
69
- retry if (attempts < 3)
70
- raise e
71
- end
66
+ request(:put, path, params)
72
67
  end
73
68
 
74
69
  def post(path, params)
75
- attempts = 0
76
- begin
77
- on_complete(https.post(request_uri(path), build_nested_query(params), @headers))
78
- rescue SystemCallError, Net::HTTPFatalError => e
79
- attempts += 1
80
- retry if (attempts < 3)
81
- raise e
82
- end
70
+ request(:post, path, params)
83
71
  end
84
72
 
85
73
  def delete(path)
74
+ request(:delete, path)
75
+ end
76
+
77
+ private
78
+
79
+ def request(http_method, path, params = {})
86
80
  attempts = 0
81
+ uri = "#{request_uri(path)}?cli_version=#{Papertrail::VERSION}&"
87
82
  begin
88
- on_complete(https.delete(request_uri(path), @headers))
89
- rescue SystemCallError, Net::HTTPFatalError => e
83
+ if http_method == :get || http_method == :delete
84
+ on_complete(https.send(http_method, uri + build_nested_query(params.merge(cli_version)), @headers))
85
+ else
86
+ on_complete(https.send(http_method, uri, build_nested_query(params), @headers))
87
+ end
88
+ rescue IOError, SystemCallError, Net::HTTPFatalError => e
90
89
  attempts += 1
91
90
  retry if (attempts < 3)
92
91
  raise e
93
92
  end
94
93
  end
95
94
 
96
- private
97
-
98
95
  def request_uri(path)
99
96
  path.start_with?('/api/v1/') ? path : "/api/v1/#{path}"
100
97
  end
101
98
 
102
99
  def https
103
- http = Net::HTTP.new('papertrailapp.com', 443)
104
- http.use_ssl = true
105
- http.verify_mode = ssl_verify_mode
106
- http.cert_store = ssl_cert_store
107
-
108
- http.cert = @ssl[:client_cert] if @ssl[:client_cert]
109
- http.key = @ssl[:client_key] if @ssl[:client_key]
110
- http.ca_file = @ssl[:ca_file] if @ssl[:ca_file]
111
- http.ca_path = @ssl[:ca_path] if @ssl[:ca_path]
112
- http.verify_depth = @ssl[:verify_depth] if @ssl[:verify_depth]
113
- http.ssl_version = @ssl[:version] if @ssl[:version]
114
-
115
- http
100
+ @https ||= Net::HTTP.new('papertrailapp.com', 443).tap do |http|
101
+ http.use_ssl = true
102
+ http.verify_mode = ssl_verify_mode
103
+ http.cert_store = ssl_cert_store
104
+
105
+ http.cert = @ssl[:client_cert] if @ssl[:client_cert]
106
+ http.key = @ssl[:client_key] if @ssl[:client_key]
107
+ http.ca_file = @ssl[:ca_file] if @ssl[:ca_file]
108
+ http.ca_path = @ssl[:ca_path] if @ssl[:ca_path]
109
+ http.verify_depth = @ssl[:verify_depth] if @ssl[:verify_depth]
110
+ http.ssl_version = @ssl[:version] if @ssl[:version]
111
+
112
+ http.keep_alive_timeout = 10 if http.respond_to?(:keep_alive_timeout=)
113
+ end
114
+ end
115
+
116
+ def cli_version
117
+ { :cli_version => Papertrail::VERSION }
116
118
  end
117
119
 
118
120
  def ssl_verify_mode
data/papertrail.gemspec CHANGED
@@ -9,8 +9,8 @@ Gem::Specification.new do |s|
9
9
  ## If your rubyforge_project name is different, then edit it and comment out
10
10
  ## the sub! line in the Rakefile
11
11
  s.name = 'papertrail'
12
- s.version = '0.10.0'
13
- s.date = '2016-12-02'
12
+ s.version = '0.11.1'
13
+ s.date = '2021-05-27'
14
14
  s.rubyforge_project = 'papertrail'
15
15
 
16
16
  ## Make sure your summary is short. The description may be as long
@@ -55,6 +55,10 @@ Gem::Specification.new do |s|
55
55
  s.add_development_dependency('minitest')
56
56
  s.add_development_dependency('mocha')
57
57
 
58
+ if RUBY_VERSION > '1.9'
59
+ s.add_development_dependency('addressable', '~> 2.4.0')
60
+ s.add_development_dependency('webmock', '~> 1.24.6')
61
+ end
58
62
  if RUBY_VERSION > '2.2.4' # ruby_dep dependency
59
63
  s.add_development_dependency('guard', '~> 2.14.0')
60
64
  s.add_development_dependency('guard-minitest', '~> 2.4.6')
@@ -93,6 +97,7 @@ Gem::Specification.new do |s|
93
97
  lib/papertrail/search_result.rb
94
98
  papertrail.gemspec
95
99
  test/connection_test.rb
100
+ test/http_client_test.rb
96
101
  test/search_query_test.rb
97
102
  test/test_helper.rb
98
103
  ]
@@ -4,6 +4,17 @@ class ConnectionTest < Minitest::Test
4
4
  let(:connection_options) { { :token => 'dummy' } }
5
5
  let(:connection) { Papertrail::Connection.new(connection_options) }
6
6
 
7
+ def test_start_finish
8
+ assert_same connection, connection.start
9
+ assert_nil connection.finish
10
+
11
+ block_args = nil
12
+ connection.start {|*args| block_args = args}
13
+ assert_equal [connection], block_args
14
+
15
+ assert_raises { connection.finish }
16
+ end
17
+
7
18
  def test_each_event
8
19
  skip
9
20
  end
@@ -0,0 +1,46 @@
1
+ require 'test_helper'
2
+
3
+ class HttpClientTest < Minitest::Test
4
+ let(:http) { Papertrail::HttpClient.new({}) }
5
+ describe 'http methods' do
6
+ def test_start_finish
7
+ assert_same http, http.start
8
+ assert_nil http.finish
9
+
10
+ block_args = nil
11
+ http.start {|*args| block_args = args}
12
+ assert_equal [http], block_args
13
+
14
+ assert_raises { http.finish }
15
+ end
16
+
17
+ def test_cli_version_present_in_http_methods
18
+ return if RUBY_VERSION < '2.0'
19
+ # if webmock.disable_net_connect intervenes with a stack trace,
20
+ # we know the http methods are not sending cli_version in params
21
+ stub_request(:get, "https://papertrailapp.com/api/v1/some-path?cli_version=#{Papertrail::VERSION}&").to_return(:status => 200, :body => '', :headers => {})
22
+ http.get('some-path')
23
+
24
+ stub_request(:post, "https://papertrailapp.com/api/v1/some-path?cli_version=#{Papertrail::VERSION}&").to_return(:status => 200, :body => '', :headers => {})
25
+ http.post('some-path', {})
26
+
27
+
28
+ stub_request(:put, "https://papertrailapp.com/api/v1/some-path?cli_version=#{Papertrail::VERSION}&").to_return(:status => 200, :body => '', :headers => {})
29
+ http.put('some-path', {})
30
+
31
+ stub_request(:delete, "https://papertrailapp.com/api/v1/some-path?cli_version=#{Papertrail::VERSION}&").to_return(:status => 200, :body => '', :headers => {})
32
+ http.delete('some-path')
33
+ end
34
+ end
35
+
36
+ describe 'build nested query' do
37
+ def test_value_accepts_hash
38
+ assert_equal(http.send(:build_nested_query, {}), "")
39
+ assert_equal(http.send(:build_nested_query, {:a => 1}), "a=1")
40
+ end
41
+ def test_value_accepts_array
42
+ assert_equal(http.send(:build_nested_query, []), "")
43
+ assert_equal(http.send(:build_nested_query, [1,2]), "%5B%5D=1&%5B%5D=2")
44
+ end
45
+ end
46
+ end
data/test/test_helper.rb CHANGED
@@ -1,6 +1,11 @@
1
+ gem "minitest"
1
2
  require 'minitest/autorun'
2
3
  require 'minitest/pride' # Color!
3
- require 'mocha/mini_test'
4
+ require 'mocha/minitest'
5
+ unless RUBY_VERSION < '1.9'
6
+ require 'webmock/minitest'
7
+ WebMock.disable_net_connect!
8
+ end
4
9
 
5
10
  require 'pp'
6
11
 
@@ -8,4 +13,4 @@ class Minitest::Test
8
13
  extend Minitest::Spec::DSL # Add let blocks
9
14
  end
10
15
 
11
- require './lib/papertrail'
16
+ require './lib/papertrail'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: papertrail
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0
4
+ version: 0.11.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Papertrail
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-02 00:00:00.000000000 Z
11
+ date: 2021-05-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: chronic
@@ -80,6 +80,34 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: addressable
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 2.4.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 2.4.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: webmock
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 1.24.6
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 1.24.6
83
111
  - !ruby/object:Gem::Dependency
84
112
  name: guard
85
113
  requirement: !ruby/object:Gem::Requirement
@@ -150,13 +178,14 @@ files:
150
178
  - lib/papertrail/search_result.rb
151
179
  - papertrail.gemspec
152
180
  - test/connection_test.rb
181
+ - test/http_client_test.rb
153
182
  - test/search_query_test.rb
154
183
  - test/test_helper.rb
155
184
  homepage: http://github.com/papertrail/papertrail-cli
156
185
  licenses:
157
186
  - MIT
158
187
  metadata: {}
159
- post_install_message:
188
+ post_install_message:
160
189
  rdoc_options:
161
190
  - "--charset=UTF-8"
162
191
  require_paths:
@@ -172,9 +201,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
172
201
  - !ruby/object:Gem::Version
173
202
  version: '0'
174
203
  requirements: []
175
- rubyforge_project: papertrail
176
- rubygems_version: 2.4.8
177
- signing_key:
204
+ rubygems_version: 3.1.6
205
+ signing_key:
178
206
  specification_version: 2
179
207
  summary: Command-line client for Papertrail hosted log management service.
180
208
  test_files: