Skip to content

Commit

Permalink
Source image (#120)
Browse files Browse the repository at this point in the history
* add source image id to artifact

* tinkering with partifact
  • Loading branch information
SwampDragons authored Oct 19, 2021
1 parent 558edf1 commit 89804d6
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 8 deletions.
5 changes: 3 additions & 2 deletions builder/vsphere/clone/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 5 additions & 3 deletions builder/vsphere/clone/step_clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 {
Expand Down
19 changes: 19 additions & 0 deletions builder/vsphere/common/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
1 change: 1 addition & 0 deletions builder/vsphere/common/step_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
2 changes: 2 additions & 0 deletions builder/vsphere/iso/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
},
}

Expand Down
8 changes: 5 additions & 3 deletions builder/vsphere/iso/step_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 89804d6

Please sign in to comment.