diff options
-rw-r--r-- | lib/chef/knife/ssh.rb | 24 |
1 files changed, 13 insertions, 11 deletions
diff --git a/lib/chef/knife/ssh.rb b/lib/chef/knife/ssh.rb index b40f845dbd..f7df0f788f 100644 --- a/lib/chef/knife/ssh.rb +++ b/lib/chef/knife/ssh.rb @@ -134,12 +134,6 @@ class Chef boolean: true, default: false - option :ssh_pty, - long: "--ssh-pty", - description: "PTY flag. Provide this option if PTY is configured on node.", - boolean: true, - default: false - def session ssh_error_handler = Proc.new do |server| if config[:on_error] @@ -364,15 +358,17 @@ class Chef subsession ||= session command = fixup_sudo(command) command.force_encoding("binary") if command.respond_to?(:force_encoding) + open_session(subsession, command) + end + + def open_session(subsession, command, pty = false) + stderr = "" + exit_status = 0 subsession.open_channel do |chan| if config[:on_error] && exit_status != 0 chan.close else - if config[:ssh_pty] - chan.request_pty do |ch, success| - raise Train::Transports::SSHPTYFailed, "Requesting PTY failed" unless success - end - end + chan.request_pty if pty chan.exec command do |ch, success| raise ArgumentError, "Cannot execute #{command}" unless success @@ -383,8 +379,14 @@ class Chef ichannel.send_data("#{get_password}\n") end end + + ch.on_extended_data do |_, _type, data| + stderr += data + end + ch.on_request "exit-status" do |ichannel, data| exit_status = [exit_status, data.read_long].max + exit_status = open_session(subsession, command, true) if exit_status != 0 && stderr.include?("sudo: sorry, you must have a tty to run sudo") end end end |