lib/chef/knife/ssh.rb in chef-10.12.0 vs lib/chef/knife/ssh.rb in chef-10.14.0.beta.1

- old
+ new

@@ -24,12 +24,14 @@ deps do require 'net/ssh' require 'net/ssh/multi' require 'readline' + require 'chef/exceptions' require 'chef/search/query' require 'chef/mixin/shell_out' + require 'mixlib/shellout' end include Chef::Mixin::ShellOut attr_writer :password @@ -178,10 +180,11 @@ ui.msg(str) end end def ssh_command(command, subsession=nil) + exit_status = 0 subsession ||= session command = fixup_sudo(command) subsession.open_channel do |ch| ch.request_pty ch.exec command do |ch, success| @@ -190,13 +193,17 @@ print_data(ichannel[:host], data) if data =~ /^knife sudo password: / ichannel.send_data("#{get_password}\n") end end + ch.on_request "exit-status" do |ichannel, data| + exit_status = data.read_long + end end end session.loop + exit_status end def get_password @password ||= ui.ask("Enter your password: ") { |q| q.echo = false } end @@ -330,16 +337,27 @@ config[:attribute] = (Chef::Config[:knife][:ssh_attribute] || config[:attribute] || "fqdn").strip end - def csshx - csshx_cmd = "csshX" + def cssh + cssh_cmd = nil + %w[csshX cssh].each do |cmd| + begin + # Unix and Mac only + cssh_cmd = shell_out!("which #{cmd}").stdout.strip + break + rescue Mixlib::ShellOut::ShellCommandFailed + end + end + raise Chef::Exceptions::Exec, "no command found for cssh" unless cssh_cmd + session.servers_for.each do |server| - csshx_cmd << " #{server.user ? "#{server.user}@#{server.host}" : server.host}" + cssh_cmd << " #{server.user ? "#{server.user}@#{server.host}" : server.host}" end - exec(csshx_cmd) + Chef::Log.debug("starting cssh session with command: #{cssh_cmd}") + exec(cssh_cmd) end def get_stripped_unfrozen_value(value) return nil if value.nil? value.strip @@ -363,25 +381,31 @@ configure_attribute configure_user configure_identity_file configure_session + exit_status = case @name_args[1] when "interactive" interactive when "screen" screen when "tmux" tmux when "macterm" macterm + when "cssh" + cssh when "csshx" - csshx + Chef::Log.warn("knife ssh csshx will be deprecated in a future release") + Chef::Log.warn("please use knife ssh cssh instead") + cssh else ssh_command(@name_args[1..-1].join(" ")) end session.close + exit_status end end end end