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