Class: Central::Cli::Services::LogsCommand

Inherits:
Clamp::Command
  • Object
show all
Includes:
Common, ServicesHelper, Central::Cli::StackOptions
Defined in:
lib/central/cli/services/logs_command.rb

Instance Method Summary collapse

Methods included from ServicesHelper

#create_service, #delete_service, #deploy_service, #get_service, #int_to_filesize, #parse_image, #parse_links, #parse_log_opts, #parse_memory, #parse_ports, #parse_relative_time, #parse_secrets, #parse_service_id, #restart_service, #scale_service, #show_service, #start_service, #stop_service, #update_service, #wait_for_deploy_to_finish

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

Methods included from Central::Cli::StackOptions

included

Instance Method Details

#color_for_container(container_id) ⇒ Object



40
41
42
43
# File 'lib/central/cli/services/logs_command.rb', line 40

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



45
46
47
# File 'lib/central/cli/services/logs_command.rb', line 45

def color_maps
  @color_maps ||= {}
end

#colorsObject



49
50
51
52
53
54
55
# File 'lib/central/cli/services/logs_command.rb', line 49

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



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/central/cli/services/logs_command.rb', line 15

def execute
  require_api_url
  token = require_token
  last_id = nil
  loop do
    query_params = []
    query_params << "limit=#{lines}"
    query_params << "from=#{last_id}" unless last_id.nil?
    query_params << "since=#{since}" if !since.nil? && last_id.nil?
    query_params << "container=#{name}-#{instance}" if instance

    result = client(token).get("services/#{current_stack}/#{name}/container_logs?#{query_params.join('&')}")
    result['logs'].each do |log|
      color = color_for_container(log['name'])
      instance_number = log['name'].match(/^.+-(\d+)$/)[1]
      name = instance_number.nil? ? log['name'] : instance_number
      prefix = "#{log['created_at']} [#{name}]:".colorize(color)
      puts "#{prefix} #{log['data']}"
      last_id = log['id']
    end
    break unless tail?
    sleep(2)
  end
end