2019-02-16 08:32:22 -05:00
|
|
|
package vm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2019-03-10 10:38:38 -04:00
|
|
|
"log"
|
2019-02-16 08:32:22 -05:00
|
|
|
|
|
|
|
vboxcommon "github.com/hashicorp/packer/builder/virtualbox/common"
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2020-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2019-02-16 08:32:22 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type StepCreateSnapshot struct {
|
|
|
|
Name string
|
|
|
|
TargetSnapshot string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepCreateSnapshot) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
|
|
|
driver := state.Get("driver").(vboxcommon.Driver)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2019-02-16 08:32:22 -05:00
|
|
|
if s.TargetSnapshot != "" {
|
2019-07-26 05:35:12 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-02-16 08:32:22 -05:00
|
|
|
ui.Say(fmt.Sprintf("Creating snapshot %s on virtual machine %s", s.TargetSnapshot, s.Name))
|
2019-03-17 15:50:13 -04:00
|
|
|
snapshotTree, err := driver.LoadSnapshots(s.Name)
|
2019-03-10 10:38:38 -04:00
|
|
|
if err != nil {
|
2019-03-17 15:50:13 -04:00
|
|
|
err = fmt.Errorf("Failed to load snapshots for VM %s: %s", s.Name, err)
|
2019-03-10 10:38:38 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
2019-03-17 15:50:13 -04:00
|
|
|
}
|
|
|
|
|
2020-06-17 05:04:45 -04:00
|
|
|
// Remove any snapshot with the target's name, if present.
|
|
|
|
if snapshotTree != nil {
|
|
|
|
currentSnapshot := snapshotTree.GetCurrentSnapshot()
|
|
|
|
targetSnapshot := currentSnapshot.GetChildWithName(s.TargetSnapshot)
|
|
|
|
if nil != targetSnapshot {
|
|
|
|
log.Printf("Deleting existing target snapshot %s", s.TargetSnapshot)
|
|
|
|
err = driver.DeleteSnapshot(s.Name, targetSnapshot)
|
|
|
|
if nil != err {
|
|
|
|
err = fmt.Errorf("Unable to delete snapshot %s from VM %s: %s", s.TargetSnapshot, s.Name, err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2019-03-10 10:38:38 -04:00
|
|
|
}
|
|
|
|
}
|
2020-06-17 05:04:45 -04:00
|
|
|
|
2019-03-10 10:38:38 -04:00
|
|
|
err = driver.CreateSnapshot(s.Name, s.TargetSnapshot)
|
2019-02-16 08:32:22 -05:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error creating snaphot VM: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ui.Say("No target snapshot defined...")
|
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2019-07-26 05:35:12 -04:00
|
|
|
func (s *StepCreateSnapshot) Cleanup(state multistep.StateBag) {}
|