Improve the SSH ready? check [GH-841]

This commit is contained in:
Mitchell Hashimoto 2012-03-28 22:30:01 -07:00
parent c9d1110b0e
commit 2ed6695f78
4 changed files with 24 additions and 10 deletions

View File

@ -1,6 +1,6 @@
## 1.1.0 (unreleased)
- Improve the SSH "ready?" check. [GH-841]
## 1.0.2 (March 25, 2012)

View File

@ -25,16 +25,15 @@ module Vagrant
def ready?
@logger.debug("Checking whether SSH is ready...")
Timeout.timeout(@vm.config.ssh.timeout) do
connect
end
# Attempt to connect. This will raise an exception if it fails.
connect
# If we reached this point then we successfully connected
@logger.info("SSH is ready!")
true
rescue Timeout::Error, Errors::SSHConnectionRefused, Net::SSH::Disconnect => e
# The above errors represent various reasons that SSH may not be
# ready yet. Return false.
rescue Errors::VagrantError => e
# We catch a `VagrantError` which would signal that something went
# wrong expectedly in the `connect`, which means we didn't connect.
@logger.info("SSH not up: #{e.inspect}")
return false
end
@ -131,11 +130,16 @@ module Vagrant
# Connect to SSH, giving it a few tries
connection = nil
begin
@logger.info("Connecting to SSH: #{ssh_info[:host]}:#{ssh_info[:port]}")
exceptions = [Errno::ECONNREFUSED, Net::SSH::Disconnect]
exceptions = [Errno::ECONNREFUSED, Net::SSH::Disconnect, Timeout::Error]
connection = retryable(:tries => @vm.config.ssh.max_tries, :on => exceptions) do
Net::SSH.start(ssh_info[:host], ssh_info[:username], opts)
Timeout.timeout(@vm.config.ssh.timeout) do
@logger.info("Attempting to connect to SSH: #{ssh_info[:host]}:#{ssh_info[:port]}")
Net::SSH.start(ssh_info[:host], ssh_info[:username], opts)
end
end
rescue Timeout::Error
# This happens if we continued to timeout when attempting to connect.
raise Errors::SSHConnectionTimeout
rescue Net::SSH::AuthenticationFailed
# This happens if authentication failed. We wrap the error in our
# own exception.

View File

@ -313,6 +313,11 @@ module Vagrant
error_key(:ssh_connection_refused)
end
class SSHConnectionTimeout < VagrantError
status_code(78)
error_key(:ssh_connection_timeout)
end
class SSHKeyBadPermissions < VagrantError
status_code(12)
error_key(:ssh_key_bad_permissions)

View File

@ -117,6 +117,11 @@ en:
If that doesn't work, destroy your VM and recreate it with a `vagrant destroy`
followed by a `vagrant up`. If that doesn't work, contact a Vagrant
maintainer (support channels listed on the website) for more assistance.
ssh_connection_timeout: |-
Vagrant timed out while attempting to connect via SSH. This usually
means that the VM booted, but there are issues with the SSH configuration
or network connectivity issues. Please try to `vagrant reload` or
`vagrant up` again.
ssh_key_bad_permissions: |-
The private key to connect to this box via SSH has invalid permissions
set on it. The permissions of the private key should be set to 0600, otherwise SSH will