Class: Central::Cli::Stacks::LogsCommand

Inherits:
Clamp::Command
  • Object
show all
Includes:
Common
Defined in:
lib/central/cli/stacks/logs_command.rb

Instance Method Summary collapse

Methods included from Common

#access_token=, #add_master, #api_url, #api_url=, #clear_current_stack, #client, #current_master, #current_master=, #current_master_index, #current_stack, #current_stack=, #ensure_custom_ssl_ca, #require_api_url, #require_current_stack, #require_token, #reset_client, #save_settings, #settings, #settings_filename

Instance Method Details

#color_for_container(container_id) ⇒ Object



64
65
66
67
# File 'lib/central/cli/stacks/logs_command.rb', line 64

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_mapsObject



69
70
71
# File 'lib/central/cli/stacks/logs_command.rb', line 69

def color_maps
  @color_maps ||= {}
end

#colorsObject



73
74
75
76
77
78
79
# File 'lib/central/cli/stacks/logs_command.rb', line 73

def colors
  if @colors.nil? || @colors.size == 0
    @colors = [:green, :yellow, :magenta, :cyan, :red,
               :light_green, :light_yellow, :ligh_magenta, :light_cyan, :light_red]
  end
  @colors
end

#executeObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/central/cli/stacks/logs_command.rb', line 12

def execute
  require_api_url
  token = require_token

  query_params = {}
  query_params[:nodes] = node_list.join(',') unless node_list.empty?
  query_params[:services] = service_list.join(',') unless service_list.empty?
  query_params[:containers] = container_list.join(',') unless container_list.empty?
  query_params[:limit] = lines if lines
  query_params[:since] = since if since

  if tail?
    @buffer = ''
    query_params[:follow] = 1
    stream_logs(token, query_params)
  else
    list_logs(token, query_params)
  end
end

#list_logs(token, query_params) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/central/cli/stacks/logs_command.rb', line 32

def list_logs(token, query_params)
  result = client(token).get("stacks/#{current_stack}/container_logs", query_params)
  result['logs'].each do |log|
    color = color_for_container(log['name'])
    prefix = ''
    prefix << "#{log['created_at']} "
    prefix << "#{log['name']}:"
    prefix = prefix.colorize(color)
    puts "#{prefix} #{log['data']}"
  end
end

#stream_logs(token, query_params) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/central/cli/stacks/logs_command.rb', line 44

def stream_logs(token, query_params)
  streamer = lambda do |chunk, _remaining_bytes, _total_bytes|
    begin
      chunk = @buffer + chunk unless @buffer.empty?
      log = JSON.parse(chunk) unless chunk.empty?
      @buffer = ''
    rescue => exc
      @buffer << chunk
    end
    if log
      color = color_for_container(log['name'])
      puts "#{log['name'].colorize(color)} | #{log['data']}"
    end
  end

  result = client(token).get_stream(
    "stacks/#{current_stack}/container_logs", streamer, query_params
  )
end