privisioners/shell: wait for reboot if we can

This commit is contained in:
Mitchell Hashimoto 2014-04-12 08:45:04 -07:00
parent 4fc6fbeef6
commit 4145aa6bcd
4 changed files with 69 additions and 1 deletions

View File

@ -0,0 +1,19 @@
module VagrantPlugins
module GuestWindows
module Cap
class Reboot
def self.wait_for_reboot(machine)
# Technically it should be possible to make it work with SSH
# too, but we don't yet.
return if machine.config.vm.communicator != :winrm
script = File.expand_path("../../scripts/reboot_detect.ps1", __FILE__)
script = File.read(script)
while machine.communicate.execute(script, error_check: false) != 0
sleep 10
end
end
end
end
end
end

View File

@ -49,6 +49,11 @@ module VagrantPlugins
Cap::MountSharedFolder
end
guest_capability(:windows, :wait_for_reboot) do
require_relative "cap/reboot"
Cap::Reboot
end
protected
def self.init!

View File

@ -0,0 +1,42 @@
# Function to check whether machine is currently shutting down
function ShuttingDown {
[string]$sourceCode = @"
using System;
using System.Runtime.InteropServices;
namespace Vagrant {
public static class RemoteManager {
private const int SM_SHUTTINGDOWN = 0x2000;
[DllImport("User32.dll", CharSet = CharSet.Unicode)]
private static extern int GetSystemMetrics(int Index);
public static bool Shutdown() {
return (0 != GetSystemMetrics(SM_SHUTTINGDOWN));
}
}
}
"@
$type = Add-Type -TypeDefinition $sourceCode -PassThru
return $type::Shutdown()
}
if (ShuttingDown) {
exit 1
} else {
# See if a reboot is scheduled in the future by trying to schedule a reboot
. shutdown.exe -f -r -t 60
if ($LASTEXITCODE -eq 1190) {
# reboot is already pending
exit 2
}
# Remove the pending reboot we just created above
if ($LASTEXITCODE -eq 0) {
. shutdown.exe -a
}
}
# no reboot in progress or scheduled
exit 0

View File

@ -74,7 +74,9 @@ module VagrantPlugins
# This provisions using WinRM, which assumes a PowerShell
# console on the other side.
def provision_winrm(args)
# TODO: Wait for rebooting
if @machine.guest.capability?(:wait_for_reboot)
@machine.guest.capability(:wait_for_reboot)
end
with_script_file do |path|
@machine.communicate.tap do |comm|