Skip to content
This repository was archived by the owner on Mar 19, 2022. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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