2020-09-18 11:09:01 -04:00
|
|
|
package common
|
2019-01-04 09:07:09 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-10-22 19:53:40 -04:00
|
|
|
"log"
|
2020-02-14 11:42:29 -05:00
|
|
|
"path/filepath"
|
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2020-01-07 19:59:31 -05:00
|
|
|
"github.com/hashicorp/packer/builder/vsphere/driver"
|
2019-01-04 09:07:09 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type StepRemoteUpload struct {
|
2020-05-05 14:34:15 -04:00
|
|
|
Datastore string
|
|
|
|
Host string
|
|
|
|
SetHostForDatastoreUploads bool
|
2020-10-22 19:53:40 -04:00
|
|
|
UploadedCustomCD bool
|
2019-01-04 09:07:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepRemoteUpload) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2020-08-31 04:34:41 -04:00
|
|
|
d := state.Get("driver").(driver.Driver)
|
2019-01-04 09:07:09 -05:00
|
|
|
|
|
|
|
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)
|
2019-01-04 09:07:09 -05:00
|
|
|
if err != nil {
|
2020-09-14 17:59:51 -04:00
|
|
|
state.Put("error", err)
|
2019-01-04 09:07:09 -05:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2020-10-22 19:53:40 -04:00
|
|
|
s.UploadedCustomCD = true
|
2020-09-14 17:59:51 -04:00
|
|
|
state.Put("cd_path", fullRemotePath)
|
|
|
|
}
|
2019-01-04 09:07:09 -05:00
|
|
|
|
2020-09-14 17:59:51 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
2019-01-04 09:07:09 -05:00
|
|
|
|
2020-10-20 17:49:27 -04:00
|
|
|
func GetRemoteDirectoryAndPath(path string, ds driver.Datastore) (string, string, string, string) {
|
|
|
|
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)
|
|
|
|
|
|
|
|
return filename, remotePath, remoteDirectory, fullRemotePath
|
|
|
|
|
|
|
|
}
|
2020-11-19 14:54:31 -05:00
|
|
|
func (s *StepRemoteUpload) uploadFile(path string, d driver.Driver, ui packersdk.Ui) (string, error) {
|
2020-09-14 17:59:51 -04:00
|
|
|
ds, err := d.FindDatastore(s.Datastore, s.Host)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("datastore doesn't exist: %v", err)
|
|
|
|
}
|
2019-01-04 09:07:09 -05:00
|
|
|
|
2020-10-20 17:49:27 -04:00
|
|
|
filename, remotePath, remoteDirectory, fullRemotePath := GetRemoteDirectoryAndPath(path, ds)
|
2020-09-14 17:59:51 -04:00
|
|
|
|
|
|
|
if exists := ds.FileExists(remotePath); exists == true {
|
2020-10-20 17:49:27 -04:00
|
|
|
ui.Say(fmt.Sprintf("File %s already exists; skipping upload.", fullRemotePath))
|
2020-09-14 17:59:51 -04:00
|
|
|
return fullRemotePath, nil
|
|
|
|
}
|
|
|
|
|
2020-10-20 17:49:27 -04:00
|
|
|
ui.Say(fmt.Sprintf("Uploading %s to %s", filename, remotePath))
|
|
|
|
|
2020-10-23 14:04:27 -04:00
|
|
|
if exists := ds.DirExists(remotePath); exists == false {
|
|
|
|
log.Printf("Remote directory doesn't exist; creating...")
|
|
|
|
if err := ds.MakeDirectory(remoteDirectory); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2020-09-14 17:59:51 -04:00
|
|
|
}
|
2019-01-04 09:07:09 -05:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-10-27 13:37:03 -04:00
|
|
|
if !s.UploadedCustomCD {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
UploadedCDPath, ok := state.GetOk("cd_path")
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2020-09-14 17:59:51 -04:00
|
|
|
d := state.Get("driver").(*driver.VCenterDriver)
|
2020-10-27 13:37:03 -04:00
|
|
|
ui.Say("Deleting cd_files image from remote datastore ...")
|
|
|
|
|
|
|
|
ds, err := d.FindDatastore(s.Datastore, s.Host)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error finding datastore to delete custom CD; please delete manually: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ds.Delete(UploadedCDPath.(string))
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error deleting custom CD from remote datastore; please delete manually: %s", err)
|
|
|
|
return
|
2020-09-14 17:59:51 -04:00
|
|
|
|
|
|
|
}
|
2019-01-04 09:07:09 -05:00
|
|
|
}
|