2013-07-15 02:02:18 -04:00
|
|
|
package ebs
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
type stepRunSourceInstance struct {
|
|
|
|
instance *ec2.Instance
|
|
|
|
}
|
|
|
|
|
2013-06-04 13:00:06 -04:00
|
|
|
func (s *stepRunSourceInstance) Run(state map[string]interface{}) multistep.StepAction {
|
2013-05-21 03:55:32 -04:00
|
|
|
config := state["config"].(config)
|
|
|
|
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,
|
|
|
|
ImageId: config.SourceAmi,
|
|
|
|
InstanceType: config.InstanceType,
|
|
|
|
MinCount: 0,
|
|
|
|
MaxCount: 0,
|
|
|
|
SecurityGroups: []ec2.SecurityGroup{ec2.SecurityGroup{Id: securityGroupId}},
|
2013-07-21 03:44:14 -04:00
|
|
|
SubnetId: config.SubnetId,
|
2013-05-21 03:55:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Launching a source AWS instance...")
|
2013-07-11 18:01:23 -04:00
|
|
|
imageResp, err := ec2conn.Images([]string{config.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" {
|
|
|
|
state["error"] = fmt.Errorf(
|
|
|
|
"The provided source AMI is instance-store based. The\n" +
|
|
|
|
"amazon-ebs bundler can only work with EBS based AMIs.")
|
|
|
|
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-06-04 14:29:59 -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
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepRunSourceInstance) Cleanup(state map[string]interface{}) {
|
|
|
|
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"}
|
|
|
|
waitForState(ec2conn, s.instance, pending, "terminated")
|
2013-05-21 03:55:32 -04:00
|
|
|
}
|