2013-08-08 19:04:38 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-04-05 17:58:48 -04:00
|
|
|
|
|
|
|
"github.com/awslabs/aws-sdk-go/aws"
|
|
|
|
"github.com/awslabs/aws-sdk-go/service/ec2"
|
2013-08-08 19:04:38 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
)
|
|
|
|
|
2013-08-09 01:51:48 -04:00
|
|
|
type StepModifyAMIAttributes struct {
|
2013-08-08 19:04:38 -04:00
|
|
|
Users []string
|
|
|
|
Groups []string
|
|
|
|
ProductCodes []string
|
|
|
|
Description string
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
func (s *StepModifyAMIAttributes) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
amis := state.Get("amis").(map[string]string)
|
2013-08-08 19:04:38 -04:00
|
|
|
|
2013-08-09 01:46:22 -04:00
|
|
|
// Determine if there is any work to do.
|
|
|
|
valid := false
|
|
|
|
valid = valid || s.Description != ""
|
|
|
|
valid = valid || (s.Users != nil && len(s.Users) > 0)
|
|
|
|
valid = valid || (s.Groups != nil && len(s.Groups) > 0)
|
|
|
|
valid = valid || (s.ProductCodes != nil && len(s.ProductCodes) > 0)
|
|
|
|
|
|
|
|
if !valid {
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-08-15 23:28:20 -04:00
|
|
|
// Construct the modify image attribute requests we're going to make.
|
|
|
|
// We need to make each separately since the EC2 API only allows changing
|
|
|
|
// one type at a kind currently.
|
2015-04-05 17:58:48 -04:00
|
|
|
options := make(map[string]*ec2.ModifyImageAttributeInput)
|
2013-08-15 23:28:20 -04:00
|
|
|
if s.Description != "" {
|
2015-04-05 17:58:48 -04:00
|
|
|
options["description"] = &ec2.ModifyImageAttributeInput{
|
|
|
|
Description: &ec2.AttributeValue{Value: &s.Description},
|
2013-08-15 23:28:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(s.Groups) > 0 {
|
2015-04-05 17:58:48 -04:00
|
|
|
groups := make([]*string, len(s.Groups))
|
|
|
|
for i, g := range s.Groups {
|
|
|
|
groups[i] = &g
|
|
|
|
}
|
|
|
|
options["groups"] = &ec2.ModifyImageAttributeInput{
|
|
|
|
UserGroups: groups,
|
2013-08-15 23:28:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(s.Users) > 0 {
|
2015-04-05 17:58:48 -04:00
|
|
|
users := make([]*string, len(s.Users))
|
|
|
|
for i, u := range s.Users {
|
|
|
|
users[i] = &u
|
|
|
|
}
|
|
|
|
options["users"] = &ec2.ModifyImageAttributeInput{
|
|
|
|
UserIDs: users,
|
2013-08-15 23:28:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(s.ProductCodes) > 0 {
|
2015-04-05 17:58:48 -04:00
|
|
|
codes := make([]*string, len(s.ProductCodes))
|
|
|
|
for i, c := range s.ProductCodes {
|
|
|
|
codes[i] = &c
|
|
|
|
}
|
|
|
|
options["product codes"] = &ec2.ModifyImageAttributeInput{
|
|
|
|
ProductCodes: codes,
|
2013-08-15 23:28:20 -04:00
|
|
|
}
|
2013-08-08 19:04:38 -04:00
|
|
|
}
|
|
|
|
|
2013-09-04 19:06:06 -04:00
|
|
|
for region, ami := range amis {
|
|
|
|
ui.Say(fmt.Sprintf("Modifying attributes on AMI (%s)...", ami))
|
2015-04-05 17:58:48 -04:00
|
|
|
regionconn := ec2.New(&aws.Config{
|
|
|
|
Credentials: ec2conn.Config.Credentials,
|
|
|
|
Region: region,
|
|
|
|
})
|
|
|
|
for name, input := range options {
|
2013-09-04 19:06:06 -04:00
|
|
|
ui.Message(fmt.Sprintf("Modifying: %s", name))
|
2015-04-05 17:58:48 -04:00
|
|
|
input.ImageID = &ami
|
|
|
|
_, err := regionconn.ModifyImageAttribute(input)
|
2013-09-04 19:06:06 -04:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error modify AMI attributes: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2013-08-15 23:28:20 -04:00
|
|
|
}
|
2013-08-08 19:04:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:58:55 -04:00
|
|
|
func (s *StepModifyAMIAttributes) Cleanup(state multistep.StateBag) {
|
2013-08-08 19:04:38 -04:00
|
|
|
// No cleanup...
|
|
|
|
}
|