Move building of spot instances into its own step
This commit is contained in:
parent
939b44b4fd
commit
2661fd7869
|
@ -5,14 +5,10 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"strconv"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/aws/aws-sdk-go/aws"
|
"github.com/aws/aws-sdk-go/aws"
|
||||||
"github.com/aws/aws-sdk-go/aws/awserr"
|
|
||||||
"github.com/aws/aws-sdk-go/service/ec2"
|
"github.com/aws/aws-sdk-go/service/ec2"
|
||||||
|
|
||||||
retry "github.com/hashicorp/packer/common"
|
|
||||||
"github.com/hashicorp/packer/packer"
|
"github.com/hashicorp/packer/packer"
|
||||||
"github.com/hashicorp/packer/template/interpolate"
|
"github.com/hashicorp/packer/template/interpolate"
|
||||||
"github.com/mitchellh/multistep"
|
"github.com/mitchellh/multistep"
|
||||||
|
@ -29,8 +25,6 @@ type StepRunSourceInstance struct {
|
||||||
InstanceInitiatedShutdownBehavior string
|
InstanceInitiatedShutdownBehavior string
|
||||||
InstanceType string
|
InstanceType string
|
||||||
SourceAMI string
|
SourceAMI string
|
||||||
SpotPrice string
|
|
||||||
SpotPriceProduct string
|
|
||||||
SubnetId string
|
SubnetId string
|
||||||
Tags map[string]string
|
Tags map[string]string
|
||||||
VolumeTags map[string]string
|
VolumeTags map[string]string
|
||||||
|
@ -39,7 +33,6 @@ type StepRunSourceInstance struct {
|
||||||
Ctx interpolate.Context
|
Ctx interpolate.Context
|
||||||
|
|
||||||
instanceId string
|
instanceId string
|
||||||
spotRequest *ec2.SpotInstanceRequest
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StepRunSourceInstance) Run(state multistep.StateBag) multistep.StepAction {
|
func (s *StepRunSourceInstance) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
|
@ -84,57 +77,6 @@ func (s *StepRunSourceInstance) Run(state multistep.StateBag) multistep.StepActi
|
||||||
return multistep.ActionHalt
|
return multistep.ActionHalt
|
||||||
}
|
}
|
||||||
|
|
||||||
spotPrice := s.SpotPrice
|
|
||||||
availabilityZone := s.AvailabilityZone
|
|
||||||
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)
|
|
||||||
resp, err := ec2conn.DescribeSpotPriceHistory(&ec2.DescribeSpotPriceHistoryInput{
|
|
||||||
InstanceTypes: []*string{&s.InstanceType},
|
|
||||||
ProductDescriptions: []*string{&s.SpotPriceProduct},
|
|
||||||
AvailabilityZone: &s.AvailabilityZone,
|
|
||||||
StartTime: &startTime,
|
|
||||||
})
|
|
||||||
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
|
|
||||||
for _, history := range resp.SpotPriceHistory {
|
|
||||||
log.Printf("[INFO] Candidate spot price: %s", *history.SpotPrice)
|
|
||||||
current, err := strconv.ParseFloat(*history.SpotPrice, 64)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("[ERR] Error parsing spot price: %s", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if price == 0 || current < price {
|
|
||||||
price = current
|
|
||||||
if s.AvailabilityZone == "" {
|
|
||||||
availabilityZone = *history.AvailabilityZone
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if price == 0 {
|
|
||||||
err := fmt.Errorf("No candidate spot prices found!")
|
|
||||||
state.Put("error", err)
|
|
||||||
ui.Error(err.Error())
|
|
||||||
return multistep.ActionHalt
|
|
||||||
} 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
|
|
||||||
}
|
|
||||||
|
|
||||||
spotPrice = strconv.FormatFloat(price, 'f', -1, 64)
|
|
||||||
}
|
|
||||||
|
|
||||||
var instanceId string
|
var instanceId string
|
||||||
|
|
||||||
ui.Say("Adding tags to source instance")
|
ui.Say("Adding tags to source instance")
|
||||||
|
@ -142,7 +84,6 @@ func (s *StepRunSourceInstance) Run(state multistep.StateBag) multistep.StepActi
|
||||||
s.Tags["Name"] = "Packer Builder"
|
s.Tags["Name"] = "Packer Builder"
|
||||||
}
|
}
|
||||||
|
|
||||||
createTagsAfterInstanceStarts := true
|
|
||||||
ec2Tags, err := ConvertToEC2Tags(s.Tags, *ec2conn.Config.Region, s.SourceAMI, s.Ctx)
|
ec2Tags, err := ConvertToEC2Tags(s.Tags, *ec2conn.Config.Region, s.SourceAMI, s.Ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err := fmt.Errorf("Error tagging source instance: %s", err)
|
err := fmt.Errorf("Error tagging source instance: %s", err)
|
||||||
|
@ -152,7 +93,6 @@ func (s *StepRunSourceInstance) Run(state multistep.StateBag) multistep.StepActi
|
||||||
}
|
}
|
||||||
ReportTags(ui, ec2Tags)
|
ReportTags(ui, ec2Tags)
|
||||||
|
|
||||||
createVolTagsAfterInstanceStarts := true
|
|
||||||
volTags, err := ConvertToEC2Tags(s.VolumeTags, *ec2conn.Config.Region, s.SourceAMI, s.Ctx)
|
volTags, err := ConvertToEC2Tags(s.VolumeTags, *ec2conn.Config.Region, s.SourceAMI, s.Ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err := fmt.Errorf("Error tagging volumes: %s", err)
|
err := fmt.Errorf("Error tagging volumes: %s", err)
|
||||||
|
@ -161,8 +101,6 @@ func (s *StepRunSourceInstance) Run(state multistep.StateBag) multistep.StepActi
|
||||||
return multistep.ActionHalt
|
return multistep.ActionHalt
|
||||||
}
|
}
|
||||||
|
|
||||||
if spotPrice == "" || spotPrice == "0" {
|
|
||||||
|
|
||||||
runOpts := &ec2.RunInstancesInput{
|
runOpts := &ec2.RunInstancesInput{
|
||||||
ImageId: &s.SourceAMI,
|
ImageId: &s.SourceAMI,
|
||||||
InstanceType: &s.InstanceType,
|
InstanceType: &s.InstanceType,
|
||||||
|
@ -184,7 +122,6 @@ func (s *StepRunSourceInstance) Run(state multistep.StateBag) multistep.StepActi
|
||||||
}
|
}
|
||||||
|
|
||||||
tagSpecs = append(tagSpecs, runTags)
|
tagSpecs = append(tagSpecs, runTags)
|
||||||
createTagsAfterInstanceStarts = false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(volTags) > 0 {
|
if len(volTags) > 0 {
|
||||||
|
@ -194,7 +131,6 @@ func (s *StepRunSourceInstance) Run(state multistep.StateBag) multistep.StepActi
|
||||||
}
|
}
|
||||||
|
|
||||||
tagSpecs = append(tagSpecs, runVolTags)
|
tagSpecs = append(tagSpecs, runVolTags)
|
||||||
createVolTagsAfterInstanceStarts = false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(tagSpecs) > 0 {
|
if len(tagSpecs) > 0 {
|
||||||
|
@ -232,83 +168,6 @@ func (s *StepRunSourceInstance) Run(state multistep.StateBag) multistep.StepActi
|
||||||
return multistep.ActionHalt
|
return multistep.ActionHalt
|
||||||
}
|
}
|
||||||
instanceId = *runResp.Instances[0].InstanceId
|
instanceId = *runResp.Instances[0].InstanceId
|
||||||
} else {
|
|
||||||
ui.Message(fmt.Sprintf(
|
|
||||||
"Requesting spot instance '%s' for: %s",
|
|
||||||
s.InstanceType, spotPrice))
|
|
||||||
|
|
||||||
runOpts := &ec2.RequestSpotLaunchSpecification{
|
|
||||||
ImageId: &s.SourceAMI,
|
|
||||||
InstanceType: &s.InstanceType,
|
|
||||||
UserData: &userData,
|
|
||||||
IamInstanceProfile: &ec2.IamInstanceProfileSpecification{Name: &s.IamInstanceProfile},
|
|
||||||
Placement: &ec2.SpotPlacement{
|
|
||||||
AvailabilityZone: &availabilityZone,
|
|
||||||
},
|
|
||||||
BlockDeviceMappings: s.BlockDevices.BuildLaunchDevices(),
|
|
||||||
EbsOptimized: &s.EbsOptimized,
|
|
||||||
}
|
|
||||||
|
|
||||||
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),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
runOpts.SubnetId = &s.SubnetId
|
|
||||||
runOpts.SecurityGroupIds = securityGroupIds
|
|
||||||
}
|
|
||||||
|
|
||||||
if keyName != "" {
|
|
||||||
runOpts.KeyName = &keyName
|
|
||||||
}
|
|
||||||
|
|
||||||
runSpotResp, err := ec2conn.RequestSpotInstances(&ec2.RequestSpotInstancesInput{
|
|
||||||
SpotPrice: &spotPrice,
|
|
||||||
LaunchSpecification: 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
|
|
||||||
}
|
|
||||||
|
|
||||||
s.spotRequest = runSpotResp.SpotInstanceRequests[0]
|
|
||||||
|
|
||||||
spotRequestId := s.spotRequest.SpotInstanceRequestId
|
|
||||||
ui.Message(fmt.Sprintf("Waiting for spot request (%s) to become active...", *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.DescribeSpotInstanceRequests(&ec2.DescribeSpotInstanceRequestsInput{
|
|
||||||
SpotInstanceRequestIds: []*string{spotRequestId},
|
|
||||||
})
|
|
||||||
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
|
|
||||||
}
|
|
||||||
instanceId = *spotResp.SpotInstanceRequests[0].InstanceId
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set the instance ID so that the cleanup works properly
|
// Set the instance ID so that the cleanup works properly
|
||||||
s.instanceId = instanceId
|
s.instanceId = instanceId
|
||||||
|
@ -331,70 +190,6 @@ func (s *StepRunSourceInstance) Run(state multistep.StateBag) multistep.StepActi
|
||||||
|
|
||||||
instance := latestInstance.(*ec2.Instance)
|
instance := latestInstance.(*ec2.Instance)
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if createVolTagsAfterInstanceStarts {
|
|
||||||
volumeIds := make([]*string, 0)
|
|
||||||
for _, v := range instance.BlockDeviceMappings {
|
|
||||||
if ebs := v.Ebs; ebs != nil {
|
|
||||||
volumeIds = append(volumeIds, ebs.VolumeId)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(volumeIds) > 0 {
|
|
||||||
ui.Say("Adding tags to source EBS Volumes")
|
|
||||||
tags, err := ConvertToEC2Tags(s.VolumeTags, *ec2conn.Config.Region, s.SourceAMI, s.Ctx)
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
ReportTags(ui, tags)
|
|
||||||
|
|
||||||
_, err = ec2conn.CreateTags(&ec2.CreateTagsInput{
|
|
||||||
Resources: volumeIds,
|
|
||||||
Tags: tags,
|
|
||||||
})
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
return multistep.ActionContinue
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
if s.Debug {
|
if s.Debug {
|
||||||
if instance.PublicDnsName != nil && *instance.PublicDnsName != "" {
|
if instance.PublicDnsName != nil && *instance.PublicDnsName != "" {
|
||||||
ui.Message(fmt.Sprintf("Public DNS: %s", *instance.PublicDnsName))
|
ui.Message(fmt.Sprintf("Public DNS: %s", *instance.PublicDnsName))
|
||||||
|
@ -419,29 +214,6 @@ func (s *StepRunSourceInstance) Cleanup(state multistep.StateBag) {
|
||||||
ec2conn := state.Get("ec2").(*ec2.EC2)
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
||||||
ui := state.Get("ui").(packer.Ui)
|
ui := state.Get("ui").(packer.Ui)
|
||||||
|
|
||||||
// Cancel the spot request if it exists
|
|
||||||
if s.spotRequest != nil {
|
|
||||||
ui.Say("Cancelling the spot request...")
|
|
||||||
input := &ec2.CancelSpotInstanceRequestsInput{
|
|
||||||
SpotInstanceRequestIds: []*string{s.spotRequest.SpotInstanceRequestId},
|
|
||||||
}
|
|
||||||
if _, err := ec2conn.CancelSpotInstanceRequests(input); 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.SpotInstanceRequestId),
|
|
||||||
Target: "cancelled",
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err := WaitForState(&stateChange)
|
|
||||||
if err != nil {
|
|
||||||
ui.Error(err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// Terminate the source instance if it exists
|
// Terminate the source instance if it exists
|
||||||
if s.instanceId != "" {
|
if s.instanceId != "" {
|
||||||
ui.Say("Terminating the source AWS instance...")
|
ui.Say("Terminating the source AWS instance...")
|
||||||
|
|
|
@ -0,0 +1,374 @@
|
||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/base64"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/aws/aws-sdk-go/aws"
|
||||||
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
||||||
|
"github.com/aws/aws-sdk-go/service/ec2"
|
||||||
|
|
||||||
|
retry "github.com/hashicorp/packer/common"
|
||||||
|
"github.com/hashicorp/packer/packer"
|
||||||
|
"github.com/hashicorp/packer/template/interpolate"
|
||||||
|
"github.com/mitchellh/multistep"
|
||||||
|
)
|
||||||
|
|
||||||
|
type StepRunSpotInstance struct {
|
||||||
|
AssociatePublicIpAddress bool
|
||||||
|
AvailabilityZone string
|
||||||
|
BlockDevices BlockDevices
|
||||||
|
Debug bool
|
||||||
|
EbsOptimized bool
|
||||||
|
ExpectedRootDevice string
|
||||||
|
IamInstanceProfile string
|
||||||
|
InstanceInitiatedShutdownBehavior string
|
||||||
|
InstanceType string
|
||||||
|
SourceAMI string
|
||||||
|
SpotPrice string
|
||||||
|
SpotPriceProduct string
|
||||||
|
SubnetId string
|
||||||
|
Tags map[string]string
|
||||||
|
VolumeTags map[string]string
|
||||||
|
UserData string
|
||||||
|
UserDataFile string
|
||||||
|
Ctx interpolate.Context
|
||||||
|
|
||||||
|
instanceId string
|
||||||
|
spotRequest *ec2.SpotInstanceRequest
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *StepRunSpotInstance) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
||||||
|
var keyName string
|
||||||
|
if name, ok := state.GetOk("keyPair"); ok {
|
||||||
|
keyName = name.(string)
|
||||||
|
}
|
||||||
|
securityGroupIds := aws.StringSlice(state.Get("securityGroupIds").([]string))
|
||||||
|
ui := state.Get("ui").(packer.Ui)
|
||||||
|
|
||||||
|
userData := s.UserData
|
||||||
|
if s.UserDataFile != "" {
|
||||||
|
contents, err := ioutil.ReadFile(s.UserDataFile)
|
||||||
|
if err != nil {
|
||||||
|
state.Put("error", fmt.Errorf("Problem reading user data file: %s", err))
|
||||||
|
return multistep.ActionHalt
|
||||||
|
}
|
||||||
|
|
||||||
|
userData = string(contents)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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))
|
||||||
|
}
|
||||||
|
|
||||||
|
ui.Say("Launching a source AWS instance...")
|
||||||
|
image, ok := state.Get("source_image").(*ec2.Image)
|
||||||
|
if !ok {
|
||||||
|
state.Put("error", fmt.Errorf("source_image type assertion failed"))
|
||||||
|
return multistep.ActionHalt
|
||||||
|
}
|
||||||
|
s.SourceAMI = *image.ImageId
|
||||||
|
|
||||||
|
if s.ExpectedRootDevice != "" && *image.RootDeviceType != s.ExpectedRootDevice {
|
||||||
|
state.Put("error", fmt.Errorf(
|
||||||
|
"The provided source AMI has an invalid root device type.\n"+
|
||||||
|
"Expected '%s', got '%s'.",
|
||||||
|
s.ExpectedRootDevice, *image.RootDeviceType))
|
||||||
|
return multistep.ActionHalt
|
||||||
|
}
|
||||||
|
|
||||||
|
spotPrice := s.SpotPrice
|
||||||
|
availabilityZone := s.AvailabilityZone
|
||||||
|
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)
|
||||||
|
resp, err := ec2conn.DescribeSpotPriceHistory(&ec2.DescribeSpotPriceHistoryInput{
|
||||||
|
InstanceTypes: []*string{&s.InstanceType},
|
||||||
|
ProductDescriptions: []*string{&s.SpotPriceProduct},
|
||||||
|
AvailabilityZone: &s.AvailabilityZone,
|
||||||
|
StartTime: &startTime,
|
||||||
|
})
|
||||||
|
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
|
||||||
|
for _, history := range resp.SpotPriceHistory {
|
||||||
|
log.Printf("[INFO] Candidate spot price: %s", *history.SpotPrice)
|
||||||
|
current, err := strconv.ParseFloat(*history.SpotPrice, 64)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("[ERR] Error parsing spot price: %s", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if price == 0 || current < price {
|
||||||
|
price = current
|
||||||
|
if s.AvailabilityZone == "" {
|
||||||
|
availabilityZone = *history.AvailabilityZone
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if price == 0 {
|
||||||
|
err := fmt.Errorf("No candidate spot prices found!")
|
||||||
|
state.Put("error", err)
|
||||||
|
ui.Error(err.Error())
|
||||||
|
return multistep.ActionHalt
|
||||||
|
} 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
|
||||||
|
}
|
||||||
|
|
||||||
|
spotPrice = strconv.FormatFloat(price, 'f', -1, 64)
|
||||||
|
}
|
||||||
|
|
||||||
|
var instanceId string
|
||||||
|
|
||||||
|
ui.Say("Adding tags to source instance")
|
||||||
|
if _, exists := s.Tags["Name"]; !exists {
|
||||||
|
s.Tags["Name"] = "Packer Builder"
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
ReportTags(ui, ec2Tags)
|
||||||
|
|
||||||
|
ui.Message(fmt.Sprintf(
|
||||||
|
"Requesting spot instance '%s' for: %s",
|
||||||
|
s.InstanceType, spotPrice))
|
||||||
|
|
||||||
|
runOpts := &ec2.RequestSpotLaunchSpecification{
|
||||||
|
ImageId: &s.SourceAMI,
|
||||||
|
InstanceType: &s.InstanceType,
|
||||||
|
UserData: &userData,
|
||||||
|
IamInstanceProfile: &ec2.IamInstanceProfileSpecification{Name: &s.IamInstanceProfile},
|
||||||
|
Placement: &ec2.SpotPlacement{
|
||||||
|
AvailabilityZone: &availabilityZone,
|
||||||
|
},
|
||||||
|
BlockDeviceMappings: s.BlockDevices.BuildLaunchDevices(),
|
||||||
|
EbsOptimized: &s.EbsOptimized,
|
||||||
|
}
|
||||||
|
|
||||||
|
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),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
runOpts.SubnetId = &s.SubnetId
|
||||||
|
runOpts.SecurityGroupIds = securityGroupIds
|
||||||
|
}
|
||||||
|
|
||||||
|
if keyName != "" {
|
||||||
|
runOpts.KeyName = &keyName
|
||||||
|
}
|
||||||
|
|
||||||
|
runSpotResp, err := ec2conn.RequestSpotInstances(&ec2.RequestSpotInstancesInput{
|
||||||
|
SpotPrice: &spotPrice,
|
||||||
|
LaunchSpecification: 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
|
||||||
|
}
|
||||||
|
|
||||||
|
s.spotRequest = runSpotResp.SpotInstanceRequests[0]
|
||||||
|
|
||||||
|
spotRequestId := s.spotRequest.SpotInstanceRequestId
|
||||||
|
ui.Message(fmt.Sprintf("Waiting for spot request (%s) to become active...", *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.DescribeSpotInstanceRequests(&ec2.DescribeSpotInstanceRequestsInput{
|
||||||
|
SpotInstanceRequestIds: []*string{spotRequestId},
|
||||||
|
})
|
||||||
|
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
|
||||||
|
}
|
||||||
|
instanceId = *spotResp.SpotInstanceRequests[0].InstanceId
|
||||||
|
|
||||||
|
// Set the instance ID so that the cleanup works properly
|
||||||
|
s.instanceId = instanceId
|
||||||
|
|
||||||
|
ui.Message(fmt.Sprintf("Instance ID: %s", instanceId))
|
||||||
|
ui.Say(fmt.Sprintf("Waiting for instance (%v) to become ready...", instanceId))
|
||||||
|
stateChangeSpot := StateChangeConf{
|
||||||
|
Pending: []string{"pending"},
|
||||||
|
Target: "running",
|
||||||
|
Refresh: InstanceStateRefreshFunc(ec2conn, instanceId),
|
||||||
|
StepState: state,
|
||||||
|
}
|
||||||
|
latestInstance, err := WaitForState(&stateChangeSpot)
|
||||||
|
if err != nil {
|
||||||
|
err := fmt.Errorf("Error waiting for instance (%s) to become ready: %s", instanceId, err)
|
||||||
|
state.Put("error", err)
|
||||||
|
ui.Error(err.Error())
|
||||||
|
return multistep.ActionHalt
|
||||||
|
}
|
||||||
|
|
||||||
|
instance := latestInstance.(*ec2.Instance)
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
|
||||||
|
volumeIds := make([]*string, 0)
|
||||||
|
for _, v := range instance.BlockDeviceMappings {
|
||||||
|
if ebs := v.Ebs; ebs != nil {
|
||||||
|
volumeIds = append(volumeIds, ebs.VolumeId)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(volumeIds) > 0 {
|
||||||
|
ui.Say("Adding tags to source EBS Volumes")
|
||||||
|
tags, err := ConvertToEC2Tags(s.VolumeTags, *ec2conn.Config.Region, s.SourceAMI, s.Ctx)
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
ReportTags(ui, tags)
|
||||||
|
|
||||||
|
_, err = ec2conn.CreateTags(&ec2.CreateTagsInput{
|
||||||
|
Resources: volumeIds,
|
||||||
|
Tags: tags,
|
||||||
|
})
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
return multistep.ActionContinue
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if s.Debug {
|
||||||
|
if instance.PublicDnsName != nil && *instance.PublicDnsName != "" {
|
||||||
|
ui.Message(fmt.Sprintf("Public DNS: %s", *instance.PublicDnsName))
|
||||||
|
}
|
||||||
|
|
||||||
|
if instance.PublicIpAddress != nil && *instance.PublicIpAddress != "" {
|
||||||
|
ui.Message(fmt.Sprintf("Public IP: %s", *instance.PublicIpAddress))
|
||||||
|
}
|
||||||
|
|
||||||
|
if instance.PrivateIpAddress != nil && *instance.PrivateIpAddress != "" {
|
||||||
|
ui.Message(fmt.Sprintf("Private IP: %s", *instance.PrivateIpAddress))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
state.Put("instance", instance)
|
||||||
|
|
||||||
|
return multistep.ActionContinue
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *StepRunSpotInstance) Cleanup(state multistep.StateBag) {
|
||||||
|
|
||||||
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
||||||
|
ui := state.Get("ui").(packer.Ui)
|
||||||
|
|
||||||
|
// Cancel the spot request if it exists
|
||||||
|
if s.spotRequest != nil {
|
||||||
|
ui.Say("Cancelling the spot request...")
|
||||||
|
input := &ec2.CancelSpotInstanceRequestsInput{
|
||||||
|
SpotInstanceRequestIds: []*string{s.spotRequest.SpotInstanceRequestId},
|
||||||
|
}
|
||||||
|
if _, err := ec2conn.CancelSpotInstanceRequests(input); 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.SpotInstanceRequestId),
|
||||||
|
Target: "cancelled",
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := WaitForState(&stateChange)
|
||||||
|
if err != nil {
|
||||||
|
ui.Error(err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Terminate the source instance if it exists
|
||||||
|
if s.instanceId != "" {
|
||||||
|
ui.Say("Terminating the source AWS instance...")
|
||||||
|
if _, err := ec2conn.TerminateInstances(&ec2.TerminateInstancesInput{InstanceIds: []*string{&s.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.instanceId),
|
||||||
|
Target: "terminated",
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := WaitForState(&stateChange)
|
||||||
|
if err != nil {
|
||||||
|
ui.Error(err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -108,6 +108,50 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
|
||||||
state.Put("hook", hook)
|
state.Put("hook", hook)
|
||||||
state.Put("ui", ui)
|
state.Put("ui", ui)
|
||||||
|
|
||||||
|
var instanceStep multistep.Step
|
||||||
|
|
||||||
|
if b.config.SpotPrice == "" || b.config.SpotPrice == "0" {
|
||||||
|
instanceStep = &awscommon.StepRunSpotInstance{
|
||||||
|
Debug: b.config.PackerDebug,
|
||||||
|
ExpectedRootDevice: "ebs",
|
||||||
|
SpotPrice: b.config.SpotPrice,
|
||||||
|
SpotPriceProduct: b.config.SpotPriceAutoProduct,
|
||||||
|
InstanceType: b.config.InstanceType,
|
||||||
|
UserData: b.config.UserData,
|
||||||
|
UserDataFile: b.config.UserDataFile,
|
||||||
|
SourceAMI: b.config.SourceAmi,
|
||||||
|
IamInstanceProfile: b.config.IamInstanceProfile,
|
||||||
|
SubnetId: b.config.SubnetId,
|
||||||
|
AssociatePublicIpAddress: b.config.AssociatePublicIpAddress,
|
||||||
|
EbsOptimized: b.config.EbsOptimized,
|
||||||
|
AvailabilityZone: b.config.AvailabilityZone,
|
||||||
|
BlockDevices: b.config.BlockDevices,
|
||||||
|
Tags: b.config.RunTags,
|
||||||
|
VolumeTags: b.config.VolumeRunTags,
|
||||||
|
Ctx: b.config.ctx,
|
||||||
|
InstanceInitiatedShutdownBehavior: b.config.InstanceInitiatedShutdownBehavior,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
instanceStep = &awscommon.StepRunSourceInstance{
|
||||||
|
Debug: b.config.PackerDebug,
|
||||||
|
ExpectedRootDevice: "ebs",
|
||||||
|
InstanceType: b.config.InstanceType,
|
||||||
|
UserData: b.config.UserData,
|
||||||
|
UserDataFile: b.config.UserDataFile,
|
||||||
|
SourceAMI: b.config.SourceAmi,
|
||||||
|
IamInstanceProfile: b.config.IamInstanceProfile,
|
||||||
|
SubnetId: b.config.SubnetId,
|
||||||
|
AssociatePublicIpAddress: b.config.AssociatePublicIpAddress,
|
||||||
|
EbsOptimized: b.config.EbsOptimized,
|
||||||
|
AvailabilityZone: b.config.AvailabilityZone,
|
||||||
|
BlockDevices: b.config.BlockDevices,
|
||||||
|
Tags: b.config.RunTags,
|
||||||
|
VolumeTags: b.config.VolumeRunTags,
|
||||||
|
Ctx: b.config.ctx,
|
||||||
|
InstanceInitiatedShutdownBehavior: b.config.InstanceInitiatedShutdownBehavior,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Build the steps
|
// Build the steps
|
||||||
steps := []multistep.Step{
|
steps := []multistep.Step{
|
||||||
&awscommon.StepPreValidate{
|
&awscommon.StepPreValidate{
|
||||||
|
@ -136,26 +180,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
|
||||||
&stepCleanupVolumes{
|
&stepCleanupVolumes{
|
||||||
BlockDevices: b.config.BlockDevices,
|
BlockDevices: b.config.BlockDevices,
|
||||||
},
|
},
|
||||||
&awscommon.StepRunSourceInstance{
|
instanceStep,
|
||||||
Debug: b.config.PackerDebug,
|
|
||||||
ExpectedRootDevice: "ebs",
|
|
||||||
SpotPrice: b.config.SpotPrice,
|
|
||||||
SpotPriceProduct: b.config.SpotPriceAutoProduct,
|
|
||||||
InstanceType: b.config.InstanceType,
|
|
||||||
UserData: b.config.UserData,
|
|
||||||
UserDataFile: b.config.UserDataFile,
|
|
||||||
SourceAMI: b.config.SourceAmi,
|
|
||||||
IamInstanceProfile: b.config.IamInstanceProfile,
|
|
||||||
SubnetId: b.config.SubnetId,
|
|
||||||
AssociatePublicIpAddress: b.config.AssociatePublicIpAddress,
|
|
||||||
EbsOptimized: b.config.EbsOptimized,
|
|
||||||
AvailabilityZone: b.config.AvailabilityZone,
|
|
||||||
BlockDevices: b.config.BlockDevices,
|
|
||||||
Tags: b.config.RunTags,
|
|
||||||
VolumeTags: b.config.VolumeRunTags,
|
|
||||||
Ctx: b.config.ctx,
|
|
||||||
InstanceInitiatedShutdownBehavior: b.config.InstanceInitiatedShutdownBehavior,
|
|
||||||
},
|
|
||||||
&awscommon.StepGetPassword{
|
&awscommon.StepGetPassword{
|
||||||
Debug: b.config.PackerDebug,
|
Debug: b.config.PackerDebug,
|
||||||
Comm: &b.config.RunConfig.Comm,
|
Comm: &b.config.RunConfig.Comm,
|
||||||
|
|
|
@ -122,6 +122,50 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
|
||||||
state.Put("hook", hook)
|
state.Put("hook", hook)
|
||||||
state.Put("ui", ui)
|
state.Put("ui", ui)
|
||||||
|
|
||||||
|
var instanceStep multistep.Step
|
||||||
|
|
||||||
|
if b.config.SpotPrice == "" || b.config.SpotPrice == "0" {
|
||||||
|
instanceStep = &awscommon.StepRunSpotInstance{
|
||||||
|
Debug: b.config.PackerDebug,
|
||||||
|
ExpectedRootDevice: "ebs",
|
||||||
|
SpotPrice: b.config.SpotPrice,
|
||||||
|
SpotPriceProduct: b.config.SpotPriceAutoProduct,
|
||||||
|
InstanceType: b.config.InstanceType,
|
||||||
|
UserData: b.config.UserData,
|
||||||
|
UserDataFile: b.config.UserDataFile,
|
||||||
|
SourceAMI: b.config.SourceAmi,
|
||||||
|
IamInstanceProfile: b.config.IamInstanceProfile,
|
||||||
|
SubnetId: b.config.SubnetId,
|
||||||
|
AssociatePublicIpAddress: b.config.AssociatePublicIpAddress,
|
||||||
|
EbsOptimized: b.config.EbsOptimized,
|
||||||
|
AvailabilityZone: b.config.AvailabilityZone,
|
||||||
|
BlockDevices: b.config.BlockDevices,
|
||||||
|
Tags: b.config.RunTags,
|
||||||
|
VolumeTags: b.config.VolumeRunTags,
|
||||||
|
Ctx: b.config.ctx,
|
||||||
|
InstanceInitiatedShutdownBehavior: b.config.InstanceInitiatedShutdownBehavior,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
instanceStep = &awscommon.StepRunSourceInstance{
|
||||||
|
Debug: b.config.PackerDebug,
|
||||||
|
ExpectedRootDevice: "ebs",
|
||||||
|
InstanceType: b.config.InstanceType,
|
||||||
|
UserData: b.config.UserData,
|
||||||
|
UserDataFile: b.config.UserDataFile,
|
||||||
|
SourceAMI: b.config.SourceAmi,
|
||||||
|
IamInstanceProfile: b.config.IamInstanceProfile,
|
||||||
|
SubnetId: b.config.SubnetId,
|
||||||
|
AssociatePublicIpAddress: b.config.AssociatePublicIpAddress,
|
||||||
|
EbsOptimized: b.config.EbsOptimized,
|
||||||
|
AvailabilityZone: b.config.AvailabilityZone,
|
||||||
|
BlockDevices: b.config.BlockDevices,
|
||||||
|
Tags: b.config.RunTags,
|
||||||
|
VolumeTags: b.config.VolumeRunTags,
|
||||||
|
Ctx: b.config.ctx,
|
||||||
|
InstanceInitiatedShutdownBehavior: b.config.InstanceInitiatedShutdownBehavior,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Build the steps
|
// Build the steps
|
||||||
steps := []multistep.Step{
|
steps := []multistep.Step{
|
||||||
&awscommon.StepPreValidate{
|
&awscommon.StepPreValidate{
|
||||||
|
@ -147,24 +191,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
|
||||||
CommConfig: &b.config.RunConfig.Comm,
|
CommConfig: &b.config.RunConfig.Comm,
|
||||||
VpcId: b.config.VpcId,
|
VpcId: b.config.VpcId,
|
||||||
},
|
},
|
||||||
&awscommon.StepRunSourceInstance{
|
instanceStep,
|
||||||
Debug: b.config.PackerDebug,
|
|
||||||
ExpectedRootDevice: "ebs",
|
|
||||||
SpotPrice: b.config.SpotPrice,
|
|
||||||
SpotPriceProduct: b.config.SpotPriceAutoProduct,
|
|
||||||
InstanceType: b.config.InstanceType,
|
|
||||||
UserData: b.config.UserData,
|
|
||||||
UserDataFile: b.config.UserDataFile,
|
|
||||||
SourceAMI: b.config.SourceAmi,
|
|
||||||
IamInstanceProfile: b.config.IamInstanceProfile,
|
|
||||||
SubnetId: b.config.SubnetId,
|
|
||||||
AssociatePublicIpAddress: b.config.AssociatePublicIpAddress,
|
|
||||||
EbsOptimized: b.config.EbsOptimized,
|
|
||||||
AvailabilityZone: b.config.AvailabilityZone,
|
|
||||||
BlockDevices: b.config.BlockDevices,
|
|
||||||
Tags: b.config.RunTags,
|
|
||||||
InstanceInitiatedShutdownBehavior: b.config.InstanceInitiatedShutdownBehavior,
|
|
||||||
},
|
|
||||||
&awscommon.StepTagEBSVolumes{
|
&awscommon.StepTagEBSVolumes{
|
||||||
VolumeRunTags: b.config.VolumeRunTags,
|
VolumeRunTags: b.config.VolumeRunTags,
|
||||||
Ctx: b.config.ctx,
|
Ctx: b.config.ctx,
|
||||||
|
|
|
@ -101,6 +101,46 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
|
||||||
state.Put("hook", hook)
|
state.Put("hook", hook)
|
||||||
state.Put("ui", ui)
|
state.Put("ui", ui)
|
||||||
|
|
||||||
|
var instanceStep multistep.Step
|
||||||
|
|
||||||
|
if b.config.SpotPrice == "" || b.config.SpotPrice == "0" {
|
||||||
|
instanceStep = &awscommon.StepRunSpotInstance{
|
||||||
|
Debug: b.config.PackerDebug,
|
||||||
|
ExpectedRootDevice: "ebs",
|
||||||
|
SpotPrice: b.config.SpotPrice,
|
||||||
|
SpotPriceProduct: b.config.SpotPriceAutoProduct,
|
||||||
|
InstanceType: b.config.InstanceType,
|
||||||
|
UserData: b.config.UserData,
|
||||||
|
UserDataFile: b.config.UserDataFile,
|
||||||
|
SourceAMI: b.config.SourceAmi,
|
||||||
|
IamInstanceProfile: b.config.IamInstanceProfile,
|
||||||
|
SubnetId: b.config.SubnetId,
|
||||||
|
AssociatePublicIpAddress: b.config.AssociatePublicIpAddress,
|
||||||
|
EbsOptimized: b.config.EbsOptimized,
|
||||||
|
AvailabilityZone: b.config.AvailabilityZone,
|
||||||
|
Tags: b.config.RunTags,
|
||||||
|
Ctx: b.config.ctx,
|
||||||
|
InstanceInitiatedShutdownBehavior: b.config.InstanceInitiatedShutdownBehavior,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
instanceStep = &awscommon.StepRunSourceInstance{
|
||||||
|
Debug: b.config.PackerDebug,
|
||||||
|
ExpectedRootDevice: "ebs",
|
||||||
|
InstanceType: b.config.InstanceType,
|
||||||
|
UserData: b.config.UserData,
|
||||||
|
UserDataFile: b.config.UserDataFile,
|
||||||
|
SourceAMI: b.config.SourceAmi,
|
||||||
|
IamInstanceProfile: b.config.IamInstanceProfile,
|
||||||
|
SubnetId: b.config.SubnetId,
|
||||||
|
AssociatePublicIpAddress: b.config.AssociatePublicIpAddress,
|
||||||
|
EbsOptimized: b.config.EbsOptimized,
|
||||||
|
AvailabilityZone: b.config.AvailabilityZone,
|
||||||
|
Tags: b.config.RunTags,
|
||||||
|
Ctx: b.config.ctx,
|
||||||
|
InstanceInitiatedShutdownBehavior: b.config.InstanceInitiatedShutdownBehavior,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Build the steps
|
// Build the steps
|
||||||
steps := []multistep.Step{
|
steps := []multistep.Step{
|
||||||
&awscommon.StepSourceAMIInfo{
|
&awscommon.StepSourceAMIInfo{
|
||||||
|
@ -122,25 +162,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
|
||||||
CommConfig: &b.config.RunConfig.Comm,
|
CommConfig: &b.config.RunConfig.Comm,
|
||||||
VpcId: b.config.VpcId,
|
VpcId: b.config.VpcId,
|
||||||
},
|
},
|
||||||
&awscommon.StepRunSourceInstance{
|
instanceStep,
|
||||||
Debug: b.config.PackerDebug,
|
|
||||||
ExpectedRootDevice: "ebs",
|
|
||||||
SpotPrice: b.config.SpotPrice,
|
|
||||||
SpotPriceProduct: b.config.SpotPriceAutoProduct,
|
|
||||||
InstanceType: b.config.InstanceType,
|
|
||||||
UserData: b.config.UserData,
|
|
||||||
UserDataFile: b.config.UserDataFile,
|
|
||||||
SourceAMI: b.config.SourceAmi,
|
|
||||||
IamInstanceProfile: b.config.IamInstanceProfile,
|
|
||||||
SubnetId: b.config.SubnetId,
|
|
||||||
AssociatePublicIpAddress: b.config.AssociatePublicIpAddress,
|
|
||||||
EbsOptimized: b.config.EbsOptimized,
|
|
||||||
AvailabilityZone: b.config.AvailabilityZone,
|
|
||||||
BlockDevices: b.config.launchBlockDevices,
|
|
||||||
Tags: b.config.RunTags,
|
|
||||||
Ctx: b.config.ctx,
|
|
||||||
InstanceInitiatedShutdownBehavior: b.config.InstanceInitiatedShutdownBehavior,
|
|
||||||
},
|
|
||||||
&stepTagEBSVolumes{
|
&stepTagEBSVolumes{
|
||||||
VolumeMapping: b.config.VolumeMappings,
|
VolumeMapping: b.config.VolumeMappings,
|
||||||
Ctx: b.config.ctx,
|
Ctx: b.config.ctx,
|
||||||
|
|
|
@ -193,6 +193,48 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
|
||||||
state.Put("hook", hook)
|
state.Put("hook", hook)
|
||||||
state.Put("ui", ui)
|
state.Put("ui", ui)
|
||||||
|
|
||||||
|
var instanceStep multistep.Step
|
||||||
|
|
||||||
|
if b.config.SpotPrice == "" || b.config.SpotPrice == "0" {
|
||||||
|
instanceStep = &awscommon.StepRunSpotInstance{
|
||||||
|
Debug: b.config.PackerDebug,
|
||||||
|
ExpectedRootDevice: "ebs",
|
||||||
|
SpotPrice: b.config.SpotPrice,
|
||||||
|
SpotPriceProduct: b.config.SpotPriceAutoProduct,
|
||||||
|
InstanceType: b.config.InstanceType,
|
||||||
|
UserData: b.config.UserData,
|
||||||
|
UserDataFile: b.config.UserDataFile,
|
||||||
|
SourceAMI: b.config.SourceAmi,
|
||||||
|
IamInstanceProfile: b.config.IamInstanceProfile,
|
||||||
|
SubnetId: b.config.SubnetId,
|
||||||
|
AssociatePublicIpAddress: b.config.AssociatePublicIpAddress,
|
||||||
|
EbsOptimized: b.config.EbsOptimized,
|
||||||
|
AvailabilityZone: b.config.AvailabilityZone,
|
||||||
|
BlockDevices: b.config.BlockDevices,
|
||||||
|
Tags: b.config.RunTags,
|
||||||
|
Ctx: b.config.ctx,
|
||||||
|
InstanceInitiatedShutdownBehavior: b.config.InstanceInitiatedShutdownBehavior,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
instanceStep = &awscommon.StepRunSourceInstance{
|
||||||
|
Debug: b.config.PackerDebug,
|
||||||
|
ExpectedRootDevice: "ebs",
|
||||||
|
InstanceType: b.config.InstanceType,
|
||||||
|
UserData: b.config.UserData,
|
||||||
|
UserDataFile: b.config.UserDataFile,
|
||||||
|
SourceAMI: b.config.SourceAmi,
|
||||||
|
IamInstanceProfile: b.config.IamInstanceProfile,
|
||||||
|
SubnetId: b.config.SubnetId,
|
||||||
|
AssociatePublicIpAddress: b.config.AssociatePublicIpAddress,
|
||||||
|
EbsOptimized: b.config.EbsOptimized,
|
||||||
|
AvailabilityZone: b.config.AvailabilityZone,
|
||||||
|
BlockDevices: b.config.BlockDevices,
|
||||||
|
Tags: b.config.RunTags,
|
||||||
|
Ctx: b.config.ctx,
|
||||||
|
InstanceInitiatedShutdownBehavior: b.config.InstanceInitiatedShutdownBehavior,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Build the steps
|
// Build the steps
|
||||||
steps := []multistep.Step{
|
steps := []multistep.Step{
|
||||||
&awscommon.StepPreValidate{
|
&awscommon.StepPreValidate{
|
||||||
|
@ -218,23 +260,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
|
||||||
SecurityGroupIds: b.config.SecurityGroupIds,
|
SecurityGroupIds: b.config.SecurityGroupIds,
|
||||||
VpcId: b.config.VpcId,
|
VpcId: b.config.VpcId,
|
||||||
},
|
},
|
||||||
&awscommon.StepRunSourceInstance{
|
instanceStep,
|
||||||
Debug: b.config.PackerDebug,
|
|
||||||
SpotPrice: b.config.SpotPrice,
|
|
||||||
SpotPriceProduct: b.config.SpotPriceAutoProduct,
|
|
||||||
InstanceType: b.config.InstanceType,
|
|
||||||
IamInstanceProfile: b.config.IamInstanceProfile,
|
|
||||||
UserData: b.config.UserData,
|
|
||||||
UserDataFile: b.config.UserDataFile,
|
|
||||||
SourceAMI: b.config.SourceAmi,
|
|
||||||
SubnetId: b.config.SubnetId,
|
|
||||||
AssociatePublicIpAddress: b.config.AssociatePublicIpAddress,
|
|
||||||
EbsOptimized: b.config.EbsOptimized,
|
|
||||||
AvailabilityZone: b.config.AvailabilityZone,
|
|
||||||
BlockDevices: b.config.BlockDevices,
|
|
||||||
Tags: b.config.RunTags,
|
|
||||||
Ctx: b.config.ctx,
|
|
||||||
},
|
|
||||||
&awscommon.StepGetPassword{
|
&awscommon.StepGetPassword{
|
||||||
Debug: b.config.PackerDebug,
|
Debug: b.config.PackerDebug,
|
||||||
Comm: &b.config.RunConfig.Comm,
|
Comm: &b.config.RunConfig.Comm,
|
||||||
|
|
Loading…
Reference in New Issue