2014-06-23 15:48:51 -04:00
|
|
|
package vagrantcloud
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-06-24 15:58:45 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
2014-06-23 15:48:51 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Version struct {
|
|
|
|
Version string `json:"version"`
|
2014-06-24 15:58:45 -04:00
|
|
|
Number uint `json:"number,omitempty"`
|
2014-06-23 15:48:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type stepCreateVersion struct {
|
|
|
|
number uint // number of the version, if needed in cleanup
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepCreateVersion) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
client := state.Get("client").(*VagrantCloudClient)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
config := state.Get("config").(Config)
|
|
|
|
box := state.Get("box").(*Box)
|
|
|
|
|
2014-06-24 15:58:45 -04:00
|
|
|
if hasVersion, v := box.HasVersion(config.Version); hasVersion {
|
|
|
|
ui.Say(fmt.Sprintf("Version exists: %s", config.Version))
|
|
|
|
state.Put("version", v)
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
path := fmt.Sprintf("box/%s/versions", box.Tag)
|
|
|
|
|
|
|
|
version := &Version{Version: config.Version}
|
2014-06-23 15:48:51 -04:00
|
|
|
|
|
|
|
// Wrap the version in a version object for the API
|
|
|
|
wrapper := make(map[string]interface{})
|
2014-06-24 15:58:45 -04:00
|
|
|
wrapper["version"] = version
|
2014-06-23 15:48:51 -04:00
|
|
|
|
2014-06-24 15:58:45 -04:00
|
|
|
ui.Say(fmt.Sprintf("Creating version: %s", config.Version))
|
2014-06-23 15:48:51 -04:00
|
|
|
|
|
|
|
resp, err := client.Post(path, wrapper)
|
|
|
|
|
|
|
|
if err != nil || (resp.StatusCode != 200) {
|
2014-06-24 15:58:45 -04:00
|
|
|
cloudErrors := &VagrantCloudErrors{}
|
|
|
|
err = decodeBody(resp, cloudErrors)
|
|
|
|
state.Put("error", fmt.Errorf("Error creating version: %s", cloudErrors.FormatErrors()))
|
2014-06-23 15:48:51 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = decodeBody(resp, version); err != nil {
|
|
|
|
state.Put("error", fmt.Errorf("Error parsing version response: %s", err))
|
2014-06-24 15:58:45 -04:00
|
|
|
return multistep.ActionHalt
|
2014-06-23 15:48:51 -04:00
|
|
|
}
|
|
|
|
|
2014-06-24 15:58:45 -04:00
|
|
|
// Save the number for cleanup
|
|
|
|
s.number = version.Number
|
2014-06-23 15:48:51 -04:00
|
|
|
|
2014-06-24 15:58:45 -04:00
|
|
|
state.Put("version", version)
|
2014-06-23 15:48:51 -04:00
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepCreateVersion) Cleanup(state multistep.StateBag) {
|
2014-06-24 15:58:45 -04:00
|
|
|
// If we didn't save the version number, it likely doesn't exist or
|
|
|
|
// already existed
|
|
|
|
if s.number == 0 {
|
|
|
|
return
|
|
|
|
}
|
2014-06-23 15:48:51 -04:00
|
|
|
|
2014-06-24 15:58:45 -04:00
|
|
|
_, cancelled := state.GetOk(multistep.StateCancelled)
|
|
|
|
_, halted := state.GetOk(multistep.StateHalted)
|
2014-06-23 15:48:51 -04:00
|
|
|
|
2014-06-24 15:58:45 -04:00
|
|
|
// Return if we didn't cancel or halt, and thus need
|
|
|
|
// no cleanup
|
|
|
|
if !cancelled && !halted {
|
|
|
|
return
|
|
|
|
}
|
2014-06-23 15:48:51 -04:00
|
|
|
|
2014-06-24 15:58:45 -04:00
|
|
|
client := state.Get("client").(*VagrantCloudClient)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
box := state.Get("box").(*Box)
|
|
|
|
|
|
|
|
path := fmt.Sprintf("box/%s/version/%v", box.Tag, s.number)
|
|
|
|
|
|
|
|
// No need for resp from the cleanup DELETE
|
|
|
|
_, err := client.Delete(path)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error destroying version: %s", err))
|
|
|
|
}
|
2014-06-23 15:48:51 -04:00
|
|
|
|
|
|
|
}
|