summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVivek Singh <vivek.singh@msystechnologies.com>2020-09-17 10:04:50 +0530
committerVivek Singh <vivek.singh@msystechnologies.com>2020-09-17 10:04:50 +0530
commitd5c2f93c654f2c2f070c336290cde472b9bc4ba3 (patch)
tree3b1084ac2143a649e0ef82d98eb8d7ff778f698b
parentf8abc8de96da8f98a7c37b6ab236b62c3a9e67b2 (diff)
downloadchef-d5c2f93c654f2c2f070c336290cde472b9bc4ba3.tar.gz
Passed block and limits resursive calls
Signed-off-by: Vivek Singh <vivek.singh@msystechnologies.com>
-rw-r--r--lib/chef/knife/bootstrap.rb22
-rw-r--r--spec/unit/knife/bootstrap_spec.rb4
2 files changed, 8 insertions, 18 deletions
diff --git a/lib/chef/knife/bootstrap.rb b/lib/chef/knife/bootstrap.rb
index de449d656b..e996b36c84 100644
--- a/lib/chef/knife/bootstrap.rb
+++ b/lib/chef/knife/bootstrap.rb
@@ -605,20 +605,20 @@ class Chef
# Actual bootstrap command to be run on the node.
# Handles recursive calls if su USER failed to authenticate.
- def bootstrap_run_command(cmd)
- r = connection.run_command(cmd) do |data|
+ def bootstrap_run_command(cmd, limit = 3)
+ r = connection.run_command(cmd) do |data, ch|
ui.msg("#{ui.color(" [#{connection.hostname}]", :cyan)} #{data}")
+ ch.send_data("#{config[:su_password] || config[:connection_password]}\n") if data == "Password: "
end
if r.exit_status != 0
stderr = (r.stderr + r.stdout).strip
if stderr.match?("su: Authentication failure")
+ limit -= 1
ui.warn("Failed to authenticate su - #{config[:su_user]} to #{server_name}")
password = ui.ask("Enter password for su - #{config[:su_user]}@#{server_name}:", echo: false)
-
- set_transport_options(su_password: password)
-
- bootstrap_run_command(cmd)
+ config[:su_password] = password
+ bootstrap_run_command(cmd, limit) if limit > 0
else
ui.error("The following error occurred on #{server_name}:")
ui.error(stderr)
@@ -907,7 +907,6 @@ class Chef
@connection_opts.merge! winrm_opts
@connection_opts.merge! ssh_opts
@connection_opts.merge! ssh_identity_opts
- @connection_opts.merge! su_user_opts
@connection_opts
end
@@ -1073,15 +1072,6 @@ class Chef
}
end
- def su_user_opts
- opts = {}
- return opts if winrm? || !config.key?(:su_user)
-
- opts[:su_user] = config[:su_user]
- opts[:su_password] = config[:su_password] || config[:connection_password]
- opts
- end
-
# This is for deprecating config options. The fallback_key can be used
# to pull an old knife config option out of the config file when the
# cli value has been renamed. This is different from the deprecated
diff --git a/spec/unit/knife/bootstrap_spec.rb b/spec/unit/knife/bootstrap_spec.rb
index 85f506d924..0cbe3720cf 100644
--- a/spec/unit/knife/bootstrap_spec.rb
+++ b/spec/unit/knife/bootstrap_spec.rb
@@ -1752,7 +1752,7 @@ describe Chef::Knife::Bootstrap do
expect(connection)
.to receive(:run_command)
.with("sh /path.sh")
- .and_yield("output here")
+ .and_yield("output here", nil)
.and_return result_mock
expect(knife.ui).to receive(:msg).with(/testhost/)
@@ -1785,7 +1785,7 @@ describe Chef::Knife::Bootstrap do
expect(connection)
.to receive(:run_command)
.with("su - USER -c 'sh /path.sh'")
- .and_yield("output here")
+ .and_yield("output here", nil)
.and_return result_mock
expect(knife.ui).to receive(:ask).and_return("password").twice
expect(connection).to receive(:run_command).with("su - USER -c 'sh /path.sh'").and_return(result_mock, result_mock2)