Method: CloudFlock::App::Common#connect_host

Defined in:
lib/cloudflock/app/common/servers.rb

#connect_host(host, define_method) ⇒ Object

Public: Attempt to log in to a target host.

host - Hash containing any applicable options mappings for the

server in question.

define_method - Name of the method to call when re-defining host to

recover from an exception.

Returns a CloudFlock::Remote::SSH object logged in to the target host.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/cloudflock/app/common/servers.rb', line 124

def connect_host(host, define_method)
  UI.spinner("Logging in to #{host[:hostname]}") do
    SSH.new(host)
  end
rescue CloudFlock::Remote::SSH::InvalidHostname => e
  error = "Cannot look up #{host[:hostname]}"
  retry_exit(e.message, 'Try another host? (Y/N)')

  host = self.send(define_method, (host.merge({hostname: nil})))
  retry
rescue CloudFlock::Remote::SSH::SSHCannotConnect => e
  retry if retry_prompt(e.message)
  retry_exit('', 'Try another host? (Y/N)')

  host = self.send(define_method, (host.merge({hostname: nil})))
  retry
rescue Net::SSH::AuthenticationFailed => e
  retry_exit("Cannot log in as #{host[:username]}.")

  options = {username: nil, password: nil}
  host = self.send(define_method, (host.merge(options)))
  retry
rescue Errno::ECONNREFUSED
  retry_exit("Connection refused from #{host[:hostname]}")
  retry
end