Method: Beaker::SshConnection#try_to_execute

Defined in:
lib/beaker/ssh_connection.rb

#try_to_execute(command, options = {}, stdout_callback = nil, stderr_callback = stdout_callback) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/beaker/ssh_connection.rb', line 195

def try_to_execute command, options = {}, stdout_callback = nil,
            stderr_callback = stdout_callback

  result = Result.new(@hostname, command)

  @ssh.open_channel do |channel|
    request_terminal_for( channel, command ) if options[:pty]

    channel.exec(command) do |terminal, success|
      raise Net::SSH::Exception.new("FAILED: to execute command on a new channel on #{@hostname}") unless success
      register_stdout_for terminal, result, stdout_callback
      register_stderr_for terminal, result, stderr_callback
      register_exit_code_for terminal, result

      process_stdin_for( terminal, options[:stdin] ) if options[:stdin]
    end
  end

  # Process SSH activity until we stop doing that - which is when our
  # channel is finished with...
  begin
    @ssh.loop
  rescue *RETRYABLE_EXCEPTIONS => e
    # this would indicate that the connection failed post execution, since the channel exec was successful
    @logger.warn "ssh channel on #{@hostname} received exception post command execution #{e.class.name} - #{e.message}"
    close
  end

  result.finalize!
  @logger.last_result = result
  result
end