Improved logging and such around SSH

This commit is contained in:
Mitchell Hashimoto 2013-02-04 11:44:56 -08:00
parent bc7237130d
commit a72cfdbc00
2 changed files with 14 additions and 4 deletions

View File

@ -1,3 +1,5 @@
require "log4r"
module Vagrant module Vagrant
module Util module Util
module Retryable module Retryable
@ -8,12 +10,16 @@ module Vagrant
# This code is adapted slightly from the following blog post: # This code is adapted slightly from the following blog post:
# http://blog.codefront.net/2008/01/14/retrying-code-blocks-in-ruby-on-exceptions-whatever/ # http://blog.codefront.net/2008/01/14/retrying-code-blocks-in-ruby-on-exceptions-whatever/
def retryable(opts=nil) def retryable(opts=nil)
logger = nil
opts = { :tries => 1, :on => Exception }.merge(opts || {}) opts = { :tries => 1, :on => Exception }.merge(opts || {})
begin begin
return yield return yield
rescue *opts[:on] rescue *opts[:on] => e
if (opts[:tries] -= 1) > 0 if (opts[:tries] -= 1) > 0
logger = Log4r::Logger.new("vagrant::util::retryable")
logger.info("Retryable exception raised: #{e.inspect}")
sleep opts[:sleep].to_f if opts[:sleep] sleep opts[:sleep].to_f if opts[:sleep]
retry retry
end end

View File

@ -163,8 +163,12 @@ module VagrantPlugins
Timeout::Error Timeout::Error
] ]
connection = retryable(:tries => @machine.config.ssh.max_tries, :on => exceptions) do retries = @machine.config.ssh.max_tries
Timeout.timeout(@machine.config.ssh.timeout) do timeout = @machine.config.ssh.timeout
@logger.info("Attempting SSH. Retries: #{retries}. Timeout: #{timeout}")
connection = retryable(:tries => retries, :on => exceptions) do
Timeout.timeout(timeout) do
@logger.info("Attempting to connect to SSH: #{ssh_info[:host]}:#{ssh_info[:port]}") @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