summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLamont Granquist <454857+lamont-granquist@users.noreply.github.com>2021-07-12 10:01:00 -0700
committerGitHub <noreply@github.com>2021-07-12 10:01:00 -0700
commiteca3b2971dc39ed04fa7b584cbfb269753200275 (patch)
treec80c74e12df128deb6c716d7ac1fdc32d2eaab1d
parent661f8421897c7fc272db46b297f5b14dd311fe14 (diff)
parent9d7a164a9d8798896457a23112af1c779bde5dcd (diff)
downloadchef-eca3b2971dc39ed04fa7b584cbfb269753200275.tar.gz
Merge pull request #11431 from gscho/gscho/knife-ssh-pty
-rw-r--r--knife/lib/chef/knife/ssh.rb46
1 files changed, 29 insertions, 17 deletions
diff --git a/knife/lib/chef/knife/ssh.rb b/knife/lib/chef/knife/ssh.rb
index e69de62bc2..b474696d02 100644
--- a/knife/lib/chef/knife/ssh.rb
+++ b/knife/lib/chef/knife/ssh.rb
@@ -134,6 +134,18 @@ class Chef
boolean: true,
default: false
+ option :pty,
+ long: "--[no-]pty",
+ description: "Request a PTY, enabled by default.",
+ boolean: true,
+ default: true
+
+ option :require_pty,
+ long: "--[no-]require-pty",
+ description: "Raise exception if a PTY cannot be acquired, disabled by default.",
+ boolean: true,
+ default: false
+
def session
ssh_error_handler = Proc.new do |server|
if config[:on_error]
@@ -353,26 +365,26 @@ class Chef
ui.msg(str)
end
- def ssh_command(command, subsession = nil)
+ # @param command [String] the command to run
+ # @param session_list [???] list of sessions, one per node
+ #
+ def ssh_command(command, session_list = session)
+ stderr = ""
exit_status = 0
- subsession ||= session
command = fixup_sudo(command)
command.force_encoding("binary") if command.respond_to?(:force_encoding)
- begin
- open_session(subsession, command)
- rescue => e
- open_session(subsession, command, true)
- end
- end
-
- def open_session(subsession, command, pty = false)
- stderr = ""
- exit_status = 0
- subsession.open_channel do |chan|
+ session_list.open_channel do |chan|
if config[:on_error] && exit_status != 0
chan.close
else
- chan.request_pty if pty
+ if config[:pty]
+ chan.request_pty do |ch, success|
+ unless success
+ ui.warn("Failed to obtain a PTY from #{ch.connection.host}")
+ raise ArgumentError, "Request for PTY failed" if config[:require_pty]
+ end
+ end
+ end
chan.exec command do |ch, success|
raise ArgumentError, "Cannot execute #{command}" unless success
@@ -383,13 +395,11 @@ class Chef
ichannel.send_data("#{get_password}\n")
end
end
-
ch.on_extended_data do |_, _type, data|
- raise ArgumentError if data.eql?("sudo: no tty present and no askpass program specified\n")
+ raise ArgumentError, "No PTY present. If a PTY is required use --require-pty" if data.eql?("sudo: no tty present and no askpass program specified\n")
stderr += data
end
-
ch.on_request "exit-status" do |ichannel, data|
exit_status = [exit_status, data.read_long].max
end
@@ -398,6 +408,8 @@ class Chef
end
session.loop
exit_status
+ ensure
+ session_list.close
end
def get_password