Merge pull request #8511 from TJM/vsphere_template_markas

[post-processor/vsphere-template] Simplify method to use vm.MarkAsTemplate (optionally)
This commit is contained in:
Megan Marsh 2020-01-08 09:41:43 -08:00 committed by GitHub
commit 49a33c04cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 10 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" default:"true" 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,8 +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 {
state.Put("error", err) state.Put("error", err)
@ -51,42 +51,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
} }

View File

@ -76,6 +76,12 @@ Optional:
- `snapshot_description` (string) - Description for the snapshot. Required - `snapshot_description` (string) - Description for the snapshot. Required
when `snapshot_enable` is `true` when `snapshot_enable` is `true`
- `reregister_vm` (boolean) - Use the method of unregister VM and reregister
as a template, rather than using the markAsTemplate method in vmWare.
NOTE: If you are getting permission denied errors when trying to mark as a
template, but it works fine in the vSphere UI, try setting this to false.
Default is true.
## Using the vSphere Template with local builders ## Using the vSphere Template with local builders
Once the [vSphere](/docs/post-processors/vsphere.html) takes an artifact from Once the [vSphere](/docs/post-processors/vsphere.html) takes an artifact from