From 7f7fee3e2b98635b5836e8996ec576ffca0c2789 Mon Sep 17 00:00:00 2001 From: Julio Tain Sueiras Date: Wed, 7 Nov 2018 23:59:12 -0500 Subject: [PATCH 1/3] Added Creation of Snapshot to vSphere Template --- .../vsphere-template/post-processor.go | 6 ++ .../vsphere-template/step_create_snapshot.go | 81 +++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 post-processor/vsphere-template/step_create_snapshot.go diff --git a/post-processor/vsphere-template/post-processor.go b/post-processor/vsphere-template/post-processor.go index d6b4fa769..a281e33fb 100644 --- a/post-processor/vsphere-template/post-processor.go +++ b/post-processor/vsphere-template/post-processor.go @@ -31,6 +31,11 @@ type Config struct { Password string `mapstructure:"password"` Datacenter string `mapstructure:"datacenter"` 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 } @@ -126,6 +131,7 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac &stepCreateFolder{ Folder: p.config.Folder, }, + NewStepCreateSnapshot(artifact, p), NewStepMarkAsTemplate(artifact), } runner := common.NewRunnerWithPauseFn(steps, p.config.PackerConfig, ui, state) diff --git a/post-processor/vsphere-template/step_create_snapshot.go b/post-processor/vsphere-template/step_create_snapshot.go new file mode 100644 index 000000000..e4db06c95 --- /dev/null +++ b/post-processor/vsphere-template/step_create_snapshot.go @@ -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) {} From f83b64e603c251d58fbd584d1af09836a1da0800 Mon Sep 17 00:00:00 2001 From: Julio Sueiras Date: Sat, 10 Nov 2018 21:14:55 -0500 Subject: [PATCH 2/3] Update docs for vsphere-template --- .../post-processors/vsphere-template.html.md | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/website/source/docs/post-processors/vsphere-template.html.md b/website/source/docs/post-processors/vsphere-template.html.md index 6f3d6f203..d2b257d58 100644 --- a/website/source/docs/post-processors/vsphere-template.html.md +++ b/website/source/docs/post-processors/vsphere-template.html.md @@ -60,7 +60,22 @@ Optional: - `insecure` (boolean) - If it's true skip verification of server certificate. Default is false - + +- `snapshot_enable` (boolean) - Enable snapshot creation before marking as a + template. Default is false + +- `snapshot_name` (string) - Name for the snapshot. + Require when `snapshot_enable` is enabled + +- `snapshot_description` (string) - Description for the snapshot. + Require when `snapshot_enable` is enabled + +- `snapshot_memory` (boolean) - Enable memory option for the snapshot. + Default is false + +- `snapshot_quiesce` (boolean) - Enable quiesce option for the snapshot. + Default is false + ## Using the vSphere Template with local builders Once the [vSphere](/docs/post-processors/vsphere.html) takes an artifact from From 51053efad2bdc66800c3a37abf6f1a95b96fc867 Mon Sep 17 00:00:00 2001 From: Julio Tain Sueiras Date: Wed, 28 Nov 2018 11:25:01 -0500 Subject: [PATCH 3/3] Docs changes & Removed memory and quiesce options from vsphere-template --- post-processor/vsphere-template/post-processor.go | 2 -- .../vsphere-template/step_create_snapshot.go | 7 +------ .../docs/post-processors/vsphere-template.html.md | 12 +++--------- 3 files changed, 4 insertions(+), 17 deletions(-) diff --git a/post-processor/vsphere-template/post-processor.go b/post-processor/vsphere-template/post-processor.go index a281e33fb..9f0b7f271 100644 --- a/post-processor/vsphere-template/post-processor.go +++ b/post-processor/vsphere-template/post-processor.go @@ -34,8 +34,6 @@ type Config struct { 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 } diff --git a/post-processor/vsphere-template/step_create_snapshot.go b/post-processor/vsphere-template/step_create_snapshot.go index e4db06c95..c7fd73e15 100644 --- a/post-processor/vsphere-template/step_create_snapshot.go +++ b/post-processor/vsphere-template/step_create_snapshot.go @@ -15,8 +15,6 @@ type stepCreateSnapshot struct { RemoteFolder string SnapshotName string SnapshotDescription string - SnapshotMemory bool - SnapshotQuiesce bool SnapshotEnable bool } @@ -36,8 +34,6 @@ func NewStepCreateSnapshot(artifact packer.Artifact, p *PostProcessor) *stepCrea SnapshotEnable: p.config.SnapshotEnable, SnapshotName: p.config.SnapshotName, SnapshotDescription: p.config.SnapshotDescription, - SnapshotMemory: p.config.SnapshotMemory, - SnapshotQuiesce: p.config.SnapshotQuiesce, } } @@ -47,7 +43,6 @@ func (s *stepCreateSnapshot) Run(_ context.Context, state multistep.StateBag) mu dcPath := state.Get("dcPath").(string) if !s.SnapshotEnable { - ui.Message("Snapshot Not Enabled, Continue...") return multistep.ActionContinue } @@ -61,7 +56,7 @@ func (s *stepCreateSnapshot) Run(_ context.Context, state multistep.StateBag) mu return multistep.ActionHalt } - task, err := vm.CreateSnapshot(context.Background(), s.SnapshotName, s.SnapshotDescription, s.SnapshotMemory, s.SnapshotQuiesce) + task, err := vm.CreateSnapshot(context.Background(), s.SnapshotName, s.SnapshotDescription, false, false) if err != nil { state.Put("error", err) diff --git a/website/source/docs/post-processors/vsphere-template.html.md b/website/source/docs/post-processors/vsphere-template.html.md index d2b257d58..9057aca14 100644 --- a/website/source/docs/post-processors/vsphere-template.html.md +++ b/website/source/docs/post-processors/vsphere-template.html.md @@ -61,20 +61,14 @@ Optional: - `insecure` (boolean) - If it's true skip verification of server certificate. Default is false -- `snapshot_enable` (boolean) - Enable snapshot creation before marking as a +- `snapshot_enable` (boolean) - Create a snapshot before marking as a template. Default is false - `snapshot_name` (string) - Name for the snapshot. - Require when `snapshot_enable` is enabled + Required when `snapshot_enable` is `true` - `snapshot_description` (string) - Description for the snapshot. - Require when `snapshot_enable` is enabled - -- `snapshot_memory` (boolean) - Enable memory option for the snapshot. - Default is false - -- `snapshot_quiesce` (boolean) - Enable quiesce option for the snapshot. - Default is false + Required when `snapshot_enable` is `true` ## Using the vSphere Template with local builders