Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remote hypervisor snapshot creation #228

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions builder/vmware/common/step_create_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,31 @@ import (
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
)

// StepCreateSnapshot step creates the intial snapshot for the VM after clean-up.
// StepCreateSnapshot step creates a snapshot for the virtual machine after the
// build has been completed.
type StepCreateSnapshot struct {
SnapshotName *string
}

func (s *StepCreateSnapshot) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
if *s.SnapshotName != "" { // if snapshot name is set create one, if not don't
driver := state.Get("driver").(Driver)
ui := state.Get("ui").(packersdk.Ui)

ui.Say("Creating inital snapshot")
vmFullPath := state.Get("vmx_path").(string)
if err := driver.CreateSnapshot(vmFullPath, *s.SnapshotName); err != nil {
err := fmt.Errorf("error creating snapshot: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
state.Put("snapshot_created", true)
} else {
// If snapshot name is not set, skip snapshot creation.
if *s.SnapshotName == "" {
state.Put("snapshot_skipped", true)
return multistep.ActionContinue
}

driver := state.Get("driver").(Driver)
ui := state.Get("ui").(packersdk.Ui)

ui.Say("Creating snapshot of virtual machine...")
vmFullPath := state.Get("vmx_path").(string)
if err := driver.CreateSnapshot(vmFullPath, *s.SnapshotName); err != nil {
err := fmt.Errorf("error creating snapshot: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
state.Put("snapshot_created", true)
return multistep.ActionContinue
}

Expand Down
15 changes: 7 additions & 8 deletions builder/vmware/common/step_create_snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,24 @@ func TestStepCreateSnapshot(t *testing.T) {
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
t.Fatalf("bad action: %#v", action)
}

if _, ok := state.GetOk("error"); ok {
t.Fatal("should NOT have error")
t.Fatal("should not error")
}

driver := state.Get("driver").(*DriverMock)
if !driver.CreateSnapshotCalled {
t.Fatalf("Should have called create snapshot.")
t.Fatalf("should call create snapshot")
}

if _, ok := state.GetOk("snapshot_skiped"); ok {
t.Fatalf("Should NOT skip snapshot creation")
if _, ok := state.GetOk("snapshot_skipped"); ok {
t.Fatalf("should not skip snapshot")
}

if _, ok := state.GetOk("snapshot_created"); !ok {
t.Fatalf("Should create snapshot")
}

// Cleanup
step.Cleanup(state)
}

Expand All @@ -61,14 +61,13 @@ func TestStepCreateSnapshot_skip(t *testing.T) {
t.Fatalf("bad action: %#v", action)
}
if _, ok := state.GetOk("error"); ok {
t.Fatal("should NOT have error")
t.Fatal("should not error")
}

driver := state.Get("driver").(*DriverMock)
if driver.CreateSnapshotCalled {
t.Fatalf("Should NOT have called create snapshot.")
t.Fatalf("should not call create snapshot")
}

// Cleanup
step.Cleanup(state)
}
6 changes: 3 additions & 3 deletions builder/vmware/iso/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,12 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
RemoveEthernetInterfaces: b.config.VMXConfig.VMXRemoveEthernet,
VNCEnabled: !b.config.DisableVNC,
},
&vmwcommon.StepCreateSnapshot{
SnapshotName: &b.config.SnapshotName,
},
&vmwcommon.StepUploadVMX{
RemoteType: b.config.RemoteType,
},
&vmwcommon.StepCreateSnapshot{
SnapshotName: &b.config.SnapshotName,
},
&vmwcommon.StepExport{
Format: b.config.Format,
SkipExport: b.config.SkipExport,
Expand Down
6 changes: 3 additions & 3 deletions builder/vmware/vmx/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,12 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
RemoveEthernetInterfaces: b.config.VMXConfig.VMXRemoveEthernet,
VNCEnabled: !b.config.DisableVNC,
},
&vmwcommon.StepCreateSnapshot{
SnapshotName: &b.config.SnapshotName,
},
&vmwcommon.StepUploadVMX{
RemoteType: b.config.RemoteType,
},
&vmwcommon.StepCreateSnapshot{
SnapshotName: &b.config.SnapshotName,
},
&vmwcommon.StepExport{
Format: b.config.Format,
SkipExport: b.config.SkipExport,
Expand Down