Unverified Commit e75c36e1 authored by Chris Roberts's avatar Chris Roberts Committed by GitHub
Browse files

Merge pull request #12011 from amaltson/make-reboot-retry-duration-configurable

Make the max reboot retry duration configurable
parents eddd21b1 4751353a
Showing with 94 additions and 15 deletions
+94 -15
......@@ -6,7 +6,9 @@ module VagrantPlugins
module Cap
class Reboot
extend Vagrant::Util::GuestInspection::Linux
MAX_REBOOT_RETRY_DURATION = 120
DEFAULT_MAX_REBOOT_RETRY_DURATION = 120
WAIT_SLEEP_TIME = 5
def self.reboot(machine)
@logger = Log4r::Logger.new("vagrant::linux::reboot")
......@@ -25,14 +27,17 @@ module VagrantPlugins
@logger.debug("Waiting for machine to finish rebooting")
wait_remaining = MAX_REBOOT_RETRY_DURATION
wait_remaining = ENV.fetch("VAGRANT_MAX_REBOOT_RETRY_DURATION",
DEFAULT_MAX_REBOOT_RETRY_DURATION).to_i
wait_remaining = DEFAULT_MAX_REBOOT_RETRY_DURATION if wait_remaining < 1
begin
wait_for_reboot(machine)
rescue Vagrant::Errors::MachineGuestNotReady => e
rescue Vagrant::Errors::MachineGuestNotReady
raise if wait_remaining < 0
@logger.warn("Machine not ready, cannot start reboot yet. Trying again")
sleep(5)
wait_remaining -= 5
sleep(WAIT_SLEEP_TIME)
wait_remaining -= WAIT_SLEEP_TIME
retry
end
end
......
......@@ -4,7 +4,8 @@ module VagrantPlugins
module GuestWindows
module Cap
class Reboot
MAX_REBOOT_RETRY_DURATION = 120
DEFAULT_MAX_REBOOT_RETRY_DURATION = 120
WAIT_SLEEP_TIME = 5
def self.reboot(machine)
@logger = Log4r::Logger.new("vagrant::windows::reboot")
......@@ -25,15 +26,18 @@ module VagrantPlugins
@logger.debug("Waiting for machine to finish rebooting")
wait_remaining = MAX_REBOOT_RETRY_DURATION
wait_remaining = ENV.fetch("VAGRANT_MAX_REBOOT_RETRY_DURATION",
DEFAULT_MAX_REBOOT_RETRY_DURATION).to_i
wait_remaining = DEFAULT_MAX_REBOOT_RETRY_DURATION if wait_remaining < 1
begin
wait_for_reboot(machine)
rescue => err
raise if wait_remaining < 0
@logger.debug("Exception caught while waiting for reboot: #{err}")
@logger.warn("Machine not ready, cannot start reboot yet. Trying again")
sleep(5)
wait_remaining -= 5
sleep(WAIT_SLEEP_TIME)
wait_remaining -= WAIT_SLEEP_TIME
retry
end
end
......
......@@ -64,14 +64,44 @@ describe "VagrantPlugins::GuestLinux::Cap::Reboot" do
communicator.verify_expectations!
end
describe ".reboot" do
it "reboots the vm" do
allow(communicator).to receive(:execute)
it "reboots the vm" do
allow(communicator).to receive(:execute)
expect(communicator).to receive(:execute).with(/systemctl reboot/, nil).and_return(0)
expect(described_class).to receive(:wait_for_reboot)
expect(communicator).to receive(:execute).with(/systemctl reboot/, nil).and_return(0)
expect(described_class).to receive(:wait_for_reboot)
described_class.reboot(machine)
end
end
context "reboot configuration" do
before do
allow(communicator).to receive(:execute)
expect(communicator).to receive(:execute).with(/reboot/, nil).and_return(0)
allow(described_class).to receive(:sleep).and_return(described_class::WAIT_SLEEP_TIME)
allow(described_class).to receive(:wait_for_reboot).and_raise(Vagrant::Errors::MachineGuestNotReady)
end
context "default retry duration value" do
let(:max_retries) { (described_class::DEFAULT_MAX_REBOOT_RETRY_DURATION / described_class::WAIT_SLEEP_TIME) + 2 }
it "should receive expected number of wait_for_reboot calls" do
expect(described_class).to receive(:wait_for_reboot).exactly(max_retries).times
expect { described_class.reboot(machine) }.to raise_error(Vagrant::Errors::MachineGuestNotReady)
end
end
context "with custom retry duration value" do
let(:duration) { 10 }
let(:max_retries) { (duration / described_class::WAIT_SLEEP_TIME) + 2 }
before do
expect(ENV).to receive(:fetch).with("VAGRANT_MAX_REBOOT_RETRY_DURATION", anything).and_return(duration)
end
described_class.reboot(machine)
it "should receive expected number of wait_for_reboot calls" do
expect(described_class).to receive(:wait_for_reboot).exactly(max_retries).times
expect { described_class.reboot(machine) }.to raise_error(Vagrant::Errors::MachineGuestNotReady)
end
end
end
......
......@@ -112,4 +112,37 @@ describe "VagrantPlugins::GuestWindows::Cap::Reboot" do
end
end
end
context "reboot configuration" do
before do
allow(communicator).to receive(:execute)
expect(communicator).to receive(:test).with(/# Function/, { error_check: false, shell: :powershell }).and_return(0)
expect(communicator).to receive(:execute).with(/shutdown/, { shell: :powershell }).and_return(0)
allow(described_class).to receive(:sleep)
allow(described_class).to receive(:wait_for_reboot).and_raise(StandardError)
end
context "default retry duration value" do
let(:max_retries) { (described_class::DEFAULT_MAX_REBOOT_RETRY_DURATION / described_class::WAIT_SLEEP_TIME) + 2 }
it "should receive expected number of wait_for_reboot calls" do
expect(described_class).to receive(:wait_for_reboot).exactly(max_retries).times
expect { described_class.reboot(machine) }.to raise_error(StandardError)
end
end
context "with custom retry duration value" do
let(:duration) { 10 }
let(:max_retries) { (duration / described_class::WAIT_SLEEP_TIME) + 2 }
before do
expect(ENV).to receive(:fetch).with("VAGRANT_MAX_REBOOT_RETRY_DURATION", anything).and_return(duration)
end
it "should receive expected number of wait_for_reboot calls" do
expect(described_class).to receive(:wait_for_reboot).exactly(max_retries).times
expect { described_class.reboot(machine) }.to raise_error(StandardError)
end
end
end
end
......@@ -225,6 +225,13 @@ and can help identify certain issues.
some knowledge of Vagrant internals. It is the best output to attach to
a support request or bug report, however.
## `VAGRANT_MAX_REBOOT_RETRY_DURATION`
By default, Vagrant will wait up to 120 seconds for a machine to reboot.
However, if you're finding your OS is taking longer than 120 seconds to
reboot successfully, you can configure this environment variable and Vagrant
will wait for the configured number of seconds.
## `VAGRANT_NO_COLOR`
If this is set to any value, then Vagrant will not use any colorized
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment