github.com/kaixiang/packer@v0.5.2-0.20140114230416-1f5786b0d7f1/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  	// See GH-729 and http://goo.gl/rNZiCs
    36  	regionName := region.Name
    37  	if regionName == "us-east-1" {
    38  		regionName = "US"
    39  	}
    40  
    41  	config.BundleUploadCommand, err = config.tpl.Process(config.BundleUploadCommand, uploadCmdData{
    42  		AccessKey:       config.AccessKey,
    43  		BucketName:      config.S3Bucket,
    44  		BundleDirectory: config.BundleDestination,
    45  		ManifestPath:    manifestPath,
    46  		Region:          regionName,
    47  		SecretKey:       config.SecretKey,
    48  	})
    49  	if err != nil {
    50  		err := fmt.Errorf("Error processing bundle upload command: %s", err)
    51  		state.Put("error", err)
    52  		ui.Error(err.Error())
    53  		return multistep.ActionHalt
    54  	}
    55  
    56  	ui.Say("Uploading the bundle...")
    57  	cmd := &packer.RemoteCmd{Command: config.BundleUploadCommand}
    58  	if err := cmd.StartWithUi(comm, ui); err != nil {
    59  		state.Put("error", fmt.Errorf("Error uploading volume: %s", err))
    60  		ui.Error(state.Get("error").(error).Error())
    61  		return multistep.ActionHalt
    62  	}
    63  
    64  	if cmd.ExitStatus != 0 {
    65  		state.Put("error", fmt.Errorf(
    66  			"Bundle upload failed. Please see the output above for more\n"+
    67  				"details on what went wrong."))
    68  		ui.Error(state.Get("error").(error).Error())
    69  		return multistep.ActionHalt
    70  	}
    71  
    72  	state.Put("remote_manifest_path", fmt.Sprintf(
    73  		"%s/%s", config.S3Bucket, manifestName))
    74  
    75  	return multistep.ActionContinue
    76  }
    77  
    78  func (s *StepUploadBundle) Cleanup(state multistep.StateBag) {}