2013-07-20 22:58:27 -04:00
|
|
|
package common
|
2013-05-21 03:55:32 -04:00
|
|
|
|
|
|
|
import (
|
2015-06-14 02:12:59 -04:00
|
|
|
"encoding/base64"
|
2013-06-11 17:15:43 -04:00
|
|
|
"fmt"
|
2014-09-06 13:44:12 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2015-06-03 17:13:52 -04:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
2017-08-11 13:43:05 -04:00
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
2015-06-03 17:13:52 -04:00
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2015-04-05 17:58:48 -04:00
|
|
|
|
2017-08-11 13:43:05 -04:00
|
|
|
retry "github.com/hashicorp/packer/common"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
2013-06-04 13:00:06 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
2013-05-21 03:55:32 -04:00
|
|
|
)
|
|
|
|
|
2013-07-20 22:58:27 -04:00
|
|
|
type StepRunSourceInstance struct {
|
2016-05-20 05:54:45 -04:00
|
|
|
AssociatePublicIpAddress bool
|
|
|
|
AvailabilityZone string
|
|
|
|
BlockDevices BlockDevices
|
|
|
|
Debug bool
|
|
|
|
EbsOptimized bool
|
|
|
|
ExpectedRootDevice string
|
|
|
|
IamInstanceProfile string
|
2017-01-18 18:11:52 -05:00
|
|
|
InstanceInitiatedShutdownBehavior string
|
|
|
|
InstanceType string
|
2016-05-20 05:54:45 -04:00
|
|
|
SourceAMI string
|
|
|
|
SpotPrice string
|
|
|
|
SpotPriceProduct string
|
|
|
|
SubnetId string
|
|
|
|
Tags map[string]string
|
|
|
|
UserData string
|
|
|
|
UserDataFile string
|
2017-01-10 05:41:28 -05:00
|
|
|
Ctx interpolate.Context
|
2014-09-06 13:44:12 -04:00
|
|
|
|
2015-07-07 13:07:38 -04:00
|
|
|
instanceId string
|
2015-04-05 17:58:48 -04:00
|
|
|
spotRequest *ec2.SpotInstanceRequest
|
2013-05-21 03:55:32 -04:00
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
func (s *StepRunSourceInstance) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
2017-02-26 20:17:49 -05:00
|
|
|
var keyName string
|
|
|
|
if name, ok := state.GetOk("keyPair"); ok {
|
|
|
|
keyName = name.(string)
|
|
|
|
}
|
2017-01-18 18:11:52 -05:00
|
|
|
securityGroupIds := aws.StringSlice(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)
|
2015-07-31 10:34:33 -04:00
|
|
|
}
|
2015-06-14 02:12:59 -04:00
|
|
|
|
2015-07-31 10:34:33 -04:00
|
|
|
// Test if it is encoded already, and if not, encode it
|
|
|
|
if _, err := base64.StdEncoding.DecodeString(userData); err != nil {
|
|
|
|
log.Printf("[DEBUG] base64 encoding user data...")
|
|
|
|
userData = base64.StdEncoding.EncodeToString([]byte(userData))
|
2013-08-12 14:52:43 -04:00
|
|
|
}
|
|
|
|
|
2013-05-21 03:55:32 -04:00
|
|
|
ui.Say("Launching a source AWS instance...")
|
2016-08-20 14:58:36 -04:00
|
|
|
image, ok := state.Get("source_image").(*ec2.Image)
|
|
|
|
if !ok {
|
|
|
|
state.Put("error", fmt.Errorf("source_image type assertion failed"))
|
2013-07-11 18:01:23 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2016-08-20 14:58:36 -04:00
|
|
|
s.SourceAMI = *image.ImageId
|
2013-07-31 18:29:03 -04:00
|
|
|
|
2016-08-20 14:58:36 -04:00
|
|
|
if s.ExpectedRootDevice != "" && *image.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'.",
|
2016-08-20 20:07:58 -04:00
|
|
|
s.ExpectedRootDevice, *image.RootDeviceType))
|
2013-07-11 18:01:23 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2014-09-06 13:44:12 -04:00
|
|
|
spotPrice := s.SpotPrice
|
2015-04-03 20:52:54 -04:00
|
|
|
availabilityZone := s.AvailabilityZone
|
2014-09-06 13:44:12 -04:00
|
|
|
if spotPrice == "auto" {
|
|
|
|
ui.Message(fmt.Sprintf(
|
|
|
|
"Finding spot price for %s %s...",
|
|
|
|
s.SpotPriceProduct, s.InstanceType))
|
|
|
|
|
|
|
|
// Detect the spot price
|
|
|
|
startTime := time.Now().Add(-1 * time.Hour)
|
2015-04-05 17:58:48 -04:00
|
|
|
resp, err := ec2conn.DescribeSpotPriceHistory(&ec2.DescribeSpotPriceHistoryInput{
|
|
|
|
InstanceTypes: []*string{&s.InstanceType},
|
|
|
|
ProductDescriptions: []*string{&s.SpotPriceProduct},
|
|
|
|
AvailabilityZone: &s.AvailabilityZone,
|
|
|
|
StartTime: &startTime,
|
2014-09-06 13:44:12 -04:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error finding spot price: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
var price float64
|
2015-04-05 17:58:48 -04:00
|
|
|
for _, history := range resp.SpotPriceHistory {
|
|
|
|
log.Printf("[INFO] Candidate spot price: %s", *history.SpotPrice)
|
|
|
|
current, err := strconv.ParseFloat(*history.SpotPrice, 64)
|
2014-09-06 13:44:12 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ERR] Error parsing spot price: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if price == 0 || current < price {
|
|
|
|
price = current
|
2015-04-03 20:52:54 -04:00
|
|
|
if s.AvailabilityZone == "" {
|
2015-05-28 12:44:33 -04:00
|
|
|
availabilityZone = *history.AvailabilityZone
|
2015-04-03 20:52:54 -04:00
|
|
|
}
|
2014-09-06 13:44:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if price == 0 {
|
|
|
|
err := fmt.Errorf("No candidate spot prices found!")
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
2016-06-29 07:00:21 -04:00
|
|
|
} else {
|
|
|
|
// Add 0.5 cents to minimum spot bid to ensure capacity will be available
|
|
|
|
// Avoids price-too-low error in active markets which can fluctuate
|
|
|
|
price = price + 0.005
|
2014-09-06 13:44:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
spotPrice = strconv.FormatFloat(price, 'f', -1, 64)
|
|
|
|
}
|
|
|
|
|
|
|
|
var instanceId string
|
2017-08-11 15:31:05 -04:00
|
|
|
|
|
|
|
ui.Say("Adding tags to source instance")
|
|
|
|
if _, exists := s.Tags["Name"]; !exists {
|
|
|
|
s.Tags["Name"] = "Packer Builder"
|
|
|
|
}
|
|
|
|
|
|
|
|
createTagsAfterInstanceStarts := true
|
2017-07-31 14:38:30 -04:00
|
|
|
ec2Tags, err := ConvertToEC2Tags(s.Tags, *ec2conn.Config.Region, s.SourceAMI, s.Ctx)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error tagging source instance: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2017-08-02 12:29:47 -04:00
|
|
|
ReportTags(ui, ec2Tags)
|
|
|
|
|
2015-10-06 17:13:17 -04:00
|
|
|
if spotPrice == "" || spotPrice == "0" {
|
2017-07-31 14:38:30 -04:00
|
|
|
|
2015-04-05 17:58:48 -04:00
|
|
|
runOpts := &ec2.RunInstancesInput{
|
2016-08-25 03:17:57 -04:00
|
|
|
ImageId: &s.SourceAMI,
|
|
|
|
InstanceType: &s.InstanceType,
|
|
|
|
UserData: &userData,
|
|
|
|
MaxCount: aws.Int64(1),
|
|
|
|
MinCount: aws.Int64(1),
|
|
|
|
IamInstanceProfile: &ec2.IamInstanceProfileSpecification{Name: &s.IamInstanceProfile},
|
|
|
|
BlockDeviceMappings: s.BlockDevices.BuildLaunchDevices(),
|
|
|
|
Placement: &ec2.Placement{AvailabilityZone: &s.AvailabilityZone},
|
|
|
|
EbsOptimized: &s.EbsOptimized,
|
2017-08-11 15:31:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(ec2Tags) > 0 {
|
|
|
|
runTags := &ec2.TagSpecification{
|
|
|
|
ResourceType: aws.String("instance"),
|
|
|
|
Tags: ec2Tags,
|
|
|
|
}
|
|
|
|
|
|
|
|
runOpts.SetTagSpecifications([]*ec2.TagSpecification{runTags})
|
|
|
|
createTagsAfterInstanceStarts = false
|
2015-04-05 17:58:48 -04:00
|
|
|
}
|
|
|
|
|
2016-10-02 16:20:36 -04:00
|
|
|
if keyName != "" {
|
|
|
|
runOpts.KeyName = &keyName
|
|
|
|
}
|
|
|
|
|
2015-04-05 17:58:48 -04:00
|
|
|
if s.SubnetId != "" && s.AssociatePublicIpAddress {
|
|
|
|
runOpts.NetworkInterfaces = []*ec2.InstanceNetworkInterfaceSpecification{
|
2016-11-01 17:08:04 -04:00
|
|
|
{
|
2015-07-28 20:10:21 -04:00
|
|
|
DeviceIndex: aws.Int64(0),
|
2015-08-17 20:44:01 -04:00
|
|
|
AssociatePublicIpAddress: &s.AssociatePublicIpAddress,
|
|
|
|
SubnetId: &s.SubnetId,
|
2015-04-05 17:58:48 -04:00
|
|
|
Groups: securityGroupIds,
|
2015-07-28 20:10:21 -04:00
|
|
|
DeleteOnTermination: aws.Bool(true),
|
2015-04-05 17:58:48 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
} else {
|
2015-08-17 20:44:01 -04:00
|
|
|
runOpts.SubnetId = &s.SubnetId
|
|
|
|
runOpts.SecurityGroupIds = securityGroupIds
|
2014-05-07 13:13:27 -04:00
|
|
|
}
|
2015-04-05 17:58:48 -04:00
|
|
|
|
2016-06-26 20:28:54 -04:00
|
|
|
if s.ExpectedRootDevice == "ebs" {
|
|
|
|
runOpts.InstanceInitiatedShutdownBehavior = &s.InstanceInitiatedShutdownBehavior
|
|
|
|
}
|
|
|
|
|
2014-05-07 13:13:27 -04:00
|
|
|
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
|
|
|
|
}
|
2015-08-17 20:44:01 -04:00
|
|
|
instanceId = *runResp.Instances[0].InstanceId
|
2014-05-07 13:13:27 -04:00
|
|
|
} else {
|
2014-09-05 19:58:17 -04:00
|
|
|
ui.Message(fmt.Sprintf(
|
|
|
|
"Requesting spot instance '%s' for: %s",
|
2014-09-06 13:44:12 -04:00
|
|
|
s.InstanceType, spotPrice))
|
2016-10-02 16:20:36 -04:00
|
|
|
|
|
|
|
runOpts := &ec2.RequestSpotLaunchSpecification{
|
|
|
|
ImageId: &s.SourceAMI,
|
|
|
|
InstanceType: &s.InstanceType,
|
|
|
|
UserData: &userData,
|
|
|
|
IamInstanceProfile: &ec2.IamInstanceProfileSpecification{Name: &s.IamInstanceProfile},
|
2015-07-15 01:33:22 -04:00
|
|
|
Placement: &ec2.SpotPlacement{
|
|
|
|
AvailabilityZone: &availabilityZone,
|
|
|
|
},
|
|
|
|
BlockDeviceMappings: s.BlockDevices.BuildLaunchDevices(),
|
|
|
|
EbsOptimized: &s.EbsOptimized,
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.SubnetId != "" && s.AssociatePublicIpAddress {
|
|
|
|
runOpts.NetworkInterfaces = []*ec2.InstanceNetworkInterfaceSpecification{
|
2016-11-24 15:12:20 -05:00
|
|
|
{
|
2016-10-02 16:20:36 -04:00
|
|
|
DeviceIndex: aws.Int64(0),
|
|
|
|
AssociatePublicIpAddress: &s.AssociatePublicIpAddress,
|
|
|
|
SubnetId: &s.SubnetId,
|
|
|
|
Groups: securityGroupIds,
|
|
|
|
DeleteOnTermination: aws.Bool(true),
|
2015-05-28 12:35:02 -04:00
|
|
|
},
|
2015-07-15 01:33:22 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
runOpts.SubnetId = &s.SubnetId
|
|
|
|
runOpts.SecurityGroupIds = securityGroupIds
|
2016-10-02 16:20:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if keyName != "" {
|
|
|
|
runOpts.KeyName = &keyName
|
|
|
|
}
|
|
|
|
|
|
|
|
runSpotResp, err := ec2conn.RequestSpotInstances(&ec2.RequestSpotInstancesInput{
|
|
|
|
SpotPrice: &spotPrice,
|
|
|
|
LaunchSpecification: runOpts,
|
2015-04-05 17:58:48 -04:00
|
|
|
})
|
2014-05-07 13:13:27 -04:00
|
|
|
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-09-05 19:58:17 -04:00
|
|
|
|
2015-04-05 17:58:48 -04:00
|
|
|
s.spotRequest = runSpotResp.SpotInstanceRequests[0]
|
2014-09-05 19:58:17 -04:00
|
|
|
|
2015-08-17 20:44:01 -04:00
|
|
|
spotRequestId := s.spotRequest.SpotInstanceRequestId
|
2015-04-05 17:58:48 -04:00
|
|
|
ui.Message(fmt.Sprintf("Waiting for spot request (%s) to become active...", *spotRequestId))
|
2014-05-07 13:13:27 -04:00
|
|
|
stateChange := StateChangeConf{
|
|
|
|
Pending: []string{"open"},
|
|
|
|
Target: "active",
|
2015-04-05 17:58:48 -04:00
|
|
|
Refresh: SpotRequestStateRefreshFunc(ec2conn, *spotRequestId),
|
2014-05-07 13:13:27 -04:00
|
|
|
StepState: state,
|
|
|
|
}
|
|
|
|
_, err = WaitForState(&stateChange)
|
|
|
|
if err != nil {
|
2015-04-05 17:58:48 -04:00
|
|
|
err := fmt.Errorf("Error waiting for spot request (%s) to become ready: %s", *spotRequestId, err)
|
2014-05-07 13:13:27 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2015-04-05 17:58:48 -04:00
|
|
|
|
|
|
|
spotResp, err := ec2conn.DescribeSpotInstanceRequests(&ec2.DescribeSpotInstanceRequestsInput{
|
2015-08-17 20:44:01 -04:00
|
|
|
SpotInstanceRequestIds: []*string{spotRequestId},
|
2015-04-05 17:58:48 -04:00
|
|
|
})
|
2014-05-07 13:13:27 -04:00
|
|
|
if err != nil {
|
2015-04-05 17:58:48 -04:00
|
|
|
err := fmt.Errorf("Error finding spot request (%s): %s", *spotRequestId, err)
|
2014-05-07 13:13:27 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2015-08-17 20:44:01 -04:00
|
|
|
instanceId = *spotResp.SpotInstanceRequests[0].InstanceId
|
2017-08-11 13:43:05 -04:00
|
|
|
|
2014-05-07 13:13:27 -04:00
|
|
|
}
|
|
|
|
|
2015-07-07 13:07:38 -04:00
|
|
|
// Set the instance ID so that the cleanup works properly
|
|
|
|
s.instanceId = instanceId
|
|
|
|
|
2014-12-15 20:18:47 -05:00
|
|
|
ui.Message(fmt.Sprintf("Instance ID: %s", instanceId))
|
|
|
|
ui.Say(fmt.Sprintf("Waiting for instance (%v) to become ready...", instanceId))
|
2013-07-25 21:49:15 -04:00
|
|
|
stateChange := StateChangeConf{
|
|
|
|
Pending: []string{"pending"},
|
|
|
|
Target: "running",
|
2014-12-15 20:18:47 -05:00
|
|
|
Refresh: InstanceStateRefreshFunc(ec2conn, instanceId),
|
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 {
|
2015-06-15 12:53:21 -04:00
|
|
|
err := fmt.Errorf("Error waiting for instance (%s) to become ready: %s", 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
|
|
|
}
|
|
|
|
|
2015-07-07 13:07:38 -04:00
|
|
|
instance := latestInstance.(*ec2.Instance)
|
2013-08-30 17:55:56 -04:00
|
|
|
|
2017-08-11 15:31:05 -04:00
|
|
|
if createTagsAfterInstanceStarts {
|
|
|
|
// Retry creating tags for about 2.5 minutes
|
|
|
|
err = retry.Retry(0.2, 30, 11, func(_ uint) (bool, error) {
|
|
|
|
_, err := ec2conn.CreateTags(&ec2.CreateTagsInput{
|
|
|
|
Tags: ec2Tags,
|
|
|
|
Resources: []*string{instance.InstanceId},
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
if awsErr, ok := err.(awserr.Error); ok {
|
|
|
|
if awsErr.Code() == "InvalidInstanceID.NotFound" {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true, err
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error tagging source instance: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2017-01-18 06:55:18 -05:00
|
|
|
}
|
2017-01-10 05:41:28 -05:00
|
|
|
|
2013-08-30 17:55:56 -04:00
|
|
|
if s.Debug {
|
2015-08-17 20:44:01 -04:00
|
|
|
if instance.PublicDnsName != nil && *instance.PublicDnsName != "" {
|
|
|
|
ui.Message(fmt.Sprintf("Public DNS: %s", *instance.PublicDnsName))
|
2013-08-30 17:55:56 -04:00
|
|
|
}
|
|
|
|
|
2015-08-17 20:44:01 -04:00
|
|
|
if instance.PublicIpAddress != nil && *instance.PublicIpAddress != "" {
|
|
|
|
ui.Message(fmt.Sprintf("Public IP: %s", *instance.PublicIpAddress))
|
2013-11-26 00:03:45 -05:00
|
|
|
}
|
|
|
|
|
2015-08-17 20:44:01 -04:00
|
|
|
if instance.PrivateIpAddress != nil && *instance.PrivateIpAddress != "" {
|
|
|
|
ui.Message(fmt.Sprintf("Private IP: %s", *instance.PrivateIpAddress))
|
2013-08-30 17:55:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-07 13:07:38 -04:00
|
|
|
state.Put("instance", 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
|
|
|
|
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
|
|
|
|
2014-05-10 12:51:35 -04:00
|
|
|
// Cancel the spot request if it exists
|
|
|
|
if s.spotRequest != nil {
|
|
|
|
ui.Say("Cancelling the spot request...")
|
2015-04-05 17:58:48 -04:00
|
|
|
input := &ec2.CancelSpotInstanceRequestsInput{
|
2015-08-17 20:44:01 -04:00
|
|
|
SpotInstanceRequestIds: []*string{s.spotRequest.SpotInstanceRequestId},
|
2015-04-05 17:58:48 -04:00
|
|
|
}
|
|
|
|
if _, err := ec2conn.CancelSpotInstanceRequests(input); err != nil {
|
2014-05-10 12:51:35 -04:00
|
|
|
ui.Error(fmt.Sprintf("Error cancelling the spot request, may still be around: %s", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
stateChange := StateChangeConf{
|
|
|
|
Pending: []string{"active", "open"},
|
2015-08-17 20:44:01 -04:00
|
|
|
Refresh: SpotRequestStateRefreshFunc(ec2conn, *s.spotRequest.SpotInstanceRequestId),
|
2014-05-10 12:51:35 -04:00
|
|
|
Target: "cancelled",
|
|
|
|
}
|
|
|
|
|
2017-03-28 23:35:22 -04:00
|
|
|
_, err := WaitForState(&stateChange)
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(err.Error())
|
|
|
|
}
|
2013-06-27 21:42:07 -04:00
|
|
|
|
2013-07-25 21:49:15 -04:00
|
|
|
}
|
|
|
|
|
2014-05-10 12:51:35 -04:00
|
|
|
// Terminate the source instance if it exists
|
2015-07-07 13:07:38 -04:00
|
|
|
if s.instanceId != "" {
|
2014-05-10 12:51:35 -04:00
|
|
|
ui.Say("Terminating the source AWS instance...")
|
2015-08-17 20:44:01 -04:00
|
|
|
if _, err := ec2conn.TerminateInstances(&ec2.TerminateInstancesInput{InstanceIds: []*string{&s.instanceId}}); err != nil {
|
2014-05-10 12:51:35 -04:00
|
|
|
ui.Error(fmt.Sprintf("Error terminating instance, may still be around: %s", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
stateChange := StateChangeConf{
|
|
|
|
Pending: []string{"pending", "running", "shutting-down", "stopped", "stopping"},
|
2015-07-07 13:07:38 -04:00
|
|
|
Refresh: InstanceStateRefreshFunc(ec2conn, s.instanceId),
|
2014-05-10 12:51:35 -04:00
|
|
|
Target: "terminated",
|
|
|
|
}
|
|
|
|
|
2017-03-28 23:35:22 -04:00
|
|
|
_, err := WaitForState(&stateChange)
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(err.Error())
|
|
|
|
}
|
2014-05-10 12:51:35 -04:00
|
|
|
}
|
2013-05-21 03:55:32 -04:00
|
|
|
}
|