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