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"
|
2021-03-18 18:22:27 -04:00
|
|
|
"os"
|
2017-06-12 20:34:32 -04:00
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2014-06-23 15:48:51 -04:00
|
|
|
)
|
|
|
|
|
2021-03-24 13:51:10 -04:00
|
|
|
const VAGRANT_CLOUD_DIRECT_UPLOAD_LIMIT = 5368709120 // Upload limit is 5G
|
2021-03-18 18:22:27 -04:00
|
|
|
|
2014-06-24 15:58:45 -04:00
|
|
|
type Upload struct {
|
2020-11-05 20:01:55 -05:00
|
|
|
UploadPath string `json:"upload_path"`
|
|
|
|
CallbackPath string `json:"callback"`
|
2014-06-24 15:58:45 -04:00
|
|
|
}
|
|
|
|
|
2014-06-23 15:48:51 -04:00
|
|
|
type stepPrepareUpload struct {
|
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepPrepareUpload) 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
|
|
|
box := state.Get("box").(*Box)
|
|
|
|
version := state.Get("version").(*Version)
|
|
|
|
provider := state.Get("provider").(*Provider)
|
|
|
|
artifactFilePath := state.Get("artifactFilePath").(string)
|
|
|
|
|
2021-03-18 18:22:27 -04:00
|
|
|
// If direct upload is enabled, the asset size must be <= 5 GB
|
|
|
|
if config.NoDirectUpload == false {
|
|
|
|
f, err := os.Stat(artifactFilePath)
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("error determining size of upload artifact: %s", artifactFilePath))
|
|
|
|
}
|
|
|
|
if f.Size() > VAGRANT_CLOUD_DIRECT_UPLOAD_LIMIT {
|
|
|
|
ui.Say(fmt.Sprintf("Asset %s is larger than the direct upload limit. Setting `NoDirectUpload` to true", artifactFilePath))
|
|
|
|
config.NoDirectUpload = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-23 20:19:42 -05:00
|
|
|
path := fmt.Sprintf("box/%s/version/%v/provider/%s/upload", box.Tag, version.Version, provider.Name)
|
2020-11-05 20:01:55 -05:00
|
|
|
if !config.NoDirectUpload {
|
|
|
|
path = path + "/direct"
|
|
|
|
}
|
2014-06-24 15:58:45 -04:00
|
|
|
upload := &Upload{}
|
|
|
|
|
|
|
|
ui.Say(fmt.Sprintf("Preparing upload of box: %s", artifactFilePath))
|
|
|
|
|
|
|
|
resp, err := client.Get(path)
|
|
|
|
|
|
|
|
if err != nil || (resp.StatusCode != 200) {
|
2017-11-02 03:13:31 -04:00
|
|
|
if resp == nil || resp.Body == nil {
|
2021-03-24 13:51:10 -04:00
|
|
|
state.Put("error", fmt.Errorf("No response from server."))
|
2017-11-02 03:13:31 -04:00
|
|
|
} else {
|
|
|
|
cloudErrors := &VagrantCloudErrors{}
|
|
|
|
err = decodeBody(resp, cloudErrors)
|
2019-09-25 19:58:26 -04:00
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("error decoding error response: %s", err))
|
|
|
|
}
|
2017-11-02 03:13:31 -04:00
|
|
|
state.Put("error", fmt.Errorf("Error preparing upload: %s", cloudErrors.FormatErrors()))
|
|
|
|
}
|
2014-06-24 15:58:45 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = decodeBody(resp, upload); err != nil {
|
|
|
|
state.Put("error", fmt.Errorf("Error parsing upload response: %s", err))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the upload details to the state
|
|
|
|
state.Put("upload", upload)
|
|
|
|
|
2014-06-23 15:48:51 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepPrepareUpload) Cleanup(state multistep.StateBag) {
|
2014-06-24 15:58:45 -04:00
|
|
|
// No cleanup
|
2014-06-23 15:48:51 -04:00
|
|
|
}
|