From e7583de847f121776b2a0366e33aaec4232c35c1 Mon Sep 17 00:00:00 2001 From: Thomas Meckel Date: Fri, 26 Jul 2019 11:35:12 +0200 Subject: [PATCH] StepCreateSnapshot.Run() will stop and create an error if the virtual machine is still running Removed left over code from StepCreateSnapshot.Cleanup() --- builder/virtualbox/vm/step_create_snapshot.go | 31 ++++++++----------- 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/builder/virtualbox/vm/step_create_snapshot.go b/builder/virtualbox/vm/step_create_snapshot.go index 1ff4e13bd..95c676069 100644 --- a/builder/virtualbox/vm/step_create_snapshot.go +++ b/builder/virtualbox/vm/step_create_snapshot.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "log" - "time" vboxcommon "github.com/hashicorp/packer/builder/virtualbox/common" "github.com/hashicorp/packer/helper/multistep" @@ -20,7 +19,18 @@ func (s *StepCreateSnapshot) Run(_ context.Context, state multistep.StateBag) mu driver := state.Get("driver").(vboxcommon.Driver) ui := state.Get("ui").(packer.Ui) if s.TargetSnapshot != "" { - time.Sleep(10 * time.Second) // Wait after the Vm has been shutdown, otherwise creating the snapshot might make the VM unstartable + running, err := driver.IsRunning(s.Name) + if err != nil { + err = fmt.Errorf("Failed to test if VM %s is still running: %s", s.Name, err) + } else if running { + err = fmt.Errorf("VM %s is still running. Unable to create snapshot %s", s.Name, s.TargetSnapshot) + } + if err != nil { + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } + ui.Say(fmt.Sprintf("Creating snapshot %s on virtual machine %s", s.TargetSnapshot, s.Name)) snapshotTree, err := driver.LoadSnapshots(s.Name) if err != nil { @@ -56,19 +66,4 @@ func (s *StepCreateSnapshot) Run(_ context.Context, state multistep.StateBag) mu return multistep.ActionContinue } -func (s *StepCreateSnapshot) Cleanup(state multistep.StateBag) { - /* - driver := state.Get("driver").(vboxcommon.Driver) - if s.TargetSnapshot != "" { - ui := state.Get("ui").(packer.Ui) - ui.Say(fmt.Sprintf("Deleting snapshot %s on virtual machine %s", s.TargetSnapshot, s.Name)) - err := driver.DeleteSnapshot(s.Name, s.TargetSnapshot) - if err != nil { - err := fmt.Errorf("Error cleaning up created snaphot VM: %s", err) - state.Put("error", err) - ui.Error(err.Error()) - return - } - } - */ -} +func (s *StepCreateSnapshot) Cleanup(state multistep.StateBag) {}