github.com/rothwerx/packer@v0.9.0/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  	accessKey := config.AccessKey
    40  	secretKey := config.SecretKey
    41  	accessConfig, err := config.AccessConfig.Config()
    42  	if err == nil && accessKey == "" && secretKey == "" {
    43  		credentials, err := accessConfig.Credentials.Get()
    44  		if err == nil {
    45  			accessKey = credentials.AccessKeyID
    46  			secretKey = credentials.SecretAccessKey
    47  		}
    48  	}
    49  
    50  	config.ctx.Data = uploadCmdData{
    51  		AccessKey:       accessKey,
    52  		BucketName:      config.S3Bucket,
    53  		BundleDirectory: config.BundleDestination,
    54  		ManifestPath:    manifestPath,
    55  		Region:          region,
    56  		SecretKey:       secretKey,
    57  	}
    58  	config.BundleUploadCommand, err = interpolate.Render(config.BundleUploadCommand, &config.ctx)
    59  	if err != nil {
    60  		err := fmt.Errorf("Error processing bundle upload command: %s", err)
    61  		state.Put("error", err)
    62  		ui.Error(err.Error())
    63  		return multistep.ActionHalt
    64  	}
    65  
    66  	ui.Say("Uploading the bundle...")
    67  	cmd := &packer.RemoteCmd{Command: config.BundleUploadCommand}
    68  
    69  	if s.Debug {
    70  		ui.Say(fmt.Sprintf("Running: %s", config.BundleUploadCommand))
    71  	}
    72  
    73  	if err := cmd.StartWithUi(comm, ui); err != nil {
    74  		state.Put("error", fmt.Errorf("Error uploading volume: %s", err))
    75  		ui.Error(state.Get("error").(error).Error())
    76  		return multistep.ActionHalt
    77  	}
    78  
    79  	if cmd.ExitStatus != 0 {
    80  		state.Put("error", fmt.Errorf(
    81  			"Bundle upload failed. Please see the output above for more\n"+
    82  				"details on what went wrong."))
    83  		ui.Error(state.Get("error").(error).Error())
    84  		return multistep.ActionHalt
    85  	}
    86  
    87  	state.Put("remote_manifest_path", fmt.Sprintf(
    88  		"%s/%s", config.S3Bucket, manifestName))
    89  
    90  	return multistep.ActionContinue
    91  }
    92  
    93  func (s *StepUploadBundle) Cleanup(state multistep.StateBag) {}