Module: ChefWorkflow::SSHHelper
- Includes:
- DebugSupport
- Defined in:
- lib/chef-workflow/support/ssh.rb
Overview
Helper for performing SSH on groups of servers. Intended to be mixed into test case classes.
Constant Summary
Constants included from DebugSupport
DebugSupport::CHEF_WORKFLOW_DEBUG_DEFAULT
Instance Method Summary collapse
-
#configure_ssh_command(ip, command) ⇒ Object
takes a block which it uses inside of the open_channel block that Net::SSH uses.
-
#ssh_capture(ip, command) ⇒ Object
run a command, and instead of capturing the exit status, return the data captured during the command run.
-
#ssh_command(ip, command) ⇒ Object
Run a command against a single IP.
-
#ssh_role_command(role, command) ⇒ Object
run a command against a group of servers.
Methods included from DebugSupport
Instance Method Details
#configure_ssh_command(ip, command) ⇒ Object
takes a block which it uses inside of the open_channel block that Net::SSH uses. Intended to provide a consistent way of setting up Net::SSH Makes heavy use of KnifeSupport to determine how to drive the command.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/chef-workflow/support/ssh.rb', line 36 def configure_ssh_command(ip, command) use_sudo = ChefWorkflow::KnifeSupport.use_sudo command = "#{use_sudo ? 'sudo ': ''}#{command}" = { } [:password] = ChefWorkflow::KnifeSupport.ssh_password if ChefWorkflow::KnifeSupport.ssh_password [:keys] = [ChefWorkflow::KnifeSupport.ssh_identity_file] if ChefWorkflow::KnifeSupport.ssh_identity_file Net::SSH.start(ip, ChefWorkflow::KnifeSupport.ssh_user, ) do |ssh| ssh.open_channel do |ch| ch.request_pty do |ch, success| if !success and use_sudo raise "The use_sudo setting requires a PTY, and your SSH is rejecting our attempt to get one." end end ch.on_open_failed do |ch, code, desc| raise "Connection Error to #{ip}: #{desc}" end ch.exec(command) do |ch, success| yield ch, success end end ssh.loop end end |
#ssh_capture(ip, command) ⇒ Object
run a command, and instead of capturing the exit status, return the data captured during the command run.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/chef-workflow/support/ssh.rb', line 90 def ssh_capture(ip, command) retval = "" configure_ssh_command(ip, command) do |ch, success| return "" unless success ch.on_data do |ch, data| retval << data end ch.on_request("exit-status") do |ch, data| return retval end end return retval end |
#ssh_command(ip, command) ⇒ Object
Run a command against a single IP. Returns the exit status.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/chef-workflow/support/ssh.rb', line 70 def ssh_command(ip, command) configure_ssh_command(ip, command) do |ch, success| return 1 unless success if_debug(2) do ch.on_data do |ch, data| $stderr.puts data end end ch.on_request("exit-status") do |ch, data| return data.read_long end end end |
#ssh_role_command(role, command) ⇒ Object
run a command against a group of servers. These commands are run in parallel, but the command itself does not complete until all the threads have finished running.
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/chef-workflow/support/ssh.rb', line 19 def ssh_role_command(role, command) t = [] ChefWorkflow::IPSupport.get_role_ips(role).each do |ip| t.push( Thread.new do ssh_command(ip, command) end ) end t.each(&:join) end |