Merge pull request #9100 from jhawk28/bug_9076

dont set the host for the datastore upload
This commit is contained in:
Megan Marsh 2020-05-27 15:39:09 -07:00 committed by GitHub
commit 7b9a33d385
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 440 additions and 424 deletions

View File

@ -33,6 +33,7 @@ type FlatConfig struct {
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"`
CPUs *int32 `mapstructure:"CPUs" cty:"CPUs"`
CpuCores *int32 `mapstructure:"cpu_cores" cty:"cpu_cores"`
CPUReservation *int64 `mapstructure:"CPU_reservation" cty:"CPU_reservation"`
@ -135,6 +136,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec {
"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},
"CPUs": &hcldec.AttrSpec{Name: "CPUs", Type: cty.Number, Required: false},
"cpu_cores": &hcldec.AttrSpec{Name: "cpu_cores", Type: cty.Number, Required: false},
"CPU_reservation": &hcldec.AttrSpec{Name: "CPU_reservation", Type: cty.Number, Required: false},

View File

@ -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 {

View File

@ -15,6 +15,7 @@ type FlatLocationConfig struct {
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.
@ -35,6 +36,7 @@ func (*FlatLocationConfig) HCL2Spec() map[string]hcldec.Spec {
"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
}

View File

@ -85,11 +85,11 @@ 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 host != "" {
if set_host_for_datastore_uploads && host != "" {
h, err := ds.driver.FindHost(host)
if err != nil {
return err

View File

@ -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)
}

View File

@ -55,6 +55,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
&StepRemoteUpload{
Datastore: b.config.Datastore,
Host: b.config.Host,
SetHostForDatastoreUploads: b.config.SetHostForDatastoreUploads,
},
)
}
@ -87,6 +88,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
Config: &b.config.FloppyConfig,
Datastore: b.config.Datastore,
Host: b.config.Host,
SetHostForDatastoreUploads: b.config.SetHostForDatastoreUploads,
},
&packerCommon.StepHTTPServer{
HTTPDir: b.config.HTTPDir,

View File

@ -39,6 +39,7 @@ type FlatConfig struct {
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"`
CPUs *int32 `mapstructure:"CPUs" cty:"CPUs"`
CpuCores *int32 `mapstructure:"cpu_cores" cty:"cpu_cores"`
CPUReservation *int64 `mapstructure:"CPU_reservation" cty:"CPU_reservation"`
@ -163,6 +164,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec {
"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},
"CPUs": &hcldec.AttrSpec{Name: "CPUs", Type: cty.Number, Required: false},
"cpu_cores": &hcldec.AttrSpec{Name: "cpu_cores", Type: cty.Number, Required: false},
"CPU_reservation": &hcldec.AttrSpec{Name: "CPU_reservation", Type: cty.Number, Required: false},

View File

@ -32,6 +32,7 @@ type StepAddFloppy struct {
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
}

View File

@ -13,6 +13,7 @@ import (
type StepRemoteUpload struct {
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
}

View File

@ -17,3 +17,6 @@
- `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.