mirror of
https://github.com/hashicorp/packer.git
synced 2026-09-29 19:24:10 -04:00
Vagrant Cloud provides support for uploading directly to the backend storage instead of streaming through Vagrant Cloud. This adds support for direct to storage uploads and sets it as the default upload method. A new option has been added to disable this behavior and revert back to streaming upload via Vagrant Cloud (`no_direct_upload`). This default for uploading directly to the backend storage also matches up with changes being added to Vagrant proper for box upload behavior: hashicorp/vagrant#11916
66 lines
1.7 KiB
Go
66 lines
1.7 KiB
Go
package vagrantcloud
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
|
"github.com/hashicorp/packer/packer"
|
|
)
|
|
|
|
type Upload struct {
|
|
UploadPath string `json:"upload_path"`
|
|
CallbackPath string `json:"callback"`
|
|
}
|
|
|
|
type stepPrepareUpload struct {
|
|
}
|
|
|
|
func (s *stepPrepareUpload) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
|
client := state.Get("client").(*VagrantCloudClient)
|
|
config := state.Get("config").(*Config)
|
|
ui := state.Get("ui").(packer.Ui)
|
|
box := state.Get("box").(*Box)
|
|
version := state.Get("version").(*Version)
|
|
provider := state.Get("provider").(*Provider)
|
|
artifactFilePath := state.Get("artifactFilePath").(string)
|
|
|
|
path := fmt.Sprintf("box/%s/version/%v/provider/%s/upload", box.Tag, version.Version, provider.Name)
|
|
if !config.NoDirectUpload {
|
|
path = path + "/direct"
|
|
}
|
|
upload := &Upload{}
|
|
|
|
ui.Say(fmt.Sprintf("Preparing upload of box: %s", artifactFilePath))
|
|
|
|
resp, err := client.Get(path)
|
|
|
|
if err != nil || (resp.StatusCode != 200) {
|
|
if resp == nil || resp.Body == nil {
|
|
state.Put("error", "No response from server.")
|
|
} else {
|
|
cloudErrors := &VagrantCloudErrors{}
|
|
err = decodeBody(resp, cloudErrors)
|
|
if err != nil {
|
|
ui.Error(fmt.Sprintf("error decoding error response: %s", err))
|
|
}
|
|
state.Put("error", fmt.Errorf("Error preparing upload: %s", cloudErrors.FormatErrors()))
|
|
}
|
|
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)
|
|
|
|
return multistep.ActionContinue
|
|
}
|
|
|
|
func (s *stepPrepareUpload) Cleanup(state multistep.StateBag) {
|
|
// No cleanup
|
|
}
|