Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move debug output to STDERR #1543

Merged
merged 1 commit into from Jan 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli/lib/kontena/callback.rb
Expand Up @@ -44,7 +44,7 @@ def self.run_callbacks(cmd_type, state, obj)
if klass.instance_methods.include?(state)
cb = klass.new(obj)
if cb.send(state).kind_of?(FalseClass)
ENV["DEBUG"] && puts("Execution aborted by #{klass}")
ENV["DEBUG"] && STDERR.puts("Execution aborted by #{klass}")
exit 1
end
end
Expand Down
Expand Up @@ -10,7 +10,7 @@ def after
return unless command.exit_code == 0
return unless config.current_master

ENV["DEBUG"] && puts("Removing current master from config")
ENV["DEBUG"] && STDERR.puts("Removing current master from config")
config.servers.delete_at(config.find_server_index(config.current_master.name))
config.current_server = nil
config.write
Expand Down
Expand Up @@ -8,8 +8,8 @@ class AuthenticateAfterDeploy < Kontena::Callback
matches_commands 'master create'

def after
ENV["DEBUG"] && puts("Command result: #{command.result.inspect}")
ENV["DEBUG"] && puts("Command exit code: #{command.exit_code.inspect}")
ENV["DEBUG"] && STDERR.puts("Command result: #{command.result.inspect}")
ENV["DEBUG"] && STDERR.puts("Command exit code: #{command.exit_code.inspect}")
return unless command.exit_code == 0
return unless command.result.kind_of?(Hash)
return unless command.result.has_key?(:public_ip)
Expand All @@ -35,11 +35,11 @@ def after

# Figure out if HTTPS works, if not, try HTTP
begin
ENV["DEBUG"] && puts("Trying to request / from #{new_master.url}")
ENV["DEBUG"] && STDERR.puts("Trying to request / from #{new_master.url}")
client = Kontena::Client.new(new_master.url, nil, ignore_ssl_errors: true)
client.get('/')
rescue
ENV["DEBUG"] && puts("HTTPS test failed: #{$!} #{$!.message}")
ENV["DEBUG"] && STDERR.puts("HTTPS test failed: #{$!} #{$!.message}")
unless retried
new_master.url = "http://#{command.result[:public_ip]}"
retried = true
Expand All @@ -51,7 +51,7 @@ def after
require 'shellwords'
cmd = "master login --no-login-info --skip-grid-auto-select --verbose --name #{command.result[:name].shellescape} --code #{command.result[:code].shellescape} #{new_master.url.shellescape}"
Retriable.retriable do
ENV["DEBUG"] && puts("Running: #{cmd}")
ENV["DEBUG"] && STDERR.puts("Running: #{cmd}")
Kontena.run(cmd)
end
end
Expand Down
Expand Up @@ -12,7 +12,7 @@ def after
return unless config.current_master.name == command.result[:name]

cmd = "grid create --silent test"
ENV["DEBUG"] && puts("Running: #{cmd}")
ENV["DEBUG"] && STDERR.puts("Running: #{cmd}")
Retriable.retriable do
Kontena.run(cmd)
end
Expand Down
Expand Up @@ -40,7 +40,7 @@ def after
end

return nil unless invite_response
ENV["DEBUG"] && puts("Got invite code: #{invite_response['invite_code']}")
ENV["DEBUG"] && STDERR.puts("Got invite code: #{invite_response['invite_code']}")

role_status = nil

Expand Down
6 changes: 3 additions & 3 deletions cli/lib/kontena/cli/common.rb
Expand Up @@ -12,7 +12,7 @@ module Common

def logger
return @logger if @logger
@logger = Logger.new(STDOUT)
@logger = Logger.new(ENV["DEBUG"] ? STDERR : STDOUT)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the Logger just always use STDERR?

@logger.level = ENV["DEBUG"].nil? ? Logger::INFO : Logger::DEBUG
@logger.progname = 'COMMON'
@logger
Expand Down Expand Up @@ -129,10 +129,10 @@ def use_refresh_token(server)
return unless server.token.refresh_token
return if server.token.expired?
client = Kontena::Client.new(server.url, server.token)
ENV["DEBUG"] && puts("Trying to invalidate refresh token on #{server.name}")
logger.debug "Trying to invalidate refresh token on #{server.name}"
client.refresh_token
rescue
ENV["DEBUG"] && puts("Refreshing failed: #{$!} : #{$!.message}")
logger.debug "Refreshing failed: #{$!} : #{$!.message}"
end

def require_current_master
Expand Down
2 changes: 1 addition & 1 deletion cli/lib/kontena/cli/config.rb
Expand Up @@ -26,7 +26,7 @@ class TokenExpiredError < StandardError; end

def initialize
super
@logger = Logger.new(STDOUT)
@logger = Logger.new(ENV["DEBUG"] ? STDERR : STDOUT)
@logger.level = ENV["DEBUG"].nil? ? Logger::INFO : Logger::DEBUG
@logger.progname = 'CONFIG'
load_settings_from_env || load_settings_from_config_file
Expand Down
6 changes: 3 additions & 3 deletions cli/lib/kontena/cli/localhost_web_server.rb
Expand Up @@ -39,7 +39,7 @@ def url
#
# @return [Hash] query_params
def serve_one
ENV["DEBUG"] && puts("Waiting for connection on port #{port}..")
ENV["DEBUG"] && STDERR.puts("Waiting for connection on port #{port}..")
socket = server.accept

content = socket.recvfrom(2048).first.split(/(?:\r)?\n/)
Expand All @@ -56,7 +56,7 @@ def serve_one

body = content.join("\n")

ENV["DEBUG"] && puts("Got request: \"#{request.inspect}\n Headers: #{headers.inspect}\n Body: #{body}\"")
ENV["DEBUG"] && STDERR.puts("Got request: \"#{request.inspect}\n Headers: #{headers.inspect}\n Body: #{body}\"")

get_request = request[/GET (\/cb.+?) HTTP/, 1]
if get_request
Expand All @@ -81,7 +81,7 @@ def serve_one
socket.close
server.close
uri = URI.parse("http://localhost#{get_request}")
ENV["DEBUG"] && puts(" * Parsing params: \"#{uri.query}\"")
ENV["DEBUG"] && STDERR.puts(" * Parsing params: \"#{uri.query}\"")
params = {}
URI.decode_www_form(uri.query).each do |key, value|
if value.to_s == ''
Expand Down
2 changes: 1 addition & 1 deletion cli/lib/kontena/cli/master/users/invite_command.rb
Expand Up @@ -43,7 +43,7 @@ def execute
end
rescue
STDERR.puts "Failed to invite #{email}".colorize(:red)
ENV["DEBUG"] && puts("#{$!} - #{$!.message} -- #{$!.backtrace}")
ENV["DEBUG"] && STDERR.puts("#{$!} - #{$!.message} -- #{$!.backtrace}")
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions cli/lib/kontena/cli/spinner.rb
Expand Up @@ -143,13 +143,13 @@ def self.spin(msg, &block)
spin_thread.kill
Kernel.puts "\r [" + "fail".colorize(:red) + "] #{msg} "
if ENV["DEBUG"]
puts "Spin aborted through fail!"
STDERR.puts "Spin aborted through fail!"
end
rescue Exception => ex
spin_thread.kill
Kernel.puts "\r [" + "fail".colorize(:red) + "] #{msg} "
if ENV["DEBUG"]
puts "#{ex} #{ex.message}\n#{ex.backtrace.join("\n")}"
STDERR.puts "#{ex} #{ex.message}\n#{ex.backtrace.join("\n")}"
end
raise ex
ensure
Expand Down
2 changes: 1 addition & 1 deletion cli/lib/kontena/cli/stacks/yaml/opto/vault_setter.rb
Expand Up @@ -3,7 +3,7 @@ module YAML
class Opto::Setters::Vault < Opto::Setter
def set(value)
require 'shellwords'
ENV["DEBUG"] && puts("Setting to vault: #{hint}")
ENV["DEBUG"] && STDERR.puts("Setting to vault: #{hint}")
Kontena.run("vault write --silent #{hint} #{value.to_s.shellescape}")
end
end
Expand Down
4 changes: 2 additions & 2 deletions cli/lib/kontena/client.rb
Expand Up @@ -46,7 +46,7 @@ def initialize(api_url, token = nil, options = {})
uri = URI.parse(@api_url)
@host = uri.host

@logger = Logger.new(STDOUT)
@logger = Logger.new(ENV["DEBUG"] ? STDERR : STDOUT)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least here this can just be STDERR, since there are only logger.debug ... calls in the class?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the logging should be unified anyways at some point.

@logger.level = ENV["DEBUG"].nil? ? Logger::INFO : Logger::DEBUG
@logger.progname = 'CLIENT'

Expand Down Expand Up @@ -135,7 +135,7 @@ def authentication_ok?(token_verify_path)
request(path: final_path)
true
rescue
ENV["DEBUG"] && puts("Authentication verification exception: #{$!} #{$!.message} #{$!.backtrace}")
logger.debug "Authentication verification exception: #{$!} #{$!.message} #{$!.backtrace}"
false
end

Expand Down
2 changes: 1 addition & 1 deletion cli/lib/kontena/command.rb
Expand Up @@ -169,7 +169,7 @@ def help_requested?
end

def run(arguments)
ENV["DEBUG"] && puts("Running #{self} -- callback matcher = '#{self.class.callback_matcher.nil? ? "nil" : self.class.callback_matcher.map(&:to_s).join(' ')}'")
ENV["DEBUG"] && STDERR.puts("Running #{self} -- callback matcher = '#{self.class.callback_matcher.nil? ? "nil" : self.class.callback_matcher.map(&:to_s).join(' ')}'")
@arguments = arguments

run_callbacks :before_parse unless help_requested?
Expand Down
2 changes: 1 addition & 1 deletion cli/lib/kontena/stacks_cache.rb
Expand Up @@ -68,7 +68,7 @@ def pull(stack, version = nil)
end

def dputs(msg)
ENV["DEBUG"] && puts(msg)
ENV["DEBUG"] && STDERR.puts(msg)
end

def cache(stack, version = nil)
Expand Down
8 changes: 4 additions & 4 deletions cli/lib/kontena_cli.rb
Expand Up @@ -7,16 +7,16 @@ module Kontena
# @param [String] command_line
# @return [Fixnum] exit_code
def self.run(cmdline = "", returning: :status)
ENV["DEBUG"] && puts("Running Kontena.run(#{cmdline.inspect}, returning: #{returning}")
ENV["DEBUG"] && STDERR.puts("Running Kontena.run(#{cmdline.inspect}, returning: #{returning}")
result = Kontena::MainCommand.new(File.basename(__FILE__)).run(cmdline.shellsplit)
ENV["DEBUG"] && puts("Command completed, result: #{result.inspect} status: 0")
ENV["DEBUG"] && STDERR.puts("Command completed, result: #{result.inspect} status: 0")
return 0 if returning == :status
return result if returning == :result
rescue SystemExit
ENV["DEBUG"] && puts("Command completed with failure, result: #{result.inspect} status: #{$!.status}")
ENV["DEBUG"] && STDERR.puts("Command completed with failure, result: #{result.inspect} status: #{$!.status}")
returning == :status ? $!.status : nil
rescue
ENV["DEBUG"] && puts("Command raised #{$!} with message: #{$!.message}\n #{$!.backtrace.join(" \n")}")
ENV["DEBUG"] && STDERR.puts("Command raised #{$!} with message: #{$!.message}\n #{$!.backtrace.join(" \n")}")
returning == :status ? 1 : nil
end

Expand Down