packer-cn/builder/vsphere/common/step_remote_upload.go

100 lines
2.6 KiB
Go
Raw Normal View History

2020-09-18 11:09:01 -04:00
package common
import (
"context"
"fmt"
"path/filepath"
"github.com/hashicorp/packer/builder/vsphere/driver"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
)
type StepRemoteUpload struct {
Datastore string
Host string
SetHostForDatastoreUploads bool
}
func (s *StepRemoteUpload) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
d := state.Get("driver").(driver.Driver)
if path, ok := state.GetOk("iso_path"); ok {
2020-09-14 17:59:51 -04:00
// user-supplied boot iso
fullRemotePath, err := s.uploadFile(path.(string), d, ui)
if err != nil {
state.Put("error", err)
return multistep.ActionHalt
}
state.Put("iso_remote_path", fullRemotePath)
}
if cdPath, ok := state.GetOk("cd_path"); ok {
// Packer-created cd_files disk
fullRemotePath, err := s.uploadFile(cdPath.(string), d, ui)
if err != nil {
2020-09-14 17:59:51 -04:00
state.Put("error", err)
return multistep.ActionHalt
}
2020-09-14 17:59:51 -04:00
state.Put("cd_path", fullRemotePath)
}
2020-09-14 17:59:51 -04:00
return multistep.ActionContinue
}
2020-09-14 17:59:51 -04:00
func (s *StepRemoteUpload) uploadFile(path string, d driver.Driver, ui packer.Ui) (string, error) {
ds, err := d.FindDatastore(s.Datastore, s.Host)
if err != nil {
return "", fmt.Errorf("datastore doesn't exist: %v", err)
}
2020-09-14 17:59:51 -04:00
filename := filepath.Base(path)
remotePath := fmt.Sprintf("packer_cache/%s", filename)
remoteDirectory := fmt.Sprintf("[%s] packer_cache/", ds.Name())
fullRemotePath := fmt.Sprintf("%s/%s", remoteDirectory, filename)
ui.Say(fmt.Sprintf("Uploading %s to %s", filename, remotePath))
if exists := ds.FileExists(remotePath); exists == true {
ui.Say(fmt.Sprintf("File %s already uploaded; continuing", filename))
return fullRemotePath, nil
}
if err := ds.MakeDirectory(remoteDirectory); err != nil {
return "", err
}
2020-09-14 17:59:51 -04:00
if err := ds.UploadFile(path, remotePath, s.Host, s.SetHostForDatastoreUploads); err != nil {
return "", err
}
return fullRemotePath, nil
}
func (s *StepRemoteUpload) Cleanup(state multistep.StateBag) {
_, cancelled := state.GetOk(multistep.StateCancelled)
_, halted := state.GetOk(multistep.StateHalted)
if !cancelled && !halted {
return
}
ui := state.Get("ui").(packer.Ui)
d := state.Get("driver").(*driver.VCenterDriver)
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)
2020-09-14 17:59:51 -04:00
return
}
2020-09-14 17:59:51 -04:00
err = ds.Delete(UploadedCDPath.(string))
if err != nil {
state.Put("error", err)
2020-09-14 17:59:51 -04:00
return
}
2020-09-14 17:59:51 -04:00
}
}