From 73205f3a912c82233bf9c0e8777c5ec6d6c941e7 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Fri, 12 Apr 2019 15:59:09 -0700 Subject: [PATCH] add keep_registered option to hyperv --- builder/hyperv/common/step_clone_vm.go | 7 +++++++ builder/hyperv/common/step_create_vm.go | 7 +++++++ builder/hyperv/iso/builder.go | 2 ++ builder/hyperv/vmcx/builder.go | 2 ++ website/source/docs/builders/hyperv-vmcx.html.md.erb | 3 +++ 5 files changed, 21 insertions(+) diff --git a/builder/hyperv/common/step_clone_vm.go b/builder/hyperv/common/step_clone_vm.go index 10a8cd7e4..8233e8306 100644 --- a/builder/hyperv/common/step_clone_vm.go +++ b/builder/hyperv/common/step_clone_vm.go @@ -31,6 +31,7 @@ type StepCloneVM struct { SecureBootTemplate string EnableVirtualizationExtensions bool MacAddress string + KeepRegistered bool } func (s *StepCloneVM) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { @@ -147,6 +148,12 @@ func (s *StepCloneVM) Cleanup(state multistep.StateBag) { driver := state.Get("driver").(Driver) ui := state.Get("ui").(packer.Ui) + + if s.KeepRegistered { + ui.Say("keep_registered set. Skipping unregister/deletion of VM.") + return + } + ui.Say("Unregistering and deleting virtual machine...") err := driver.DeleteVirtualMachine(s.VMName) diff --git a/builder/hyperv/common/step_create_vm.go b/builder/hyperv/common/step_create_vm.go index f205437cb..a9371c245 100644 --- a/builder/hyperv/common/step_create_vm.go +++ b/builder/hyperv/common/step_create_vm.go @@ -35,6 +35,7 @@ type StepCreateVM struct { MacAddress string FixedVHD bool Version string + KeepRegistered bool } func (s *StepCreateVM) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { @@ -168,6 +169,12 @@ func (s *StepCreateVM) Cleanup(state multistep.StateBag) { driver := state.Get("driver").(Driver) ui := state.Get("ui").(packer.Ui) + + if s.KeepRegistered { + ui.Say("keep_registered set. Skipping unregister/deletion of VM.") + return + } + ui.Say("Unregistering and deleting virtual machine...") err := driver.DeleteVirtualMachine(s.VMName) diff --git a/builder/hyperv/iso/builder.go b/builder/hyperv/iso/builder.go index 004c6e9ee..a064bc0e7 100644 --- a/builder/hyperv/iso/builder.go +++ b/builder/hyperv/iso/builder.go @@ -98,6 +98,7 @@ type Config struct { EnableVirtualizationExtensions bool `mapstructure:"enable_virtualization_extensions"` TempPath string `mapstructure:"temp_path"` Version string `mapstructure:"configuration_version"` + KeepRegistered bool `mapstructure:"keep_registered"` Communicator string `mapstructure:"communicator"` @@ -420,6 +421,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack MacAddress: b.config.MacAddress, FixedVHD: b.config.FixedVHD, Version: b.config.Version, + KeepRegistered: b.config.KeepRegistered, }, &hypervcommon.StepEnableIntegrationService{}, diff --git a/builder/hyperv/vmcx/builder.go b/builder/hyperv/vmcx/builder.go index c9041c3fe..e152294be 100644 --- a/builder/hyperv/vmcx/builder.go +++ b/builder/hyperv/vmcx/builder.go @@ -95,6 +95,7 @@ type Config struct { EnableVirtualizationExtensions bool `mapstructure:"enable_virtualization_extensions"` TempPath string `mapstructure:"temp_path"` Version string `mapstructure:"configuration_version"` + KeepRegistered bool `mapstructure:"keep_registered"` Communicator string `mapstructure:"communicator"` @@ -441,6 +442,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack SecureBootTemplate: b.config.SecureBootTemplate, EnableVirtualizationExtensions: b.config.EnableVirtualizationExtensions, MacAddress: b.config.MacAddress, + KeepRegistered: b.config.KeepRegistered, }, &hypervcommon.StepEnableIntegrationService{}, diff --git a/website/source/docs/builders/hyperv-vmcx.html.md.erb b/website/source/docs/builders/hyperv-vmcx.html.md.erb index fec366b58..a492fa935 100644 --- a/website/source/docs/builders/hyperv-vmcx.html.md.erb +++ b/website/source/docs/builders/hyperv-vmcx.html.md.erb @@ -195,6 +195,9 @@ builder. port, set an identical value for `http_port_min` and `http_port_max`. By default the values are 8000 and 9000, respectively. +- `keep_registered` (bool) - If "true", Packer will not delete the VM from + The Hyper-V manager. + - `mac_address` (string) - This allows a specific MAC address to be used on the default virtual network card. The MAC address must be a string with no delimiters, for example "0000deadbeef".