2014-06-04 17:58:11 -04:00
|
|
|
package common
|
2013-07-29 20:37:22 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
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
|
|
|
|
}
|
2013-07-29 20:37:22 -04:00
|
|
|
|
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
|
|
|
|
|
|
|
ui.Say("Inspecting the source AMI...")
|
2015-08-17 20:44:01 -04:00
|
|
|
imageResp, err := ec2conn.DescribeImages(&ec2.DescribeImagesInput{ImageIds: []*string{&s.SourceAmi}})
|
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 {
|
2014-06-04 17:58:11 -04:00
|
|
|
err := fmt.Errorf("Source AMI '%s' was not found!", 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
|
|
|
|
}
|
|
|
|
|
2015-04-05 17:58:48 -04:00
|
|
|
image := imageResp.Images[0]
|
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) {}
|