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-11-25 22:44:39 -05:00
|
|
|
"github.com/CpuID/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-11-25 20:32:08 -05:00
|
|
|
Debug bool
|
|
|
|
ExpectedRootDevice string
|
|
|
|
InstanceType string
|
|
|
|
UserData string
|
|
|
|
UserDataFile string
|
|
|
|
SourceAMI string
|
|
|
|
IamInstanceProfile string
|
|
|
|
SubnetId string
|
2013-11-25 22:44:39 -05:00
|
|
|
AssociatePublicIpAddress string
|
2013-11-25 20:32:08 -05:00
|
|
|
AvailabilityZone string
|
|
|
|
BlockDevices BlockDevices
|
2013-07-20 22:58:27 -04:00
|
|
|
|
2013-05-21 03:55:32 -04:00
|
|
|
instance *ec2.Instance
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
func (s *StepRunSourceInstance) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
|
|
|
keyName := state.Get("keyPair").(string)
|
2013-10-02 13:52:16 -04:00
|
|
|
securityGroupIds := state.Get("securityGroupIds").([]string)
|
2013-08-31 15:58:55 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-05-21 03:55:32 -04:00
|
|
|
|
2013-08-12 14:52:43 -04:00
|
|
|
userData := s.UserData
|
|
|
|
if s.UserDataFile != "" {
|
|
|
|
contents, err := ioutil.ReadFile(s.UserDataFile)
|
|
|
|
if err != nil {
|
2013-08-31 15:58:55 -04:00
|
|
|
state.Put("error", fmt.Errorf("Problem reading user data file: %s", err))
|
2013-08-12 14:52:43 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
userData = string(contents)
|
|
|
|
}
|
|
|
|
|
2013-10-02 13:52:16 -04:00
|
|
|
securityGroups := make([]ec2.SecurityGroup, len(securityGroupIds))
|
|
|
|
for n, securityGroupId := range securityGroupIds {
|
|
|
|
securityGroups[n] = ec2.SecurityGroup{Id: securityGroupId}
|
|
|
|
}
|
|
|
|
|
2013-05-21 03:55:32 -04:00
|
|
|
runOpts := &ec2.RunInstances{
|
2013-11-25 20:32:08 -05:00
|
|
|
KeyName: keyName,
|
|
|
|
ImageId: s.SourceAMI,
|
|
|
|
InstanceType: s.InstanceType,
|
|
|
|
UserData: []byte(userData),
|
|
|
|
MinCount: 0,
|
|
|
|
MaxCount: 0,
|
|
|
|
SecurityGroups: securityGroups,
|
|
|
|
IamInstanceProfile: s.IamInstanceProfile,
|
|
|
|
SubnetId: s.SubnetId,
|
|
|
|
AssociatePublicIpAddress: s.AssociatePublicIpAddress,
|
|
|
|
BlockDevices: s.BlockDevices.BuildLaunchDevices(),
|
|
|
|
AvailZone: s.AvailabilityZone,
|
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 {
|
2013-08-31 15:58:55 -04:00
|
|
|
state.Put("error", fmt.Errorf("There was a problem with the source AMI: %s", err))
|
2013-07-11 18:01:23 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2013-07-31 18:29:03 -04:00
|
|
|
|
2013-07-31 17:20:25 -04:00
|
|
|
if len(imageResp.Images) != 1 {
|
2013-08-31 15:58:55 -04:00
|
|
|
state.Put("error", fmt.Errorf("The source AMI '%s' could not be found.", s.SourceAMI))
|
2013-07-31 17:20:25 -04:00
|
|
|
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-08-31 15:58:55 -04:00
|
|
|
state.Put("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'.",
|
2013-08-31 15:58:55 -04:00
|
|
|
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)
|
2013-08-31 15:58:55 -04:00
|
|
|
state.Put("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-08-31 15:58:55 -04:00
|
|
|
state.Put("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-08-31 15:58:55 -04:00
|
|
|
state.Put("instance", s.instance)
|
2013-05-21 03:55:32 -04:00
|
|
|
|
2013-06-04 13:00:06 -04:00
|
|
|
return multistep.ActionContinue
|
2013-05-21 03:55:32 -04:00
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
func (s *StepRunSourceInstance) Cleanup(state multistep.StateBag) {
|
2013-05-21 03:55:32 -04:00
|
|
|
if s.instance == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-05-21 03:55:32 -04:00
|
|
|
|
|
|
|
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
|
|
|
}
|