builder/amazon/ebs: new multistep API
This commit is contained in:
parent
80ed7eddf4
commit
b04cff5a9e
|
@ -5,17 +5,18 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/mitchellh/goamz/ec2"
|
"github.com/mitchellh/goamz/ec2"
|
||||||
|
"github.com/mitchellh/multistep"
|
||||||
"github.com/mitchellh/packer/communicator/ssh"
|
"github.com/mitchellh/packer/communicator/ssh"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// SSHAddress returns a function that can be given to the SSH communicator
|
// SSHAddress returns a function that can be given to the SSH communicator
|
||||||
// for determining the SSH address based on the instance DNS name.
|
// for determining the SSH address based on the instance DNS name.
|
||||||
func SSHAddress(e *ec2.EC2, port int) func(map[string]interface{}) (string, error) {
|
func SSHAddress(e *ec2.EC2, port int) func(multistep.StateBag) (string, error) {
|
||||||
return func(state map[string]interface{}) (string, error) {
|
return func(state multistep.StateBag) (string, error) {
|
||||||
for j := 0; j < 2; j++ {
|
for j := 0; j < 2; j++ {
|
||||||
var host string
|
var host string
|
||||||
i := state["instance"].(*ec2.Instance)
|
i := state.Get("instance").(*ec2.Instance)
|
||||||
if i.DNSName != "" {
|
if i.DNSName != "" {
|
||||||
host = i.DNSName
|
host = i.DNSName
|
||||||
} else if i.VpcId != "" {
|
} else if i.VpcId != "" {
|
||||||
|
@ -35,7 +36,7 @@ func SSHAddress(e *ec2.EC2, port int) func(map[string]interface{}) (string, erro
|
||||||
return "", fmt.Errorf("instance not found: %s", i.InstanceId)
|
return "", fmt.Errorf("instance not found: %s", i.InstanceId)
|
||||||
}
|
}
|
||||||
|
|
||||||
state["instance"] = &r.Reservations[0].Instances[0]
|
state.Put("instance", &r.Reservations[0].Instances[0])
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,9 +47,9 @@ func SSHAddress(e *ec2.EC2, port int) func(map[string]interface{}) (string, erro
|
||||||
// SSHConfig returns a function that can be used for the SSH communicator
|
// SSHConfig returns a function that can be used for the SSH communicator
|
||||||
// config for connecting to the instance created over SSH using the generated
|
// config for connecting to the instance created over SSH using the generated
|
||||||
// private key.
|
// private key.
|
||||||
func SSHConfig(username string) func(map[string]interface{}) (*gossh.ClientConfig, error) {
|
func SSHConfig(username string) func(multistep.StateBag) (*gossh.ClientConfig, error) {
|
||||||
return func(state map[string]interface{}) (*gossh.ClientConfig, error) {
|
return func(state multistep.StateBag) (*gossh.ClientConfig, error) {
|
||||||
privateKey := state["privateKey"].(string)
|
privateKey := state.Get("privateKey").(string)
|
||||||
|
|
||||||
keyring := new(ssh.SimpleKeychain)
|
keyring := new(ssh.SimpleKeychain)
|
||||||
if err := keyring.AddPEMKey(privateKey); err != nil {
|
if err := keyring.AddPEMKey(privateKey); err != nil {
|
||||||
|
|
|
@ -73,11 +73,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{
|
||||||
|
@ -136,18 +136,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,
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,11 +10,11 @@ import (
|
||||||
|
|
||||||
type stepCreateAMI struct{}
|
type stepCreateAMI struct{}
|
||||||
|
|
||||||
func (s *stepCreateAMI) Run(state map[string]interface{}) multistep.StepAction {
|
func (s *stepCreateAMI) 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)
|
||||||
instance := state["instance"].(*ec2.Instance)
|
instance := state.Get("instance").(*ec2.Instance)
|
||||||
ui := state["ui"].(packer.Ui)
|
ui := state.Get("ui").(packer.Ui)
|
||||||
|
|
||||||
// Create the image
|
// Create the image
|
||||||
ui.Say(fmt.Sprintf("Creating the AMI: %s", config.AMIName))
|
ui.Say(fmt.Sprintf("Creating the AMI: %s", config.AMIName))
|
||||||
|
@ -27,7 +27,7 @@ func (s *stepCreateAMI) Run(state map[string]interface{}) multistep.StepAction {
|
||||||
createResp, err := ec2conn.CreateImage(createOpts)
|
createResp, err := ec2conn.CreateImage(createOpts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err := fmt.Errorf("Error creating AMI: %s", err)
|
err := fmt.Errorf("Error creating AMI: %s", err)
|
||||||
state["error"] = err
|
state.Put("error", err)
|
||||||
ui.Error(err.Error())
|
ui.Error(err.Error())
|
||||||
return multistep.ActionHalt
|
return multistep.ActionHalt
|
||||||
}
|
}
|
||||||
|
@ -36,13 +36,13 @@ func (s *stepCreateAMI) Run(state map[string]interface{}) multistep.StepAction {
|
||||||
ui.Say(fmt.Sprintf("AMI: %s", createResp.ImageId))
|
ui.Say(fmt.Sprintf("AMI: %s", createResp.ImageId))
|
||||||
amis := make(map[string]string)
|
amis := make(map[string]string)
|
||||||
amis[ec2conn.Region.Name] = createResp.ImageId
|
amis[ec2conn.Region.Name] = createResp.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, createResp.ImageId); err != nil {
|
if err := awscommon.WaitForAMI(ec2conn, createResp.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
|
||||||
}
|
}
|
||||||
|
@ -50,6 +50,6 @@ func (s *stepCreateAMI) Run(state map[string]interface{}) multistep.StepAction {
|
||||||
return multistep.ActionContinue
|
return multistep.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *stepCreateAMI) Cleanup(map[string]interface{}) {
|
func (s *stepCreateAMI) Cleanup(multistep.StateBag) {
|
||||||
// No cleanup...
|
// No cleanup...
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,17 +10,17 @@ import (
|
||||||
|
|
||||||
type stepStopInstance struct{}
|
type stepStopInstance struct{}
|
||||||
|
|
||||||
func (s *stepStopInstance) Run(state map[string]interface{}) multistep.StepAction {
|
func (s *stepStopInstance) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
ec2conn := state["ec2"].(*ec2.EC2)
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
||||||
instance := state["instance"].(*ec2.Instance)
|
instance := state.Get("instance").(*ec2.Instance)
|
||||||
ui := state["ui"].(packer.Ui)
|
ui := state.Get("ui").(packer.Ui)
|
||||||
|
|
||||||
// Stop the instance so we can create an AMI from it
|
// Stop the instance so we can create an AMI from it
|
||||||
ui.Say("Stopping the source instance...")
|
ui.Say("Stopping the source instance...")
|
||||||
_, err := ec2conn.StopInstances(instance.InstanceId)
|
_, err := ec2conn.StopInstances(instance.InstanceId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err := fmt.Errorf("Error stopping instance: %s", err)
|
err := fmt.Errorf("Error stopping instance: %s", err)
|
||||||
state["error"] = err
|
state.Put("error", err)
|
||||||
ui.Error(err.Error())
|
ui.Error(err.Error())
|
||||||
return multistep.ActionHalt
|
return multistep.ActionHalt
|
||||||
}
|
}
|
||||||
|
@ -37,7 +37,7 @@ func (s *stepStopInstance) Run(state map[string]interface{}) multistep.StepActio
|
||||||
_, err = awscommon.WaitForState(&stateChange)
|
_, err = awscommon.WaitForState(&stateChange)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err := fmt.Errorf("Error waiting for instance to stop: %s", err)
|
err := fmt.Errorf("Error waiting for instance to stop: %s", err)
|
||||||
state["error"] = err
|
state.Put("error", err)
|
||||||
ui.Error(err.Error())
|
ui.Error(err.Error())
|
||||||
return multistep.ActionHalt
|
return multistep.ActionHalt
|
||||||
}
|
}
|
||||||
|
@ -45,6 +45,6 @@ func (s *stepStopInstance) Run(state map[string]interface{}) multistep.StepActio
|
||||||
return multistep.ActionContinue
|
return multistep.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *stepStopInstance) Cleanup(map[string]interface{}) {
|
func (s *stepStopInstance) Cleanup(multistep.StateBag) {
|
||||||
// No cleanup...
|
// No cleanup...
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue