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

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/aws/aws-sdk-go/aws"
     7  	"github.com/aws/aws-sdk-go/aws/session"
     8  	"github.com/aws/aws-sdk-go/service/ec2"
     9  	"github.com/mitchellh/multistep"
    10  	"github.com/mitchellh/packer/packer"
    11  )
    12  
    13  type StepModifyAMIAttributes struct {
    14  	Users        []string
    15  	Groups       []string
    16  	ProductCodes []string
    17  	Description  string
    18  }
    19  
    20  func (s *StepModifyAMIAttributes) Run(state multistep.StateBag) multistep.StepAction {
    21  	ec2conn := state.Get("ec2").(*ec2.EC2)
    22  	ui := state.Get("ui").(packer.Ui)
    23  	amis := state.Get("amis").(map[string]string)
    24  
    25  	// Determine if there is any work to do.
    26  	valid := false
    27  	valid = valid || s.Description != ""
    28  	valid = valid || (s.Users != nil && len(s.Users) > 0)
    29  	valid = valid || (s.Groups != nil && len(s.Groups) > 0)
    30  	valid = valid || (s.ProductCodes != nil && len(s.ProductCodes) > 0)
    31  
    32  	if !valid {
    33  		return multistep.ActionContinue
    34  	}
    35  
    36  	// Construct the modify image attribute requests we're going to make.
    37  	// We need to make each separately since the EC2 API only allows changing
    38  	// one type at a kind currently.
    39  	options := make(map[string]*ec2.ModifyImageAttributeInput)
    40  	if s.Description != "" {
    41  		options["description"] = &ec2.ModifyImageAttributeInput{
    42  			Description: &ec2.AttributeValue{Value: &s.Description},
    43  		}
    44  	}
    45  
    46  	if len(s.Groups) > 0 {
    47  		groups := make([]*string, len(s.Groups))
    48  		adds := make([]*ec2.LaunchPermission, len(s.Groups))
    49  		addGroups := &ec2.ModifyImageAttributeInput{
    50  			LaunchPermission: &ec2.LaunchPermissionModifications{},
    51  		}
    52  
    53  		for i, g := range s.Groups {
    54  			groups[i] = aws.String(g)
    55  			adds[i] = &ec2.LaunchPermission{
    56  				Group: aws.String(g),
    57  			}
    58  		}
    59  		addGroups.UserGroups = groups
    60  		addGroups.LaunchPermission.Add = adds
    61  
    62  		options["groups"] = addGroups
    63  	}
    64  
    65  	if len(s.Users) > 0 {
    66  		users := make([]*string, len(s.Users))
    67  		adds := make([]*ec2.LaunchPermission, len(s.Users))
    68  		for i, u := range s.Users {
    69  			users[i] = aws.String(u)
    70  			adds[i] = &ec2.LaunchPermission{UserId: aws.String(u)}
    71  		}
    72  		options["users"] = &ec2.ModifyImageAttributeInput{
    73  			UserIds: users,
    74  			LaunchPermission: &ec2.LaunchPermissionModifications{
    75  				Add: adds,
    76  			},
    77  		}
    78  	}
    79  
    80  	if len(s.ProductCodes) > 0 {
    81  		codes := make([]*string, len(s.ProductCodes))
    82  		for i, c := range s.ProductCodes {
    83  			codes[i] = &c
    84  		}
    85  		options["product codes"] = &ec2.ModifyImageAttributeInput{
    86  			ProductCodes: codes,
    87  		}
    88  	}
    89  
    90  	for region, ami := range amis {
    91  		ui.Say(fmt.Sprintf("Modifying attributes on AMI (%s)...", ami))
    92  		awsConfig := aws.Config{
    93  			Credentials: ec2conn.Config.Credentials,
    94  			Region:      aws.String(region),
    95  		}
    96  		session := session.New(&awsConfig)
    97  		regionconn := ec2.New(session)
    98  		for name, input := range options {
    99  			ui.Message(fmt.Sprintf("Modifying: %s", name))
   100  			input.ImageId = &ami
   101  			_, err := regionconn.ModifyImageAttribute(input)
   102  			if err != nil {
   103  				err := fmt.Errorf("Error modify AMI attributes: %s", err)
   104  				state.Put("error", err)
   105  				ui.Error(err.Error())
   106  				return multistep.ActionHalt
   107  			}
   108  		}
   109  	}
   110  
   111  	return multistep.ActionContinue
   112  }
   113  
   114  func (s *StepModifyAMIAttributes) Cleanup(state multistep.StateBag) {
   115  	// No cleanup...
   116  }