2019-01-04 09:07:09 -05:00
|
|
|
package iso
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-01-07 19:59:31 -05:00
|
|
|
"github.com/hashicorp/packer/builder/vsphere/driver"
|
2019-01-04 09:07:09 -05:00
|
|
|
"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-01-04 09:07:09 -05:00
|
|
|
|
2019-07-08 10:48:37 -04:00
|
|
|
ui.Say(fmt.Sprintf("Uploading %s to %s", filename, remotePath))
|
2019-01-04 09:07:09 -05:00
|
|
|
|
2019-07-08 10:48:37 -04:00
|
|
|
if exists := ds.FileExists(remotePath); exists == true {
|
2020-01-13 14:43:02 -05:00
|
|
|
ui.Say("File already uploaded; continuing")
|
2019-07-08 10:48:37 -04:00
|
|
|
state.Put("iso_remote_path", fullRemotePath)
|
2019-01-04 09:07:09 -05:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2019-07-08 10:48:37 -04:00
|
|
|
if err := ds.MakeDirectory(remoteDirectory); err != nil {
|
2019-01-04 09:07:09 -05:00
|
|
|
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 {
|
2019-01-04 09:07:09 -05:00
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2019-07-08 10:48:37 -04:00
|
|
|
state.Put("iso_remote_path", fullRemotePath)
|
2019-01-04 09:07:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepRemoteUpload) Cleanup(state multistep.StateBag) {}
|