Use vm.MarkAsTemplate, optionally reregister
This commit is contained in:
parent
b7ee807cf1
commit
7f7b54ea31
|
@ -37,6 +37,7 @@ type Config struct {
|
|||
SnapshotEnable bool `mapstructure:"snapshot_enable"`
|
||||
SnapshotName string `mapstructure:"snapshot_name"`
|
||||
SnapshotDescription string `mapstructure:"snapshot_description"`
|
||||
ReregisterVM bool `mapstructure:"reregister_vm" default:true`
|
||||
|
||||
ctx interpolate.Context
|
||||
}
|
||||
|
@ -135,7 +136,7 @@ func (p *PostProcessor) PostProcess(ctx context.Context, ui packer.Ui, artifact
|
|||
Folder: p.config.Folder,
|
||||
},
|
||||
NewStepCreateSnapshot(artifact, p),
|
||||
NewStepMarkAsTemplate(artifact),
|
||||
NewStepMarkAsTemplate(artifact, p),
|
||||
}
|
||||
runner := common.NewRunnerWithPauseFn(steps, p.config.PackerConfig, ui, state)
|
||||
runner.Run(ctx, state)
|
||||
|
|
|
@ -25,6 +25,7 @@ type FlatConfig struct {
|
|||
SnapshotEnable *bool `mapstructure:"snapshot_enable" cty:"snapshot_enable"`
|
||||
SnapshotName *string `mapstructure:"snapshot_name" cty:"snapshot_name"`
|
||||
SnapshotDescription *string `mapstructure:"snapshot_description" cty:"snapshot_description"`
|
||||
ReregisterVM *bool `mapstructure:"reregister_vm" cty:"reregister_vm"`
|
||||
}
|
||||
|
||||
// FlatMapstructure returns a new FlatConfig.
|
||||
|
@ -55,6 +56,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec {
|
|||
"snapshot_enable": &hcldec.AttrSpec{Name: "snapshot_enable", Type: cty.Bool, Required: false},
|
||||
"snapshot_name": &hcldec.AttrSpec{Name: "snapshot_name", Type: cty.String, Required: false},
|
||||
"snapshot_description": &hcldec.AttrSpec{Name: "snapshot_description", Type: cty.String, Required: false},
|
||||
"reregister_vm": &hcldec.AttrSpec{Name: "reregister_vm", Type: cty.Bool, Required: false},
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
|
|
@ -18,9 +18,10 @@ import (
|
|||
type stepMarkAsTemplate struct {
|
||||
VMName string
|
||||
RemoteFolder string
|
||||
ReregisterVM bool
|
||||
}
|
||||
|
||||
func NewStepMarkAsTemplate(artifact packer.Artifact) *stepMarkAsTemplate {
|
||||
func NewStepMarkAsTemplate(artifact packer.Artifact, p *PostProcessor) *stepMarkAsTemplate {
|
||||
remoteFolder := "Discovered virtual machine"
|
||||
vmname := artifact.Id()
|
||||
|
||||
|
@ -33,6 +34,7 @@ func NewStepMarkAsTemplate(artifact packer.Artifact) *stepMarkAsTemplate {
|
|||
return &stepMarkAsTemplate{
|
||||
VMName: vmname,
|
||||
RemoteFolder: remoteFolder,
|
||||
ReregisterVM: p.config.ReregisterVM,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -42,7 +44,6 @@ func (s *stepMarkAsTemplate) Run(ctx context.Context, state multistep.StateBag)
|
|||
folder := state.Get("folder").(*object.Folder)
|
||||
dcPath := state.Get("dcPath").(string)
|
||||
|
||||
ui.Message("Marking as a template...")
|
||||
|
||||
vm, err := findRuntimeVM(cli, dcPath, s.VMName, s.RemoteFolder)
|
||||
if err != nil {
|
||||
|
@ -51,42 +52,56 @@ func (s *stepMarkAsTemplate) Run(ctx context.Context, state multistep.StateBag)
|
|||
return multistep.ActionHalt
|
||||
}
|
||||
|
||||
// Use a simple "MarkAsTemplate" method unless `reregister_vm` is true
|
||||
if !s.ReregisterVM {
|
||||
ui.Message("Marking as a template...")
|
||||
|
||||
if err := vm.MarkAsTemplate(context.Background()); err != nil {
|
||||
state.Put("error", err)
|
||||
ui.Error("vm.MarkAsTemplate:" + err.Error())
|
||||
return multistep.ActionHalt
|
||||
}
|
||||
return multistep.ActionContinue
|
||||
}
|
||||
|
||||
ui.Message("Re-register VM as a template...")
|
||||
|
||||
dsPath, err := datastorePath(vm)
|
||||
if err != nil {
|
||||
state.Put("error", err)
|
||||
ui.Error(err.Error())
|
||||
ui.Error("datastorePath:" + err.Error())
|
||||
return multistep.ActionHalt
|
||||
}
|
||||
|
||||
host, err := vm.HostSystem(context.Background())
|
||||
if err != nil {
|
||||
state.Put("error", err)
|
||||
ui.Error(err.Error())
|
||||
ui.Error("vm.HostSystem:" + err.Error())
|
||||
return multistep.ActionHalt
|
||||
}
|
||||
|
||||
if err := vm.Unregister(context.Background()); err != nil {
|
||||
state.Put("error", err)
|
||||
ui.Error(err.Error())
|
||||
ui.Error("vm.Unregister:" + err.Error())
|
||||
return multistep.ActionHalt
|
||||
}
|
||||
|
||||
if err := unregisterPreviousVM(cli, folder, s.VMName); err != nil {
|
||||
state.Put("error", err)
|
||||
ui.Error(err.Error())
|
||||
ui.Error("unregisterPreviousVM:" + err.Error())
|
||||
return multistep.ActionHalt
|
||||
}
|
||||
|
||||
task, err := folder.RegisterVM(context.Background(), dsPath.String(), s.VMName, true, nil, host)
|
||||
if err != nil {
|
||||
state.Put("error", err)
|
||||
ui.Error(err.Error())
|
||||
ui.Error("RegisterVM:" + err.Error())
|
||||
return multistep.ActionHalt
|
||||
}
|
||||
|
||||
if err = task.Wait(context.Background()); err != nil {
|
||||
state.Put("error", err)
|
||||
ui.Error(err.Error())
|
||||
ui.Error("task.Wait:" + err.Error())
|
||||
return multistep.ActionHalt
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue