From c77cace5225eb16d35865664754e88f4d67bba7f Mon Sep 17 00:00:00 2001 From: elijah Date: Thu, 23 Jun 2016 15:49:03 -0700 Subject: migrate commands to use new ssh system: node init, test, add-user --- lib/leap_cli/ssh/formatter.rb | 75 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 lib/leap_cli/ssh/formatter.rb (limited to 'lib/leap_cli/ssh/formatter.rb') diff --git a/lib/leap_cli/ssh/formatter.rb b/lib/leap_cli/ssh/formatter.rb new file mode 100644 index 00000000..84a8e797 --- /dev/null +++ b/lib/leap_cli/ssh/formatter.rb @@ -0,0 +1,75 @@ +# +# A custom SSHKit formatter that uses LeapLogger. +# + +require 'sshkit' + +module LeapCli + module SSH + + class Formatter < SSHKit::Formatter::Abstract + + DEFAULT_OPTIONS = { + :log_cmd => false, # log what the command is that gets run. + :log_output => true, # log each output from the command as it is received. + :log_finish => false # log the exit status and time to completion. + } + + def initialize(logger, host, options={}) + @logger = logger + @host = host + @options = DEFAULT_OPTIONS.merge(options) + end + + def write(obj) + @logger.log(obj.to_s, :host => @host) + end + + def log_command_start(command) + if @options[:log_cmd] + @logger.log(:running, "`" + command.to_s + "`", :host => @host) + end + end + + def log_command_data(command, stream_type, stream_data) + if @options[:log_output] + color = \ + case stream_type + when :stdout then :cyan + when :stderr then :red + else raise "Unrecognised stream_type #{stream_type}, expected :stdout or :stderr" + end + @logger.log(stream_data.to_s.chomp, + :color => color, :host => @host, :wrap => options[:log_wrap]) + end + end + + def log_command_exit(command) + if @options[:log_finish] + runtime = sprintf('%5.3fs', command.runtime) + if command.failure? + message = "in #{runtime} with status #{command.exit_status}." + @logger.log(:failed, message, :host => @host) + else + message = "in #{runtime}." + @logger.log(:completed, message, :host => @host) + end + end + end + end + + end +end + + # + # A custom InteractionHandler that will output the results as they come in. + # + #class LoggingInteractionHandler + # def initialize(hostname, logger=nil) + # @hostname = hostname + # @logger = logger || LeapCli.new_logger + # end + # def on_data(command, stream_name, data, channel) + # @logger.log(data, host: @hostname, wrap: true) + # end + #end -- cgit v1.2.3 From 67c0610ee049a388d8ffd0a0e3912ee0d5d9a957 Mon Sep 17 00:00:00 2001 From: elijah Date: Wed, 29 Jun 2016 13:49:53 -0700 Subject: ssh logging: set the correct host --- lib/leap_cli/ssh/formatter.rb | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) (limited to 'lib/leap_cli/ssh/formatter.rb') diff --git a/lib/leap_cli/ssh/formatter.rb b/lib/leap_cli/ssh/formatter.rb index 84a8e797..bab43011 100644 --- a/lib/leap_cli/ssh/formatter.rb +++ b/lib/leap_cli/ssh/formatter.rb @@ -22,25 +22,20 @@ module LeapCli end def write(obj) - @logger.log(obj.to_s, :host => @host) + @logger.log(obj.to_s, :host => @host.hostname) end def log_command_start(command) if @options[:log_cmd] - @logger.log(:running, "`" + command.to_s + "`", :host => @host) + @logger.log(:running, "`" + command.to_s + "`", :host => @host.hostname) end end def log_command_data(command, stream_type, stream_data) if @options[:log_output] - color = \ - case stream_type - when :stdout then :cyan - when :stderr then :red - else raise "Unrecognised stream_type #{stream_type}, expected :stdout or :stderr" - end + color = stream_type == :stderr ? :red : nil @logger.log(stream_data.to_s.chomp, - :color => color, :host => @host, :wrap => options[:log_wrap]) + :color => color, :host => @host.hostname, :wrap => options[:log_wrap]) end end @@ -49,10 +44,10 @@ module LeapCli runtime = sprintf('%5.3fs', command.runtime) if command.failure? message = "in #{runtime} with status #{command.exit_status}." - @logger.log(:failed, message, :host => @host) + @logger.log(:failed, message, :host => @host.hostname) else message = "in #{runtime}." - @logger.log(:completed, message, :host => @host) + @logger.log(:completed, message, :host => @host.hostname) end end end -- cgit v1.2.3 From 8116e007cfd4dbee8282247348cf45473dcde45e Mon Sep 17 00:00:00 2001 From: elijah Date: Wed, 31 Aug 2016 14:54:46 -0700 Subject: added support for Let's Encrypt --- lib/leap_cli/ssh/formatter.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/leap_cli/ssh/formatter.rb') diff --git a/lib/leap_cli/ssh/formatter.rb b/lib/leap_cli/ssh/formatter.rb index bab43011..c2e386dc 100644 --- a/lib/leap_cli/ssh/formatter.rb +++ b/lib/leap_cli/ssh/formatter.rb @@ -16,7 +16,7 @@ module LeapCli } def initialize(logger, host, options={}) - @logger = logger + @logger = logger || LeapCli.new_logger @host = host @options = DEFAULT_OPTIONS.merge(options) end -- cgit v1.2.3