github.com/rothwerx/packer@v0.9.0/builder/amazon/common/step_source_ami_info.go (about)

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/aws/aws-sdk-go/service/ec2"
     7  	"github.com/mitchellh/multistep"
     8  	"github.com/mitchellh/packer/packer"
     9  )
    10  
    11  // StepSourceAMIInfo extracts critical information from the source AMI
    12  // that is used throughout the AMI creation process.
    13  //
    14  // Produces:
    15  //   source_image *ec2.Image - the source AMI info
    16  type StepSourceAMIInfo struct {
    17  	SourceAmi          string
    18  	EnhancedNetworking bool
    19  }
    20  
    21  func (s *StepSourceAMIInfo) Run(state multistep.StateBag) multistep.StepAction {
    22  	ec2conn := state.Get("ec2").(*ec2.EC2)
    23  	ui := state.Get("ui").(packer.Ui)
    24  
    25  	ui.Say("Inspecting the source AMI...")
    26  	imageResp, err := ec2conn.DescribeImages(&ec2.DescribeImagesInput{ImageIds: []*string{&s.SourceAmi}})
    27  	if err != nil {
    28  		err := fmt.Errorf("Error querying AMI: %s", err)
    29  		state.Put("error", err)
    30  		ui.Error(err.Error())
    31  		return multistep.ActionHalt
    32  	}
    33  
    34  	if len(imageResp.Images) == 0 {
    35  		err := fmt.Errorf("Source AMI '%s' was not found!", s.SourceAmi)
    36  		state.Put("error", err)
    37  		ui.Error(err.Error())
    38  		return multistep.ActionHalt
    39  	}
    40  
    41  	image := imageResp.Images[0]
    42  
    43  	// Enhanced Networking (SriovNetSupport) can only be enabled on HVM AMIs.
    44  	// See http://goo.gl/icuXh5
    45  	if s.EnhancedNetworking && *image.VirtualizationType != "hvm" {
    46  		err := fmt.Errorf("Cannot enable enhanced networking, source AMI '%s' is not HVM", s.SourceAmi)
    47  		state.Put("error", err)
    48  		ui.Error(err.Error())
    49  		return multistep.ActionHalt
    50  	}
    51  
    52  	state.Put("source_image", image)
    53  	return multistep.ActionContinue
    54  }
    55  
    56  func (s *StepSourceAMIInfo) Cleanup(multistep.StateBag) {}