2014-06-23 15:48:51 -04:00
|
|
|
package vagrantcloud
|
|
|
|
|
|
|
|
import (
|
2014-06-24 15:58:45 -04:00
|
|
|
"fmt"
|
2017-06-12 20:34:32 -04:00
|
|
|
"log"
|
2015-06-18 04:25:47 -04:00
|
|
|
|
2017-06-12 20:34:32 -04:00
|
|
|
"github.com/hashicorp/packer/common"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2014-06-23 15:48:51 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
)
|
|
|
|
|
|
|
|
type stepUpload struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepUpload) Run(state multistep.StateBag) multistep.StepAction {
|
2014-06-24 15:58:45 -04:00
|
|
|
client := state.Get("client").(*VagrantCloudClient)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
upload := state.Get("upload").(*Upload)
|
|
|
|
artifactFilePath := state.Get("artifactFilePath").(string)
|
|
|
|
url := upload.UploadPath
|
|
|
|
|
|
|
|
ui.Say(fmt.Sprintf("Uploading box: %s", artifactFilePath))
|
2015-06-18 04:25:47 -04:00
|
|
|
ui.Message(
|
|
|
|
"Depending on your internet connection and the size of the box,\n" +
|
|
|
|
"this may take some time")
|
|
|
|
|
2017-06-12 20:34:32 -04:00
|
|
|
err := common.Retry(10, 10, 3, func(i uint) (bool, error) {
|
|
|
|
ui.Message(fmt.Sprintf("Uploading box, attempt %d", i+1))
|
2014-06-24 15:58:45 -04:00
|
|
|
|
2015-06-18 04:25:47 -04:00
|
|
|
resp, err := client.Upload(artifactFilePath, url)
|
|
|
|
if err != nil {
|
|
|
|
ui.Message(fmt.Sprintf(
|
|
|
|
"Error uploading box! Will retry in 10 seconds. Error: %s", err))
|
2017-06-12 20:34:32 -04:00
|
|
|
return false, nil
|
2015-06-18 04:25:47 -04:00
|
|
|
}
|
|
|
|
if resp.StatusCode != 200 {
|
2017-06-12 20:34:32 -04:00
|
|
|
log.Printf("bad HTTP status: %d", resp.StatusCode)
|
2015-06-18 04:25:47 -04:00
|
|
|
ui.Message(fmt.Sprintf(
|
|
|
|
"Error uploading box! Will retry in 10 seconds. Status: %d",
|
|
|
|
resp.StatusCode))
|
2017-06-12 20:34:32 -04:00
|
|
|
return false, nil
|
2015-06-18 04:25:47 -04:00
|
|
|
}
|
2017-06-12 20:34:32 -04:00
|
|
|
return true, nil
|
|
|
|
})
|
2014-06-25 12:46:25 -04:00
|
|
|
|
2017-06-12 20:34:32 -04:00
|
|
|
if err != nil {
|
|
|
|
state.Put("error", err)
|
2014-06-24 15:58:45 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2017-03-28 20:45:01 -04:00
|
|
|
ui.Message("Box successfully uploaded")
|
2014-06-25 12:46:25 -04:00
|
|
|
|
2014-06-23 15:48:51 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepUpload) Cleanup(state multistep.StateBag) {
|
2014-06-24 15:58:45 -04:00
|
|
|
// No cleanup
|
2014-06-23 15:48:51 -04:00
|
|
|
}
|