logs-cf-plugin 0.0.38.pre → 0.0.39.pre

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,13 @@
1
+ module LogsCfPlugin
2
+ class ClientConfig
3
+ attr_reader :loggregator_host, :user_token, :output, :trace, :use_ssl
4
+
5
+ def initialize(loggregator_host, user_token, output, trace, use_ssl = true)
6
+ @output = output
7
+ @loggregator_host = loggregator_host
8
+ @user_token = user_token
9
+ @trace = trace
10
+ @use_ssl = use_ssl
11
+ end
12
+ end
13
+ end
@@ -6,7 +6,9 @@ require 'uri'
6
6
 
7
7
  module LogsCfPlugin
8
8
  require 'logs-cf-plugin/message_writer'
9
- require 'logs-cf-plugin/loggregator_client'
9
+ require 'logs-cf-plugin/client_config'
10
+ require 'logs-cf-plugin/tailing_logs_client'
11
+ require 'logs-cf-plugin/recent_logs_client'
10
12
 
11
13
  class Plugin < CF::CLI
12
14
  include LoginRequirements
@@ -25,13 +27,10 @@ module LogsCfPlugin
25
27
  fail "Please provide an application to log."
26
28
  end
27
29
 
28
- loggregator_client = LoggregatorClient.new(loggregator_host, client.token.auth_header, STDOUT, input[:trace], use_ssl)
30
+ client_config = ClientConfig.new(loggregator_host, client.token.auth_header, STDOUT, input[:trace], use_ssl)
29
31
 
30
- if input[:recent]
31
- loggregator_client.dump_messages(input[:app])
32
- else
33
- loggregator_client.listen(input[:app])
34
- end
32
+ client_clazz = input[:recent] ? RecentLogsClient : TailingLogsClient
33
+ client_clazz.new(client_config).logs_for(input[:app])
35
34
  end
36
35
 
37
36
  ::ManifestsPlugin.default_to_app_from_manifest(:logs, false)
@@ -0,0 +1,96 @@
1
+ module LogsCfPlugin
2
+ class RecentLogsClient
3
+ include CFoundry::TraceHelpers
4
+ include MessageWriter
5
+
6
+ def initialize(config)
7
+ @config = config
8
+ end
9
+
10
+ def logs_for(app)
11
+ prefix = use_ssl ? 'https' : 'http'
12
+ uri = "#{prefix}://#{loggregator_host}/dump/?app=#{app.guid}"
13
+ response = make_dump_request(uri)
14
+
15
+ case response.code
16
+ when "200"
17
+ # fall thru
18
+ when "302"
19
+ response = make_dump_request(response['location'])
20
+ when "401"
21
+ output.puts("Unauthorized")
22
+ return
23
+ when "404"
24
+ output.puts("App #{app.name} not found")
25
+ return
26
+ else
27
+ output.puts("Error connecting to server #{response.code}")
28
+ return
29
+ end
30
+
31
+ response_bytes = StringIO.new(response.body)
32
+ messages = []
33
+ while len = response_bytes.read(4)
34
+ len = len.unpack("N")[0] # 32-bit length, BigEndian stylie
35
+ record = response_bytes.read(len) # This returns a string even if len is 0.
36
+ msg = LogMessage.decode(record)
37
+ messages << msg
38
+ end
39
+
40
+ if trace
41
+ response_hash = {
42
+ :headers => sane_headers(response),
43
+ :status => response.code,
44
+ :body => messages
45
+ }
46
+ output.puts(response_trace(response_hash))
47
+ end
48
+
49
+ messages.each do |m|
50
+ write(app, output, m)
51
+ end
52
+ rescue Beefcake::Message::WrongTypeError, Beefcake::Message::RequiredFieldNotSetError, Beefcake::Message::InvalidValueError
53
+ output.puts("Error parsing data. Please ensure your gem is the latest version.")
54
+ end
55
+
56
+ private
57
+
58
+ attr_reader :config
59
+
60
+ delegate :output, :loggregator_host, :user_token, :trace, :use_ssl,
61
+ to: :config
62
+
63
+ def make_dump_request(uri)
64
+ uri = URI.parse(uri)
65
+ http = Net::HTTP.new(uri.host, uri.port)
66
+ http.use_ssl = use_ssl
67
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
68
+
69
+ request = Net::HTTP::Get.new(uri.request_uri)
70
+ request['Authorization'] = user_token
71
+
72
+ if trace
73
+ request_hash = {
74
+ :url => uri.to_s,
75
+ :method => "GET",
76
+ :headers => sane_headers(request),
77
+ :body => ""
78
+ }
79
+ output.puts(request_trace(request_hash))
80
+ end
81
+
82
+ response = http.request(request)
83
+ response
84
+ end
85
+
86
+ def sane_headers(obj)
87
+ hds = {}
88
+
89
+ obj.each_header do |k, v|
90
+ hds[k] = v
91
+ end
92
+
93
+ hds
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,73 @@
1
+ module LogsCfPlugin
2
+ class TailingLogsClient
3
+ #include CFoundry::TraceHelpers
4
+ include MessageWriter
5
+
6
+ def initialize(config)
7
+ @config = config
8
+ end
9
+
10
+ def logs_for(app)
11
+ if use_ssl
12
+ websocket_address = "wss://#{loggregator_host}:4443/tail/?app=#{app.guid}"
13
+ else
14
+ websocket_address = "ws://#{loggregator_host}/tail/?app=#{app.guid}"
15
+ end
16
+
17
+ output.puts "websocket_address: #{websocket_address}" if trace
18
+
19
+ EM.run {
20
+ ws = Faye::WebSocket::Client.new(websocket_address, nil, :headers => {"Origin" => "http://localhost", "Authorization" => user_token})
21
+
22
+ ws.on :open do |event|
23
+ output.puts("Connected to server.")
24
+ EventMachine.add_periodic_timer(keep_alive_interval) do
25
+ ws.send([42])
26
+ end
27
+ end
28
+
29
+ ws.on :message do |event|
30
+ begin
31
+ received_message = LogMessage.decode(event.data.pack("C*"))
32
+ write(app, output, received_message)
33
+ rescue Beefcake::Message::WrongTypeError, Beefcake::Message::RequiredFieldNotSetError, Beefcake::Message::InvalidValueError
34
+ output.puts("Error parsing data. Please ensure your gem is the latest version.")
35
+ ws.close
36
+ EM.stop
37
+ end
38
+ end
39
+
40
+ ws.on :error do |event|
41
+ @config.output.puts("Server error")
42
+ @config.output.puts(event.data.inspect) if trace
43
+ end
44
+
45
+ ws.on :close do |event|
46
+ ws.close
47
+ case event.code
48
+ when 4000
49
+ @config.output.puts("Error: No space given.")
50
+ when 4001
51
+ @config.output.puts("Error: No authorization token given.")
52
+ when 4002
53
+ @config.output.puts("Error: Not authorized.")
54
+ end
55
+ @config.output.puts("Server dropped connection...goodbye.")
56
+ EM.stop
57
+ ws = nil
58
+ end
59
+ }
60
+ end
61
+
62
+ private
63
+
64
+ attr_reader :config
65
+
66
+ delegate :output, :loggregator_host, :user_token, :trace, :use_ssl,
67
+ to: :config
68
+
69
+ def keep_alive_interval
70
+ 25
71
+ end
72
+ end
73
+ end
metadata CHANGED
@@ -1,18 +1,20 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logs-cf-plugin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.38.pre
4
+ version: 0.0.39.pre
5
+ prerelease: 7
5
6
  platform: ruby
6
7
  authors:
7
8
  - Pivotal
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2013-09-03 00:00:00.000000000 Z
12
+ date: 2013-09-04 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: cf
15
16
  requirement: !ruby/object:Gem::Requirement
17
+ none: false
16
18
  requirements:
17
19
  - - ~>
18
20
  - !ruby/object:Gem::Version
@@ -20,6 +22,7 @@ dependencies:
20
22
  type: :runtime
21
23
  prerelease: false
22
24
  version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
23
26
  requirements:
24
27
  - - ~>
25
28
  - !ruby/object:Gem::Version
@@ -27,6 +30,7 @@ dependencies:
27
30
  - !ruby/object:Gem::Dependency
28
31
  name: faye-websocket
29
32
  requirement: !ruby/object:Gem::Requirement
33
+ none: false
30
34
  requirements:
31
35
  - - ~>
32
36
  - !ruby/object:Gem::Version
@@ -34,6 +38,7 @@ dependencies:
34
38
  type: :runtime
35
39
  prerelease: false
36
40
  version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
37
42
  requirements:
38
43
  - - ~>
39
44
  - !ruby/object:Gem::Version
@@ -41,6 +46,7 @@ dependencies:
41
46
  - !ruby/object:Gem::Dependency
42
47
  name: beefcake
43
48
  requirement: !ruby/object:Gem::Requirement
49
+ none: false
44
50
  requirements:
45
51
  - - ~>
46
52
  - !ruby/object:Gem::Version
@@ -48,6 +54,7 @@ dependencies:
48
54
  type: :runtime
49
55
  prerelease: false
50
56
  version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
51
58
  requirements:
52
59
  - - ~>
53
60
  - !ruby/object:Gem::Version
@@ -55,6 +62,7 @@ dependencies:
55
62
  - !ruby/object:Gem::Dependency
56
63
  name: loggregator_messages
57
64
  requirement: !ruby/object:Gem::Requirement
65
+ none: false
58
66
  requirements:
59
67
  - - ~>
60
68
  - !ruby/object:Gem::Version
@@ -62,6 +70,7 @@ dependencies:
62
70
  type: :runtime
63
71
  prerelease: false
64
72
  version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
65
74
  requirements:
66
75
  - - ~>
67
76
  - !ruby/object:Gem::Version
@@ -73,33 +82,36 @@ executables: []
73
82
  extensions: []
74
83
  extra_rdoc_files: []
75
84
  files:
76
- - lib/logs-cf-plugin/loggregator_client.rb
85
+ - lib/logs-cf-plugin/client_config.rb
77
86
  - lib/logs-cf-plugin/message_writer.rb
78
87
  - lib/logs-cf-plugin/plugin.rb
88
+ - lib/logs-cf-plugin/recent_logs_client.rb
89
+ - lib/logs-cf-plugin/tailing_logs_client.rb
79
90
  - README.md
80
91
  homepage: http://github.com/cloudfoundry/logs-cf-plugin
81
92
  licenses:
82
93
  - Apache 2.0
83
- metadata: {}
84
94
  post_install_message:
85
95
  rdoc_options: []
86
96
  require_paths:
87
97
  - lib
88
98
  - vendor
89
99
  required_ruby_version: !ruby/object:Gem::Requirement
100
+ none: false
90
101
  requirements:
91
102
  - - ! '>='
92
103
  - !ruby/object:Gem::Version
93
104
  version: 1.9.3
94
105
  required_rubygems_version: !ruby/object:Gem::Requirement
106
+ none: false
95
107
  requirements:
96
108
  - - ! '>'
97
109
  - !ruby/object:Gem::Version
98
110
  version: 1.3.1
99
111
  requirements: []
100
112
  rubyforge_project:
101
- rubygems_version: 2.0.4
113
+ rubygems_version: 1.8.25
102
114
  signing_key:
103
- specification_version: 4
115
+ specification_version: 3
104
116
  summary: CF Logs
105
117
  test_files: []
checksums.yaml DELETED
@@ -1,15 +0,0 @@
1
- ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- ZjE3ZDZkYmZmMGY0YzljNGMxNDFjNDcxY2Y4YmVkY2MzNDU5NmNiMw==
5
- data.tar.gz: !binary |-
6
- ZmUxMWYxM2MwNzg3NGIxZjk3YTA2OGJlNGE2YWVmM2VjMWZkODUyZQ==
7
- !binary "U0hBNTEy":
8
- metadata.gz: !binary |-
9
- YTg1YWI4MzI3YjYzNWExNGZjMjhhZWIxMjVmN2Y3ZTE1ZWJmOTQ2YTkyZjMz
10
- YzcxNTQxMjY1ZDlmYTE3ZGIzZTMyOWI1MjM1YzBhM2E2ZTk2OWNmODBlZjUy
11
- Nzk4N2Q1ZjY3MzhkYzY4OGYzYzQ5MGU5MmIyYzhjOWYxMWNiYzM=
12
- data.tar.gz: !binary |-
13
- NWEwMWE3ZWFlMWViMzlkZWE1ZDRhOGY4MmJkZjI2NGQxZmQyYTZkMTZhNzVi
14
- ODkxMDY1MzA5ZWYyNTk0OGYwN2VlYjdhYmExZDY2YTQzODkwOGMzNjc2YzE5
15
- MmQzYjQwNmZmNDQ1NDI2OGQ1N2VjZjE3OGI4ZDFlMWQ3ZmI5YmI=
@@ -1,150 +0,0 @@
1
- module LogsCfPlugin
2
- class LoggregatorClient
3
- include CFoundry::TraceHelpers
4
- include MessageWriter
5
-
6
- def initialize(loggregator_host, user_token, output, trace, use_ssl = true)
7
- @output = output
8
- @loggregator_host = loggregator_host
9
- @user_token = user_token
10
- @trace = trace
11
- @use_ssl = use_ssl
12
- end
13
-
14
- def listen(app)
15
- if use_ssl
16
- websocket_address = "wss://#{loggregator_host}:4443/tail/?app=#{app.guid}"
17
- else
18
- websocket_address = "ws://#{loggregator_host}/tail/?app=#{app.guid}"
19
- end
20
-
21
- output.puts "websocket_address: #{websocket_address}" if trace
22
-
23
- EM.run {
24
- ws = Faye::WebSocket::Client.new(websocket_address, nil, :headers => {"Origin" => "http://localhost", "Authorization" => user_token})
25
-
26
- ws.on :open do |event|
27
- output.puts("Connected to server.")
28
- EventMachine.add_periodic_timer(keep_alive_interval) do
29
- ws.send([42])
30
- end
31
- end
32
-
33
- ws.on :message do |event|
34
- begin
35
- received_message = LogMessage.decode(event.data.pack("C*"))
36
- write(app, output, received_message)
37
- rescue Beefcake::Message::WrongTypeError, Beefcake::Message::RequiredFieldNotSetError, Beefcake::Message::InvalidValueError
38
- output.puts("Error parsing data. Please ensure your gem is the latest version.")
39
- ws.close
40
- EM.stop
41
- end
42
- end
43
-
44
- ws.on :error do |event|
45
- output.puts("Server error")
46
- output.puts(event.data.inspect) if trace
47
- end
48
-
49
- ws.on :close do |event|
50
- ws.close
51
- case event.code
52
- when 4000
53
- output.puts("Error: No space given.")
54
- when 4001
55
- output.puts("Error: No authorization token given.")
56
- when 4002
57
- output.puts("Error: Not authorized.")
58
- end
59
- output.puts("Server dropped connection...goodbye.")
60
- EM.stop
61
- ws = nil
62
- end
63
- }
64
- end
65
-
66
- def dump_messages(app)
67
- prefix = use_ssl ? 'https' : 'http'
68
- uri = URI.parse("#{prefix}://#{loggregator_host}/dump/?app=#{app.guid}")
69
- http = Net::HTTP.new(uri.host, uri.port)
70
- http.use_ssl = use_ssl
71
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
72
-
73
- request = Net::HTTP::Get.new(uri.request_uri)
74
- request['Authorization'] = user_token
75
-
76
- if trace
77
- request_hash = {
78
- :url => uri.request_uri,
79
- :method => "GET",
80
- :headers => sane_headers(request),
81
- :body => ""
82
- }
83
- output.puts(request_trace(request_hash))
84
- end
85
-
86
- response = http.request(request)
87
-
88
- case response.code
89
- when "200"
90
- # fall thru
91
- when "401"
92
- output.puts("Unauthorized")
93
- return
94
- when "404"
95
- output.puts("App #{app.name} not found")
96
- return
97
- else
98
- output.puts("Error connecting to server #{response.code}")
99
- return
100
- end
101
-
102
- response_bytes = StringIO.new(response.body)
103
- messages = []
104
- while len = response_bytes.read(4)
105
- len = len.unpack("N")[0] # 32-bit length, BigEndian stylie
106
- record = response_bytes.read(len) # This returns a string even if len is 0.
107
- msg = LogMessage.decode(record)
108
- messages << msg
109
- end
110
-
111
- if trace
112
- response_hash = {
113
- :headers => sane_headers(response),
114
- :status => response.code,
115
- :body => messages
116
- }
117
- output.puts(response_trace(response_hash))
118
- end
119
-
120
- messages.each do |m|
121
- write(app, output, m)
122
- end
123
- rescue Beefcake::Message::WrongTypeError, Beefcake::Message::RequiredFieldNotSetError, Beefcake::Message::InvalidValueError
124
- output.puts("Error parsing data. Please ensure your gem is the latest version.")
125
- []
126
- end
127
-
128
- private
129
-
130
- def sane_headers(obj)
131
- hds = {}
132
-
133
- obj.each_header do |k, v|
134
- hds[k] = v
135
- end
136
-
137
- hds
138
- end
139
-
140
- def keep_alive_interval
141
- 25
142
- end
143
-
144
- def hash_to_query(hash)
145
- return URI.encode(hash.map { |k, v| "#{k}=#{v}" }.join("&"))
146
- end
147
-
148
- attr_reader :output, :loggregator_host, :user_token, :trace, :use_ssl
149
- end
150
- end