github.com/marksheahan/packer@v0.10.2-0.20160613200515-1acb2d6645a0/post-processor/vagrant-cloud/step_upload.go (about)

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