From a72cfdbc00514d6a312eca58a29d3826b6119178 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 4 Feb 2013 11:44:56 -0800 Subject: [PATCH] Improved logging and such around SSH --- lib/vagrant/util/retryable.rb | 10 ++++++++-- plugins/communicators/ssh/communicator.rb | 8 ++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/vagrant/util/retryable.rb b/lib/vagrant/util/retryable.rb index 12fe2e251..780c4416a 100644 --- a/lib/vagrant/util/retryable.rb +++ b/lib/vagrant/util/retryable.rb @@ -1,3 +1,5 @@ +require "log4r" + module Vagrant module Util module Retryable @@ -8,12 +10,16 @@ module Vagrant # 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/ def retryable(opts=nil) - opts = { :tries => 1, :on => Exception }.merge(opts || {}) + logger = nil + opts = { :tries => 1, :on => Exception }.merge(opts || {}) begin return yield - rescue *opts[:on] + rescue *opts[:on] => e 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] retry end diff --git a/plugins/communicators/ssh/communicator.rb b/plugins/communicators/ssh/communicator.rb index 289df8306..94d19afaf 100644 --- a/plugins/communicators/ssh/communicator.rb +++ b/plugins/communicators/ssh/communicator.rb @@ -163,8 +163,12 @@ module VagrantPlugins Timeout::Error ] - connection = retryable(:tries => @machine.config.ssh.max_tries, :on => exceptions) do - Timeout.timeout(@machine.config.ssh.timeout) do + retries = @machine.config.ssh.max_tries + 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]}") Net::SSH.start(ssh_info[:host], ssh_info[:username], opts) end