add set_host_for_datastore_uploads flag

This commit is contained in:
Joshua Foster 2020-05-05 14:34:15 -04:00 committed by Megan Marsh
parent 9895f90451
commit 43714049e8
8 changed files with 48 additions and 27 deletions

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

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

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