github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/builder/amazon/instance/step_upload_bundle.go (about) 1 package instance 2 3 import ( 4 "fmt" 5 6 "github.com/hashicorp/packer/packer" 7 "github.com/hashicorp/packer/template/interpolate" 8 "github.com/mitchellh/multistep" 9 ) 10 11 type uploadCmdData struct { 12 AccessKey string 13 BucketName string 14 BundleDirectory string 15 ManifestPath string 16 Region string 17 SecretKey string 18 Token string 19 } 20 21 type StepUploadBundle struct { 22 Debug bool 23 } 24 25 func (s *StepUploadBundle) Run(state multistep.StateBag) multistep.StepAction { 26 comm := state.Get("communicator").(packer.Communicator) 27 config := state.Get("config").(*Config) 28 manifestName := state.Get("manifest_name").(string) 29 manifestPath := state.Get("manifest_path").(string) 30 ui := state.Get("ui").(packer.Ui) 31 32 region, err := config.Region() 33 if err != nil { 34 err := fmt.Errorf("Error retrieving region: %s", err) 35 state.Put("error", err) 36 ui.Error(err.Error()) 37 return multistep.ActionHalt 38 } 39 40 accessKey := config.AccessKey 41 secretKey := config.SecretKey 42 session, err := config.AccessConfig.Session() 43 accessConfig := session.Config 44 var token string 45 if err == nil && accessKey == "" && secretKey == "" { 46 credentials, err := accessConfig.Credentials.Get() 47 if err == nil { 48 accessKey = credentials.AccessKeyID 49 secretKey = credentials.SecretAccessKey 50 token = credentials.SessionToken 51 } 52 } 53 54 config.ctx.Data = uploadCmdData{ 55 AccessKey: accessKey, 56 BucketName: config.S3Bucket, 57 BundleDirectory: config.BundleDestination, 58 ManifestPath: manifestPath, 59 Region: region, 60 SecretKey: secretKey, 61 Token: token, 62 } 63 config.BundleUploadCommand, err = interpolate.Render(config.BundleUploadCommand, &config.ctx) 64 if err != nil { 65 err := fmt.Errorf("Error processing bundle upload command: %s", err) 66 state.Put("error", err) 67 ui.Error(err.Error()) 68 return multistep.ActionHalt 69 } 70 71 ui.Say("Uploading the bundle...") 72 cmd := &packer.RemoteCmd{Command: config.BundleUploadCommand} 73 74 if s.Debug { 75 ui.Say(fmt.Sprintf("Running: %s", config.BundleUploadCommand)) 76 } 77 78 if err := cmd.StartWithUi(comm, ui); err != nil { 79 state.Put("error", fmt.Errorf("Error uploading volume: %s", err)) 80 ui.Error(state.Get("error").(error).Error()) 81 return multistep.ActionHalt 82 } 83 84 if cmd.ExitStatus != 0 { 85 if cmd.ExitStatus == 3 { 86 ui.Error(fmt.Sprintf("Please check that the bucket `%s` "+ 87 "does not exist, or exists and is writable. This error "+ 88 "indicates that the bucket may be owned by somebody else.", 89 config.S3Bucket)) 90 } 91 state.Put("error", fmt.Errorf( 92 "Bundle upload failed. Please see the output above for more\n"+ 93 "details on what went wrong.")) 94 ui.Error(state.Get("error").(error).Error()) 95 return multistep.ActionHalt 96 } 97 98 state.Put("remote_manifest_path", fmt.Sprintf( 99 "%s/%s", config.S3Bucket, manifestName)) 100 101 return multistep.ActionContinue 102 } 103 104 func (s *StepUploadBundle) Cleanup(state multistep.StateBag) {}