From 8aa74a03a09bfab203d1866fc5f3e4476bb1657f Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Fri, 13 Jul 2018 15:23:09 -0700 Subject: [PATCH] Differentiate checkpoints and automatic checkpoints Provide separate configuration settings for enabling/disabling checkpoints and automatic checkpoints with Hyper-V provider. --- plugins/providers/hyperv/action/configure.rb | 1 + plugins/providers/hyperv/config.rb | 16 ++++++++++++-- .../providers/hyperv/scripts/configure_vm.ps1 | 20 ++++++++++++++++- .../providers/hyperv/action/configure_test.rb | 1 + .../plugins/providers/hyperv/config_test.rb | 22 ++++++++++++++++++- 5 files changed, 56 insertions(+), 4 deletions(-) diff --git a/plugins/providers/hyperv/action/configure.rb b/plugins/providers/hyperv/action/configure.rb index 30831ac12..f25737f8a 100644 --- a/plugins/providers/hyperv/action/configure.rb +++ b/plugins/providers/hyperv/action/configure.rb @@ -70,6 +70,7 @@ module VagrantPlugins "AutoStartAction" => env[:machine].provider_config.auto_start_action, "AutoStopAction" => env[:machine].provider_config.auto_stop_action, "EnableCheckpoints" => env[:machine].provider_config.enable_checkpoints, + "EnableAutomaticCheckpoints" => env[:machine].provider_config.enable_automatic_checkpoints, "VirtualizationExtensions" => !!env[:machine].provider_config.enable_virtualization_extensions, } options.delete_if{|_,v| v.nil? } diff --git a/plugins/providers/hyperv/config.rb b/plugins/providers/hyperv/config.rb index 3403ef682..96014258c 100644 --- a/plugins/providers/hyperv/config.rb +++ b/plugins/providers/hyperv/config.rb @@ -40,8 +40,10 @@ module VagrantPlugins attr_accessor :auto_start_action # @return [String] Automatic action on stop of host. Default: ShutDown (ShutDown, TurnOff, Save) attr_accessor :auto_stop_action - # @return [Boolean] Enable automatic checkpoints. Default: false + # @return [Boolean] Enable checkpoints. Default: true attr_accessor :enable_checkpoints + # @return [Boolean] Enable automatic checkpoints. Default: false + attr_accessor :enable_automatic_checkpoints # @return [Boolean] Enable virtualization extensions attr_accessor :enable_virtualization_extensions # @return [Hash] Options for VMServiceIntegration @@ -60,6 +62,7 @@ module VagrantPlugins @auto_start_action = UNSET_VALUE @auto_stop_action = UNSET_VALUE @enable_virtualization_extensions = UNSET_VALUE + @enable_automatic_checkpoints = UNSET_VALUE @enable_checkpoints = UNSET_VALUE @vm_integration_services = {} end @@ -85,11 +88,20 @@ module VagrantPlugins @auto_start_action = "Nothing" if @auto_start_action == UNSET_VALUE @auto_stop_action = "ShutDown" if @auto_stop_action == UNSET_VALUE @enable_virtualization_extensions = false if @enable_virtualization_extensions == UNSET_VALUE + + if @enable_automatic_checkpoints == UNSET_VALUE + @enable_automatic_checkpoints = false + else + @enable_automatic_checkpoints = !!@enable_automatic_checkpoints + end if @enable_checkpoints == UNSET_VALUE - @enable_checkpoints = false + @enable_checkpoints = true else @enable_checkpoints = !!@enable_checkpoints end + + # If automatic checkpoints are enabled, checkpoints will automatically be enabled + @enable_checkpoints ||= @enable_automatic_checkpoints end def validate(machine) diff --git a/plugins/providers/hyperv/scripts/configure_vm.ps1 b/plugins/providers/hyperv/scripts/configure_vm.ps1 index ff2b8ca3b..54f878f25 100644 --- a/plugins/providers/hyperv/scripts/configure_vm.ps1 +++ b/plugins/providers/hyperv/scripts/configure_vm.ps1 @@ -18,7 +18,9 @@ param( [parameter (Mandatory=$false)] [switch] $VirtualizationExtensions, [parameter (Mandatory=$false)] - [switch] $EnableCheckpoints + [switch] $EnableCheckpoints, + [parameter (Mandatory=$false)] + [switch] $EnableAutomaticCheckpoints ) $ErrorActionPreference = "Stop" @@ -76,6 +78,7 @@ if($SwitchID) { Set-VagrantVMSwitch -VM $VM -SwitchName $SwitchName } catch { Write-ErrorMessage "Failed to configure network adapter: ${PSItem}" + exit 1 } } @@ -93,3 +96,18 @@ try { Write-ErrorMessage "Failed to ${CheckpointAction} checkpoints on VM: ${PSItem}" exit 1 } + +if($EnableAutomaticCheckpoints) { + $autochecks = 1 + $AutoAction = "enabled" +} else { + $autochecks = 0 + $AutoAction = "disable" +} + +try { + Hyper-V\Set-VM -VM $VM -AutomaticCheckpointsEnabled $autochecks +} catch { + Write-ErrorMessage "Failed to ${AutoAction} automatic checkpoints on VM: ${PSItem}" + exit 1 +} diff --git a/test/unit/plugins/providers/hyperv/action/configure_test.rb b/test/unit/plugins/providers/hyperv/action/configure_test.rb index 9b2d8222b..504e416ef 100644 --- a/test/unit/plugins/providers/hyperv/action/configure_test.rb +++ b/test/unit/plugins/providers/hyperv/action/configure_test.rb @@ -26,6 +26,7 @@ describe VagrantPlugins::HyperV::Action::Configure do auto_start_action: "Nothing", auto_stop_action: "Save", enable_checkpoints: false, + enable_automatic_checkpoints: true, enable_virtualization_extensions: false, vm_integration_services: vm_integration_services ) diff --git a/test/unit/plugins/providers/hyperv/config_test.rb b/test/unit/plugins/providers/hyperv/config_test.rb index 23328cf3b..4b57a508a 100644 --- a/test/unit/plugins/providers/hyperv/config_test.rb +++ b/test/unit/plugins/providers/hyperv/config_test.rb @@ -163,11 +163,31 @@ describe VagrantPlugins::HyperV::Config do end describe "#enable_checkpoints" do - it "is false by default" do + it "is true by default" do + subject.finalize! + expect(subject.enable_checkpoints).to eq(true) + end + + it "can be set" do + subject.enable_checkpoints = false subject.finalize! expect(subject.enable_checkpoints).to eq(false) end + it "is enabled automatically when enable_automatic_checkpoints is enabled" do + subject.enable_checkpoints = false + subject.enable_automatic_checkpoints = true + subject.finalize! + expect(subject.enable_checkpoints).to eq(true) + end + end + + describe "#enable_automatic_checkpoints" do + it "is false by default" do + subject.finalize! + expect(subject.enable_automatic_checkpoints).to eq(false) + end + it "can be set" do subject.enable_checkpoints = true subject.finalize!