From 19bd997f117d1e99e9c6725f87314af31ec5a01c Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Fri, 23 Oct 2020 11:04:27 -0700 Subject: [PATCH] add directory existence check --- builder/vsphere/common/step_remote_upload.go | 7 +++++-- builder/vsphere/driver/datastore.go | 9 +++++++++ builder/vsphere/driver/datastore_mock.go | 3 +++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/builder/vsphere/common/step_remote_upload.go b/builder/vsphere/common/step_remote_upload.go index 8a0a872a7..9df7668e3 100644 --- a/builder/vsphere/common/step_remote_upload.go +++ b/builder/vsphere/common/step_remote_upload.go @@ -69,8 +69,11 @@ func (s *StepRemoteUpload) uploadFile(path string, d driver.Driver, ui packer.Ui ui.Say(fmt.Sprintf("Uploading %s to %s", filename, remotePath)) - if err := ds.MakeDirectory(remoteDirectory); err != nil { - return "", err + if exists := ds.DirExists(remotePath); exists == false { + log.Printf("Remote directory doesn't exist; creating...") + if err := ds.MakeDirectory(remoteDirectory); err != nil { + return "", err + } } if err := ds.UploadFile(path, remotePath, s.Host, s.SetHostForDatastoreUploads); err != nil { diff --git a/builder/vsphere/driver/datastore.go b/builder/vsphere/driver/datastore.go index f8eaf6eaf..adc13447f 100644 --- a/builder/vsphere/driver/datastore.go +++ b/builder/vsphere/driver/datastore.go @@ -16,6 +16,7 @@ import ( type Datastore interface { Info(params ...string) (*mo.Datastore, error) FileExists(path string) bool + DirExists(path string) bool Name() string ResolvePath(path string) string UploadFile(src, dst, host string, setHost bool) error @@ -102,6 +103,14 @@ func (ds *DatastoreDriver) Info(params ...string) (*mo.Datastore, error) { return &info, nil } +func (ds *DatastoreDriver) DirExists(filepath string) bool { + _, err := ds.ds.Stat(ds.driver.ctx, filepath) + if _, ok := err.(object.DatastoreNoSuchDirectoryError); ok { + return false + } + return true +} + func (ds *DatastoreDriver) FileExists(path string) bool { _, err := ds.ds.Stat(ds.driver.ctx, path) return err == nil diff --git a/builder/vsphere/driver/datastore_mock.go b/builder/vsphere/driver/datastore_mock.go index 889dd254a..28757ee83 100644 --- a/builder/vsphere/driver/datastore_mock.go +++ b/builder/vsphere/driver/datastore_mock.go @@ -37,6 +37,9 @@ func (ds *DatastoreMock) FileExists(path string) bool { return ds.FileExistsReturn } +func (ds *DatastoreMock) DirExists(path string) bool { + return true +} func (ds *DatastoreMock) Name() string { if ds.NameReturn == "" { return "datastore-mock"