2013-07-20 22:58:27 -04:00
|
|
|
package common
|
2013-05-21 03:55:32 -04:00
|
|
|
|
|
|
|
import (
|
2013-06-11 17:15:43 -04:00
|
|
|
"fmt"
|
2013-05-21 03:55:32 -04:00
|
|
|
"github.com/mitchellh/goamz/ec2"
|
2013-06-04 13:00:06 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
2013-05-21 03:55:32 -04:00
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
2013-07-20 22:58:27 -04:00
|
|
|
type StepRunSourceInstance struct {
|
|
|
|
ExpectedRootDevice string
|
|
|
|
InstanceType string
|
|
|
|
SourceAMI string
|
2013-07-22 01:46:11 -04:00
|
|
|
SubnetId string
|
2013-07-20 22:58:27 -04:00
|
|
|
|
2013-05-21 03:55:32 -04:00
|
|
|
instance *ec2.Instance
|
|
|
|
}
|
|
|
|
|
2013-07-20 22:58:27 -04:00
|
|
|
func (s *StepRunSourceInstance) Run(state map[string]interface{}) multistep.StepAction {
|
2013-05-21 03:55:32 -04:00
|
|
|
ec2conn := state["ec2"].(*ec2.EC2)
|
|
|
|
keyName := state["keyPair"].(string)
|
2013-06-11 17:37:10 -04:00
|
|
|
securityGroupId := state["securityGroupId"].(string)
|
2013-05-21 03:55:32 -04:00
|
|
|
ui := state["ui"].(packer.Ui)
|
|
|
|
|
|
|
|
runOpts := &ec2.RunInstances{
|
2013-06-11 17:37:10 -04:00
|
|
|
KeyName: keyName,
|
2013-07-20 22:58:27 -04:00
|
|
|
ImageId: s.SourceAMI,
|
|
|
|
InstanceType: s.InstanceType,
|
2013-06-11 17:37:10 -04:00
|
|
|
MinCount: 0,
|
|
|
|
MaxCount: 0,
|
|
|
|
SecurityGroups: []ec2.SecurityGroup{ec2.SecurityGroup{Id: securityGroupId}},
|
2013-07-22 01:46:11 -04:00
|
|
|
SubnetId: s.SubnetId,
|
2013-05-21 03:55:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Launching a source AWS instance...")
|
2013-07-20 22:58:27 -04:00
|
|
|
imageResp, err := ec2conn.Images([]string{s.SourceAMI}, ec2.NewFilter())
|
2013-07-11 18:01:23 -04:00
|
|
|
if err != nil {
|
|
|
|
state["error"] = fmt.Errorf("There was a problem with the source AMI: %s", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-07-20 22:58:27 -04:00
|
|
|
if s.ExpectedRootDevice != "" && imageResp.Images[0].RootDeviceType != s.ExpectedRootDevice {
|
2013-07-11 18:01:23 -04:00
|
|
|
state["error"] = fmt.Errorf(
|
2013-07-20 22:58:27 -04:00
|
|
|
"The provided source AMI has an invalid root device type.\n"+
|
|
|
|
"Expected '%s', got '%s'.",
|
|
|
|
s.ExpectedRootDevice, imageResp.Images[0].RootDeviceType)
|
2013-07-11 18:01:23 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-05-21 03:55:32 -04:00
|
|
|
runResp, err := ec2conn.RunInstances(runOpts)
|
|
|
|
if err != nil {
|
2013-06-19 23:54:02 -04:00
|
|
|
err := fmt.Errorf("Error launching source instance: %s", err)
|
|
|
|
state["error"] = err
|
2013-05-21 03:55:32 -04:00
|
|
|
ui.Error(err.Error())
|
2013-06-04 13:00:06 -04:00
|
|
|
return multistep.ActionHalt
|
2013-05-21 03:55:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
s.instance = &runResp.Instances[0]
|
|
|
|
log.Printf("instance id: %s", s.instance.InstanceId)
|
|
|
|
|
2013-07-11 15:49:43 -04:00
|
|
|
ui.Say(fmt.Sprintf("Waiting for instance (%s) to become ready...", s.instance.InstanceId))
|
2013-07-20 22:58:27 -04:00
|
|
|
s.instance, err = WaitForState(ec2conn, s.instance, []string{"pending"}, "running")
|
2013-05-21 03:55:32 -04:00
|
|
|
if err != nil {
|
2013-07-11 15:49:43 -04:00
|
|
|
err := fmt.Errorf("Error waiting for instance (%s) to become ready: %s", s.instance.InstanceId, err)
|
2013-06-19 23:54:02 -04:00
|
|
|
state["error"] = err
|
2013-05-21 03:55:32 -04:00
|
|
|
ui.Error(err.Error())
|
2013-06-04 13:00:06 -04:00
|
|
|
return multistep.ActionHalt
|
2013-05-21 03:55:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
state["instance"] = s.instance
|
|
|
|
|
2013-06-04 13:00:06 -04:00
|
|
|
return multistep.ActionContinue
|
2013-05-21 03:55:32 -04:00
|
|
|
}
|
|
|
|
|
2013-07-20 22:58:27 -04:00
|
|
|
func (s *StepRunSourceInstance) Cleanup(state map[string]interface{}) {
|
2013-05-21 03:55:32 -04:00
|
|
|
if s.instance == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ec2conn := state["ec2"].(*ec2.EC2)
|
|
|
|
ui := state["ui"].(packer.Ui)
|
|
|
|
|
|
|
|
ui.Say("Terminating the source AWS instance...")
|
2013-06-11 17:15:43 -04:00
|
|
|
if _, err := ec2conn.TerminateInstances([]string{s.instance.InstanceId}); err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error terminating instance, may still be around: %s", err))
|
2013-06-27 21:42:07 -04:00
|
|
|
return
|
2013-06-11 17:15:43 -04:00
|
|
|
}
|
2013-06-27 21:42:07 -04:00
|
|
|
|
|
|
|
pending := []string{"pending", "running", "shutting-down", "stopped", "stopping"}
|
2013-07-20 22:58:27 -04:00
|
|
|
WaitForState(ec2conn, s.instance, pending, "terminated")
|
2013-05-21 03:55:32 -04:00
|
|
|
}
|