diff --git a/builder/amazon/ebs/instance.go b/builder/amazon/common/instance.go similarity index 89% rename from builder/amazon/ebs/instance.go rename to builder/amazon/common/instance.go index d5a0a8006..53dab50ce 100644 --- a/builder/amazon/ebs/instance.go +++ b/builder/amazon/common/instance.go @@ -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 diff --git a/builder/amazon/ebs/step_run_source_instance.go b/builder/amazon/common/step_run_source_instance.go similarity index 71% rename from builder/amazon/ebs/step_run_source_instance.go rename to builder/amazon/common/step_run_source_instance.go index ada21de20..b7c7d4ab5 100644 --- a/builder/amazon/ebs/step_run_source_instance.go +++ b/builder/amazon/common/step_run_source_instance.go @@ -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") } diff --git a/builder/amazon/common/step_security_group.go b/builder/amazon/common/step_security_group.go index e5743f202..dec317262 100644 --- a/builder/amazon/common/step_security_group.go +++ b/builder/amazon/common/step_security_group.go @@ -12,7 +12,7 @@ import ( type StepSecurityGroup struct { SecurityGroupId string - SSHPort int + SSHPort int createdGroupId string } diff --git a/builder/amazon/ebs/builder.go b/builder/amazon/ebs/builder.go index a0461efb6..03c23bf64 100644 --- a/builder/amazon/ebs/builder.go +++ b/builder/amazon/ebs/builder.go @@ -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, diff --git a/builder/amazon/ebs/step_stop_instance.go b/builder/amazon/ebs/step_stop_instance.go index ac81bda6b..5c608632f 100644 --- a/builder/amazon/ebs/step_stop_instance.go +++ b/builder/amazon/ebs/step_stop_instance.go @@ -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 diff --git a/builder/amazon/instance/builder.go b/builder/amazon/instance/builder.go index 66680903c..908a23a53 100644 --- a/builder/amazon/instance/builder.go +++ b/builder/amazon/instance/builder.go @@ -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, }, }