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) ## 1.1.0 (unreleased)
- Improve the SSH "ready?" check. [GH-841]
## 1.0.2 (March 25, 2012) ## 1.0.2 (March 25, 2012)

View File

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

View File

@ -313,6 +313,11 @@ module Vagrant
error_key(:ssh_connection_refused) error_key(:ssh_connection_refused)
end end
class SSHConnectionTimeout < VagrantError
status_code(78)
error_key(:ssh_connection_timeout)
end
class SSHKeyBadPermissions < VagrantError class SSHKeyBadPermissions < VagrantError
status_code(12) status_code(12)
error_key(:ssh_key_bad_permissions) 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` 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 followed by a `vagrant up`. If that doesn't work, contact a Vagrant
maintainer (support channels listed on the website) for more assistance. 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: |- ssh_key_bad_permissions: |-
The private key to connect to this box via SSH has invalid 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 set on it. The permissions of the private key should be set to 0600, otherwise SSH will