builder/amazon/instance: new multistep API
This commit is contained in:
parent
b5606af9e2
commit
8e7c2796fc
@ -176,11 +176,11 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
|
|||||||
ec2conn := ec2.New(auth, region)
|
ec2conn := ec2.New(auth, region)
|
||||||
|
|
||||||
// Setup the state bag and initial state for the steps
|
// Setup the state bag and initial state for the steps
|
||||||
state := make(map[string]interface{})
|
state := new(multistep.BasicStateBag)
|
||||||
state["config"] = &b.config
|
state.Put("config", &b.config)
|
||||||
state["ec2"] = ec2conn
|
state.Put("ec2", ec2conn)
|
||||||
state["hook"] = hook
|
state.Put("hook", hook)
|
||||||
state["ui"] = ui
|
state.Put("ui", ui)
|
||||||
|
|
||||||
// Build the steps
|
// Build the steps
|
||||||
steps := []multistep.Step{
|
steps := []multistep.Step{
|
||||||
@ -242,18 +242,18 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
|
|||||||
b.runner.Run(state)
|
b.runner.Run(state)
|
||||||
|
|
||||||
// If there was an error, return that
|
// If there was an error, return that
|
||||||
if rawErr, ok := state["error"]; ok {
|
if rawErr, ok := state.GetOk("error"); ok {
|
||||||
return nil, rawErr.(error)
|
return nil, rawErr.(error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// If there are no AMIs, then just return
|
// If there are no AMIs, then just return
|
||||||
if _, ok := state["amis"]; !ok {
|
if _, ok := state.GetOk("amis"); !ok {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build the artifact and return it
|
// Build the artifact and return it
|
||||||
artifact := &awscommon.Artifact{
|
artifact := &awscommon.Artifact{
|
||||||
Amis: state["amis"].(map[string]string),
|
Amis: state.Get("amis").(map[string]string),
|
||||||
BuilderIdValue: BuilderId,
|
BuilderIdValue: BuilderId,
|
||||||
Conn: ec2conn,
|
Conn: ec2conn,
|
||||||
}
|
}
|
||||||
|
@ -19,13 +19,13 @@ type bundleCmdData struct {
|
|||||||
|
|
||||||
type StepBundleVolume struct{}
|
type StepBundleVolume struct{}
|
||||||
|
|
||||||
func (s *StepBundleVolume) Run(state map[string]interface{}) multistep.StepAction {
|
func (s *StepBundleVolume) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
comm := state["communicator"].(packer.Communicator)
|
comm := state.Get("communicator").(packer.Communicator)
|
||||||
config := state["config"].(*Config)
|
config := state.Get("config").(*Config)
|
||||||
instance := state["instance"].(*ec2.Instance)
|
instance := state.Get("instance").(*ec2.Instance)
|
||||||
ui := state["ui"].(packer.Ui)
|
ui := state.Get("ui").(packer.Ui)
|
||||||
x509RemoteCertPath := state["x509RemoteCertPath"].(string)
|
x509RemoteCertPath := state.Get("x509RemoteCertPath").(string)
|
||||||
x509RemoteKeyPath := state["x509RemoteKeyPath"].(string)
|
x509RemoteKeyPath := state.Get("x509RemoteKeyPath").(string)
|
||||||
|
|
||||||
// Bundle the volume
|
// Bundle the volume
|
||||||
var err error
|
var err error
|
||||||
@ -40,7 +40,7 @@ func (s *StepBundleVolume) Run(state map[string]interface{}) multistep.StepActio
|
|||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err := fmt.Errorf("Error processing bundle volume command: %s", err)
|
err := fmt.Errorf("Error processing bundle volume command: %s", err)
|
||||||
state["error"] = err
|
state.Put("error", err)
|
||||||
ui.Error(err.Error())
|
ui.Error(err.Error())
|
||||||
return multistep.ActionHalt
|
return multistep.ActionHalt
|
||||||
}
|
}
|
||||||
@ -49,26 +49,26 @@ func (s *StepBundleVolume) Run(state map[string]interface{}) multistep.StepActio
|
|||||||
cmd := new(packer.RemoteCmd)
|
cmd := new(packer.RemoteCmd)
|
||||||
cmd.Command = config.BundleVolCommand
|
cmd.Command = config.BundleVolCommand
|
||||||
if err := cmd.StartWithUi(comm, ui); err != nil {
|
if err := cmd.StartWithUi(comm, ui); err != nil {
|
||||||
state["error"] = fmt.Errorf("Error bundling volume: %s", err)
|
state.Put("error", fmt.Errorf("Error bundling volume: %s", err))
|
||||||
ui.Error(state["error"].(error).Error())
|
ui.Error(state.Get("error").(error).Error())
|
||||||
return multistep.ActionHalt
|
return multistep.ActionHalt
|
||||||
}
|
}
|
||||||
|
|
||||||
if cmd.ExitStatus != 0 {
|
if cmd.ExitStatus != 0 {
|
||||||
state["error"] = fmt.Errorf(
|
state.Put("error", fmt.Errorf(
|
||||||
"Volume bundling failed. Please see the output above for more\n"+
|
"Volume bundling failed. Please see the output above for more\n"+
|
||||||
"details on what went wrong.")
|
"details on what went wrong."))
|
||||||
ui.Error(state["error"].(error).Error())
|
ui.Error(state.Get("error").(error).Error())
|
||||||
return multistep.ActionHalt
|
return multistep.ActionHalt
|
||||||
}
|
}
|
||||||
|
|
||||||
// Store the manifest path
|
// Store the manifest path
|
||||||
manifestName := config.BundlePrefix + ".manifest.xml"
|
manifestName := config.BundlePrefix + ".manifest.xml"
|
||||||
state["manifest_name"] = manifestName
|
state.Put("manifest_name", manifestName)
|
||||||
state["manifest_path"] = fmt.Sprintf(
|
state.Put("manifest_path", fmt.Sprintf(
|
||||||
"%s/%s", config.BundleDestination, manifestName)
|
"%s/%s", config.BundleDestination, manifestName))
|
||||||
|
|
||||||
return multistep.ActionContinue
|
return multistep.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StepBundleVolume) Cleanup(map[string]interface{}) {}
|
func (s *StepBundleVolume) Cleanup(multistep.StateBag) {}
|
||||||
|
@ -10,11 +10,11 @@ import (
|
|||||||
|
|
||||||
type StepRegisterAMI struct{}
|
type StepRegisterAMI struct{}
|
||||||
|
|
||||||
func (s *StepRegisterAMI) Run(state map[string]interface{}) multistep.StepAction {
|
func (s *StepRegisterAMI) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
config := state["config"].(*Config)
|
config := state.Get("config").(*Config)
|
||||||
ec2conn := state["ec2"].(*ec2.EC2)
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
||||||
manifestPath := state["remote_manifest_path"].(string)
|
manifestPath := state.Get("remote_manifest_path").(string)
|
||||||
ui := state["ui"].(packer.Ui)
|
ui := state.Get("ui").(packer.Ui)
|
||||||
|
|
||||||
ui.Say("Registering the AMI...")
|
ui.Say("Registering the AMI...")
|
||||||
registerOpts := &ec2.RegisterImage{
|
registerOpts := &ec2.RegisterImage{
|
||||||
@ -25,8 +25,8 @@ func (s *StepRegisterAMI) Run(state map[string]interface{}) multistep.StepAction
|
|||||||
|
|
||||||
registerResp, err := ec2conn.RegisterImage(registerOpts)
|
registerResp, err := ec2conn.RegisterImage(registerOpts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
state["error"] = fmt.Errorf("Error registering AMI: %s", err)
|
state.Put("error", fmt.Errorf("Error registering AMI: %s", err))
|
||||||
ui.Error(state["error"].(error).Error())
|
ui.Error(state.Get("error").(error).Error())
|
||||||
return multistep.ActionHalt
|
return multistep.ActionHalt
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -34,13 +34,13 @@ func (s *StepRegisterAMI) Run(state map[string]interface{}) multistep.StepAction
|
|||||||
ui.Say(fmt.Sprintf("AMI: %s", registerResp.ImageId))
|
ui.Say(fmt.Sprintf("AMI: %s", registerResp.ImageId))
|
||||||
amis := make(map[string]string)
|
amis := make(map[string]string)
|
||||||
amis[ec2conn.Region.Name] = registerResp.ImageId
|
amis[ec2conn.Region.Name] = registerResp.ImageId
|
||||||
state["amis"] = amis
|
state.Put("amis", amis)
|
||||||
|
|
||||||
// Wait for the image to become ready
|
// Wait for the image to become ready
|
||||||
ui.Say("Waiting for AMI to become ready...")
|
ui.Say("Waiting for AMI to become ready...")
|
||||||
if err := awscommon.WaitForAMI(ec2conn, registerResp.ImageId); err != nil {
|
if err := awscommon.WaitForAMI(ec2conn, registerResp.ImageId); err != nil {
|
||||||
err := fmt.Errorf("Error waiting for AMI: %s", err)
|
err := fmt.Errorf("Error waiting for AMI: %s", err)
|
||||||
state["error"] = err
|
state.Put("error", err)
|
||||||
ui.Error(err.Error())
|
ui.Error(err.Error())
|
||||||
return multistep.ActionHalt
|
return multistep.ActionHalt
|
||||||
}
|
}
|
||||||
@ -48,4 +48,4 @@ func (s *StepRegisterAMI) Run(state map[string]interface{}) multistep.StepAction
|
|||||||
return multistep.ActionContinue
|
return multistep.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StepRegisterAMI) Cleanup(map[string]interface{}) {}
|
func (s *StepRegisterAMI) Cleanup(multistep.StateBag) {}
|
||||||
|
@ -16,12 +16,12 @@ type uploadCmdData struct {
|
|||||||
|
|
||||||
type StepUploadBundle struct{}
|
type StepUploadBundle struct{}
|
||||||
|
|
||||||
func (s *StepUploadBundle) Run(state map[string]interface{}) multistep.StepAction {
|
func (s *StepUploadBundle) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
comm := state["communicator"].(packer.Communicator)
|
comm := state.Get("communicator").(packer.Communicator)
|
||||||
config := state["config"].(*Config)
|
config := state.Get("config").(*Config)
|
||||||
manifestName := state["manifest_name"].(string)
|
manifestName := state.Get("manifest_name").(string)
|
||||||
manifestPath := state["manifest_path"].(string)
|
manifestPath := state.Get("manifest_path").(string)
|
||||||
ui := state["ui"].(packer.Ui)
|
ui := state.Get("ui").(packer.Ui)
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
config.BundleUploadCommand, err = config.tpl.Process(config.BundleUploadCommand, uploadCmdData{
|
config.BundleUploadCommand, err = config.tpl.Process(config.BundleUploadCommand, uploadCmdData{
|
||||||
@ -33,7 +33,7 @@ func (s *StepUploadBundle) Run(state map[string]interface{}) multistep.StepActio
|
|||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err := fmt.Errorf("Error processing bundle upload command: %s", err)
|
err := fmt.Errorf("Error processing bundle upload command: %s", err)
|
||||||
state["error"] = err
|
state.Put("error", err)
|
||||||
ui.Error(err.Error())
|
ui.Error(err.Error())
|
||||||
return multistep.ActionHalt
|
return multistep.ActionHalt
|
||||||
}
|
}
|
||||||
@ -41,23 +41,23 @@ func (s *StepUploadBundle) Run(state map[string]interface{}) multistep.StepActio
|
|||||||
ui.Say("Uploading the bundle...")
|
ui.Say("Uploading the bundle...")
|
||||||
cmd := &packer.RemoteCmd{Command: config.BundleUploadCommand}
|
cmd := &packer.RemoteCmd{Command: config.BundleUploadCommand}
|
||||||
if err := cmd.StartWithUi(comm, ui); err != nil {
|
if err := cmd.StartWithUi(comm, ui); err != nil {
|
||||||
state["error"] = fmt.Errorf("Error uploading volume: %s", err)
|
state.Put("error", fmt.Errorf("Error uploading volume: %s", err))
|
||||||
ui.Error(state["error"].(error).Error())
|
ui.Error(state.Get("error").(error).Error())
|
||||||
return multistep.ActionHalt
|
return multistep.ActionHalt
|
||||||
}
|
}
|
||||||
|
|
||||||
if cmd.ExitStatus != 0 {
|
if cmd.ExitStatus != 0 {
|
||||||
state["error"] = fmt.Errorf(
|
state.Put("error", fmt.Errorf(
|
||||||
"Bundle upload failed. Please see the output above for more\n"+
|
"Bundle upload failed. Please see the output above for more\n"+
|
||||||
"details on what went wrong.")
|
"details on what went wrong."))
|
||||||
ui.Error(state["error"].(error).Error())
|
ui.Error(state.Get("error").(error).Error())
|
||||||
return multistep.ActionHalt
|
return multistep.ActionHalt
|
||||||
}
|
}
|
||||||
|
|
||||||
state["remote_manifest_path"] = fmt.Sprintf(
|
state.Put("remote_manifest_path", fmt.Sprintf(
|
||||||
"%s/%s", config.S3Bucket, manifestName)
|
"%s/%s", config.S3Bucket, manifestName))
|
||||||
|
|
||||||
return multistep.ActionContinue
|
return multistep.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StepUploadBundle) Cleanup(state map[string]interface{}) {}
|
func (s *StepUploadBundle) Cleanup(state multistep.StateBag) {}
|
||||||
|
@ -9,34 +9,34 @@ import (
|
|||||||
|
|
||||||
type StepUploadX509Cert struct{}
|
type StepUploadX509Cert struct{}
|
||||||
|
|
||||||
func (s *StepUploadX509Cert) Run(state map[string]interface{}) multistep.StepAction {
|
func (s *StepUploadX509Cert) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
comm := state["communicator"].(packer.Communicator)
|
comm := state.Get("communicator").(packer.Communicator)
|
||||||
config := state["config"].(*Config)
|
config := state.Get("config").(*Config)
|
||||||
ui := state["ui"].(packer.Ui)
|
ui := state.Get("ui").(packer.Ui)
|
||||||
|
|
||||||
x509RemoteCertPath := config.X509UploadPath + "/cert.pem"
|
x509RemoteCertPath := config.X509UploadPath + "/cert.pem"
|
||||||
x509RemoteKeyPath := config.X509UploadPath + "/key.pem"
|
x509RemoteKeyPath := config.X509UploadPath + "/key.pem"
|
||||||
|
|
||||||
ui.Say("Uploading X509 Certificate...")
|
ui.Say("Uploading X509 Certificate...")
|
||||||
if err := s.uploadSingle(comm, x509RemoteCertPath, config.X509CertPath); err != nil {
|
if err := s.uploadSingle(comm, x509RemoteCertPath, config.X509CertPath); err != nil {
|
||||||
state["error"] = fmt.Errorf("Error uploading X509 cert: %s", err)
|
state.Put("error", fmt.Errorf("Error uploading X509 cert: %s", err))
|
||||||
ui.Error(state["error"].(error).Error())
|
ui.Error(state.Get("error").(error).Error())
|
||||||
return multistep.ActionHalt
|
return multistep.ActionHalt
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := s.uploadSingle(comm, x509RemoteKeyPath, config.X509KeyPath); err != nil {
|
if err := s.uploadSingle(comm, x509RemoteKeyPath, config.X509KeyPath); err != nil {
|
||||||
state["error"] = fmt.Errorf("Error uploading X509 cert: %s", err)
|
state.Put("error", fmt.Errorf("Error uploading X509 cert: %s", err))
|
||||||
ui.Error(state["error"].(error).Error())
|
ui.Error(state.Get("error").(error).Error())
|
||||||
return multistep.ActionHalt
|
return multistep.ActionHalt
|
||||||
}
|
}
|
||||||
|
|
||||||
state["x509RemoteCertPath"] = x509RemoteCertPath
|
state.Put("x509RemoteCertPath", x509RemoteCertPath)
|
||||||
state["x509RemoteKeyPath"] = x509RemoteKeyPath
|
state.Put("x509RemoteKeyPath", x509RemoteKeyPath)
|
||||||
|
|
||||||
return multistep.ActionContinue
|
return multistep.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StepUploadX509Cert) Cleanup(map[string]interface{}) {}
|
func (s *StepUploadX509Cert) Cleanup(multistep.StateBag) {}
|
||||||
|
|
||||||
func (s *StepUploadX509Cert) uploadSingle(comm packer.Communicator, dst, src string) error {
|
func (s *StepUploadX509Cert) uploadSingle(comm packer.Communicator, dst, src string) error {
|
||||||
f, err := os.Open(src)
|
f, err := os.Open(src)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user