add directory existence check

This commit is contained in:
Megan Marsh 2020-10-23 11:04:27 -07:00
parent 7fac596b37
commit 19bd997f11
3 changed files with 17 additions and 2 deletions

View File

@ -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)) ui.Say(fmt.Sprintf("Uploading %s to %s", filename, remotePath))
if err := ds.MakeDirectory(remoteDirectory); err != nil { if exists := ds.DirExists(remotePath); exists == false {
return "", err 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 { if err := ds.UploadFile(path, remotePath, s.Host, s.SetHostForDatastoreUploads); err != nil {

View File

@ -16,6 +16,7 @@ import (
type Datastore interface { type Datastore interface {
Info(params ...string) (*mo.Datastore, error) Info(params ...string) (*mo.Datastore, error)
FileExists(path string) bool FileExists(path string) bool
DirExists(path string) bool
Name() string Name() string
ResolvePath(path string) string ResolvePath(path string) string
UploadFile(src, dst, host string, setHost bool) error UploadFile(src, dst, host string, setHost bool) error
@ -102,6 +103,14 @@ func (ds *DatastoreDriver) Info(params ...string) (*mo.Datastore, error) {
return &info, nil 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 { func (ds *DatastoreDriver) FileExists(path string) bool {
_, err := ds.ds.Stat(ds.driver.ctx, path) _, err := ds.ds.Stat(ds.driver.ctx, path)
return err == nil return err == nil

View File

@ -37,6 +37,9 @@ func (ds *DatastoreMock) FileExists(path string) bool {
return ds.FileExistsReturn return ds.FileExistsReturn
} }
func (ds *DatastoreMock) DirExists(path string) bool {
return true
}
func (ds *DatastoreMock) Name() string { func (ds *DatastoreMock) Name() string {
if ds.NameReturn == "" { if ds.NameReturn == "" {
return "datastore-mock" return "datastore-mock"