Skip to content
This repository has been archived by the owner on Mar 19, 2022. It is now read-only.

Commit

Permalink
fix: try to rescue Net::SSH::Disconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
Xuanzhong Wei committed Dec 23, 2018
1 parent ce8c223 commit 9f1af2a
Showing 1 changed file with 28 additions and 22 deletions.
50 changes: 28 additions & 22 deletions lib/knife-solo/ssh_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,34 +51,40 @@ def password
def run_command(command, output = nil)
result = ExecResult.new

session.open_channel do |channel|
channel.request_pty
channel.exec(command) do |_, success|
raise "ssh.channel.exec failure" unless success
begin
session.open_channel do |channel|
channel.request_pty
channel.exec(command) do |_, success|
raise "ssh.channel.exec failure" unless success

channel.on_data do |ch, data| # stdout
if data =~ /^knife sudo password: /
ch.send_data("#{password}\n")
else
Chef::Log.debug("#{command} stdout: #{data}")
channel.on_data do |ch, data| # stdout
if data =~ /^knife sudo password: /
ch.send_data("#{password}\n")
else
Chef::Log.debug("#{command} stdout: #{data}")
output << data if output
result.stdout << data
end
end

channel.on_extended_data do |ch, type, data|
next unless type == 1
Chef::Log.debug("#{command} stderr: #{data}")
output << data if output
result.stdout << data
result.stderr << data
end
end

channel.on_extended_data do |ch, type, data|
next unless type == 1
Chef::Log.debug("#{command} stderr: #{data}")
output << data if output
result.stderr << data
end
channel.on_request("exit-status") do |ch, data|
result.exit_code = data.read_long
end

channel.on_request("exit-status") do |ch, data|
result.exit_code = data.read_long
end

end
end.wait
end.wait
rescue Net::SSH::Disconnect
@session = nil
Chef::Log.warn("SSH connection is disconnected. Retry #{command} with new SSH session.")
retry
end

result
end
Expand Down

0 comments on commit 9f1af2a

Please sign in to comment.