Re-use SSH connection

This commit is contained in:
Mitchell Hashimoto 2012-01-07 11:37:08 -08:00
parent 7d56dbb755
commit 0f580fd2c0
3 changed files with 25 additions and 7 deletions

View File

@ -87,6 +87,26 @@ module Vagrant
# Opens an SSH connection and yields it to a block. # Opens an SSH connection and yields it to a block.
def connect def connect
if @connection && !@connection.closed?
# There is a chance that the socket is closed despite us checking
# 'closed?' above. To test this we need to send data through the
# socket.
begin
@connection.exec!("")
rescue IOError
@logger.info("Connection has been closed. Not re-using.")
@connection = nil
end
# If the @connection is still around, then it is valid,
# and we use it.
if @connection
@logger.info("Re-using SSH connection.")
return yield @connection if block_given?
return
end
end
ssh_info = @vm.ssh.info ssh_info = @vm.ssh.info
# Build the options we'll use to initiate the connection via Net::SSH # Build the options we'll use to initiate the connection via Net::SSH
@ -104,12 +124,14 @@ module Vagrant
@vm.ssh.check_key_permissions(ssh_info[:private_key_path]) @vm.ssh.check_key_permissions(ssh_info[:private_key_path])
# Connect to SSH, giving it a few tries # Connect to SSH, giving it a few tries
@logger.debug("Connecting to SSH: #{ssh_info[:host]}:#{ssh_info[:port]}") @logger.info("Connecting to SSH: #{ssh_info[:host]}:#{ssh_info[:port]}")
exceptions = [Errno::ECONNREFUSED, Net::SSH::Disconnect] 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
Net::SSH.start(ssh_info[:host], ssh_info[:username], opts) Net::SSH.start(ssh_info[:host], ssh_info[:username], opts)
end end
@connection = connection
# This is hacky but actually helps with some issues where # This is hacky but actually helps with some issues where
# Net::SSH is simply not robust enough to handle... see # Net::SSH is simply not robust enough to handle... see
# issue #391, #455, etc. # issue #391, #455, etc.
@ -127,7 +149,7 @@ module Vagrant
raise Errors::SSHConnectionRefused raise Errors::SSHConnectionRefused
ensure ensure
# Be sure the connection is always closed # Be sure the connection is always closed
connection.close if connection && !connection.closed? # connection.close if connection && !connection.closed?
end end
# Executes the command on an SSH connection within a login shell. # Executes the command on an SSH connection within a login shell.

View File

@ -90,10 +90,6 @@ en:
Vagrant assumes that this means the command failed! Vagrant assumes that this means the command failed!
%{command} %{command}
The output of the command prior to failing is outputted below:
%{output}
ssh_connection_refused: |- ssh_connection_refused: |-
SSH connection was refused! This usually happens if the VM failed to SSH connection was refused! This usually happens if the VM failed to
boot properly. Some steps to try to fix this: First, try reloading your boot properly. Some steps to try to fix this: First, try reloading your

View File

@ -19,7 +19,7 @@ Gem::Specification.new do |s|
s.add_dependency "erubis", "~> 2.7.0" s.add_dependency "erubis", "~> 2.7.0"
s.add_dependency "json", "~> 1.5.1" s.add_dependency "json", "~> 1.5.1"
s.add_dependency "log4r", "~> 1.1.9" s.add_dependency "log4r", "~> 1.1.9"
s.add_dependency "net-ssh", "~> 2.1.4" s.add_dependency "net-ssh", "~> 2.2.2"
s.add_dependency "net-scp", "~> 1.0.4" s.add_dependency "net-scp", "~> 1.0.4"
s.add_dependency "i18n", "~> 0.6.0" s.add_dependency "i18n", "~> 0.6.0"