2014-06-23 15:48:51 -04:00
|
|
|
package vagrantcloud
|
|
|
|
|
|
|
|
import (
|
2014-06-24 15:58:45 -04:00
|
|
|
"fmt"
|
2017-06-12 20:34:32 -04:00
|
|
|
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2014-06-23 15:48:51 -04:00
|
|
|
)
|
|
|
|
|
2014-06-24 15:58:45 -04:00
|
|
|
type Upload struct {
|
|
|
|
UploadPath string `json:"upload_path"`
|
|
|
|
}
|
|
|
|
|
2014-06-23 15:48:51 -04:00
|
|
|
type stepPrepareUpload struct {
|
|
|
|
}
|
|
|
|
|
2018-01-22 18:31:41 -05:00
|
|
|
func (s *stepPrepareUpload) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2014-06-24 15:58:45 -04:00
|
|
|
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)
|
|
|
|
|
2014-12-23 20:19:42 -05:00
|
|
|
path := fmt.Sprintf("box/%s/version/%v/provider/%s/upload", box.Tag, version.Version, provider.Name)
|
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 {
|
|
|
|
state.Put("error", "No response from server.")
|
|
|
|
} else {
|
|
|
|
cloudErrors := &VagrantCloudErrors{}
|
|
|
|
err = decodeBody(resp, cloudErrors)
|
|
|
|
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
|
|
|
}
|