github.com/emate/packer@v0.8.1-0.20150625195101-fe0fde195dc6/builder/amazon/instance/step_upload_bundle.go (about)

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