builder/amazon: extract StepRunSourceInstance

This commit is contained in:
Mitchell Hashimoto 2013-07-20 19:58:27 -07:00
parent 30ab70388b
commit 64ced7ff2c
6 changed files with 34 additions and 20 deletions

View File

@ -1,4 +1,4 @@
package ebs
package common
import (
"fmt"
@ -7,7 +7,7 @@ import (
"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)
i = originalInstance

View File

@ -1,4 +1,4 @@
package ebs
package common
import (
"fmt"
@ -8,12 +8,15 @@ import (
"log"
)
type stepRunSourceInstance struct {
type StepRunSourceInstance struct {
ExpectedRootDevice string
InstanceType string
SourceAMI string
instance *ec2.Instance
}
func (s *stepRunSourceInstance) Run(state map[string]interface{}) multistep.StepAction {
config := state["config"].(config)
func (s *StepRunSourceInstance) Run(state map[string]interface{}) multistep.StepAction {
ec2conn := state["ec2"].(*ec2.EC2)
keyName := state["keyPair"].(string)
securityGroupId := state["securityGroupId"].(string)
@ -21,8 +24,8 @@ func (s *stepRunSourceInstance) Run(state map[string]interface{}) multistep.Step
runOpts := &ec2.RunInstances{
KeyName: keyName,
ImageId: config.SourceAmi,
InstanceType: config.InstanceType,
ImageId: s.SourceAMI,
InstanceType: s.InstanceType,
MinCount: 0,
MaxCount: 0,
SecurityGroups: []ec2.SecurityGroup{ec2.SecurityGroup{Id: securityGroupId}},
@ -30,16 +33,17 @@ func (s *stepRunSourceInstance) Run(state map[string]interface{}) multistep.Step
}
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 {
state["error"] = fmt.Errorf("There was a problem with the source AMI: %s", err)
return multistep.ActionHalt
}
if imageResp.Images[0].RootDeviceType != "ebs" {
if s.ExpectedRootDevice != "" && imageResp.Images[0].RootDeviceType != s.ExpectedRootDevice {
state["error"] = fmt.Errorf(
"The provided source AMI is instance-store based. The\n" +
"amazon-ebs bundler can only work with EBS based AMIs.")
"The provided source AMI has an invalid root device type.\n"+
"Expected '%s', got '%s'.",
s.ExpectedRootDevice, imageResp.Images[0].RootDeviceType)
return multistep.ActionHalt
}
@ -55,7 +59,7 @@ func (s *stepRunSourceInstance) Run(state map[string]interface{}) multistep.Step
log.Printf("instance id: %s", 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 {
err := fmt.Errorf("Error waiting for instance (%s) to become ready: %s", s.instance.InstanceId, err)
state["error"] = err
@ -68,7 +72,7 @@ func (s *stepRunSourceInstance) Run(state map[string]interface{}) multistep.Step
return multistep.ActionContinue
}
func (s *stepRunSourceInstance) Cleanup(state map[string]interface{}) {
func (s *StepRunSourceInstance) Cleanup(state map[string]interface{}) {
if s.instance == nil {
return
}
@ -83,5 +87,5 @@ func (s *stepRunSourceInstance) Cleanup(state map[string]interface{}) {
}
pending := []string{"pending", "running", "shutting-down", "stopped", "stopping"}
waitForState(ec2conn, s.instance, pending, "terminated")
WaitForState(ec2conn, s.instance, pending, "terminated")
}

View File

@ -12,7 +12,7 @@ import (
type StepSecurityGroup struct {
SecurityGroupId string
SSHPort int
SSHPort int
createdGroupId string
}

View File

@ -93,9 +93,13 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&awscommon.StepKeyPair{},
&awscommon.StepSecurityGroup{
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{
SSHAddress: sshAddress,
SSHConfig: sshConfig,

View File

@ -4,6 +4,7 @@ import (
"fmt"
"github.com/mitchellh/goamz/ec2"
"github.com/mitchellh/multistep"
awscommon "github.com/mitchellh/packer/builder/amazon/common"
"github.com/mitchellh/packer/packer"
)
@ -26,7 +27,7 @@ func (s *stepStopInstance) Run(state map[string]interface{}) multistep.StepActio
// Wait for the instance to actual 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 {
err := fmt.Errorf("Error waiting for instance to stop: %s", err)
state["error"] = err

View File

@ -72,7 +72,12 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&awscommon.StepKeyPair{},
&awscommon.StepSecurityGroup{
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,
},
}