Merge pull request #10155 from hashicorp/logging_for_10042

builder/vsphere: Don't try to delete a cd file from the remote datastore if the upload of the cd failed
This commit is contained in:
Megan Marsh 2020-10-27 10:54:07 -07:00 committed by GitHub
commit dc38fadeeb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 15 deletions

View File

@ -3,6 +3,7 @@ package common
import ( import (
"context" "context"
"fmt" "fmt"
"log"
"path/filepath" "path/filepath"
"github.com/hashicorp/packer/builder/vsphere/driver" "github.com/hashicorp/packer/builder/vsphere/driver"
@ -14,6 +15,7 @@ type StepRemoteUpload struct {
Datastore string Datastore string
Host string Host string
SetHostForDatastoreUploads bool SetHostForDatastoreUploads bool
UploadedCustomCD bool
} }
func (s *StepRemoteUpload) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { func (s *StepRemoteUpload) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
@ -36,6 +38,7 @@ func (s *StepRemoteUpload) Run(_ context.Context, state multistep.StateBag) mult
state.Put("error", err) state.Put("error", err)
return multistep.ActionHalt return multistep.ActionHalt
} }
s.UploadedCustomCD = true
state.Put("cd_path", fullRemotePath) state.Put("cd_path", fullRemotePath)
} }
@ -66,9 +69,12 @@ 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 exists := ds.DirExists(remotePath); exists == false {
log.Printf("Remote directory doesn't exist; creating...")
if err := ds.MakeDirectory(remoteDirectory); err != nil { if err := ds.MakeDirectory(remoteDirectory); err != nil {
return "", err 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 {
return "", err return "", err
@ -83,23 +89,29 @@ func (s *StepRemoteUpload) Cleanup(state multistep.StateBag) {
return return
} }
if !s.UploadedCustomCD {
return
}
UploadedCDPath, ok := state.GetOk("cd_path")
if !ok {
return
}
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
d := state.Get("driver").(*driver.VCenterDriver) d := state.Get("driver").(*driver.VCenterDriver)
if UploadedCDPath, ok := state.GetOk("cd_path"); ok {
ui.Say("Deleting cd_files image from remote datastore ...") ui.Say("Deleting cd_files image from remote datastore ...")
ds, err := d.FindDatastore(s.Datastore, s.Host) ds, err := d.FindDatastore(s.Datastore, s.Host)
if err != nil { if err != nil {
state.Put("error", err) log.Printf("Error finding datastore to delete custom CD; please delete manually: %s", err)
return return
} }
err = ds.Delete(UploadedCDPath.(string)) err = ds.Delete(UploadedCDPath.(string))
if err != nil { if err != nil {
state.Put("error", err) log.Printf("Error deleting custom CD from remote datastore; please delete manually: %s", err)
return return
}
} }
} }

View File

@ -11,7 +11,11 @@ import (
func TestStepRemoteUpload_Run(t *testing.T) { func TestStepRemoteUpload_Run(t *testing.T) {
state := basicStateBag(nil) state := basicStateBag(nil)
dsMock := driver.DatastoreMock{
DirExistsReturn: false,
}
driverMock := driver.NewDriverMock() driverMock := driver.NewDriverMock()
driverMock.DatastoreMock = &dsMock
state.Put("driver", driverMock) state.Put("driver", driverMock)
state.Put("iso_path", "[datastore] iso/path") state.Put("iso_path", "[datastore] iso/path")

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

@ -9,6 +9,9 @@ type DatastoreMock struct {
FileExistsCalled bool FileExistsCalled bool
FileExistsReturn bool FileExistsReturn bool
DirExistsCalled bool
DirExistsReturn bool
NameReturn string NameReturn string
MakeDirectoryCalled bool MakeDirectoryCalled bool
@ -37,6 +40,10 @@ func (ds *DatastoreMock) FileExists(path string) bool {
return ds.FileExistsReturn return ds.FileExistsReturn
} }
func (ds *DatastoreMock) DirExists(path string) bool {
ds.DirExistsCalled = true
return ds.DirExistsReturn
}
func (ds *DatastoreMock) Name() string { func (ds *DatastoreMock) Name() string {
if ds.NameReturn == "" { if ds.NameReturn == "" {
return "datastore-mock" return "datastore-mock"