Skip to content

Commit

Permalink
Only cleanup VM when it was created (#169)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
edigaryev authored Sep 25, 2024
1 parent dc9464f commit 033de1a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 22 deletions.
23 changes: 6 additions & 17 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
19 changes: 14 additions & 5 deletions builder/tart/step_cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
}
2 changes: 2 additions & 0 deletions builder/tart/step_clone_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 2 additions & 0 deletions builder/tart/step_create_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 033de1a

Please sign in to comment.