Added Creation of Snapshot to vSphere Template
This commit is contained in:
parent
254e18ece9
commit
7f7fee3e2b
|
@ -31,6 +31,11 @@ type Config struct {
|
||||||
Password string `mapstructure:"password"`
|
Password string `mapstructure:"password"`
|
||||||
Datacenter string `mapstructure:"datacenter"`
|
Datacenter string `mapstructure:"datacenter"`
|
||||||
Folder string `mapstructure:"folder"`
|
Folder string `mapstructure:"folder"`
|
||||||
|
SnapshotEnable bool `mapstructure:"snapshot_enable"`
|
||||||
|
SnapshotName string `mapstructure:"snapshot_name"`
|
||||||
|
SnapshotDescription string `mapstructure:"snapshot_description"`
|
||||||
|
SnapshotMemory bool `mapstructure:"snapshot_memory"`
|
||||||
|
SnapshotQuiesce bool `mapstructure:"snapshot_quiesce"`
|
||||||
|
|
||||||
ctx interpolate.Context
|
ctx interpolate.Context
|
||||||
}
|
}
|
||||||
|
@ -126,6 +131,7 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac
|
||||||
&stepCreateFolder{
|
&stepCreateFolder{
|
||||||
Folder: p.config.Folder,
|
Folder: p.config.Folder,
|
||||||
},
|
},
|
||||||
|
NewStepCreateSnapshot(artifact, p),
|
||||||
NewStepMarkAsTemplate(artifact),
|
NewStepMarkAsTemplate(artifact),
|
||||||
}
|
}
|
||||||
runner := common.NewRunnerWithPauseFn(steps, p.config.PackerConfig, ui, state)
|
runner := common.NewRunnerWithPauseFn(steps, p.config.PackerConfig, ui, state)
|
||||||
|
|
|
@ -0,0 +1,81 @@
|
||||||
|
package vsphere_template
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/hashicorp/packer/helper/multistep"
|
||||||
|
"github.com/hashicorp/packer/packer"
|
||||||
|
"github.com/hashicorp/packer/post-processor/vsphere"
|
||||||
|
"github.com/vmware/govmomi"
|
||||||
|
)
|
||||||
|
|
||||||
|
type stepCreateSnapshot struct {
|
||||||
|
VMName string
|
||||||
|
RemoteFolder string
|
||||||
|
SnapshotName string
|
||||||
|
SnapshotDescription string
|
||||||
|
SnapshotMemory bool
|
||||||
|
SnapshotQuiesce bool
|
||||||
|
SnapshotEnable bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewStepCreateSnapshot(artifact packer.Artifact, p *PostProcessor) *stepCreateSnapshot {
|
||||||
|
remoteFolder := "Discovered virtual machine"
|
||||||
|
vmname := artifact.Id()
|
||||||
|
|
||||||
|
if artifact.BuilderId() == vsphere.BuilderId {
|
||||||
|
id := strings.Split(artifact.Id(), "::")
|
||||||
|
remoteFolder = id[1]
|
||||||
|
vmname = id[2]
|
||||||
|
}
|
||||||
|
|
||||||
|
return &stepCreateSnapshot{
|
||||||
|
VMName: vmname,
|
||||||
|
RemoteFolder: remoteFolder,
|
||||||
|
SnapshotEnable: p.config.SnapshotEnable,
|
||||||
|
SnapshotName: p.config.SnapshotName,
|
||||||
|
SnapshotDescription: p.config.SnapshotDescription,
|
||||||
|
SnapshotMemory: p.config.SnapshotMemory,
|
||||||
|
SnapshotQuiesce: p.config.SnapshotQuiesce,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *stepCreateSnapshot) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
||||||
|
ui := state.Get("ui").(packer.Ui)
|
||||||
|
cli := state.Get("client").(*govmomi.Client)
|
||||||
|
dcPath := state.Get("dcPath").(string)
|
||||||
|
|
||||||
|
if !s.SnapshotEnable {
|
||||||
|
ui.Message("Snapshot Not Enabled, Continue...")
|
||||||
|
return multistep.ActionContinue
|
||||||
|
}
|
||||||
|
|
||||||
|
ui.Message("Creating a Snapshot...")
|
||||||
|
|
||||||
|
vm, err := findRuntimeVM(cli, dcPath, s.VMName, s.RemoteFolder)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
state.Put("error", err)
|
||||||
|
ui.Error(err.Error())
|
||||||
|
return multistep.ActionHalt
|
||||||
|
}
|
||||||
|
|
||||||
|
task, err := vm.CreateSnapshot(context.Background(), s.SnapshotName, s.SnapshotDescription, s.SnapshotMemory, s.SnapshotQuiesce)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
state.Put("error", err)
|
||||||
|
ui.Error(err.Error())
|
||||||
|
return multistep.ActionHalt
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = task.Wait(context.Background()); err != nil {
|
||||||
|
state.Put("error", err)
|
||||||
|
ui.Error(err.Error())
|
||||||
|
return multistep.ActionHalt
|
||||||
|
}
|
||||||
|
|
||||||
|
return multistep.ActionContinue
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *stepCreateSnapshot) Cleanup(multistep.StateBag) {}
|
Loading…
Reference in New Issue