Method: Beaker::SshConnection#connect_block

Defined in:
lib/beaker/ssh_connection.rb

#connect_block(host, user, ssh_opts, options) ⇒ Net::SSH::Connection::Session

Note:

For more information about Net::SSH library, check out these docs:

Setup and return the ssh connection object

Parameters:

  • hostname of the machine to connect to

  • username to login to the host as

  • Options hash passed directly to Net::SSH.start method

  • Options hash to control method conditionals

Options Hash (options):

  • :max_connection_tries (Integer)

    Limit the number of connection start tries to this number (default: 11)

  • :silent (Boolean)

    Stops logging attempt failure messages if set to true (default: true)

Returns:

  • session returned from Net::SSH.start method



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/beaker/ssh_connection.rb', line 64

def connect_block host, user, ssh_opts, options
  try = 1
  last_wait = 2
  wait = 3
  max_connection_tries = options[:max_connection_tries] || 11
  begin
     @logger.debug "Attempting ssh connection to #{host}, user: #{user}, opts: #{ssh_opts}"

     if ssh_opts.include?(:strict_host_key_checking) && (Net::SSH::Version::CURRENT.major > 5)
       ssh_opts[:paranoid] = ssh_opts.delete(:strict_host_key_checking)
     end

     Net::SSH.start(host, user, ssh_opts)
  rescue *RETRYABLE_EXCEPTIONS => e
    if try <= max_connection_tries
      @logger.warn "Try #{try} -- Host #{host} unreachable: #{e.class.name} - #{e.message}" unless options[:silent]
      @logger.warn "Trying again in #{wait} seconds" unless options[:silent]

      sleep wait
      (last_wait, wait) = wait, last_wait + wait
      try += 1

      retry
    else
      @logger.warn "Failed to connect to #{host}, after #{try} attempts" unless options[:silent]
      nil
    end
  end
end