github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/post-processor/vagrant-cloud/step_upload.go (about)

     1  package vagrantcloud
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/hashicorp/packer/common"
     8  	"github.com/hashicorp/packer/packer"
     9  	"github.com/mitchellh/multistep"
    10  )
    11  
    12  type stepUpload struct {
    13  }
    14  
    15  func (s *stepUpload) Run(state multistep.StateBag) multistep.StepAction {
    16  	client := state.Get("client").(*VagrantCloudClient)
    17  	ui := state.Get("ui").(packer.Ui)
    18  	upload := state.Get("upload").(*Upload)
    19  	artifactFilePath := state.Get("artifactFilePath").(string)
    20  	url := upload.UploadPath
    21  
    22  	ui.Say(fmt.Sprintf("Uploading box: %s", artifactFilePath))
    23  	ui.Message(
    24  		"Depending on your internet connection and the size of the box,\n" +
    25  			"this may take some time")
    26  
    27  	err := common.Retry(10, 10, 3, func(i uint) (bool, error) {
    28  		ui.Message(fmt.Sprintf("Uploading box, attempt %d", i+1))
    29  
    30  		resp, err := client.Upload(artifactFilePath, url)
    31  		if err != nil {
    32  			ui.Message(fmt.Sprintf(
    33  				"Error uploading box! Will retry in 10 seconds. Error: %s", err))
    34  			return false, nil
    35  		}
    36  		if resp.StatusCode != 200 {
    37  			log.Printf("bad HTTP status: %d", resp.StatusCode)
    38  			ui.Message(fmt.Sprintf(
    39  				"Error uploading box! Will retry in 10 seconds. Status: %d",
    40  				resp.StatusCode))
    41  			return false, nil
    42  		}
    43  		return true, nil
    44  	})
    45  
    46  	if err != nil {
    47  		state.Put("error", err)
    48  		return multistep.ActionHalt
    49  	}
    50  
    51  	ui.Message("Box successfully uploaded")
    52  
    53  	return multistep.ActionContinue
    54  }
    55  
    56  func (s *stepUpload) Cleanup(state multistep.StateBag) {
    57  	// No cleanup
    58  }