Method: Beaker::SshConnection#wait_for_connection_failure

Defined in:
lib/beaker/ssh_connection.rb

#wait_for_connection_failure(options = {}, stdout_callback = nil, stderr_callback = stdout_callback) ⇒ Boolean

Wait for the ssh connection to fail, returns true on connection failure and false otherwise

Parameters:

  • options (Hash{Symbol=>String}) (defaults to: {})

    Options hash to control method conditionals

  • stdout_callback (IO) (defaults to: nil)

    An IO stream to send connection stdout to, defaults to nil

  • stderr_callback (IO) (defaults to: stdout_callback)

    An IO stream to send connection stderr to, defaults to nil

Options Hash (options):

  • :pty (Boolean)

    Should we request a terminal when attempting to send a command over this connection?

  • :stdin (String)

    Any input to be sent along with the command

Returns:

  • (Boolean)

    true if connection failed, false otherwise



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/beaker/ssh_connection.rb', line 151

def wait_for_connection_failure options = {}, stdout_callback = nil, stderr_callback = stdout_callback
  try = 1
  last_wait = 2
  wait = 3
  command = 'echo echo' #can be run on all platforms (I'm looking at you, windows)
  while try < 11
    result = Result.new(@hostname, command)
    begin
      @logger.notify "Waiting for connection failure on #{@hostname} (attempt #{try}, try again in #{wait} second(s))"
      @logger.debug("\n#{@hostname} #{Time.new.strftime('%H:%M:%S')}$ #{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
       loop_tries = 0
       #loop is actually loop_forever, so let it try 3 times and then quit instead of endless blocking
       @ssh.loop { loop_tries += 1 ; loop_tries < 4 }
    rescue *RETRYABLE_EXCEPTIONS => e
      @logger.debug "Connection on #{@hostname} failed as expected (#{e.class.name} - #{e.message})"
      close #this connection is bad, shut it down
      return true
    end
    slept = 0
    stdout_callback.call("sleep #{wait} second(s): ")
    while slept < wait
      sleep slept
      stdout_callback.call('.')
      slept += 1
    end
    stdout_callback.call("\n")
    (last_wait, wait) = wait, last_wait + wait
    try += 1
  end
  false
end