2019-09-19 14:53:28 -04:00
|
|
|
package common
|
2013-11-07 15:28:41 -05:00
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2013-11-07 15:28:41 -05:00
|
|
|
"fmt"
|
2017-07-23 01:31:46 -04:00
|
|
|
"log"
|
|
|
|
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-11-07 15:28:41 -05:00
|
|
|
)
|
|
|
|
|
2017-07-24 00:17:18 -04:00
|
|
|
// stepRemoteUpload uploads some thing from the state bag to a remote driver
|
|
|
|
// (if it can) and stores that new remote path into the state bag.
|
2019-09-19 14:53:28 -04:00
|
|
|
type StepRemoteUpload struct {
|
2020-05-28 05:02:09 -04:00
|
|
|
Key string
|
|
|
|
Message string
|
|
|
|
DoCleanup bool
|
|
|
|
Checksum string
|
2013-11-07 15:28:41 -05:00
|
|
|
}
|
|
|
|
|
2019-09-19 14:53:28 -04:00
|
|
|
func (s *StepRemoteUpload) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
|
|
|
driver := state.Get("driver").(Driver)
|
2013-11-07 15:28:41 -05:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
2019-09-19 14:53:28 -04:00
|
|
|
remote, ok := driver.(RemoteDriver)
|
2013-11-07 15:28:41 -05:00
|
|
|
if !ok {
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2014-05-03 05:23:20 -04:00
|
|
|
path, ok := state.Get(s.Key).(string)
|
|
|
|
if !ok {
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2019-09-19 14:53:28 -04:00
|
|
|
if esx5, ok := remote.(*ESX5Driver); ok {
|
2018-10-25 17:17:35 -04:00
|
|
|
remotePath := esx5.CachePath(path)
|
2017-11-10 21:55:26 -05:00
|
|
|
|
2020-05-28 05:02:09 -04:00
|
|
|
if esx5.VerifyChecksum(s.Checksum, remotePath) {
|
2017-11-10 21:55:26 -05:00
|
|
|
ui.Say("Remote cache was verified skipping remote upload...")
|
|
|
|
state.Put(s.Key, remotePath)
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-07-24 00:11:30 -04:00
|
|
|
ui.Say(s.Message)
|
|
|
|
log.Printf("Remote uploading: %s", path)
|
2020-08-17 14:35:42 -04:00
|
|
|
newPath, err := remote.UploadISO(path, s.Checksum, ui)
|
2017-07-24 00:11:30 -04:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error uploading file: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
2013-11-07 15:28:41 -05:00
|
|
|
}
|
2017-07-24 00:11:30 -04:00
|
|
|
state.Put(s.Key, newPath)
|
2013-11-07 15:28:41 -05:00
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2019-09-19 14:53:28 -04:00
|
|
|
func (s *StepRemoteUpload) Cleanup(state multistep.StateBag) {
|
2017-03-17 20:02:43 -04:00
|
|
|
if !s.DoCleanup {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-19 14:53:28 -04:00
|
|
|
driver := state.Get("driver").(Driver)
|
2017-03-17 20:02:43 -04:00
|
|
|
|
2019-09-19 14:53:28 -04:00
|
|
|
remote, ok := driver.(RemoteDriver)
|
2017-03-17 20:02:43 -04:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
path, ok := state.Get(s.Key).(string)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Cleaning up remote path: %s", path)
|
2018-04-26 13:56:48 -04:00
|
|
|
err := remote.RemoveCache(path)
|
2017-03-17 20:02:43 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error cleaning up: %s", err)
|
|
|
|
}
|
2013-11-07 15:28:41 -05:00
|
|
|
}
|