From 006343cd1f867637b07fdd2a4c7ee0020147a83b Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Mon, 18 Oct 2021 17:12:39 -0700 Subject: [PATCH] Source image (#120) * add source image id to artifact * tinkering with partifact --- builder/vsphere/clone/builder.go | 5 +++-- builder/vsphere/clone/step_clone.go | 8 +++++--- builder/vsphere/common/artifact.go | 19 +++++++++++++++++++ builder/vsphere/common/step_download.go | 1 + builder/vsphere/iso/builder.go | 2 ++ builder/vsphere/iso/step_create.go | 8 +++++--- 6 files changed, 35 insertions(+), 8 deletions(-) diff --git a/builder/vsphere/clone/builder.go b/builder/vsphere/clone/builder.go index 5b439a93..ebc05874 100644 --- a/builder/vsphere/clone/builder.go +++ b/builder/vsphere/clone/builder.go @@ -173,8 +173,9 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) ContentLibraryConfig: b.config.ContentLibraryDestinationConfig, VM: vm, StateData: map[string]interface{}{ - "generated_data": state.Get("generated_data"), - "metadata": state.Get("metadata"), + "generated_data": state.Get("generated_data"), + "metadata": state.Get("metadata"), + "source_template": b.config.Template, }, } if b.config.Export != nil { diff --git a/builder/vsphere/clone/step_clone.go b/builder/vsphere/clone/step_clone.go index 875915d3..2992d314 100644 --- a/builder/vsphere/clone/step_clone.go +++ b/builder/vsphere/clone/step_clone.go @@ -10,6 +10,7 @@ import ( "github.com/hashicorp/packer-plugin-sdk/multistep" packersdk "github.com/hashicorp/packer-plugin-sdk/packer" + "github.com/hashicorp/packer-plugin-sdk/packerbuilderdata" "github.com/hashicorp/packer-plugin-vsphere/builder/vsphere/common" "github.com/hashicorp/packer-plugin-vsphere/builder/vsphere/driver" ) @@ -68,9 +69,10 @@ func (c *CloneConfig) Prepare() []error { } type StepCloneVM struct { - Config *CloneConfig - Location *common.LocationConfig - Force bool + Config *CloneConfig + Location *common.LocationConfig + Force bool + GeneratedData *packerbuilderdata.GeneratedData } func (s *StepCloneVM) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { diff --git a/builder/vsphere/common/artifact.go b/builder/vsphere/common/artifact.go index c766605d..eca36020 100644 --- a/builder/vsphere/common/artifact.go +++ b/builder/vsphere/common/artifact.go @@ -73,13 +73,32 @@ func (a *Artifact) stateHCPPackerRegistryMetadata() interface{} { if a.ContentLibraryConfig != nil { labels["content_library_destination"] = fmt.Sprintf("%s/%s", a.ContentLibraryConfig.Library, a.ContentLibraryConfig.Name) } + // this is where the iso was downloaded from + sourceURL, ok := a.StateData["SourceImageURL"].(string) + if ok { + labels["source_image_url"] = sourceURL + } + // This is where the iso was uploaded to on the remote vsphere datastore + var sourceID string + isoPath, ok := a.StateData["iso_path"].(string) + if ok { + sourceID = isoPath + } + + // If a clone, the source comes from a different place. + templatePath, ok := a.StateData["source_template"].(string) + if ok { + sourceID = templatePath + } img, _ := registryimage.FromArtifact(a, registryimage.WithID(a.Name), registryimage.WithRegion(a.Datacenter.Name()), registryimage.WithProvider("vsphere"), + registryimage.WithSourceID(sourceID), registryimage.SetLabels(labels), ) + return img } diff --git a/builder/vsphere/common/step_download.go b/builder/vsphere/common/step_download.go index 978e21a6..28d65f63 100644 --- a/builder/vsphere/common/step_download.go +++ b/builder/vsphere/common/step_download.go @@ -55,6 +55,7 @@ func (s *StepDownload) Run(ctx context.Context, state multistep.StateBag) multis if exists := ds.FileExists(remotePath); exists { ui.Say(fmt.Sprintf("File %s already uploaded; continuing", targetPath)) state.Put(s.ResultKey, targetPath) + state.Put("SourceImageURL", source) return multistep.ActionContinue } } diff --git a/builder/vsphere/iso/builder.go b/builder/vsphere/iso/builder.go index 3c724ffd..0956c4d4 100644 --- a/builder/vsphere/iso/builder.go +++ b/builder/vsphere/iso/builder.go @@ -178,6 +178,8 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) StateData: map[string]interface{}{ "generated_data": state.Get("generated_data"), "metadata": state.Get("metadata"), + "SourceImageURL": state.Get("SourceImageURL"), + "iso_path": state.Get("iso_path"), }, } diff --git a/builder/vsphere/iso/step_create.go b/builder/vsphere/iso/step_create.go index faf3611b..17dcc266 100644 --- a/builder/vsphere/iso/step_create.go +++ b/builder/vsphere/iso/step_create.go @@ -10,6 +10,7 @@ import ( "github.com/hashicorp/packer-plugin-sdk/multistep" packersdk "github.com/hashicorp/packer-plugin-sdk/packer" + "github.com/hashicorp/packer-plugin-sdk/packerbuilderdata" "github.com/hashicorp/packer-plugin-vsphere/builder/vsphere/common" "github.com/hashicorp/packer-plugin-vsphere/builder/vsphere/driver" ) @@ -117,9 +118,10 @@ func (c *CreateConfig) Prepare() []error { } type StepCreateVM struct { - Config *CreateConfig - Location *common.LocationConfig - Force bool + Config *CreateConfig + Location *common.LocationConfig + Force bool + GeneratedData *packerbuilderdata.GeneratedData } func (s *StepCreateVM) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {