Files
Packer-Cn/post-processor/vagrant-cloud/step_prepare_upload.go
T

61 lines
1.6 KiB
Go
Raw Normal View History

2014-06-23 15:48:51 -04:00
package vagrantcloud
import (
2018-01-22 15:32:33 -08:00
"context"
"fmt"
2018-01-19 16:18:44 -08:00
"github.com/hashicorp/packer/helper/multistep"
2017-04-04 13:39:01 -07:00
"github.com/hashicorp/packer/packer"
2014-06-23 15:48:51 -04:00
)
type Upload struct {
UploadPath string `json:"upload_path"`
}
2014-06-23 15:48:51 -04:00
type stepPrepareUpload struct {
}
func (s *stepPrepareUpload) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
client := state.Get("client").(*VagrantCloudClient)
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)
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 00:13:31 -07:00
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))
}
2017-11-02 00:13:31 -07:00
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)
2014-06-23 15:48:51 -04:00
return multistep.ActionContinue
}
func (s *stepPrepareUpload) Cleanup(state multistep.StateBag) {
// No cleanup
2014-06-23 15:48:51 -04:00
}