Retry SSH commands 5 times if an IOError occurs

This commit is contained in:
Mitchell Hashimoto 2010-06-03 20:55:46 -07:00
parent dc760c73b9
commit 8dc57c6796
3 changed files with 32 additions and 14 deletions

View File

@ -160,6 +160,9 @@ module Vagrant
ch[:result] << data if [:stdout, :stderr].include?(type)
end
tries = 5
begin
metach = session.open_channel do |channel|
channel.exec(command) do |ch, success|
raise "could not execute command: #{command.inspect}" unless success
@ -180,6 +183,10 @@ module Vagrant
end
end
end
rescue IOError
retry if (tries -= 1) > 0
raise
end
metach.wait
metach[:result]

View File

@ -139,6 +139,7 @@ class ForwardPortsActionTest < Test::Unit::TestCase
@vm.expects(:forwarded_ports).returns(forwarded_ports)
@vm.expects(:save).once
@runner.expects(:reload!).once
@action.forward_ports
end
end
@ -151,6 +152,7 @@ class ForwardPortsActionTest < Test::Unit::TestCase
@vm.expects(:network_adapters).returns([network_adapter])
network_adapter.expects(:attachment_type).returns(:host_only)
@vm.expects(:save).once
@runner.expects(:reload!).once
@action.forward_ports
end
end

View File

@ -8,6 +8,15 @@ class SshTest < Test::Unit::TestCase
@instance = @klass.new(@session)
end
context "exec!" do
should "retry 5 times" do
@session.expects(:open_channel).times(5).raises(IOError)
assert_raises(IOError) {
@instance.exec!("foo")
}
end
end
context "checking exit status" do
should "raise an ActionException if its non-zero" do
assert_raises(Vagrant::Actions::ActionException) {