From 185d5210cb379d3a9c8939f6a9dd3112bec32a52 Mon Sep 17 00:00:00 2001 From: remi-espie Date: Thu, 2 Nov 2023 11:49:07 +0100 Subject: [PATCH 1/3] fix: fix #7 --- builder/hyperv/common/step_create_build_dir.go | 9 +++++++++ builder/hyperv/iso/builder.go | 3 ++- builder/hyperv/vmcx/builder.go | 3 ++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/builder/hyperv/common/step_create_build_dir.go b/builder/hyperv/common/step_create_build_dir.go index 622b70b2..d2a93171 100644 --- a/builder/hyperv/common/step_create_build_dir.go +++ b/builder/hyperv/common/step_create_build_dir.go @@ -24,6 +24,9 @@ type StepCreateBuildDir struct { // The full path to the build directory. This is the concatenation of // TempPath plus a directory uniquely named for the build buildDir string + // If true, the build directory will not be deleted when the step + // Cleanup() method is called + KeepRegistered bool } // Creates the main directory used to house the VMs files and folders @@ -62,6 +65,12 @@ func (s *StepCreateBuildDir) Cleanup(state multistep.StateBag) { } ui := state.Get("ui").(packersdk.Ui) + + if s.KeepRegistered { + ui.Say("Keeping build directory...") + return + } + ui.Say("Deleting build directory...") err := os.RemoveAll(s.buildDir) diff --git a/builder/hyperv/iso/builder.go b/builder/hyperv/iso/builder.go index 058efff3..d3d39ba2 100644 --- a/builder/hyperv/iso/builder.go +++ b/builder/hyperv/iso/builder.go @@ -202,7 +202,8 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) steps := []multistep.Step{ &hypervcommon.StepCreateBuildDir{ - TempPath: b.config.TempPath, + TempPath: b.config.TempPath, + KeepRegistered: b.config.KeepRegistered, }, &commonsteps.StepOutputDir{ Force: b.config.PackerForce, diff --git a/builder/hyperv/vmcx/builder.go b/builder/hyperv/vmcx/builder.go index 0a2048a4..ef7b2529 100644 --- a/builder/hyperv/vmcx/builder.go +++ b/builder/hyperv/vmcx/builder.go @@ -243,7 +243,8 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) steps := []multistep.Step{ &hypervcommon.StepCreateBuildDir{ - TempPath: b.config.TempPath, + TempPath: b.config.TempPath, + KeepRegistered: b.config.KeepRegistered, }, &commonsteps.StepOutputDir{ Force: b.config.PackerForce, From e9b1aee00c633730f18d28914bb7b88e2cb78003 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Espi=C3=A9=2ER?= <60436978+remi-espie@users.noreply.github.com> Date: Thu, 2 Nov 2023 15:44:55 +0100 Subject: [PATCH 2/3] remove artifacts anyway if the build failed or was aborted Co-authored-by: Lucas Bajolet <105649352+lbajolet-hashicorp@users.noreply.github.com> --- builder/hyperv/common/step_create_build_dir.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/builder/hyperv/common/step_create_build_dir.go b/builder/hyperv/common/step_create_build_dir.go index d2a93171..9a927451 100644 --- a/builder/hyperv/common/step_create_build_dir.go +++ b/builder/hyperv/common/step_create_build_dir.go @@ -66,7 +66,10 @@ func (s *StepCreateBuildDir) Cleanup(state multistep.StateBag) { ui := state.Get("ui").(packersdk.Ui) - if s.KeepRegistered { + _, cancelled := state.GetOk(multistep.StateCancelled) + _, halted := state.GetOk(multistep.StateHalted) + + if s.KeepRegistered && !(cancelled || halted) { ui.Say("Keeping build directory...") return } From 26e4af22135403c3208b2b1000198a3b99f9ad1f Mon Sep 17 00:00:00 2001 From: remi-espie Date: Tue, 21 Nov 2023 16:33:56 +0100 Subject: [PATCH 3/3] feat: rephrase `keep_registered` documentation --- .web-docs/components/builder/iso/README.md | 2 ++ .web-docs/components/builder/vmcx/README.md | 2 ++ builder/hyperv/common/config.go | 2 ++ .../builder/hyperv/common/CommonConfig-not-required.mdx | 2 ++ 4 files changed, 8 insertions(+) diff --git a/.web-docs/components/builder/iso/README.md b/.web-docs/components/builder/iso/README.md index 61c918fe..cce32d8e 100644 --- a/.web-docs/components/builder/iso/README.md +++ b/.web-docs/components/builder/iso/README.md @@ -318,6 +318,8 @@ created, must be empty prior to running the builder. By default this is - `keep_registered` (bool) - If "true", Packer will not delete the VM from The Hyper-V manager. + The resulting VM will be housed in a randomly generated folder under %TEMP% by default. + You can set the `temp_path` variable to change the location of the folder. - `skip_compaction` (bool) - If true skip compacting the hard disk for the virtual machine when exporting. This defaults to false. diff --git a/.web-docs/components/builder/vmcx/README.md b/.web-docs/components/builder/vmcx/README.md index de465ee9..36f80bc5 100644 --- a/.web-docs/components/builder/vmcx/README.md +++ b/.web-docs/components/builder/vmcx/README.md @@ -345,6 +345,8 @@ In HCL2: - `keep_registered` (bool) - If "true", Packer will not delete the VM from The Hyper-V manager. + The resulting VM will be housed in a randomly generated folder under %TEMP% by default. + You can set the `temp_path` variable to change the location of the folder. - `skip_compaction` (bool) - If true skip compacting the hard disk for the virtual machine when exporting. This defaults to false. diff --git a/builder/hyperv/common/config.go b/builder/hyperv/common/config.go index b7ab80fe..707e24dd 100644 --- a/builder/hyperv/common/config.go +++ b/builder/hyperv/common/config.go @@ -139,6 +139,8 @@ type CommonConfig struct { Version string `mapstructure:"configuration_version" required:"false"` // If "true", Packer will not delete the VM from // The Hyper-V manager. + // The resulting VM will be housed in a randomly generated folder under %TEMP% by default. + // You can set the `temp_path` variable to change the location of the folder. KeepRegistered bool `mapstructure:"keep_registered" required:"false"` // If true skip compacting the hard disk for // the virtual machine when exporting. This defaults to false. diff --git a/docs-partials/builder/hyperv/common/CommonConfig-not-required.mdx b/docs-partials/builder/hyperv/common/CommonConfig-not-required.mdx index 1e17038b..816be863 100644 --- a/docs-partials/builder/hyperv/common/CommonConfig-not-required.mdx +++ b/docs-partials/builder/hyperv/common/CommonConfig-not-required.mdx @@ -97,6 +97,8 @@ - `keep_registered` (bool) - If "true", Packer will not delete the VM from The Hyper-V manager. + The resulting VM will be housed in a randomly generated folder under %TEMP% by default. + You can set the `temp_path` variable to change the location of the folder. - `skip_compaction` (bool) - If true skip compacting the hard disk for the virtual machine when exporting. This defaults to false.