don't try to delete a cd file if the upload of the cd itself failed

This commit is contained in:
Megan Marsh 2020-10-22 16:53:40 -07:00
parent 3be53e10af
commit 7fac596b37
1 changed files with 17 additions and 13 deletions

View File

@ -3,6 +3,7 @@ package common
import (
"context"
"fmt"
"log"
"path/filepath"
"github.com/hashicorp/packer/builder/vsphere/driver"
@ -14,6 +15,7 @@ type StepRemoteUpload struct {
Datastore string
Host string
SetHostForDatastoreUploads bool
UploadedCustomCD bool
}
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)
return multistep.ActionHalt
}
s.UploadedCustomCD = true
state.Put("cd_path", fullRemotePath)
}
@ -86,20 +89,21 @@ func (s *StepRemoteUpload) Cleanup(state multistep.StateBag) {
ui := state.Get("ui").(packer.Ui)
d := state.Get("driver").(*driver.VCenterDriver)
if s.UploadedCustomCD {
if UploadedCDPath, ok := state.GetOk("cd_path"); ok {
ui.Say("Deleting cd_files image from remote datastore ...")
ds, err := d.FindDatastore(s.Datastore, s.Host)
if err != nil {
state.Put("error", err)
log.Printf("Error finding datastore to delete custom CD; please delete manually: %s", err)
return
}
err = ds.Delete(UploadedCDPath.(string))
if err != nil {
state.Put("error", err)
log.Printf("Error deleting custom CD from remote datastore; please delete manually: %s", err)
return
}
}
}
}