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,25 +160,32 @@ module Vagrant
ch[:result] << data if [:stdout, :stderr].include?(type)
end
metach = session.open_channel do |channel|
channel.exec(command) do |ch, success|
raise "could not execute command: #{command.inspect}" unless success
tries = 5
# Output stdout data to the block
channel.on_data do |ch2, data|
block.call(ch2, :stdout, data)
end
begin
metach = session.open_channel do |channel|
channel.exec(command) do |ch, success|
raise "could not execute command: #{command.inspect}" unless success
# Output stderr data to the block
channel.on_extended_data do |ch2, type, data|
block.call(ch2, :stderr, data)
end
# Output stdout data to the block
channel.on_data do |ch2, data|
block.call(ch2, :stdout, data)
end
# Output exit status information to the block
channel.on_request("exit-status") do |ch2, data|
block.call(ch2, :exit_status, data.read_long)
# Output stderr data to the block
channel.on_extended_data do |ch2, type, data|
block.call(ch2, :stderr, data)
end
# Output exit status information to the block
channel.on_request("exit-status") do |ch2, data|
block.call(ch2, :exit_status, data.read_long)
end
end
end
rescue IOError
retry if (tries -= 1) > 0
raise
end
metach.wait

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) {