github.com/raghuse92/packer@v1.3.2/post-processor/vagrant-cloud/step_release_version.go (about)

     1  package vagrantcloud
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"github.com/hashicorp/packer/helper/multistep"
     9  	"github.com/hashicorp/packer/packer"
    10  )
    11  
    12  type stepReleaseVersion struct {
    13  }
    14  
    15  func (s *stepReleaseVersion) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
    16  	client := state.Get("client").(*VagrantCloudClient)
    17  	ui := state.Get("ui").(packer.Ui)
    18  	box := state.Get("box").(*Box)
    19  	version := state.Get("version").(*Version)
    20  	config := state.Get("config").(Config)
    21  
    22  	ui.Say(fmt.Sprintf("Releasing version: %s", version.Version))
    23  
    24  	if config.NoRelease {
    25  		ui.Message("Not releasing version due to configuration")
    26  		return multistep.ActionContinue
    27  	}
    28  
    29  	path := fmt.Sprintf("box/%s/version/%v/release", box.Tag, version.Version)
    30  
    31  	resp, err := client.Put(path)
    32  
    33  	if err != nil || (resp.StatusCode != 200) {
    34  		cloudErrors := &VagrantCloudErrors{}
    35  		if err := decodeBody(resp, cloudErrors); err != nil {
    36  			state.Put("error", fmt.Errorf("Error parsing provider response: %s", err))
    37  			return multistep.ActionHalt
    38  		}
    39  		if strings.Contains(cloudErrors.FormatErrors(), "already been released") {
    40  			ui.Message("Not releasing version, already released")
    41  			return multistep.ActionContinue
    42  		}
    43  		state.Put("error", fmt.Errorf("Error releasing version: %s", cloudErrors.FormatErrors()))
    44  		return multistep.ActionHalt
    45  	}
    46  
    47  	ui.Message(fmt.Sprintf("Version successfully released and available"))
    48  
    49  	return multistep.ActionContinue
    50  }
    51  
    52  func (s *stepReleaseVersion) Cleanup(state multistep.StateBag) {
    53  	// No cleanup
    54  }