2013-08-08 19:04:38 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/goamz/ec2"
|
|
|
|
"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-09 01:51:48 -04:00
|
|
|
func (s *StepModifyAMIAttributes) Run(state map[string]interface{}) multistep.StepAction {
|
2013-08-08 19:04:38 -04:00
|
|
|
ec2conn := state["ec2"].(*ec2.EC2)
|
|
|
|
ui := state["ui"].(packer.Ui)
|
|
|
|
amis := state["amis"].(map[string]string)
|
|
|
|
ami := amis[ec2conn.Region.Name]
|
|
|
|
|
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-09 01:43:29 -04:00
|
|
|
options := &ec2.ModifyImageAttribute{
|
|
|
|
Description: s.Description,
|
|
|
|
AddUsers: s.Users,
|
|
|
|
AddGroups: s.Groups,
|
|
|
|
ProductCodes: s.ProductCodes,
|
2013-08-08 19:04:38 -04:00
|
|
|
}
|
|
|
|
|
2013-08-09 01:43:29 -04:00
|
|
|
ui.Say("Modifying AMI attributes...")
|
|
|
|
_, err := ec2conn.ModifyImageAttribute(ami, options)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error modify AMI attributes: %s", err)
|
|
|
|
state["error"] = err
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
2013-08-08 19:04:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-08-09 01:51:48 -04:00
|
|
|
func (s *StepModifyAMIAttributes) Cleanup(state map[string]interface{}) {
|
2013-08-08 19:04:38 -04:00
|
|
|
// No cleanup...
|
|
|
|
}
|