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"
|
2018-02-02 23:16:23 -05: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
|
|
|
|
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 {
|
2016-05-20 05:54:45 -04:00
|
|
|
AssociatePublicIpAddress bool
|
|
|
|
BlockDevices BlockDevices
|
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
|
|
|
|
IamInstanceProfile 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
|
2018-02-02 23:16:23 -05:00
|
|
|
Tags TagMap
|
2016-05-20 05:54:45 -04:00
|
|
|
UserData string
|
|
|
|
UserDataFile string
|
2018-02-02 23:16:23 -05:00
|
|
|
VolumeTags TagMap
|
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))
|
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"
|
|
|
|
}
|
|
|
|
|
2018-03-30 04:47:11 -04:00
|
|
|
ec2Tags, err := 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
|
|
|
|
2018-03-30 04:47:11 -04:00
|
|
|
volTags, err := 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),
|
|
|
|
IamInstanceProfile: &ec2.IamInstanceProfileSpecification{Name: &s.IamInstanceProfile},
|
|
|
|
BlockDeviceMappings: s.BlockDevices.BuildLaunchDevices(),
|
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
|
|
|
|
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
|
|
|
|
2017-10-04 05:29:38 -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
|
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
|
|
|
}
|
2018-04-23 15:57:04 -04:00
|
|
|
if err := ec2conn.WaitUntilInstanceRunningWithContext(ctx, describeInstance); 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
|
|
|
}
|
|
|
|
|
2018-01-08 17:26:40 -05:00
|
|
|
r, err := ec2conn.DescribeInstances(describeInstance)
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
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)
|
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-04-08 11:57:27 -04:00
|
|
|
err = retry.Config{
|
|
|
|
Tries: 11,
|
|
|
|
ShouldRetry: func(error) bool {
|
|
|
|
if awsErr, ok := err.(awserr.Error); ok {
|
|
|
|
switch awsErr.Code() {
|
|
|
|
case "InvalidInstanceID.NotFound":
|
|
|
|
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 {
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(volumeIds) > 0 && s.VolumeTags.IsSet() {
|
|
|
|
ui.Say("Adding tags to source EBS Volumes")
|
|
|
|
|
2018-03-30 04:47:11 -04:00
|
|
|
volumeTags, err := 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
|
|
|
|
}
|
|
|
|
|
2018-06-01 19:17:30 -04:00
|
|
|
if err := 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
|
|
|
}
|