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

58 lines
1.5 KiB
Go
Raw Normal View History

package iso
import (
"context"
"fmt"
"github.com/hashicorp/packer/builder/vsphere/driver"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"path/filepath"
)
type StepRemoteUpload struct {
Datastore string
Host string
}
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 {
filename := filepath.Base(path.(string))
ds, err := d.FindDatastore(s.Datastore, s.Host)
if err != nil {
state.Put("error", fmt.Errorf("datastore doesn't exist: %v", err))
return multistep.ActionHalt
}
2019-07-08 10:48:37 -04:00
remotePath := fmt.Sprintf("packer_cache/%s", filename)
remoteDirectory := fmt.Sprintf("[%s] packer_cache/", ds.Name())
fullRemotePath := fmt.Sprintf("%s/%s", remoteDirectory, filename)
2019-07-08 10:48:37 -04:00
ui.Say(fmt.Sprintf("Uploading %s to %s", filename, remotePath))
2019-07-08 10:48:37 -04:00
if exists := ds.FileExists(remotePath); exists == true {
ui.Say("File already upload")
2019-07-08 10:48:37 -04:00
state.Put("iso_remote_path", fullRemotePath)
return multistep.ActionContinue
}
2019-07-08 10:48:37 -04:00
if err := ds.MakeDirectory(remoteDirectory); err != nil {
state.Put("error", err)
return multistep.ActionHalt
}
2019-07-08 10:48:37 -04:00
if err := ds.UploadFile(path.(string), remotePath, s.Host); err != nil {
state.Put("error", err)
return multistep.ActionHalt
}
2019-07-08 10:48:37 -04:00
state.Put("iso_remote_path", fullRemotePath)
}
return multistep.ActionContinue
}
func (s *StepRemoteUpload) Cleanup(state multistep.StateBag) {}