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

     1  package instance
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/aws/aws-sdk-go/service/ec2"
     8  	"github.com/hashicorp/packer/helper/multistep"
     9  	"github.com/hashicorp/packer/packer"
    10  	"github.com/hashicorp/packer/template/interpolate"
    11  )
    12  
    13  type bundleCmdData struct {
    14  	AccountId    string
    15  	Architecture string
    16  	CertPath     string
    17  	Destination  string
    18  	KeyPath      string
    19  	Prefix       string
    20  	PrivatePath  string
    21  }
    22  
    23  type StepBundleVolume struct {
    24  	Debug bool
    25  }
    26  
    27  func (s *StepBundleVolume) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
    28  	comm := state.Get("communicator").(packer.Communicator)
    29  	config := state.Get("config").(*Config)
    30  	instance := state.Get("instance").(*ec2.Instance)
    31  	ui := state.Get("ui").(packer.Ui)
    32  	x509RemoteCertPath := state.Get("x509RemoteCertPath").(string)
    33  	x509RemoteKeyPath := state.Get("x509RemoteKeyPath").(string)
    34  
    35  	// Bundle the volume
    36  	var err error
    37  	config.ctx.Data = bundleCmdData{
    38  		AccountId:    config.AccountId,
    39  		Architecture: *instance.Architecture,
    40  		CertPath:     x509RemoteCertPath,
    41  		Destination:  config.BundleDestination,
    42  		KeyPath:      x509RemoteKeyPath,
    43  		Prefix:       config.BundlePrefix,
    44  		PrivatePath:  config.X509UploadPath,
    45  	}
    46  	config.BundleVolCommand, err = interpolate.Render(config.BundleVolCommand, &config.ctx)
    47  	if err != nil {
    48  		err := fmt.Errorf("Error processing bundle volume command: %s", err)
    49  		state.Put("error", err)
    50  		ui.Error(err.Error())
    51  		return multistep.ActionHalt
    52  	}
    53  
    54  	ui.Say("Bundling the volume...")
    55  	cmd := new(packer.RemoteCmd)
    56  	cmd.Command = config.BundleVolCommand
    57  
    58  	if s.Debug {
    59  		ui.Say(fmt.Sprintf("Running: %s", config.BundleVolCommand))
    60  	}
    61  
    62  	if err := cmd.StartWithUi(comm, ui); err != nil {
    63  		state.Put("error", fmt.Errorf("Error bundling 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  			"Volume bundling failed. Please see the output above for more\n"+
    71  				"details on what went wrong.\n\n"+
    72  				"One common cause for this error is ec2-bundle-vol not being\n"+
    73  				"available on the target instance."))
    74  		ui.Error(state.Get("error").(error).Error())
    75  		return multistep.ActionHalt
    76  	}
    77  
    78  	// Store the manifest path
    79  	manifestName := config.BundlePrefix + ".manifest.xml"
    80  	state.Put("manifest_name", manifestName)
    81  	state.Put("manifest_path", fmt.Sprintf(
    82  		"%s/%s", config.BundleDestination, manifestName))
    83  
    84  	return multistep.ActionContinue
    85  }
    86  
    87  func (s *StepBundleVolume) Cleanup(multistep.StateBag) {}