Class: AmiSpec::WaitForSSH

Inherits:
Object
  • Object
show all
Defined in:
lib/ami_spec/wait_for_ssh.rb

Class Method Summary collapse

Class Method Details

.wait(ip_address, user, key, max_retries) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ami_spec/wait_for_ssh.rb', line 5

def self.wait(ip_address, user, key, max_retries)
  last_error = nil
  retries = 0

  while retries < max_retries
    begin
      Net::SSH.start(ip_address, user, keys: [key], :verify_host_key => :never) { |ssh| ssh.exec 'echo boo!' }
    rescue Errno::ETIMEDOUT, Errno::ECONNREFUSED, Timeout::Error, Net::SSH::Exception => error
      last_error = error
      sleep 1
    else
      break
    end

    retries = retries + 1
  end

  if retries > max_retries - 1
    raise AmiSpec::InstanceConnectionTimeout.new("Timed out waiting for SSH to become available: #{last_error}")
  end
end