github.phpd.cn/hashicorp/packer@v1.3.2/builder/amazon/instance/step_upload_bundle.go (about)

     1  package instance
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/hashicorp/packer/helper/multistep"
     8  	"github.com/hashicorp/packer/packer"
     9  	"github.com/hashicorp/packer/template/interpolate"
    10  )
    11  
    12  type uploadCmdData struct {
    13  	AccessKey       string
    14  	BucketName      string
    15  	BundleDirectory string
    16  	ManifestPath    string
    17  	Region          string
    18  	SecretKey       string
    19  	Token           string
    20  }
    21  
    22  type StepUploadBundle struct {
    23  	Debug bool
    24  }
    25  
    26  func (s *StepUploadBundle) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
    27  	comm := state.Get("communicator").(packer.Communicator)
    28  	config := state.Get("config").(*Config)
    29  	manifestName := state.Get("manifest_name").(string)
    30  	manifestPath := state.Get("manifest_path").(string)
    31  	ui := state.Get("ui").(packer.Ui)
    32  
    33  	accessKey := config.AccessKey
    34  	secretKey := config.SecretKey
    35  	session, err := config.AccessConfig.Session()
    36  	region := *session.Config.Region
    37  	accessConfig := session.Config
    38  	var token string
    39  	if err == nil && accessKey == "" && secretKey == "" {
    40  		credentials, err := accessConfig.Credentials.Get()
    41  		if err == nil {
    42  			accessKey = credentials.AccessKeyID
    43  			secretKey = credentials.SecretAccessKey
    44  			token = credentials.SessionToken
    45  		}
    46  	}
    47  
    48  	config.ctx.Data = uploadCmdData{
    49  		AccessKey:       accessKey,
    50  		BucketName:      config.S3Bucket,
    51  		BundleDirectory: config.BundleDestination,
    52  		ManifestPath:    manifestPath,
    53  		Region:          region,
    54  		SecretKey:       secretKey,
    55  		Token:           token,
    56  	}
    57  	config.BundleUploadCommand, err = interpolate.Render(config.BundleUploadCommand, &config.ctx)
    58  	if err != nil {
    59  		err := fmt.Errorf("Error processing bundle upload command: %s", err)
    60  		state.Put("error", err)
    61  		ui.Error(err.Error())
    62  		return multistep.ActionHalt
    63  	}
    64  
    65  	ui.Say("Uploading the bundle...")
    66  	cmd := &packer.RemoteCmd{Command: config.BundleUploadCommand}
    67  
    68  	if s.Debug {
    69  		ui.Say(fmt.Sprintf("Running: %s", config.BundleUploadCommand))
    70  	}
    71  
    72  	if err := cmd.StartWithUi(comm, ui); err != nil {
    73  		state.Put("error", fmt.Errorf("Error uploading volume: %s", err))
    74  		ui.Error(state.Get("error").(error).Error())
    75  		return multistep.ActionHalt
    76  	}
    77  
    78  	if cmd.ExitStatus != 0 {
    79  		if cmd.ExitStatus == 3 {
    80  			ui.Error(fmt.Sprintf("Please check that the bucket `%s` "+
    81  				"does not exist, or exists and is writable. This error "+
    82  				"indicates that the bucket may be owned by somebody else.",
    83  				config.S3Bucket))
    84  		}
    85  		state.Put("error", fmt.Errorf(
    86  			"Bundle upload failed. Please see the output above for more\n"+
    87  				"details on what went wrong."))
    88  		ui.Error(state.Get("error").(error).Error())
    89  		return multistep.ActionHalt
    90  	}
    91  
    92  	state.Put("remote_manifest_path", fmt.Sprintf(
    93  		"%s/%s", config.S3Bucket, manifestName))
    94  
    95  	return multistep.ActionContinue
    96  }
    97  
    98  func (s *StepUploadBundle) Cleanup(state multistep.StateBag) {}