Use vm.MarkAsTemplate, optionally reregister

This commit is contained in:
Tommy McNeely 2019-12-26 08:41:55 -07:00
parent b7ee807cf1
commit 7f7b54ea31
3 changed files with 27 additions and 9 deletions

View File

@ -37,6 +37,7 @@ type Config struct {
SnapshotEnable bool `mapstructure:"snapshot_enable"` SnapshotEnable bool `mapstructure:"snapshot_enable"`
SnapshotName string `mapstructure:"snapshot_name"` SnapshotName string `mapstructure:"snapshot_name"`
SnapshotDescription string `mapstructure:"snapshot_description"` SnapshotDescription string `mapstructure:"snapshot_description"`
ReregisterVM bool `mapstructure:"reregister_vm" default:true`
ctx interpolate.Context ctx interpolate.Context
} }
@ -135,7 +136,7 @@ func (p *PostProcessor) PostProcess(ctx context.Context, ui packer.Ui, artifact
Folder: p.config.Folder, Folder: p.config.Folder,
}, },
NewStepCreateSnapshot(artifact, p), NewStepCreateSnapshot(artifact, p),
NewStepMarkAsTemplate(artifact), NewStepMarkAsTemplate(artifact, p),
} }
runner := common.NewRunnerWithPauseFn(steps, p.config.PackerConfig, ui, state) runner := common.NewRunnerWithPauseFn(steps, p.config.PackerConfig, ui, state)
runner.Run(ctx, state) runner.Run(ctx, state)

View File

@ -25,6 +25,7 @@ type FlatConfig struct {
SnapshotEnable *bool `mapstructure:"snapshot_enable" cty:"snapshot_enable"` SnapshotEnable *bool `mapstructure:"snapshot_enable" cty:"snapshot_enable"`
SnapshotName *string `mapstructure:"snapshot_name" cty:"snapshot_name"` SnapshotName *string `mapstructure:"snapshot_name" cty:"snapshot_name"`
SnapshotDescription *string `mapstructure:"snapshot_description" cty:"snapshot_description"` SnapshotDescription *string `mapstructure:"snapshot_description" cty:"snapshot_description"`
ReregisterVM *bool `mapstructure:"reregister_vm" cty:"reregister_vm"`
} }
// FlatMapstructure returns a new FlatConfig. // 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_enable": &hcldec.AttrSpec{Name: "snapshot_enable", Type: cty.Bool, Required: false},
"snapshot_name": &hcldec.AttrSpec{Name: "snapshot_name", Type: cty.String, 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}, "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 return s
} }

View File

@ -18,9 +18,10 @@ import (
type stepMarkAsTemplate struct { type stepMarkAsTemplate struct {
VMName string VMName string
RemoteFolder string RemoteFolder string
ReregisterVM bool
} }
func NewStepMarkAsTemplate(artifact packer.Artifact) *stepMarkAsTemplate { func NewStepMarkAsTemplate(artifact packer.Artifact, p *PostProcessor) *stepMarkAsTemplate {
remoteFolder := "Discovered virtual machine" remoteFolder := "Discovered virtual machine"
vmname := artifact.Id() vmname := artifact.Id()
@ -33,6 +34,7 @@ func NewStepMarkAsTemplate(artifact packer.Artifact) *stepMarkAsTemplate {
return &stepMarkAsTemplate{ return &stepMarkAsTemplate{
VMName: vmname, VMName: vmname,
RemoteFolder: remoteFolder, 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) folder := state.Get("folder").(*object.Folder)
dcPath := state.Get("dcPath").(string) dcPath := state.Get("dcPath").(string)
ui.Message("Marking as a template...")
vm, err := findRuntimeVM(cli, dcPath, s.VMName, s.RemoteFolder) vm, err := findRuntimeVM(cli, dcPath, s.VMName, s.RemoteFolder)
if err != nil { if err != nil {
@ -51,42 +52,56 @@ func (s *stepMarkAsTemplate) Run(ctx context.Context, state multistep.StateBag)
return multistep.ActionHalt 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) dsPath, err := datastorePath(vm)
if err != nil { if err != nil {
state.Put("error", err) state.Put("error", err)
ui.Error(err.Error()) ui.Error("datastorePath:" + err.Error())
return multistep.ActionHalt return multistep.ActionHalt
} }
host, err := vm.HostSystem(context.Background()) host, err := vm.HostSystem(context.Background())
if err != nil { if err != nil {
state.Put("error", err) state.Put("error", err)
ui.Error(err.Error()) ui.Error("vm.HostSystem:" + err.Error())
return multistep.ActionHalt return multistep.ActionHalt
} }
if err := vm.Unregister(context.Background()); err != nil { if err := vm.Unregister(context.Background()); err != nil {
state.Put("error", err) state.Put("error", err)
ui.Error(err.Error()) ui.Error("vm.Unregister:" + err.Error())
return multistep.ActionHalt return multistep.ActionHalt
} }
if err := unregisterPreviousVM(cli, folder, s.VMName); err != nil { if err := unregisterPreviousVM(cli, folder, s.VMName); err != nil {
state.Put("error", err) state.Put("error", err)
ui.Error(err.Error()) ui.Error("unregisterPreviousVM:" + err.Error())
return multistep.ActionHalt return multistep.ActionHalt
} }
task, err := folder.RegisterVM(context.Background(), dsPath.String(), s.VMName, true, nil, host) task, err := folder.RegisterVM(context.Background(), dsPath.String(), s.VMName, true, nil, host)
if err != nil { if err != nil {
state.Put("error", err) state.Put("error", err)
ui.Error(err.Error()) ui.Error("RegisterVM:" + err.Error())
return multistep.ActionHalt return multistep.ActionHalt
} }
if err = task.Wait(context.Background()); err != nil { if err = task.Wait(context.Background()); err != nil {
state.Put("error", err) state.Put("error", err)
ui.Error(err.Error()) ui.Error("task.Wait:" + err.Error())
return multistep.ActionHalt return multistep.ActionHalt
} }