github.com/askholme/packer@v0.7.2-0.20140924152349-70d9566a6852/post-processor/vagrant-cloud/step_create_version.go (about)

     1  package vagrantcloud
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/mitchellh/multistep"
     6  	"github.com/mitchellh/packer/packer"
     7  )
     8  
     9  type Version struct {
    10  	Version     string `json:"version"`
    11  	Description string `json:"description,omitempty"`
    12  	Number      uint   `json:"number,omitempty"`
    13  }
    14  
    15  type stepCreateVersion struct {
    16  	number uint // number of the version, if needed in cleanup
    17  }
    18  
    19  func (s *stepCreateVersion) Run(state multistep.StateBag) multistep.StepAction {
    20  	client := state.Get("client").(*VagrantCloudClient)
    21  	ui := state.Get("ui").(packer.Ui)
    22  	config := state.Get("config").(Config)
    23  	box := state.Get("box").(*Box)
    24  
    25  	ui.Say(fmt.Sprintf("Creating version: %s", config.Version))
    26  
    27  	if hasVersion, v := box.HasVersion(config.Version); hasVersion {
    28  		ui.Message(fmt.Sprintf("Version exists, skipping creation"))
    29  		state.Put("version", v)
    30  		return multistep.ActionContinue
    31  	}
    32  
    33  	path := fmt.Sprintf("box/%s/versions", box.Tag)
    34  
    35  	version := &Version{Version: config.Version, Description: config.VersionDescription}
    36  
    37  	// Wrap the version in a version object for the API
    38  	wrapper := make(map[string]interface{})
    39  	wrapper["version"] = version
    40  
    41  	resp, err := client.Post(path, wrapper)
    42  
    43  	if err != nil || (resp.StatusCode != 200) {
    44  		cloudErrors := &VagrantCloudErrors{}
    45  		err = decodeBody(resp, cloudErrors)
    46  		state.Put("error", fmt.Errorf("Error creating version: %s", cloudErrors.FormatErrors()))
    47  		return multistep.ActionHalt
    48  	}
    49  
    50  	if err = decodeBody(resp, version); err != nil {
    51  		state.Put("error", fmt.Errorf("Error parsing version response: %s", err))
    52  		return multistep.ActionHalt
    53  	}
    54  
    55  	// Save the number for cleanup
    56  	s.number = version.Number
    57  
    58  	state.Put("version", version)
    59  
    60  	return multistep.ActionContinue
    61  }
    62  
    63  func (s *stepCreateVersion) Cleanup(state multistep.StateBag) {
    64  	client := state.Get("client").(*VagrantCloudClient)
    65  	ui := state.Get("ui").(packer.Ui)
    66  	config := state.Get("config").(Config)
    67  	box := state.Get("box").(*Box)
    68  
    69  	// If we didn't save the version number, it likely doesn't exist or
    70  	// already existed
    71  	if s.number == 0 {
    72  		ui.Message("Version was not created or previously existed, not deleting")
    73  		return
    74  	}
    75  
    76  	_, cancelled := state.GetOk(multistep.StateCancelled)
    77  	_, halted := state.GetOk(multistep.StateHalted)
    78  
    79  	// Return if we didn't cancel or halt, and thus need
    80  	// no cleanup
    81  	if !cancelled && !halted {
    82  		return
    83  	}
    84  
    85  	path := fmt.Sprintf("box/%s/version/%v", box.Tag, s.number)
    86  
    87  	ui.Say("Cleaning up version")
    88  	ui.Message(fmt.Sprintf("Deleting version: %s", config.Version))
    89  
    90  	// No need for resp from the cleanup DELETE
    91  	_, err := client.Delete(path)
    92  
    93  	if err != nil {
    94  		ui.Error(fmt.Sprintf("Error destroying version: %s", err))
    95  	}
    96  
    97  }