github.com/sneal/packer@v0.5.2/builder/amazon/instance/step_upload_bundle.go (about)

     1  package instance
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/mitchellh/multistep"
     6  	"github.com/mitchellh/packer/packer"
     7  )
     8  
     9  type uploadCmdData struct {
    10  	AccessKey       string
    11  	BucketName      string
    12  	BundleDirectory string
    13  	ManifestPath    string
    14  	Region          string
    15  	SecretKey       string
    16  }
    17  
    18  type StepUploadBundle struct{}
    19  
    20  func (s *StepUploadBundle) Run(state multistep.StateBag) multistep.StepAction {
    21  	comm := state.Get("communicator").(packer.Communicator)
    22  	config := state.Get("config").(*Config)
    23  	manifestName := state.Get("manifest_name").(string)
    24  	manifestPath := state.Get("manifest_path").(string)
    25  	ui := state.Get("ui").(packer.Ui)
    26  
    27  	region, err := config.Region()
    28  	if err != nil {
    29  		err := fmt.Errorf("Error retrieving region: %s", err)
    30  		state.Put("error", err)
    31  		ui.Error(err.Error())
    32  		return multistep.ActionHalt
    33  	}
    34  
    35  	config.BundleUploadCommand, err = config.tpl.Process(config.BundleUploadCommand, uploadCmdData{
    36  		AccessKey:       config.AccessKey,
    37  		BucketName:      config.S3Bucket,
    38  		BundleDirectory: config.BundleDestination,
    39  		ManifestPath:    manifestPath,
    40  		Region:          region.Name,
    41  		SecretKey:       config.SecretKey,
    42  	})
    43  	if err != nil {
    44  		err := fmt.Errorf("Error processing bundle upload command: %s", err)
    45  		state.Put("error", err)
    46  		ui.Error(err.Error())
    47  		return multistep.ActionHalt
    48  	}
    49  
    50  	ui.Say("Uploading the bundle...")
    51  	cmd := &packer.RemoteCmd{Command: config.BundleUploadCommand}
    52  	if err := cmd.StartWithUi(comm, ui); err != nil {
    53  		state.Put("error", fmt.Errorf("Error uploading volume: %s", err))
    54  		ui.Error(state.Get("error").(error).Error())
    55  		return multistep.ActionHalt
    56  	}
    57  
    58  	if cmd.ExitStatus != 0 {
    59  		state.Put("error", fmt.Errorf(
    60  			"Bundle upload failed. Please see the output above for more\n"+
    61  				"details on what went wrong."))
    62  		ui.Error(state.Get("error").(error).Error())
    63  		return multistep.ActionHalt
    64  	}
    65  
    66  	state.Put("remote_manifest_path", fmt.Sprintf(
    67  		"%s/%s", config.S3Bucket, manifestName))
    68  
    69  	return multistep.ActionContinue
    70  }
    71  
    72  func (s *StepUploadBundle) Cleanup(state multistep.StateBag) {}