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

View File

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

View File

@ -8,6 +8,15 @@ class SshTest < Test::Unit::TestCase
@instance = @klass.new(@session) @instance = @klass.new(@session)
end 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 context "checking exit status" do
should "raise an ActionException if its non-zero" do should "raise an ActionException if its non-zero" do
assert_raises(Vagrant::Actions::ActionException) { assert_raises(Vagrant::Actions::ActionException) {