2014-06-23 15:48:51 -04:00
|
|
|
package vagrantcloud
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2014-06-24 15:58:45 -04:00
|
|
|
"fmt"
|
2017-06-12 20:34:32 -04:00
|
|
|
"log"
|
2020-11-05 20:01:55 -05:00
|
|
|
"net/http"
|
2019-04-08 11:57:27 -04:00
|
|
|
"time"
|
2015-06-18 04:25:47 -04:00
|
|
|
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2020-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2020-11-12 17:44:02 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/retry"
|
2014-06-23 15:48:51 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type stepUpload struct {
|
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepUpload) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2014-06-24 15:58:45 -04:00
|
|
|
client := state.Get("client").(*VagrantCloudClient)
|
2020-11-05 20:01:55 -05:00
|
|
|
config := state.Get("config").(*Config)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2014-06-24 15:58:45 -04:00
|
|
|
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")
|
|
|
|
|
2019-04-08 11:57:27 -04:00
|
|
|
err := retry.Config{
|
|
|
|
Tries: 3,
|
|
|
|
RetryDelay: (&retry.Backoff{InitialBackoff: 10 * time.Second, MaxBackoff: 10 * time.Second, Multiplier: 2}).Linear,
|
|
|
|
}.Run(ctx, func(ctx context.Context) error {
|
|
|
|
ui.Message(fmt.Sprintf("Uploading box"))
|
2014-06-24 15:58:45 -04:00
|
|
|
|
2020-11-05 20:01:55 -05:00
|
|
|
var err error
|
|
|
|
var resp *http.Response
|
|
|
|
|
|
|
|
if config.NoDirectUpload {
|
|
|
|
resp, err = client.Upload(artifactFilePath, url)
|
|
|
|
} else {
|
|
|
|
resp, err = client.DirectUpload(artifactFilePath, url)
|
|
|
|
}
|
2015-06-18 04:25:47 -04:00
|
|
|
if err != nil {
|
|
|
|
ui.Message(fmt.Sprintf(
|
|
|
|
"Error uploading box! Will retry in 10 seconds. Error: %s", err))
|
2019-04-08 11:57:27 -04:00
|
|
|
return err
|
2015-06-18 04:25:47 -04:00
|
|
|
}
|
|
|
|
if resp.StatusCode != 200 {
|
2019-04-08 11:57:27 -04:00
|
|
|
err := fmt.Errorf("bad HTTP status: %d", resp.StatusCode)
|
|
|
|
log.Print(err)
|
2015-06-18 04:25:47 -04:00
|
|
|
ui.Message(fmt.Sprintf(
|
|
|
|
"Error uploading box! Will retry in 10 seconds. Status: %d",
|
|
|
|
resp.StatusCode))
|
2019-04-08 11:57:27 -04:00
|
|
|
return err
|
2015-06-18 04:25:47 -04:00
|
|
|
}
|
2019-04-08 11:57:27 -04:00
|
|
|
return err
|
2017-06-12 20:34:32 -04:00
|
|
|
})
|
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
|
|
|
}
|