Sha256: ee9d18e0c69842d1cfed04bed4cf91329138ca0d66018f158cfde9db2622b2f0

Contents?: true

Size: 1.89 KB

Versions: 6

Compression:

Stored size: 1.89 KB

Contents

require_relative '../helpers/log_helper'

module Kontena::Cli::Grids
  class LogsCommand < Kontena::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

6 entries across 6 versions & 1 rubygems

Version Path
kontena-cli-0.16.0.pre6 lib/kontena/cli/grids/logs_command.rb
kontena-cli-0.16.0.pre5 lib/kontena/cli/grids/logs_command.rb
kontena-cli-0.16.0.pre4 lib/kontena/cli/grids/logs_command.rb
kontena-cli-0.16.0.pre3 lib/kontena/cli/grids/logs_command.rb
kontena-cli-0.16.0.pre2 lib/kontena/cli/grids/logs_command.rb
kontena-cli-0.16.0.pre1 lib/kontena/cli/grids/logs_command.rb