From 033de1a10d8d56750175f166c44d6eb60b92e453 Mon Sep 17 00:00:00 2001 From: Nikolay Edigaryev Date: Thu, 26 Sep 2024 01:13:10 +0400 Subject: [PATCH] Only cleanup VM when it was created (#169) * Only cleanup VM when it was created * Ignore gosec's G115 linter because we know what we're doing * .golangci.yml: remove mentions of fully deprecated linters --- .golangci.yml | 23 ++++++----------------- builder/tart/step_cleanup.go | 19 ++++++++++++++----- builder/tart/step_clone_vm.go | 2 ++ builder/tart/step_create_vm.go | 2 ++ 4 files changed, 24 insertions(+), 22 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 736ffb8..0c27179 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,19 +1,15 @@ run: timeout: 5m +linters-settings: + gosec: + excludes: + - G115 + linters: enable-all: true disable: - # Messages like "struct of size 104 bytes could be of size 96 bytes" from a package - # that was last updated 2 years ago[1] are barely helpful. - # - # After all, we're writing the code for other people, so let's trust the compiler here (that's - # constantly evolving compared to this linter) and revisit this if memory usage becomes a problem. - # - # [1]: https://github.com/mdempsky/maligned/commit/6e39bd26a8c8b58c5a22129593044655a9e25959 - - maligned - # We don't have high-performance requirements at this moment, so sacrificing # the code readability for marginal performance gains is not worth it. - prealloc @@ -26,19 +22,12 @@ linters: # Unfortunately, we use globals due to how spf13/cobra works. - gochecknoglobals - # That's fine that some Proto objects don't have all fields initialized - - exhaustivestruct - # Style linters that are total nuts. - wsl - gofumpt - goimports - funlen - # This conflicts with the Protocol Buffers Version 3 design, - # which is largely based on default values for struct fields. - - exhaustivestruct - # Enough parallelism for now. - paralleltest @@ -77,7 +66,7 @@ linters: - exhaustruct # This is not a library, so it's OK to use dynamic errors - - goerr113 + - err113 # fmt.Sprintf() looks a bit nicer than string addition - perfsprint diff --git a/builder/tart/step_cleanup.go b/builder/tart/step_cleanup.go index eecb47f..487351e 100644 --- a/builder/tart/step_cleanup.go +++ b/builder/tart/step_cleanup.go @@ -13,14 +13,23 @@ func (s *stepCleanVM) Run(ctx context.Context, state multistep.StateBag) multist } func (s *stepCleanVM) Cleanup(state multistep.StateBag) { - config := state.Get("config").(*Config) ui := state.Get("ui").(packersdk.Ui) _, cancelled := state.GetOk(multistep.StateCancelled) _, halted := state.GetOk(multistep.StateHalted) - if cancelled || halted { - ui.Say("Cleaning up virtual machine...") - cmdArgs := []string{"delete", config.VMName} - _, _ = TartExec(context.Background(), ui, cmdArgs...) + + // Only cleanup on cancellation + if !(cancelled || halted) { + return + } + + // Only cleanup when the VM was created + vmName, ok := state.Get("vm_name").(string) + if !ok { + return } + + ui.Say("Cleaning up virtual machine...") + cmdArgs := []string{"delete", vmName} + _, _ = TartExec(context.Background(), ui, cmdArgs...) } diff --git a/builder/tart/step_clone_vm.go b/builder/tart/step_clone_vm.go index 27d7bf5..06d4644 100644 --- a/builder/tart/step_clone_vm.go +++ b/builder/tart/step_clone_vm.go @@ -32,6 +32,8 @@ func (s *stepCloneVM) Run(ctx context.Context, state multistep.StateBag) multist return multistep.ActionHalt } + state.Put("vm_name", config.VMName) + return multistep.ActionContinue } diff --git a/builder/tart/step_create_vm.go b/builder/tart/step_create_vm.go index 780ae6c..d0381d7 100644 --- a/builder/tart/step_create_vm.go +++ b/builder/tart/step_create_vm.go @@ -38,6 +38,8 @@ func (s *stepCreateVM) Run(ctx context.Context, state multistep.StateBag) multis return multistep.ActionHalt } + state.Put("vm_name", config.VMName) + if config.CreateGraceTime != 0 { message := fmt.Sprintf("Waiting %v to let the Virtualization.Framework's installation process "+ "to finish correctly...", config.CreateGraceTime)