2013-05-21 03:55:32 -04:00
|
|
|
package amazonebs
|
|
|
|
|
|
|
|
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-05-21 03:55:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Launching a source AWS instance...")
|
|
|
|
runResp, err := ec2conn.RunInstances(runOpts)
|
|
|
|
if err != nil {
|
|
|
|
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)
|
|
|
|
|
|
|
|
ui.Say("Waiting for instance to become ready...")
|
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 {
|
|
|
|
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-05-21 03:55:32 -04:00
|
|
|
}
|