Commit 64ced7ff authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

builder/amazon: extract StepRunSourceInstance

parent 30ab7038
package ebs package common
import ( import (
"fmt" "fmt"
...@@ -7,7 +7,7 @@ import ( ...@@ -7,7 +7,7 @@ import (
"time" "time"
) )
func waitForState(ec2conn *ec2.EC2, originalInstance *ec2.Instance, pending []string, target string) (i *ec2.Instance, err error) { func WaitForState(ec2conn *ec2.EC2, originalInstance *ec2.Instance, pending []string, target string) (i *ec2.Instance, err error) {
log.Printf("Waiting for instance state to become: %s", target) log.Printf("Waiting for instance state to become: %s", target)
i = originalInstance i = originalInstance
......
package ebs package common
import ( import (
"fmt" "fmt"
...@@ -8,12 +8,15 @@ import ( ...@@ -8,12 +8,15 @@ import (
"log" "log"
) )
type stepRunSourceInstance struct { type StepRunSourceInstance struct {
ExpectedRootDevice string
InstanceType string
SourceAMI string
instance *ec2.Instance instance *ec2.Instance
} }
func (s *stepRunSourceInstance) Run(state map[string]interface{}) multistep.StepAction { func (s *StepRunSourceInstance) Run(state map[string]interface{}) multistep.StepAction {
config := state["config"].(config)
ec2conn := state["ec2"].(*ec2.EC2) ec2conn := state["ec2"].(*ec2.EC2)
keyName := state["keyPair"].(string) keyName := state["keyPair"].(string)
securityGroupId := state["securityGroupId"].(string) securityGroupId := state["securityGroupId"].(string)
...@@ -21,8 +24,8 @@ func (s *stepRunSourceInstance) Run(state map[string]interface{}) multistep.Step ...@@ -21,8 +24,8 @@ func (s *stepRunSourceInstance) Run(state map[string]interface{}) multistep.Step
runOpts := &ec2.RunInstances{ runOpts := &ec2.RunInstances{
KeyName: keyName, KeyName: keyName,
ImageId: config.SourceAmi, ImageId: s.SourceAMI,
InstanceType: config.InstanceType, InstanceType: s.InstanceType,
MinCount: 0, MinCount: 0,
MaxCount: 0, MaxCount: 0,
SecurityGroups: []ec2.SecurityGroup{ec2.SecurityGroup{Id: securityGroupId}}, SecurityGroups: []ec2.SecurityGroup{ec2.SecurityGroup{Id: securityGroupId}},
...@@ -30,16 +33,17 @@ func (s *stepRunSourceInstance) Run(state map[string]interface{}) multistep.Step ...@@ -30,16 +33,17 @@ func (s *stepRunSourceInstance) Run(state map[string]interface{}) multistep.Step
} }
ui.Say("Launching a source AWS instance...") ui.Say("Launching a source AWS instance...")
imageResp, err := ec2conn.Images([]string{config.SourceAmi}, ec2.NewFilter()) imageResp, err := ec2conn.Images([]string{s.SourceAMI}, ec2.NewFilter())
if err != nil { if err != nil {
state["error"] = fmt.Errorf("There was a problem with the source AMI: %s", err) state["error"] = fmt.Errorf("There was a problem with the source AMI: %s", err)
return multistep.ActionHalt return multistep.ActionHalt
} }
if imageResp.Images[0].RootDeviceType != "ebs" { if s.ExpectedRootDevice != "" && imageResp.Images[0].RootDeviceType != s.ExpectedRootDevice {
state["error"] = fmt.Errorf( state["error"] = fmt.Errorf(
"The provided source AMI is instance-store based. The\n" + "The provided source AMI has an invalid root device type.\n"+
"amazon-ebs bundler can only work with EBS based AMIs.") "Expected '%s', got '%s'.",
s.ExpectedRootDevice, imageResp.Images[0].RootDeviceType)
return multistep.ActionHalt return multistep.ActionHalt
} }
...@@ -55,7 +59,7 @@ func (s *stepRunSourceInstance) Run(state map[string]interface{}) multistep.Step ...@@ -55,7 +59,7 @@ func (s *stepRunSourceInstance) Run(state map[string]interface{}) multistep.Step
log.Printf("instance id: %s", s.instance.InstanceId) log.Printf("instance id: %s", s.instance.InstanceId)
ui.Say(fmt.Sprintf("Waiting for instance (%s) to become ready...", s.instance.InstanceId)) ui.Say(fmt.Sprintf("Waiting for instance (%s) to become ready...", s.instance.InstanceId))
s.instance, err = waitForState(ec2conn, s.instance, []string{"pending"}, "running") s.instance, err = WaitForState(ec2conn, s.instance, []string{"pending"}, "running")
if err != nil { if err != nil {
err := fmt.Errorf("Error waiting for instance (%s) to become ready: %s", s.instance.InstanceId, err) err := fmt.Errorf("Error waiting for instance (%s) to become ready: %s", s.instance.InstanceId, err)
state["error"] = err state["error"] = err
...@@ -68,7 +72,7 @@ func (s *stepRunSourceInstance) Run(state map[string]interface{}) multistep.Step ...@@ -68,7 +72,7 @@ func (s *stepRunSourceInstance) Run(state map[string]interface{}) multistep.Step
return multistep.ActionContinue return multistep.ActionContinue
} }
func (s *stepRunSourceInstance) Cleanup(state map[string]interface{}) { func (s *StepRunSourceInstance) Cleanup(state map[string]interface{}) {
if s.instance == nil { if s.instance == nil {
return return
} }
...@@ -83,5 +87,5 @@ func (s *stepRunSourceInstance) Cleanup(state map[string]interface{}) { ...@@ -83,5 +87,5 @@ func (s *stepRunSourceInstance) Cleanup(state map[string]interface{}) {
} }
pending := []string{"pending", "running", "shutting-down", "stopped", "stopping"} pending := []string{"pending", "running", "shutting-down", "stopped", "stopping"}
waitForState(ec2conn, s.instance, pending, "terminated") WaitForState(ec2conn, s.instance, pending, "terminated")
} }
...@@ -12,7 +12,7 @@ import ( ...@@ -12,7 +12,7 @@ import (
type StepSecurityGroup struct { type StepSecurityGroup struct {
SecurityGroupId string SecurityGroupId string
SSHPort int SSHPort int
createdGroupId string createdGroupId string
} }
......
...@@ -93,9 +93,13 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe ...@@ -93,9 +93,13 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&awscommon.StepKeyPair{}, &awscommon.StepKeyPair{},
&awscommon.StepSecurityGroup{ &awscommon.StepSecurityGroup{
SecurityGroupId: b.config.SecurityGroupId, SecurityGroupId: b.config.SecurityGroupId,
SSHPort: b.config.SSHPort, SSHPort: b.config.SSHPort,
},
&awscommon.StepRunSourceInstance{
ExpectedRootDevice: "ebs",
InstanceType: b.config.InstanceType,
SourceAMI: b.config.SourceAmi,
}, },
&stepRunSourceInstance{},
&common.StepConnectSSH{ &common.StepConnectSSH{
SSHAddress: sshAddress, SSHAddress: sshAddress,
SSHConfig: sshConfig, SSHConfig: sshConfig,
......
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"github.com/mitchellh/goamz/ec2" "github.com/mitchellh/goamz/ec2"
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
awscommon "github.com/mitchellh/packer/builder/amazon/common"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
) )
...@@ -26,7 +27,7 @@ func (s *stepStopInstance) Run(state map[string]interface{}) multistep.StepActio ...@@ -26,7 +27,7 @@ func (s *stepStopInstance) Run(state map[string]interface{}) multistep.StepActio
// Wait for the instance to actual stop // Wait for the instance to actual stop
ui.Say("Waiting for the instance to stop...") ui.Say("Waiting for the instance to stop...")
instance, err = waitForState(ec2conn, instance, []string{"running", "stopping"}, "stopped") instance, err = awscommon.WaitForState(ec2conn, instance, []string{"running", "stopping"}, "stopped")
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["error"] = err
......
...@@ -72,7 +72,12 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe ...@@ -72,7 +72,12 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&awscommon.StepKeyPair{}, &awscommon.StepKeyPair{},
&awscommon.StepSecurityGroup{ &awscommon.StepSecurityGroup{
SecurityGroupId: b.config.SecurityGroupId, SecurityGroupId: b.config.SecurityGroupId,
SSHPort: b.config.SSHPort, SSHPort: b.config.SSHPort,
},
&awscommon.StepRunSourceInstance{
ExpectedRootDevice: "instance-store",
InstanceType: b.config.InstanceType,
SourceAMI: b.config.SourceAmi,
}, },
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment