diff options
author | Vivek Singh <vivek.singh@msystechnologies.com> | 2020-09-17 10:04:50 +0530 |
---|---|---|
committer | Tim Smith <tsmith84@gmail.com> | 2021-02-02 11:57:59 -0800 |
commit | 547ee37d530aa1c3b2c2bc0f9122261cd16e65f2 (patch) | |
tree | 11aca1cac052a3552798561737c98f4cacb3af75 | |
parent | 3bacc50081b3144d1487498aa86016bd921adf96 (diff) | |
download | chef-547ee37d530aa1c3b2c2bc0f9122261cd16e65f2.tar.gz |
Passed block and limits resursive calls
Signed-off-by: Vivek Singh <vivek.singh@msystechnologies.com>
-rw-r--r-- | lib/chef/knife/bootstrap.rb | 22 | ||||
-rw-r--r-- | spec/unit/knife/bootstrap_spec.rb | 4 |
2 files changed, 8 insertions, 18 deletions
diff --git a/lib/chef/knife/bootstrap.rb b/lib/chef/knife/bootstrap.rb index 44561d64bd..578511958e 100644 --- a/lib/chef/knife/bootstrap.rb +++ b/lib/chef/knife/bootstrap.rb @@ -606,20 +606,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) @@ -908,7 +908,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 @@ -1074,15 +1073,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 36d6d3efa0..ede8d029c6 100644 --- a/spec/unit/knife/bootstrap_spec.rb +++ b/spec/unit/knife/bootstrap_spec.rb @@ -1740,7 +1740,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/) @@ -1773,7 +1773,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) |