2014-06-23 15:48:51 -04:00
|
|
|
package vagrantcloud
|
|
|
|
|
|
|
|
import (
|
2014-06-24 15:58:45 -04:00
|
|
|
"fmt"
|
2015-06-18 04:25:47 -04:00
|
|
|
"time"
|
|
|
|
|
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")
|
|
|
|
|
|
|
|
var finalErr error
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
if i > 0 {
|
|
|
|
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 {
|
|
|
|
finalErr = err
|
|
|
|
ui.Message(fmt.Sprintf(
|
|
|
|
"Error uploading box! Will retry in 10 seconds. Error: %s", err))
|
|
|
|
time.Sleep(10 * time.Second)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
finalErr = fmt.Errorf("bad HTTP status: %d", resp.StatusCode)
|
|
|
|
ui.Message(fmt.Sprintf(
|
|
|
|
"Error uploading box! Will retry in 10 seconds. Status: %d",
|
|
|
|
resp.StatusCode))
|
|
|
|
time.Sleep(10 * time.Second)
|
|
|
|
continue
|
|
|
|
}
|
2014-06-25 12:46:25 -04:00
|
|
|
|
2015-06-18 04:25:47 -04:00
|
|
|
finalErr = nil
|
|
|
|
}
|
2014-06-24 15:58:45 -04:00
|
|
|
|
2015-06-18 04:25:47 -04:00
|
|
|
if finalErr != nil {
|
|
|
|
state.Put("error", finalErr)
|
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
|
|
|
}
|