2013-07-21 14:32:59 -04:00
|
|
|
package instance
|
|
|
|
|
|
|
|
import (
|
2013-07-24 16:41:49 -04:00
|
|
|
"bytes"
|
2013-07-24 00:19:44 -04:00
|
|
|
"fmt"
|
2013-07-24 16:41:49 -04:00
|
|
|
"github.com/mitchellh/goamz/ec2"
|
2013-07-21 14:32:59 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
2013-07-24 00:19:44 -04:00
|
|
|
"github.com/mitchellh/packer/packer"
|
2013-07-24 16:41:49 -04:00
|
|
|
"text/template"
|
2013-07-21 14:32:59 -04:00
|
|
|
)
|
|
|
|
|
2013-07-24 16:41:49 -04:00
|
|
|
type bundleCmdData struct {
|
|
|
|
AccountId string
|
|
|
|
Architecture string
|
|
|
|
CertPath string
|
|
|
|
KeyPath string
|
|
|
|
PrivatePath string
|
|
|
|
}
|
|
|
|
|
2013-07-21 14:32:59 -04:00
|
|
|
type StepBundleVolume struct{}
|
|
|
|
|
|
|
|
func (s *StepBundleVolume) Run(state map[string]interface{}) multistep.StepAction {
|
2013-07-24 00:19:44 -04:00
|
|
|
comm := state["communicator"].(packer.Communicator)
|
2013-07-24 16:41:49 -04:00
|
|
|
config := state["config"].(*Config)
|
|
|
|
instance := state["instance"].(*ec2.Instance)
|
2013-07-24 00:19:44 -04:00
|
|
|
ui := state["ui"].(packer.Ui)
|
2013-07-24 16:41:49 -04:00
|
|
|
x509RemoteCertPath := state["x509RemoteCertPath"].(string)
|
|
|
|
x509RemoteKeyPath := state["x509RemoteKeyPath"].(string)
|
2013-07-24 00:19:44 -04:00
|
|
|
|
|
|
|
// Verify the AMI tools are available
|
|
|
|
ui.Say("Checking for EC2 AMI tools...")
|
|
|
|
cmd := &packer.RemoteCmd{Command: "ec2-ami-tools-version"}
|
|
|
|
if err := comm.Start(cmd); err != nil {
|
|
|
|
state["error"] = fmt.Errorf("Error checking for AMI tools: %s", err)
|
|
|
|
ui.Error(state["error"].(error).Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
cmd.Wait()
|
|
|
|
|
|
|
|
if cmd.ExitStatus != 0 {
|
|
|
|
state["error"] = fmt.Errorf(
|
|
|
|
"The EC2 AMI tools could not be detected. These must be manually\n" +
|
|
|
|
"via a provisioner or some other means and are required for Packer\n" +
|
|
|
|
"to create an instance-store AMI.")
|
|
|
|
ui.Error(state["error"].(error).Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-07-24 16:41:49 -04:00
|
|
|
// Bundle the volume
|
|
|
|
var bundleCmd bytes.Buffer
|
|
|
|
tData := bundleCmdData{
|
|
|
|
AccountId: config.AccountId,
|
|
|
|
Architecture: instance.Architecture,
|
|
|
|
CertPath: x509RemoteCertPath,
|
|
|
|
KeyPath: x509RemoteKeyPath,
|
|
|
|
PrivatePath: config.X509UploadPath,
|
|
|
|
}
|
|
|
|
t := template.Must(template.New("bundleCmd").Parse(config.BundleVolCommand))
|
|
|
|
t.Execute(&bundleCmd, tData)
|
|
|
|
|
|
|
|
ui.Say("Bundling the volume...")
|
|
|
|
cmd = new(packer.RemoteCmd)
|
|
|
|
cmd.Command = bundleCmd.String()
|
|
|
|
if err := cmd.StartWithUi(comm, ui); err != nil {
|
|
|
|
state["error"] = fmt.Errorf("Error bundling volume: %s", err)
|
|
|
|
ui.Error(state["error"].(error).Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-07-24 16:44:58 -04:00
|
|
|
if cmd.ExitStatus != 0 {
|
|
|
|
state["error"] = fmt.Errorf(
|
|
|
|
"Volume bundling failed. Please see the output above for more\n" +
|
|
|
|
"details on what went wrong.")
|
|
|
|
ui.Error(state["error"].(error).Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-07-21 14:32:59 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepBundleVolume) Cleanup(map[string]interface{}) {}
|