Sha256: 28b4e78b073e5e907c92b85c5320b5ca33cdc7478754471ab0e22c2636e94b1a

Contents?: true

Size: 1.89 KB

Versions: 9

Compression:

Stored size: 1.89 KB

Contents

require_relative '../helpers/log_helper'

module Kontena::Cli::Grids
  class LogsCommand < Clamp::Command
    include Kontena::Cli::Common
    include Kontena::Cli::Helpers::LogHelper

    option ["-t", "--tail"], :flag, "Tail (follow) logs", default: false
    option "--lines", "LINES", "Number of lines to show from the end of the logs"
    option "--since", "SINCE", "Show logs since given timestamp"
    option "--node", "NODE", "Filter by node name", multivalued: true
    option "--service", "SERVICE", "Filter by service name", multivalued: true
    option ["-c", "--container"], "CONTAINER", "Filter by container", multivalued: true

    # @return [String]
    def token
      @token ||= require_token
    end

    def execute
      require_api_url

      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?
        tail_logs(query_params)
      else
        list_logs(query_params)
      end
    end

    def list_logs(query_params)
      result = client(token).get("grids/#{current_grid}/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

    # @param [String] token
    # @param [Hash] query_params
    def tail_logs(query_params)
      stream_logs("grids/#{current_grid}/container_logs", query_params) do |log|
        color = color_for_container(log['name'])
        puts "#{log['name'].colorize(color)} | #{log['data']}"
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
kontena-cli-0.15.5 lib/kontena/cli/grids/logs_command.rb
kontena-cli-0.15.5.rc2 lib/kontena/cli/grids/logs_command.rb
kontena-cli-0.15.5.rc1 lib/kontena/cli/grids/logs_command.rb
kontena-cli-0.15.4 lib/kontena/cli/grids/logs_command.rb
kontena-cli-0.15.4.rc2 lib/kontena/cli/grids/logs_command.rb
kontena-cli-0.15.4.rc1 lib/kontena/cli/grids/logs_command.rb
kontena-cli-0.15.4.pre1 lib/kontena/cli/grids/logs_command.rb
kontena-cli-0.15.3 lib/kontena/cli/grids/logs_command.rb
kontena-cli-0.15.2 lib/kontena/cli/grids/logs_command.rb