2014-06-04 17:58:11 -04:00
|
|
|
package common
|
2013-07-29 20:37:22 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-08-20 19:08:45 -04:00
|
|
|
"log"
|
|
|
|
"sort"
|
|
|
|
"time"
|
2014-06-04 17:58:11 -04:00
|
|
|
|
2015-06-03 17:13:52 -04:00
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2013-07-29 20:37:22 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
// StepSourceAMIInfo extracts critical information from the source AMI
|
|
|
|
// that is used throughout the AMI creation process.
|
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// source_image *ec2.Image - the source AMI info
|
2014-06-04 17:58:11 -04:00
|
|
|
type StepSourceAMIInfo struct {
|
|
|
|
SourceAmi string
|
|
|
|
EnhancedNetworking bool
|
2016-08-20 19:34:22 -04:00
|
|
|
AmiFilters AmiFilterOptions
|
2016-08-20 14:58:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build a slice of AMI filter options from the filters provided.
|
|
|
|
func buildAmiFilters(input map[*string]*string) []*ec2.Filter {
|
|
|
|
var filters []*ec2.Filter
|
|
|
|
for k, v := range input {
|
|
|
|
filters = append(filters, &ec2.Filter{
|
|
|
|
Name: k,
|
|
|
|
Values: []*string{v},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return filters
|
2014-06-04 17:58:11 -04:00
|
|
|
}
|
2013-07-29 20:37:22 -04:00
|
|
|
|
2016-08-20 19:08:45 -04:00
|
|
|
type imageSort []*ec2.Image
|
|
|
|
|
|
|
|
func (a imageSort) Len() int { return len(a) }
|
|
|
|
func (a imageSort) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
|
|
func (a imageSort) Less(i, j int) bool {
|
|
|
|
itime, _ := time.Parse(time.RFC3339, *a[i].CreationDate)
|
|
|
|
jtime, _ := time.Parse(time.RFC3339, *a[j].CreationDate)
|
|
|
|
return itime.Unix() < jtime.Unix()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the most recent AMI out of a slice of images.
|
|
|
|
func mostRecentAmi(images []*ec2.Image) *ec2.Image {
|
|
|
|
sortedImages := images
|
|
|
|
sort.Sort(imageSort(sortedImages))
|
|
|
|
return sortedImages[len(sortedImages)-1]
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
func (s *StepSourceAMIInfo) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-07-29 20:37:22 -04:00
|
|
|
|
2016-08-20 14:58:36 -04:00
|
|
|
params := &ec2.DescribeImagesInput{}
|
2016-08-20 19:08:45 -04:00
|
|
|
|
|
|
|
if s.SourceAmi != "" {
|
|
|
|
params.ImageIds = []*string{&s.SourceAmi}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have filters to apply
|
|
|
|
if len(s.AmiFilters.Filters) > 0 {
|
|
|
|
params.Filters = buildAmiFilters(s.AmiFilters.Filters)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Using AMI Filters %v", params)
|
2016-08-20 14:58:36 -04:00
|
|
|
imageResp, err := ec2conn.DescribeImages(params)
|
2013-07-29 20:37:22 -04:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error querying AMI: %s", err)
|
2013-08-31 15:58:55 -04:00
|
|
|
state.Put("error", err)
|
2013-07-29 20:37:22 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(imageResp.Images) == 0 {
|
2016-08-20 19:08:45 -04:00
|
|
|
err := fmt.Errorf("No AMI was found matching filters: %v", params)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(imageResp.Images) > 1 && s.AmiFilters.MostRecent == false {
|
|
|
|
err := fmt.Errorf("Your query returned more than one result. Please try a more specific search, or set most_recent to true.")
|
2013-08-31 15:58:55 -04:00
|
|
|
state.Put("error", err)
|
2013-07-29 20:37:22 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2016-08-20 19:08:45 -04:00
|
|
|
var image *ec2.Image
|
|
|
|
if s.AmiFilters.MostRecent {
|
|
|
|
image = mostRecentAmi(imageResp.Images)
|
|
|
|
} else {
|
|
|
|
image = imageResp.Images[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf(fmt.Sprintf("Got Image %v", image))
|
2013-07-29 20:37:22 -04:00
|
|
|
|
2014-06-04 17:58:11 -04:00
|
|
|
// Enhanced Networking (SriovNetSupport) can only be enabled on HVM AMIs.
|
|
|
|
// See http://goo.gl/icuXh5
|
2015-04-05 17:58:48 -04:00
|
|
|
if s.EnhancedNetworking && *image.VirtualizationType != "hvm" {
|
2014-06-04 17:58:11 -04:00
|
|
|
err := fmt.Errorf("Cannot enable enhanced networking, source AMI '%s' is not HVM", s.SourceAmi)
|
2013-08-31 15:58:55 -04:00
|
|
|
state.Put("error", err)
|
2013-07-29 20:37:22 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
state.Put("source_image", image)
|
2013-07-29 20:37:22 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
func (s *StepSourceAMIInfo) Cleanup(multistep.StateBag) {}
|