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"
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
SubnetId string
|
|
|
|
Tags map[string]string
|
2017-10-02 18:05:40 -04:00
|
|
|
VolumeTags map[string]string
|
2016-05-20 05:54:45 -04:00
|
|
|
UserData string
|
|
|
|
UserDataFile string
|
2017-01-10 05:41:28 -05:00
|
|
|
Ctx interpolate.Context
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
2017-10-02 18:05:40 -04:00
|
|
|
volTags, err := ConvertToEC2Tags(s.VolumeTags, *ec2conn.Config.Region, s.SourceAMI, s.Ctx)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error tagging volumes: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
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(),
|
|
|
|
Placement: &ec2.Placement{AvailabilityZone: &s.AvailabilityZone},
|
|
|
|
EbsOptimized: &s.EbsOptimized,
|
|
|
|
}
|
2017-10-02 18:05:40 -04:00
|
|
|
|
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
|
|
|
|
2017-10-04 05:29:38 -04:00
|
|
|
if len(tagSpecs) > 0 {
|
|
|
|
runOpts.SetTagSpecifications(tagSpecs)
|
|
|
|
}
|
2015-04-05 17:58:48 -04:00
|
|
|
|
2017-10-04 05:29:38 -04:00
|
|
|
if keyName != "" {
|
|
|
|
runOpts.KeyName = &keyName
|
|
|
|
}
|
2016-06-26 20:28:54 -04:00
|
|
|
|
2017-10-04 05:29:38 -04:00
|
|
|
if s.SubnetId != "" && s.AssociatePublicIpAddress {
|
|
|
|
runOpts.NetworkInterfaces = []*ec2.InstanceNetworkInterfaceSpecification{
|
|
|
|
{
|
|
|
|
DeviceIndex: aws.Int64(0),
|
|
|
|
AssociatePublicIpAddress: &s.AssociatePublicIpAddress,
|
|
|
|
SubnetId: &s.SubnetId,
|
|
|
|
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 {
|
|
|
|
runOpts.SubnetId = &s.SubnetId
|
|
|
|
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
|
|
|
|
|
|
|
describeInstanceStatus := &ec2.DescribeInstanceStatusInput{
|
|
|
|
InstanceIds: []*string{aws.String(instanceId)},
|
2013-07-25 21:49:15 -04:00
|
|
|
}
|
2017-12-07 15:31:50 -05:00
|
|
|
if err := ec2conn.WaitUntilInstanceStatusOk(describeInstanceStatus); 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
|
|
|
}
|
|
|
|
|
2017-12-07 15:31:50 -05:00
|
|
|
r, err := ec2conn.DescribeInstances(&ec2.DescribeInstancesInput{
|
|
|
|
InstanceIds: []*string{aws.String(instanceId)},
|
|
|
|
})
|
|
|
|
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
|
|
|
|
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
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|