2013-07-20 19:58:27 -07:00
|
|
|
package common
|
2013-05-21 00:55:32 -07:00
|
|
|
|
|
|
|
import (
|
2013-06-11 14:15:43 -07:00
|
|
|
"fmt"
|
2013-11-26 13:46:32 +10:00
|
|
|
"github.com/mitchellh/goamz/ec2"
|
2013-06-04 10:00:06 -07:00
|
|
|
"github.com/mitchellh/multistep"
|
2013-05-21 00:55:32 -07:00
|
|
|
"github.com/mitchellh/packer/packer"
|
2013-08-12 11:52:43 -07:00
|
|
|
"io/ioutil"
|
2013-05-21 00:55:32 -07:00
|
|
|
)
|
|
|
|
|
2013-07-20 19:58:27 -07:00
|
|
|
type StepRunSourceInstance struct {
|
2013-12-27 20:54:35 -07:00
|
|
|
AssociatePublicIpAddress bool
|
2014-05-08 01:13:27 +08:00
|
|
|
SpotPrice string
|
2013-12-27 20:54:35 -07:00
|
|
|
AvailabilityZone string
|
|
|
|
BlockDevices BlockDevices
|
2013-11-26 11:32:08 +10:00
|
|
|
Debug bool
|
|
|
|
ExpectedRootDevice string
|
|
|
|
InstanceType string
|
|
|
|
IamInstanceProfile string
|
2013-12-27 20:54:35 -07:00
|
|
|
SourceAMI string
|
2013-11-26 11:32:08 +10:00
|
|
|
SubnetId string
|
2013-12-27 20:54:35 -07:00
|
|
|
Tags map[string]string
|
|
|
|
UserData string
|
|
|
|
UserDataFile string
|
2014-05-11 00:51:35 +08:00
|
|
|
spotRequest *ec2.SpotRequestResult
|
|
|
|
instance *ec2.Instance
|
2013-05-21 00:55:32 -07:00
|
|
|
}
|
|
|
|
|
2013-08-31 12:58:55 -07:00
|
|
|
func (s *StepRunSourceInstance) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
|
|
|
keyName := state.Get("keyPair").(string)
|
2013-10-02 10:52:16 -07:00
|
|
|
securityGroupIds := state.Get("securityGroupIds").([]string)
|
2013-08-31 12:58:55 -07:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-05-21 00:55:32 -07:00
|
|
|
|
2013-08-12 11:52:43 -07:00
|
|
|
userData := s.UserData
|
|
|
|
if s.UserDataFile != "" {
|
|
|
|
contents, err := ioutil.ReadFile(s.UserDataFile)
|
|
|
|
if err != nil {
|
2013-08-31 12:58:55 -07:00
|
|
|
state.Put("error", fmt.Errorf("Problem reading user data file: %s", err))
|
2013-08-12 11:52:43 -07:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
userData = string(contents)
|
|
|
|
}
|
|
|
|
|
2013-10-02 10:52:16 -07:00
|
|
|
securityGroups := make([]ec2.SecurityGroup, len(securityGroupIds))
|
|
|
|
for n, securityGroupId := range securityGroupIds {
|
|
|
|
securityGroups[n] = ec2.SecurityGroup{Id: securityGroupId}
|
|
|
|
}
|
|
|
|
|
2013-05-21 00:55:32 -07:00
|
|
|
ui.Say("Launching a source AWS instance...")
|
2013-07-20 19:58:27 -07:00
|
|
|
imageResp, err := ec2conn.Images([]string{s.SourceAMI}, ec2.NewFilter())
|
2013-07-12 07:01:23 +09:00
|
|
|
if err != nil {
|
2013-08-31 12:58:55 -07:00
|
|
|
state.Put("error", fmt.Errorf("There was a problem with the source AMI: %s", err))
|
2013-07-12 07:01:23 +09:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2013-07-31 15:29:03 -07:00
|
|
|
|
2013-07-31 14:20:25 -07:00
|
|
|
if len(imageResp.Images) != 1 {
|
2013-08-31 12:58:55 -07:00
|
|
|
state.Put("error", fmt.Errorf("The source AMI '%s' could not be found.", s.SourceAMI))
|
2013-07-31 14:20:25 -07:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2013-07-12 07:01:23 +09:00
|
|
|
|
2013-07-20 19:58:27 -07:00
|
|
|
if s.ExpectedRootDevice != "" && imageResp.Images[0].RootDeviceType != s.ExpectedRootDevice {
|
2013-08-31 12:58:55 -07:00
|
|
|
state.Put("error", fmt.Errorf(
|
2013-07-20 19:58:27 -07:00
|
|
|
"The provided source AMI has an invalid root device type.\n"+
|
|
|
|
"Expected '%s', got '%s'.",
|
2013-08-31 12:58:55 -07:00
|
|
|
s.ExpectedRootDevice, imageResp.Images[0].RootDeviceType))
|
2013-07-12 07:01:23 +09:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2014-05-08 01:13:27 +08:00
|
|
|
var instanceId []string
|
|
|
|
if s.SpotPrice == "" {
|
|
|
|
runOpts := &ec2.RunInstances{
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
runResp, err := ec2conn.RunInstances(runOpts)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error launching source instance: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
instanceId = []string{runResp.Instances[0].InstanceId}
|
|
|
|
} else {
|
|
|
|
runOpts := &ec2.RequestSpotInstances{
|
|
|
|
SpotPrice: s.SpotPrice,
|
|
|
|
KeyName: keyName,
|
|
|
|
ImageId: s.SourceAMI,
|
|
|
|
InstanceType: s.InstanceType,
|
|
|
|
UserData: []byte(userData),
|
|
|
|
SecurityGroups: securityGroups,
|
|
|
|
IamInstanceProfile: s.IamInstanceProfile,
|
|
|
|
SubnetId: s.SubnetId,
|
|
|
|
AssociatePublicIpAddress: s.AssociatePublicIpAddress,
|
|
|
|
BlockDevices: s.BlockDevices.BuildLaunchDevices(),
|
|
|
|
AvailZone: s.AvailabilityZone,
|
|
|
|
}
|
|
|
|
runSpotResp, err := ec2conn.RequestSpotInstances(runOpts)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error launching source spot instance: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2014-05-12 12:51:13 +08:00
|
|
|
s.spotRequest = &runSpotResp.SpotRequestResults[0]
|
|
|
|
spotRequestId := s.spotRequest.SpotRequestId
|
2014-05-08 01:13:27 +08:00
|
|
|
ui.Say(fmt.Sprintf("Waiting for spot request (%s) to become ready...", spotRequestId))
|
|
|
|
stateChange := StateChangeConf{
|
|
|
|
Pending: []string{"open"},
|
|
|
|
Target: "active",
|
|
|
|
Refresh: SpotRequestStateRefreshFunc(ec2conn, spotRequestId),
|
|
|
|
StepState: state,
|
|
|
|
}
|
|
|
|
_, err = WaitForState(&stateChange)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error waiting for spot request (%s) to become ready: %s", spotRequestId, err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
spotResp, err := ec2conn.DescribeSpotRequests([]string{spotRequestId}, nil)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error finding spot request (%s): %s", spotRequestId, err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2014-05-12 12:51:13 +08:00
|
|
|
instanceId = []string{spotResp.SpotRequestResults[0].InstanceId}
|
2014-05-08 01:13:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
instanceResp, err := ec2conn.Instances(instanceId, nil)
|
2013-05-21 00:55:32 -07:00
|
|
|
if err != nil {
|
2014-05-08 01:13:27 +08:00
|
|
|
err := fmt.Errorf("Error finding source instance (%s): %s", instanceId, err)
|
2013-08-31 12:58:55 -07:00
|
|
|
state.Put("error", err)
|
2013-05-21 00:55:32 -07:00
|
|
|
ui.Error(err.Error())
|
2013-06-04 10:00:06 -07:00
|
|
|
return multistep.ActionHalt
|
2013-05-21 00:55:32 -07:00
|
|
|
}
|
2014-05-08 01:13:27 +08:00
|
|
|
s.instance = &instanceResp.Reservations[0].Instances[0]
|
2013-12-16 18:11:23 -08:00
|
|
|
ui.Message(fmt.Sprintf("Instance ID: %s", s.instance.InstanceId))
|
|
|
|
|
2013-07-11 19:49:43 +00:00
|
|
|
ui.Say(fmt.Sprintf("Waiting for instance (%s) to become ready...", s.instance.InstanceId))
|
2013-07-25 20:49:15 -05:00
|
|
|
stateChange := StateChangeConf{
|
|
|
|
Pending: []string{"pending"},
|
|
|
|
Target: "running",
|
2013-07-29 18:55:11 -07:00
|
|
|
Refresh: InstanceStateRefreshFunc(ec2conn, s.instance),
|
2013-07-25 20:49:15 -05:00
|
|
|
StepState: state,
|
|
|
|
}
|
2013-07-29 18:47:43 -07:00
|
|
|
latestInstance, err := WaitForState(&stateChange)
|
2013-05-21 00:55:32 -07:00
|
|
|
if err != nil {
|
2013-07-11 19:49:43 +00:00
|
|
|
err := fmt.Errorf("Error waiting for instance (%s) to become ready: %s", s.instance.InstanceId, err)
|
2013-08-31 12:58:55 -07:00
|
|
|
state.Put("error", err)
|
2013-05-21 00:55:32 -07:00
|
|
|
ui.Error(err.Error())
|
2013-06-04 10:00:06 -07:00
|
|
|
return multistep.ActionHalt
|
2013-05-21 00:55:32 -07:00
|
|
|
}
|
|
|
|
|
2013-08-17 09:45:23 -06:00
|
|
|
s.instance = latestInstance.(*ec2.Instance)
|
2013-08-30 14:55:56 -07:00
|
|
|
|
2014-09-04 18:43:35 -07:00
|
|
|
ec2Tags := make([]ec2.Tag, 1, len(s.Tags)+1)
|
|
|
|
ec2Tags[0] = ec2.Tag{"Name", "Packer Builder"}
|
|
|
|
for k, v := range s.Tags {
|
|
|
|
ec2Tags = append(ec2Tags, ec2.Tag{k, v})
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = ec2conn.CreateTags([]string{s.instance.InstanceId}, ec2Tags)
|
|
|
|
if err != nil {
|
|
|
|
ui.Message(
|
|
|
|
fmt.Sprintf("Failed to tag a Name on the builder instance: %s", err))
|
|
|
|
}
|
|
|
|
|
2013-08-30 14:55:56 -07:00
|
|
|
if s.Debug {
|
|
|
|
if s.instance.DNSName != "" {
|
|
|
|
ui.Message(fmt.Sprintf("Public DNS: %s", s.instance.DNSName))
|
|
|
|
}
|
|
|
|
|
2013-11-26 15:03:45 +10:00
|
|
|
if s.instance.PublicIpAddress != "" {
|
|
|
|
ui.Message(fmt.Sprintf("Public IP: %s", s.instance.PublicIpAddress))
|
|
|
|
}
|
|
|
|
|
2013-08-30 14:55:56 -07:00
|
|
|
if s.instance.PrivateIpAddress != "" {
|
|
|
|
ui.Message(fmt.Sprintf("Private IP: %s", s.instance.PrivateIpAddress))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-31 12:58:55 -07:00
|
|
|
state.Put("instance", s.instance)
|
2013-05-21 00:55:32 -07:00
|
|
|
|
2013-06-04 10:00:06 -07:00
|
|
|
return multistep.ActionContinue
|
2013-05-21 00:55:32 -07:00
|
|
|
}
|
|
|
|
|
2013-08-31 12:58:55 -07:00
|
|
|
func (s *StepRunSourceInstance) Cleanup(state multistep.StateBag) {
|
2013-05-21 00:55:32 -07:00
|
|
|
|
2013-08-31 12:58:55 -07:00
|
|
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-05-21 00:55:32 -07:00
|
|
|
|
2014-05-11 00:51:35 +08:00
|
|
|
// Cancel the spot request if it exists
|
|
|
|
if s.spotRequest != nil {
|
|
|
|
ui.Say("Cancelling the spot request...")
|
|
|
|
if _, err := ec2conn.CancelSpotRequests([]string{s.spotRequest.SpotRequestId}); err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error cancelling the spot request, may still be around: %s", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
stateChange := StateChangeConf{
|
|
|
|
Pending: []string{"active", "open"},
|
|
|
|
Refresh: SpotRequestStateRefreshFunc(ec2conn, s.spotRequest.SpotRequestId),
|
|
|
|
Target: "cancelled",
|
|
|
|
}
|
|
|
|
|
|
|
|
WaitForState(&stateChange)
|
2013-06-27 21:42:07 -04:00
|
|
|
|
2013-07-25 20:49:15 -05:00
|
|
|
}
|
|
|
|
|
2014-05-11 00:51:35 +08:00
|
|
|
// Terminate the source instance if it exists
|
|
|
|
if s.instance != nil {
|
|
|
|
|
|
|
|
ui.Say("Terminating the source AWS instance...")
|
|
|
|
if _, err := ec2conn.TerminateInstances([]string{s.instance.InstanceId}); err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error terminating instance, may still be around: %s", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
stateChange := StateChangeConf{
|
|
|
|
Pending: []string{"pending", "running", "shutting-down", "stopped", "stopping"},
|
|
|
|
Refresh: InstanceStateRefreshFunc(ec2conn, s.instance),
|
|
|
|
Target: "terminated",
|
|
|
|
}
|
|
|
|
|
|
|
|
WaitForState(&stateChange)
|
|
|
|
}
|
2013-05-21 00:55:32 -07:00
|
|
|
}
|