2018-11-02 20:50:54 +00:00
|
|
|
require "log4r"
|
|
|
|
|
2014-04-12 15:45:04 +00:00
|
|
|
module VagrantPlugins
|
|
|
|
module GuestWindows
|
|
|
|
module Cap
|
|
|
|
class Reboot
|
2018-11-02 20:50:54 +00:00
|
|
|
MAX_REBOOT_RETRY_DURATION = 120
|
|
|
|
|
|
|
|
def self.reboot(machine)
|
|
|
|
@logger = Log4r::Logger.new("vagrant::windows::reboot")
|
|
|
|
reboot_script = "shutdown /r /t 5 /f /d p:4:1 /c \"Vagrant Reboot Computer\""
|
|
|
|
|
|
|
|
comm = machine.communicate
|
2014-04-12 15:45:04 +00:00
|
|
|
|
|
|
|
script = File.expand_path("../../scripts/reboot_detect.ps1", __FILE__)
|
|
|
|
script = File.read(script)
|
2018-11-02 20:50:54 +00:00
|
|
|
if comm.test(script, error_check: false, shell: :powershell)
|
|
|
|
@logger.debug("Issuing reboot command for guest")
|
|
|
|
comm.execute(reboot_script, shell: :powershell)
|
|
|
|
else
|
|
|
|
@logger.debug("A reboot is already in progress")
|
|
|
|
end
|
|
|
|
|
|
|
|
@logger.debug("Waiting for machine to finish rebooting")
|
|
|
|
|
|
|
|
wait_remaining = MAX_REBOOT_RETRY_DURATION
|
|
|
|
begin
|
|
|
|
wait_for_reboot(machine)
|
|
|
|
rescue Vagrant::Errors::MachineGuestNotReady, WinRM::WinRMHTTPTransportError => e
|
|
|
|
raise if wait_remaining < 0
|
|
|
|
@logger.warn("Machine not ready, cannot start reboot yet. Trying again")
|
|
|
|
sleep(5)
|
|
|
|
wait_remaining -= 5
|
|
|
|
retry
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.wait_for_reboot(machine)
|
|
|
|
script = File.expand_path("../../scripts/reboot_detect.ps1", __FILE__)
|
|
|
|
script = File.read(script)
|
|
|
|
while machine.communicate.execute(script, error_check: false, shell: :powershell) != 0
|
2014-04-12 15:45:04 +00:00
|
|
|
sleep 10
|
|
|
|
end
|
2014-04-29 19:58:47 +00:00
|
|
|
|
|
|
|
# This re-establishes our symbolic links if they were
|
|
|
|
# created between now and a reboot
|
2018-11-02 20:50:54 +00:00
|
|
|
machine.communicate.execute("net use", error_check: false, shell: :powershell)
|
2014-04-12 15:45:04 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|