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

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

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

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

View File

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

View File

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

View File

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