github.com/rothwerx/packer@v0.9.0/builder/amazon/instance/step_bundle_volume.go (about)

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