2013-07-20 22:58:27 -04:00
|
|
|
package common
|
2013-05-21 03:55:32 -04:00
|
|
|
|
|
|
|
import (
|
2018-01-18 01:49:03 -05:00
|
|
|
"context"
|
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"
|
2019-04-08 11:57:27 -04:00
|
|
|
"time"
|
2014-09-06 13:44:12 -04:00
|
|
|
|
2015-06-03 17:13:52 -04:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2015-04-05 17:58:48 -04:00
|
|
|
|
2019-04-08 11:57:27 -04:00
|
|
|
"github.com/hashicorp/packer/common/retry"
|
2018-08-29 05:23:59 -04:00
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
2013-05-21 03:55:32 -04:00
|
|
|
)
|
|
|
|
|
2013-07-20 22:58:27 -04:00
|
|
|
type StepRunSourceInstance struct {
|
2020-08-17 11:09:19 -04:00
|
|
|
PollingConfig *AWSPollingConfig
|
2016-05-20 05:54:45 -04:00
|
|
|
AssociatePublicIpAddress bool
|
2019-06-18 10:02:08 -04:00
|
|
|
LaunchMappings EC2BlockDeviceMappingsBuilder
|
2018-08-29 05:23:59 -04:00
|
|
|
Comm *communicator.Config
|
2018-02-02 23:16:23 -05:00
|
|
|
Ctx interpolate.Context
|
2016-05-20 05:54:45 -04:00
|
|
|
Debug bool
|
|
|
|
EbsOptimized bool
|
2018-05-13 11:32:27 -04:00
|
|
|
EnableT2Unlimited bool
|
2016-05-20 05:54:45 -04:00
|
|
|
ExpectedRootDevice string
|
2017-01-18 18:11:52 -05:00
|
|
|
InstanceInitiatedShutdownBehavior string
|
|
|
|
InstanceType string
|
2018-02-02 23:16:23 -05:00
|
|
|
IsRestricted bool
|
2016-05-20 05:54:45 -04:00
|
|
|
SourceAMI string
|
2020-03-16 12:47:44 -04:00
|
|
|
Tags map[string]string
|
2016-05-20 05:54:45 -04:00
|
|
|
UserData string
|
|
|
|
UserDataFile string
|
2020-03-16 12:47:44 -04:00
|
|
|
VolumeTags map[string]string
|
2019-11-19 14:52:18 -05:00
|
|
|
NoEphemeral bool
|
2014-09-06 13:44:12 -04:00
|
|
|
|
2017-10-04 05:29:38 -04:00
|
|
|
instanceId string
|
2013-05-21 03:55:32 -04:00
|
|
|
}
|
|
|
|
|
2018-04-23 15:57:04 -04:00
|
|
|
func (s *StepRunSourceInstance) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2013-08-31 15:58:55 -04:00
|
|
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
2018-08-29 05:23:59 -04:00
|
|
|
|
2017-01-18 18:11:52 -05:00
|
|
|
securityGroupIds := aws.StringSlice(state.Get("securityGroupIds").([]string))
|
2019-10-19 05:56:18 -04:00
|
|
|
iamInstanceProfile := aws.String(state.Get("iamInstanceProfile").(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
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
2020-03-16 12:47:44 -04:00
|
|
|
ec2Tags, err := TagMap(s.Tags).EC2Tags(s.Ctx, *ec2conn.Config.Region, state)
|
2017-07-31 14:38:30 -04:00
|
|
|
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
|
|
|
|
2020-03-16 12:47:44 -04:00
|
|
|
volTags, err := TagMap(s.VolumeTags).EC2Tags(s.Ctx, *ec2conn.Config.Region, state)
|
2017-10-02 18:05:40 -04:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error tagging volumes: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2018-08-14 06:04:13 -04:00
|
|
|
az := state.Get("availability_zone").(string)
|
2017-10-04 05:29:38 -04:00
|
|
|
runOpts := &ec2.RunInstancesInput{
|
|
|
|
ImageId: &s.SourceAMI,
|
|
|
|
InstanceType: &s.InstanceType,
|
|
|
|
UserData: &userData,
|
|
|
|
MaxCount: aws.Int64(1),
|
|
|
|
MinCount: aws.Int64(1),
|
2019-10-19 05:56:18 -04:00
|
|
|
IamInstanceProfile: &ec2.IamInstanceProfileSpecification{Name: iamInstanceProfile},
|
2019-06-18 06:44:24 -04:00
|
|
|
BlockDeviceMappings: s.LaunchMappings.BuildEC2BlockDeviceMappings(),
|
2018-08-14 06:04:13 -04:00
|
|
|
Placement: &ec2.Placement{AvailabilityZone: &az},
|
2017-10-04 05:29:38 -04:00
|
|
|
EbsOptimized: &s.EbsOptimized,
|
|
|
|
}
|
2017-10-02 18:05:40 -04:00
|
|
|
|
2019-11-19 14:52:18 -05:00
|
|
|
if s.NoEphemeral {
|
|
|
|
// This is only relevant for windows guests. Ephemeral drives by
|
|
|
|
// default are assigned to drive names xvdca-xvdcz.
|
|
|
|
// When vms are launched from the AWS console, they're automatically
|
|
|
|
// removed from the block devices if the user hasn't said to use them,
|
|
|
|
// but the SDK does not perform this cleanup. The following code just
|
|
|
|
// manually removes the ephemeral drives from the mapping so that they
|
|
|
|
// don't clutter up console views and cause confusion.
|
|
|
|
log.Printf("no_ephemeral was set, so creating drives xvdca-xvdcz as empty mappings")
|
|
|
|
DefaultEphemeralDeviceLetters := "abcdefghijklmnopqrstuvwxyz"
|
2019-11-21 15:46:31 -05:00
|
|
|
for _, letter := range DefaultEphemeralDeviceLetters {
|
2019-11-19 14:52:18 -05:00
|
|
|
bd := &ec2.BlockDeviceMapping{
|
2019-11-21 15:46:31 -05:00
|
|
|
DeviceName: aws.String("xvdc" + string(letter)),
|
2019-11-19 14:52:18 -05:00
|
|
|
NoDevice: aws.String(""),
|
|
|
|
}
|
|
|
|
runOpts.BlockDeviceMappings = append(runOpts.BlockDeviceMappings, bd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-13 11:32:27 -04:00
|
|
|
if s.EnableT2Unlimited {
|
|
|
|
creditOption := "unlimited"
|
|
|
|
runOpts.CreditSpecification = &ec2.CreditSpecificationRequest{CpuCredits: &creditOption}
|
|
|
|
}
|
|
|
|
|
2018-02-02 23:16:23 -05:00
|
|
|
// Collect tags for tagging on resource creation
|
2017-10-04 05:29:38 -04:00
|
|
|
var tagSpecs []*ec2.TagSpecification
|
2017-08-11 15:31:05 -04:00
|
|
|
|
2017-10-04 05:29:38 -04:00
|
|
|
if len(ec2Tags) > 0 {
|
|
|
|
runTags := &ec2.TagSpecification{
|
|
|
|
ResourceType: aws.String("instance"),
|
|
|
|
Tags: ec2Tags,
|
2015-04-05 17:58:48 -04:00
|
|
|
}
|
|
|
|
|
2017-10-04 05:29:38 -04:00
|
|
|
tagSpecs = append(tagSpecs, runTags)
|
|
|
|
}
|
2017-10-02 18:05:40 -04:00
|
|
|
|
2017-10-04 05:29:38 -04:00
|
|
|
if len(volTags) > 0 {
|
|
|
|
runVolTags := &ec2.TagSpecification{
|
|
|
|
ResourceType: aws.String("volume"),
|
|
|
|
Tags: volTags,
|
2017-10-02 18:05:40 -04:00
|
|
|
}
|
|
|
|
|
2017-10-04 05:29:38 -04:00
|
|
|
tagSpecs = append(tagSpecs, runVolTags)
|
|
|
|
}
|
2016-10-02 16:20:36 -04:00
|
|
|
|
2018-02-02 23:16:23 -05:00
|
|
|
// If our region supports it, set tag specifications
|
|
|
|
if len(tagSpecs) > 0 && !s.IsRestricted {
|
2017-10-04 05:29:38 -04:00
|
|
|
runOpts.SetTagSpecifications(tagSpecs)
|
2018-02-02 23:16:23 -05:00
|
|
|
ec2Tags.Report(ui)
|
|
|
|
volTags.Report(ui)
|
2017-10-04 05:29:38 -04:00
|
|
|
}
|
2015-04-05 17:58:48 -04:00
|
|
|
|
2018-08-29 05:23:59 -04:00
|
|
|
if s.Comm.SSHKeyPairName != "" {
|
|
|
|
runOpts.KeyName = &s.Comm.SSHKeyPairName
|
2017-10-04 05:29:38 -04:00
|
|
|
}
|
2016-06-26 20:28:54 -04:00
|
|
|
|
2018-08-14 06:04:13 -04:00
|
|
|
subnetId := state.Get("subnet_id").(string)
|
2018-06-12 06:05:16 -04:00
|
|
|
|
2018-08-14 06:04:13 -04:00
|
|
|
if subnetId != "" && s.AssociatePublicIpAddress {
|
2017-10-04 05:29:38 -04:00
|
|
|
runOpts.NetworkInterfaces = []*ec2.InstanceNetworkInterfaceSpecification{
|
|
|
|
{
|
|
|
|
DeviceIndex: aws.Int64(0),
|
|
|
|
AssociatePublicIpAddress: &s.AssociatePublicIpAddress,
|
2018-08-14 06:04:13 -04:00
|
|
|
SubnetId: aws.String(subnetId),
|
2017-10-04 05:29:38 -04:00
|
|
|
Groups: securityGroupIds,
|
|
|
|
DeleteOnTermination: aws.Bool(true),
|
2015-07-15 01:33:22 -04:00
|
|
|
},
|
2014-05-07 13:13:27 -04:00
|
|
|
}
|
2017-10-04 05:29:38 -04:00
|
|
|
} else {
|
2018-08-14 06:04:13 -04:00
|
|
|
runOpts.SubnetId = aws.String(subnetId)
|
2017-10-04 05:29:38 -04:00
|
|
|
runOpts.SecurityGroupIds = securityGroupIds
|
|
|
|
}
|
2015-04-05 17:58:48 -04:00
|
|
|
|
2017-10-04 05:29:38 -04:00
|
|
|
if s.ExpectedRootDevice == "ebs" {
|
|
|
|
runOpts.InstanceInitiatedShutdownBehavior = &s.InstanceInitiatedShutdownBehavior
|
|
|
|
}
|
2017-08-11 13:43:05 -04:00
|
|
|
|
2019-11-29 12:11:10 -05:00
|
|
|
var runResp *ec2.Reservation
|
|
|
|
err = retry.Config{
|
|
|
|
Tries: 11,
|
|
|
|
ShouldRetry: func(err error) bool {
|
2020-07-15 12:47:07 -04:00
|
|
|
if IsAWSErr(err, "InvalidParameterValue", "iamInstanceProfile") {
|
2019-11-29 12:11:10 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
RetryDelay: (&retry.Backoff{InitialBackoff: 200 * time.Millisecond, MaxBackoff: 30 * time.Second, Multiplier: 2}).Linear,
|
|
|
|
}.Run(ctx, func(ctx context.Context) error {
|
|
|
|
runResp, err = ec2conn.RunInstances(runOpts)
|
|
|
|
return err
|
|
|
|
})
|
2019-11-08 16:13:45 -05:00
|
|
|
|
2020-07-15 12:47:07 -04:00
|
|
|
if IsAWSErr(err, "VPCIdNotSpecified", "No default VPC for this user") && subnetId == "" {
|
2019-11-08 16:13:45 -05:00
|
|
|
err := fmt.Errorf("Error launching source instance: a valid Subnet Id was not specified")
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2017-10-04 05:29:38 -04:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error launching source instance: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
2014-05-07 13:13:27 -04:00
|
|
|
}
|
2017-10-04 05:29:38 -04:00
|
|
|
instanceId = *runResp.Instances[0].InstanceId
|
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))
|
2017-12-07 15:31:50 -05:00
|
|
|
|
2018-01-08 17:26:40 -05:00
|
|
|
describeInstance := &ec2.DescribeInstancesInput{
|
2017-12-07 15:31:50 -05:00
|
|
|
InstanceIds: []*string{aws.String(instanceId)},
|
2013-07-25 21:49:15 -04:00
|
|
|
}
|
2020-02-05 18:09:09 -05:00
|
|
|
|
2020-08-17 11:09:19 -04:00
|
|
|
if err := s.PollingConfig.WaitUntilInstanceRunning(ctx, ec2conn, instanceId); 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
|
|
|
}
|
|
|
|
|
2019-09-26 17:49:28 -04:00
|
|
|
// there's a race condition that can happen because of AWS's eventual
|
|
|
|
// consistency where even though the wait is complete, the describe call
|
|
|
|
// will fail. Retry a couple of times to try to mitigate that race.
|
|
|
|
|
|
|
|
var r *ec2.DescribeInstancesOutput
|
2019-11-12 15:27:47 -05:00
|
|
|
err = retry.Config{Tries: 11, ShouldRetry: func(err error) bool {
|
2020-07-15 12:47:07 -04:00
|
|
|
if IsAWSErr(err, "InvalidInstanceID.NotFound", "") {
|
2019-11-12 15:27:47 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
},
|
2019-09-26 17:49:28 -04:00
|
|
|
RetryDelay: (&retry.Backoff{InitialBackoff: 200 * time.Millisecond, MaxBackoff: 30 * time.Second, Multiplier: 2}).Linear,
|
|
|
|
}.Run(ctx, func(ctx context.Context) error {
|
|
|
|
r, err = ec2conn.DescribeInstances(describeInstance)
|
|
|
|
return err
|
|
|
|
})
|
2017-12-07 15:31:50 -05:00
|
|
|
if err != nil || len(r.Reservations) == 0 || len(r.Reservations[0].Instances) == 0 {
|
|
|
|
err := fmt.Errorf("Error finding source instance.")
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2019-09-26 17:49:28 -04:00
|
|
|
|
2017-12-07 15:31:50 -05:00
|
|
|
instance := r.Reservations[0].Instances[0]
|
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)
|
2019-12-13 14:57:01 -05:00
|
|
|
// instance_id is the generic term used so that users can have access to the
|
|
|
|
// instance id inside of the provisioners, used in step_provision.
|
|
|
|
state.Put("instance_id", instance.InstanceId)
|
2013-05-21 03:55:32 -04:00
|
|
|
|
2018-02-02 23:16:23 -05:00
|
|
|
// If we're in a region that doesn't support tagging on instance creation,
|
|
|
|
// do that now.
|
|
|
|
|
|
|
|
if s.IsRestricted {
|
|
|
|
ec2Tags.Report(ui)
|
|
|
|
// Retry creating tags for about 2.5 minutes
|
2019-11-12 15:27:47 -05:00
|
|
|
err = retry.Config{Tries: 11, ShouldRetry: func(error) bool {
|
2020-07-15 12:47:07 -04:00
|
|
|
if IsAWSErr(err, "InvalidInstanceID.NotFound", "") {
|
2019-11-12 15:27:47 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
},
|
2019-04-08 11:57:27 -04:00
|
|
|
RetryDelay: (&retry.Backoff{InitialBackoff: 200 * time.Millisecond, MaxBackoff: 30 * time.Second, Multiplier: 2}).Linear,
|
|
|
|
}.Run(ctx, func(ctx context.Context) error {
|
2018-02-02 23:16:23 -05:00
|
|
|
_, err := ec2conn.CreateTags(&ec2.CreateTagsInput{
|
|
|
|
Tags: ec2Tags,
|
|
|
|
Resources: []*string{instance.InstanceId},
|
|
|
|
})
|
2019-04-08 11:57:27 -04:00
|
|
|
return err
|
2018-02-02 23:16:23 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error tagging source instance: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now tag volumes
|
|
|
|
|
|
|
|
volumeIds := make([]*string, 0)
|
|
|
|
for _, v := range instance.BlockDeviceMappings {
|
|
|
|
if ebs := v.Ebs; ebs != nil {
|
|
|
|
volumeIds = append(volumeIds, ebs.VolumeId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-16 12:47:44 -04:00
|
|
|
if len(volumeIds) > 0 && len(s.VolumeTags) > 0 {
|
2018-02-02 23:16:23 -05:00
|
|
|
ui.Say("Adding tags to source EBS Volumes")
|
|
|
|
|
2020-03-16 12:47:44 -04:00
|
|
|
volumeTags, err := TagMap(s.VolumeTags).EC2Tags(s.Ctx, *ec2conn.Config.Region, state)
|
2018-02-02 23:16:23 -05:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error tagging source EBS Volumes on %s: %s", *instance.InstanceId, err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
volumeTags.Report(ui)
|
|
|
|
|
|
|
|
_, err = ec2conn.CreateTags(&ec2.CreateTagsInput{
|
|
|
|
Resources: volumeIds,
|
|
|
|
Tags: volumeTags,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error tagging source EBS Volumes on %s: %s", *instance.InstanceId, err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2020-08-17 11:09:19 -04:00
|
|
|
if err := s.PollingConfig.WaitUntilInstanceTerminated(aws.BackgroundContext(), ec2conn, s.instanceId); err != nil {
|
2017-03-28 23:35:22 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
}
|
2014-05-10 12:51:35 -04:00
|
|
|
}
|
2013-05-21 03:55:32 -04:00
|
|
|
}
|