Module: Kontena::Cli::Helpers::LogHelper

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/kontena/cli/helpers/log_helper.rb', line 4

def self.included(base)
  if base.respond_to?(:option)
    base.option ["-f", "--follow"], :flag, "Follow log output", :attribute_name => :tail, default: false
    base.option ['--tail', '--lines'], "LINES", "Number of lines to show from the end of the logs", :attribute_name => :lines, default: 100 do |s|
      Integer(s)
    end
    base.option "--since", "SINCE", "Show logs since given timestamp"
  end
end

Instance Method Details

#buffered_log_json(chunk) ⇒ Hash, NilClass

Parameters:

Returns:

  • (Hash, NilClass)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/kontena/cli/helpers/log_helper.rb', line 64

def buffered_log_json(chunk)
  @buffer = '' if @buffer.nil?
  return if @buffer.empty? && chunk.strip.empty?
  begin
    orig_chunk = chunk
    unless @buffer.empty?
      chunk = @buffer + chunk
    end
    unless chunk.empty?
      log = JSON.parse(chunk)
    end
    @buffer = ''
    log
  rescue
    @buffer << orig_chunk
    nil
  end
end

#color_for_container(container_id) ⇒ Symbol

Parameters:

Returns:

  • (Symbol)


92
93
94
95
# File 'lib/kontena/cli/helpers/log_helper.rb', line 92

def color_for_container(container_id)
  color_maps[container_id] = colors.shift unless color_maps[container_id]
  color_maps[container_id].to_sym
end

#color_mapsHash

Returns:

  • (Hash)


98
99
100
# File 'lib/kontena/cli/helpers/log_helper.rb', line 98

def color_maps
  @color_maps ||= {}
end

#colorsArray<Symbol>

Returns:

  • (Array<Symbol>)


103
104
105
106
107
108
109
110
111
# File 'lib/kontena/cli/helpers/log_helper.rb', line 103

def colors
  if(@colors.nil? || @colors.size == 0)
    @colors = %i(
      red green yellow blue magenta cyan bright_red bright_green
      bright_yellow bright_blue bright_magenta bright_cyan
    )
  end
  @colors
end

#get_logs(url, query_params) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/kontena/cli/helpers/log_helper.rb', line 27

def get_logs(url, query_params)
  query_params[:limit] = lines if lines
  query_params[:since] = since if since

  result = client(token).get(url, query_params)
  result['logs'].each do |log|
    yield log
  end
end

#show_log(log) ⇒ Object



83
84
85
86
87
88
# File 'lib/kontena/cli/helpers/log_helper.rb', line 83

def show_log(log)
  color = color_for_container(log['name'])
  prefix = "#{log['created_at']} #{log['name']}:"

  puts "#{pastel.send(color, prefix)} #{log['data']}"
end

#show_logs(url, query_params = { }, &block) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/kontena/cli/helpers/log_helper.rb', line 19

def show_logs(url, query_params = { }, &block)
  if tail?
    stream_logs(url, query_params, &block)
  else
    get_logs(url, query_params, &block)
  end
end

#stream_logs(url, query_params) ⇒ Object

Parameters:

  • url (String)
  • query_params (Hash)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/kontena/cli/helpers/log_helper.rb', line 39

def stream_logs(url, query_params)
  query_params[:limit] = lines if lines
  query_params[:since] = since if since
  query_params[:follow] = 1

  last_seen = nil
  streamer = lambda do |chunk, remaining_bytes, total_bytes|
    log = buffered_log_json(chunk)
    if log
      yield log
      last_seen = log['id']
    end
  end

  begin
    query_params[:from] = last_seen if last_seen
    client(token).get_stream(url, streamer, query_params)
  rescue => exc
    retry if exc.cause.is_a?(EOFError) # Excon wraps the EOFerror into SocketError
    raise
  end
end

#tokenString

Returns:



15
16
17
# File 'lib/kontena/cli/helpers/log_helper.rb', line 15

def token
  @token ||= require_token
end