lib/ztk/ssh.rb in ztk-0.2.1 vs lib/ztk/ssh.rb in ztk-0.2.2
- old
+ new
@@ -173,10 +173,18 @@
# config.user = ENV["USER"]
# config.host_name = "127.0.0.1"
# end
# puts ssh.exec("hostname -f").inspect
def exec(command, options={})
+
+ def log_header(tag)
+ count = 8
+ sep = ("=" * count)
+ header = [sep, "[ #{tag} ]", sep, "[ #{self.inspect} ]", sep, "[ #{tag} ]", sep].join
+ "#{header}\n"
+ end
+
log(:debug) { "config(#{@config.inspect})" }
log(:info) { "exec(#{command.inspect}, #{options.inspect})" }
options = OpenStruct.new({ :silence => false }.merge(options))
log(:debug) { "options(#{options.inspect})" }
@@ -188,18 +196,18 @@
ZTK::RescueRetry.try(:tries => 3, :on => EOFError) do
@ssh = Net::SSH.start(@config.host_name, @config.user, ssh_options)
channel = ssh.open_channel do |chan|
log(:debug) { "Channel opened." }
- direct_log(:debug) { "===[OPENED]===[OPENED]===[#{self.inspect}]===[OPENED]===[OPENED]===\n" }
+ direct_log(:debug) { log_header("OPENED") }
chan.exec(command) do |ch, success|
raise SSHError, "Could not execute '#{command}'." unless success
ch.on_data do |c, data|
if !stdout_header
- direct_log(:debug) { "===[STDOUT]===[STDOUT]===[#{self.inspect}]===[STDOUT]===[STDOUT]===\n" }
+ direct_log(:debug) { log_header("STDOUT") }
stdout_header = true
stderr_header = false
end
direct_log(:debug) { data }
@@ -207,15 +215,15 @@
output += data
end
ch.on_extended_data do |c, type, data|
if !stderr_header
- direct_log(:debug) { "===[STDERR]===[STDERR]===[#{self.inspect}]===[STDERR]===[STDERR]===\n" }
+ direct_log(:warn) { log_header("STDERR") }
stderr_header = true
stdout_header = false
end
- direct_log(:debug) { data }
+ direct_log(:warn) { data }
@config.stderr.print(data) unless options.silence
output += data
end
@@ -225,10 +233,10 @@
end
end
channel.wait
- direct_log(:debug) { "===[CLOSED]===[CLOSED]===[#{self.inspect}]===[CLOSED]===[CLOSED]===\n" }
+ direct_log(:debug) { log_header("CLOSED") }
log(:debug) { "Channel closed." }
end
OpenStruct.new(:output => output, :exit => $?)
end