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"
|
2013-08-12 14:52:43 -04:00
|
|
|
"io/ioutil"
|
2013-05-21 03:55:32 -04:00
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
2013-07-20 22:58:27 -04:00
|
|
|
type StepRunSourceInstance struct {
|
2013-08-30 17:55:56 -04:00
|
|
|
Debug bool
|
2013-07-20 22:58:27 -04:00
|
|
|
ExpectedRootDevice string
|
|
|
|
InstanceType string
|
2013-08-07 17:33:20 -04:00
|
|
|
UserData string
|
2013-08-12 14:52:43 -04:00
|
|
|
UserDataFile string
|
2013-07-20 22:58:27 -04:00
|
|
|
SourceAMI string
|
2013-07-30 14:55:50 -04:00
|
|
|
IamInstanceProfile string
|
2013-07-22 01:46:11 -04:00
|
|
|
SubnetId string
|
2013-08-15 17:05:08 -04:00
|
|
|
BlockDevices BlockDevices
|
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)
|
|
|
|
|
2013-08-12 14:52:43 -04:00
|
|
|
userData := s.UserData
|
|
|
|
if s.UserDataFile != "" {
|
|
|
|
contents, err := ioutil.ReadFile(s.UserDataFile)
|
|
|
|
if err != nil {
|
|
|
|
state["error"] = fmt.Errorf("Problem reading user data file: %s", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
userData = string(contents)
|
|
|
|
}
|
|
|
|
|
2013-05-21 03:55:32 -04:00
|
|
|
runOpts := &ec2.RunInstances{
|
2013-07-30 14:55:50 -04:00
|
|
|
KeyName: keyName,
|
|
|
|
ImageId: s.SourceAMI,
|
|
|
|
InstanceType: s.InstanceType,
|
2013-08-12 14:52:43 -04:00
|
|
|
UserData: []byte(userData),
|
2013-07-30 14:55:50 -04:00
|
|
|
MinCount: 0,
|
|
|
|
MaxCount: 0,
|
|
|
|
SecurityGroups: []ec2.SecurityGroup{ec2.SecurityGroup{Id: securityGroupId}},
|
|
|
|
IamInstanceProfile: s.IamInstanceProfile,
|
|
|
|
SubnetId: s.SubnetId,
|
2013-08-15 17:05:08 -04:00
|
|
|
BlockDevices: s.BlockDevices.BuildLaunchDevices(),
|
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-31 18:29:03 -04:00
|
|
|
|
2013-07-31 17:20:25 -04:00
|
|
|
if len(imageResp.Images) != 1 {
|
|
|
|
state["error"] = fmt.Errorf("The source AMI '%s' could not be found.", s.SourceAMI)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2013-07-11 18:01:23 -04:00
|
|
|
|
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-25 21:49:15 -04:00
|
|
|
stateChange := StateChangeConf{
|
|
|
|
Conn: ec2conn,
|
|
|
|
Pending: []string{"pending"},
|
|
|
|
Target: "running",
|
2013-07-29 21:55:11 -04:00
|
|
|
Refresh: InstanceStateRefreshFunc(ec2conn, s.instance),
|
2013-07-25 21:49:15 -04:00
|
|
|
StepState: state,
|
|
|
|
}
|
2013-07-29 21:47:43 -04:00
|
|
|
latestInstance, err := WaitForState(&stateChange)
|
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
|
|
|
}
|
|
|
|
|
2013-08-17 11:45:23 -04:00
|
|
|
s.instance = latestInstance.(*ec2.Instance)
|
2013-08-30 17:55:56 -04:00
|
|
|
|
|
|
|
if s.Debug {
|
|
|
|
if s.instance.DNSName != "" {
|
|
|
|
ui.Message(fmt.Sprintf("Public DNS: %s", s.instance.DNSName))
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.instance.PrivateIpAddress != "" {
|
|
|
|
ui.Message(fmt.Sprintf("Private IP: %s", s.instance.PrivateIpAddress))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2013-07-25 21:49:15 -04:00
|
|
|
stateChange := StateChangeConf{
|
2013-07-29 21:55:11 -04:00
|
|
|
Conn: ec2conn,
|
|
|
|
Pending: []string{"pending", "running", "shutting-down", "stopped", "stopping"},
|
|
|
|
Refresh: InstanceStateRefreshFunc(ec2conn, s.instance),
|
2013-07-31 12:37:43 -04:00
|
|
|
Target: "terminated",
|
2013-07-25 21:49:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
WaitForState(&stateChange)
|
2013-05-21 03:55:32 -04:00
|
|
|
}
|