github.com/askholme/packer@v0.7.2-0.20140924152349-70d9566a6852/builder/amazon/instance/step_bundle_volume.go (about)

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