From 43714049e86d8d1955332d3ab3b7b2bb82542809 Mon Sep 17 00:00:00 2001 From: Joshua Foster Date: Tue, 5 May 2020 14:34:15 -0400 Subject: [PATCH] add set_host_for_datastore_uploads flag --- builder/vsphere/common/config_location.go | 3 +++ .../common/config_location.hcl2spec.go | 26 ++++++++++--------- builder/vsphere/driver/datastore.go | 11 +++++++- builder/vsphere/driver/datastore_acc_test.go | 4 +-- builder/vsphere/iso/builder.go | 12 +++++---- builder/vsphere/iso/step_add_floppy.go | 9 ++++--- builder/vsphere/iso/step_remote_upload.go | 7 ++--- .../common/LocationConfig-not-required.mdx | 3 +++ 8 files changed, 48 insertions(+), 27 deletions(-) diff --git a/builder/vsphere/common/config_location.go b/builder/vsphere/common/config_location.go index 248bc7e1c..1eda68ac7 100644 --- a/builder/vsphere/common/config_location.go +++ b/builder/vsphere/common/config_location.go @@ -23,6 +23,9 @@ type LocationConfig struct { // VMWare datastore. Required if `host` is a cluster, or if `host` has // multiple datastores. Datastore string `mapstructure:"datastore"` + // Set this to true if packer should the host for uploading files + // to the datastore. Defaults to false. + SetHostForDatastoreUploads bool `mapstructure:"set_host_for_datastore_uploads"` } func (c *LocationConfig) Prepare() []error { diff --git a/builder/vsphere/common/config_location.hcl2spec.go b/builder/vsphere/common/config_location.hcl2spec.go index be5b02f03..7c2139909 100644 --- a/builder/vsphere/common/config_location.hcl2spec.go +++ b/builder/vsphere/common/config_location.hcl2spec.go @@ -9,12 +9,13 @@ import ( // FlatLocationConfig is an auto-generated flat version of LocationConfig. // Where the contents of a field with a `mapstructure:,squash` tag are bubbled up. type FlatLocationConfig struct { - VMName *string `mapstructure:"vm_name" cty:"vm_name"` - Folder *string `mapstructure:"folder" cty:"folder"` - Cluster *string `mapstructure:"cluster" cty:"cluster"` - Host *string `mapstructure:"host" cty:"host"` - ResourcePool *string `mapstructure:"resource_pool" cty:"resource_pool"` - Datastore *string `mapstructure:"datastore" cty:"datastore"` + VMName *string `mapstructure:"vm_name" cty:"vm_name"` + Folder *string `mapstructure:"folder" cty:"folder"` + Cluster *string `mapstructure:"cluster" cty:"cluster"` + Host *string `mapstructure:"host" cty:"host"` + ResourcePool *string `mapstructure:"resource_pool" cty:"resource_pool"` + Datastore *string `mapstructure:"datastore" cty:"datastore"` + SetHostForDatastoreUploads *bool `mapstructure:"set_host_for_datastore_uploads" cty:"set_host_for_datastore_uploads"` } // FlatMapstructure returns a new FlatLocationConfig. @@ -29,12 +30,13 @@ func (*LocationConfig) FlatMapstructure() interface{ HCL2Spec() map[string]hclde // The decoded values from this spec will then be applied to a FlatLocationConfig. func (*FlatLocationConfig) HCL2Spec() map[string]hcldec.Spec { s := map[string]hcldec.Spec{ - "vm_name": &hcldec.AttrSpec{Name: "vm_name", Type: cty.String, Required: false}, - "folder": &hcldec.AttrSpec{Name: "folder", Type: cty.String, Required: false}, - "cluster": &hcldec.AttrSpec{Name: "cluster", Type: cty.String, Required: false}, - "host": &hcldec.AttrSpec{Name: "host", Type: cty.String, Required: false}, - "resource_pool": &hcldec.AttrSpec{Name: "resource_pool", Type: cty.String, Required: false}, - "datastore": &hcldec.AttrSpec{Name: "datastore", Type: cty.String, Required: false}, + "vm_name": &hcldec.AttrSpec{Name: "vm_name", Type: cty.String, Required: false}, + "folder": &hcldec.AttrSpec{Name: "folder", Type: cty.String, Required: false}, + "cluster": &hcldec.AttrSpec{Name: "cluster", Type: cty.String, Required: false}, + "host": &hcldec.AttrSpec{Name: "host", Type: cty.String, Required: false}, + "resource_pool": &hcldec.AttrSpec{Name: "resource_pool", Type: cty.String, Required: false}, + "datastore": &hcldec.AttrSpec{Name: "datastore", Type: cty.String, Required: false}, + "set_host_for_datastore_uploads": &hcldec.AttrSpec{Name: "set_host_for_datastore_uploads", Type: cty.Bool, Required: false}, } return s } diff --git a/builder/vsphere/driver/datastore.go b/builder/vsphere/driver/datastore.go index 12cf5ff6e..d6400bb58 100644 --- a/builder/vsphere/driver/datastore.go +++ b/builder/vsphere/driver/datastore.go @@ -85,9 +85,18 @@ func (ds *Datastore) ResolvePath(path string) string { return ds.ds.Path(path) } -func (ds *Datastore) UploadFile(src, dst string, host string) error { +func (ds *Datastore) UploadFile(src, dst, host string, set_host_for_datastore_uploads bool) error { p := soap.DefaultUpload ctx := ds.driver.ctx + + if set_host_for_datastore_uploads && host != "" { + h, err := ds.driver.FindHost(host) + if err != nil { + return err + } + ctx = ds.ds.HostContext(ctx, h.host) + } + return ds.ds.UploadFile(ctx, src, dst, &p) } diff --git a/builder/vsphere/driver/datastore_acc_test.go b/builder/vsphere/driver/datastore_acc_test.go index a45b2ffb4..45cc4e76e 100644 --- a/builder/vsphere/driver/datastore_acc_test.go +++ b/builder/vsphere/driver/datastore_acc_test.go @@ -44,7 +44,7 @@ func TestFileUpload(t *testing.T) { t.Fatalf("Cannot find datastore '%v': %v", dsName, err) } - err = ds.UploadFile(tmpFile.Name(), fileName, hostName) + err = ds.UploadFile(tmpFile.Name(), fileName, hostName, true) if err != nil { t.Fatalf("Cannot upload file: %v", err) } @@ -80,7 +80,7 @@ func TestFileUploadDRS(t *testing.T) { t.Fatalf("Cannot find datastore '%v': %v", dsName, err) } - err = ds.UploadFile(tmpFile.Name(), fileName, hostName) + err = ds.UploadFile(tmpFile.Name(), fileName, hostName, false) if err != nil { t.Fatalf("Cannot upload file: %v", err) } diff --git a/builder/vsphere/iso/builder.go b/builder/vsphere/iso/builder.go index d18709f3f..e0b2fffab 100644 --- a/builder/vsphere/iso/builder.go +++ b/builder/vsphere/iso/builder.go @@ -53,8 +53,9 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack Url: b.config.ISOUrls, }, &StepRemoteUpload{ - Datastore: b.config.Datastore, - Host: b.config.Host, + Datastore: b.config.Datastore, + Host: b.config.Host, + SetHostForDatastoreUploads: b.config.SetHostForDatastoreUploads, }, ) } @@ -84,9 +85,10 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack Label: b.config.FloppyLabel, }, &StepAddFloppy{ - Config: &b.config.FloppyConfig, - Datastore: b.config.Datastore, - Host: b.config.Host, + Config: &b.config.FloppyConfig, + Datastore: b.config.Datastore, + Host: b.config.Host, + SetHostForDatastoreUploads: b.config.SetHostForDatastoreUploads, }, &packerCommon.StepHTTPServer{ HTTPDir: b.config.HTTPDir, diff --git a/builder/vsphere/iso/step_add_floppy.go b/builder/vsphere/iso/step_add_floppy.go index 9a8e34a7a..722993236 100644 --- a/builder/vsphere/iso/step_add_floppy.go +++ b/builder/vsphere/iso/step_add_floppy.go @@ -29,9 +29,10 @@ type FloppyConfig struct { } type StepAddFloppy struct { - Config *FloppyConfig - Datastore string - Host string + Config *FloppyConfig + Datastore string + Host string + SetHostForDatastoreUploads bool } func (s *StepAddFloppy) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { @@ -54,7 +55,7 @@ func (s *StepAddFloppy) Run(_ context.Context, state multistep.StateBag) multist } uploadPath := fmt.Sprintf("%v/packer-tmp-created-floppy.flp", vmDir) - if err := ds.UploadFile(floppyPath.(string), uploadPath, s.Host); err != nil { + if err := ds.UploadFile(floppyPath.(string), uploadPath, s.Host, s.SetHostForDatastoreUploads); err != nil { state.Put("error", err) return multistep.ActionHalt } diff --git a/builder/vsphere/iso/step_remote_upload.go b/builder/vsphere/iso/step_remote_upload.go index 9eca285dc..9b0eeaeb0 100644 --- a/builder/vsphere/iso/step_remote_upload.go +++ b/builder/vsphere/iso/step_remote_upload.go @@ -11,8 +11,9 @@ import ( ) type StepRemoteUpload struct { - Datastore string - Host string + Datastore string + Host string + SetHostForDatastoreUploads bool } func (s *StepRemoteUpload) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { @@ -45,7 +46,7 @@ func (s *StepRemoteUpload) Run(_ context.Context, state multistep.StateBag) mult return multistep.ActionHalt } - if err := ds.UploadFile(path.(string), remotePath, s.Host); err != nil { + if err := ds.UploadFile(path.(string), remotePath, s.Host, s.SetHostForDatastoreUploads); err != nil { state.Put("error", err) return multistep.ActionHalt } diff --git a/website/pages/partials/builder/vsphere/common/LocationConfig-not-required.mdx b/website/pages/partials/builder/vsphere/common/LocationConfig-not-required.mdx index 40695b1a6..4a176822f 100644 --- a/website/pages/partials/builder/vsphere/common/LocationConfig-not-required.mdx +++ b/website/pages/partials/builder/vsphere/common/LocationConfig-not-required.mdx @@ -16,4 +16,7 @@ - `datastore` (string) - VMWare datastore. Required if `host` is a cluster, or if `host` has multiple datastores. + +- `set_host_for_datastore_uploads` (bool) - Set this to true if packer should the host for uploading files + to the datastore. Defaults to false. \ No newline at end of file