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

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,10 +1,11 @@
1
1
  module LogsCfPlugin
2
2
  class ClientConfig
3
- attr_reader :loggregator_host, :user_token, :output, :trace, :use_ssl
3
+ attr_reader :loggregator_host, :loggregator_port, :user_token, :output, :trace, :use_ssl
4
4
 
5
- def initialize(loggregator_host, user_token, output, trace, use_ssl = true)
5
+ def initialize(loggregator_host, loggregator_port, user_token, output, trace, use_ssl = true)
6
6
  @output = output
7
7
  @loggregator_host = loggregator_host
8
+ @loggregator_port = loggregator_port
8
9
  @user_token = user_token
9
10
  @trace = trace
10
11
  @use_ssl = use_ssl
@@ -26,8 +26,8 @@ module LogsCfPlugin
26
26
  Mothership::Help.command_help(@@commands[:logs])
27
27
  fail "Please provide an application to log."
28
28
  end
29
-
30
- client_config = ClientConfig.new(loggregator_host, client.token.auth_header, STDOUT, input[:trace], use_ssl)
29
+ port = input[:recent] ? nil : 4443
30
+ client_config = ClientConfig.new(loggregator_host, port, client.token.auth_header, STDOUT, input[:trace], use_ssl)
31
31
 
32
32
  client_clazz = input[:recent] ? RecentLogsClient : TailingLogsClient
33
33
  client_clazz.new(client_config).logs_for(input[:app])
@@ -9,24 +9,9 @@ module LogsCfPlugin
9
9
 
10
10
  def logs_for(app)
11
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
12
+ uri = "#{prefix}://#{loggregator_host}#{loggregator_port ? ":#{loggregator_port}" : ""}/dump/?app=#{app.guid}"
13
+ response = make_dump_request(uri, app)
14
+ return unless response
30
15
 
31
16
  response_bytes = StringIO.new(response.body)
32
17
  messages = []
@@ -57,10 +42,10 @@ module LogsCfPlugin
57
42
 
58
43
  attr_reader :config
59
44
 
60
- delegate :output, :loggregator_host, :user_token, :trace, :use_ssl,
45
+ delegate :output, :loggregator_host, :loggregator_port, :user_token, :trace, :use_ssl,
61
46
  to: :config
62
47
 
63
- def make_dump_request(uri)
48
+ def make_dump_request(uri, app)
64
49
  uri = URI.parse(uri)
65
50
  http = Net::HTTP.new(uri.host, uri.port)
66
51
  http.use_ssl = use_ssl
@@ -80,7 +65,23 @@ module LogsCfPlugin
80
65
  end
81
66
 
82
67
  response = http.request(request)
83
- response
68
+
69
+ case response.code
70
+ when "200"
71
+ return response
72
+ when "302"
73
+ response = make_dump_request(response['location'], app)
74
+ return response
75
+ when "401"
76
+ output.puts("Unauthorized")
77
+ return
78
+ when "404"
79
+ output.puts("App #{app.name} not found")
80
+ return
81
+ else
82
+ output.puts("Error connecting to server #{response.code}")
83
+ return
84
+ end
84
85
  end
85
86
 
86
87
  def sane_headers(obj)
@@ -8,62 +8,86 @@ module LogsCfPlugin
8
8
  end
9
9
 
10
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
11
+ protocol = use_ssl ? "wss" : "ws"
12
+ websocket_address = "#{protocol}://#{loggregator_host}#{loggregator_port ? ":#{loggregator_port}" : ""}/tail/?app=#{app.guid}"
16
13
 
17
14
  output.puts "websocket_address: #{websocket_address}" if trace
18
15
 
19
- EM.run {
20
- ws = Faye::WebSocket::Client.new(websocket_address, nil, :headers => {"Origin" => "http://localhost", "Authorization" => user_token})
16
+ make_websocket_request(app, websocket_address)
17
+ end
21
18
 
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])
19
+ def make_websocket_request(app, websocket_address)
20
+ redirect_uri = nil
21
+ @em_client_thread = Thread.new do
22
+ EM.run {
23
+ ws = Faye::WebSocket::Client.new(websocket_address, nil, :headers => {"Origin" => "http://localhost", "Authorization" => user_token})
24
+ ws.on :open do |event|
25
+ output.puts("Connected to server.")
26
+ EventMachine.add_periodic_timer(keep_alive_interval) do
27
+ ws.send([42])
28
+ end
26
29
  end
27
- end
28
30
 
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
31
+ ws.on :message do |event|
32
+ begin
33
+ received_message = LogMessage.decode(event.data.pack("C*"))
34
+ write(app, output, received_message)
35
+ rescue Beefcake::Message::WrongTypeError, Beefcake::Message::RequiredFieldNotSetError, Beefcake::Message::InvalidValueError
36
+ output.puts("Error parsing data. Please ensure your gem is the latest version.")
37
+ ws.close
38
+ @em_client_thread.kill
39
+ end
37
40
  end
38
- end
39
41
 
40
- ws.on :error do |event|
41
- @config.output.puts("Server error")
42
- @config.output.puts(event.data.inspect) if trace
43
- end
42
+ ws.on :error do |event|
43
+ if !redirect_uri
44
+ if event.current_target.status == 302
45
+ redirect_uri = event.current_target.headers["location"]
46
+ ws.close
47
+ @em_client_thread.kill
48
+ ws = nil
49
+ else
50
+ output.puts("Server error: #{websocket_address}")
51
+ output.puts(event.data.inspect) if trace
52
+ end
53
+ end
54
+ end
44
55
 
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.")
56
+ ws.on :close do |event|
57
+ unless redirect_uri
58
+ ws.close
59
+ case event.code
60
+ when 4000
61
+ output.puts("Error: No space given.")
62
+ when 4001
63
+ output.puts("Error: No authorization token given.")
64
+ when 4002
65
+ output.puts("Error: Not authorized.")
66
+ end
67
+ output.puts("Server dropped connection...goodbye.")
68
+ @em_client_thread.kill
69
+ ws = nil
70
+ end
54
71
  end
55
- @config.output.puts("Server dropped connection...goodbye.")
56
- EM.stop
57
- ws = nil
58
- end
59
- }
72
+ }
73
+ end
74
+
75
+ wait_for_em_thread_to_finish
76
+
77
+ make_websocket_request(app, redirect_uri) if redirect_uri
78
+ end
79
+
80
+ def wait_for_em_thread_to_finish
81
+ while @em_client_thread.alive? do
82
+ sleep 0.1
83
+ end
60
84
  end
61
85
 
62
86
  private
63
87
 
64
88
  attr_reader :config
65
89
 
66
- delegate :output, :loggregator_host, :user_token, :trace, :use_ssl,
90
+ delegate :output, :loggregator_host, :loggregator_port, :user_token, :trace, :use_ssl,
67
91
  to: :config
68
92
 
69
93
  def keep_alive_interval
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logs-cf-plugin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.39.pre
4
+ version: 0.0.40.pre
5
5
  prerelease: 7
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-09-04 00:00:00.000000000 Z
12
+ date: 2013-09-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: cf