2013-08-08 19:04:38 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2013-08-08 19:04:38 -04:00
|
|
|
"fmt"
|
2020-02-14 11:42:29 -05:00
|
|
|
|
2015-06-03 17:13:52 -04:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
2015-10-30 14:58:56 -04:00
|
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
2015-06-03 17:13:52 -04:00
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2020-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2020-11-12 17:44:02 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/packerbuilderdata"
|
2020-11-11 13:21:37 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/template/interpolate"
|
2013-08-08 19:04:38 -04:00
|
|
|
)
|
|
|
|
|
2013-08-09 01:51:48 -04:00
|
|
|
type StepModifyAMIAttributes struct {
|
2016-12-02 03:49:21 -05:00
|
|
|
Users []string
|
|
|
|
Groups []string
|
|
|
|
SnapshotUsers []string
|
|
|
|
SnapshotGroups []string
|
|
|
|
ProductCodes []string
|
|
|
|
Description string
|
2017-01-10 05:41:28 -05:00
|
|
|
Ctx interpolate.Context
|
2020-01-20 10:29:38 -05:00
|
|
|
|
2020-11-04 18:44:05 -05:00
|
|
|
GeneratedData *packerbuilderdata.GeneratedData
|
2013-08-08 19:04:38 -04:00
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepModifyAMIAttributes) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2013-08-31 15:58:55 -04:00
|
|
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
2017-12-19 14:04:17 -05:00
|
|
|
session := state.Get("awsSession").(*session.Session)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2013-08-31 15:58:55 -04:00
|
|
|
amis := state.Get("amis").(map[string]string)
|
2016-09-11 08:37:24 -04:00
|
|
|
snapshots := state.Get("snapshots").(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)
|
2016-12-02 03:49:21 -05:00
|
|
|
valid = valid || (s.SnapshotUsers != nil && len(s.SnapshotUsers) > 0)
|
|
|
|
valid = valid || (s.SnapshotGroups != nil && len(s.SnapshotGroups) > 0)
|
2013-08-09 01:46:22 -04:00
|
|
|
|
|
|
|
if !valid {
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2017-01-10 05:41:28 -05:00
|
|
|
var err error
|
2020-01-20 10:29:38 -05:00
|
|
|
s.Ctx.Data = extractBuildInfo(*ec2conn.Config.Region, state, s.GeneratedData)
|
2017-01-10 05:41:28 -05:00
|
|
|
s.Description, err = interpolate.Render(s.Description, &s.Ctx)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Error interpolating AMI description: %s", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2016-09-11 08:37:24 -04:00
|
|
|
// Construct the modify image and snapshot 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
|
|
|
}
|
|
|
|
}
|
2016-09-11 08:37:24 -04:00
|
|
|
snapshotOptions := make(map[string]*ec2.ModifySnapshotAttributeInput)
|
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))
|
2016-09-11 08:37:24 -04:00
|
|
|
addsImage := make([]*ec2.LaunchPermission, len(s.Groups))
|
2015-06-25 20:48:38 -04:00
|
|
|
addGroups := &ec2.ModifyImageAttributeInput{
|
|
|
|
LaunchPermission: &ec2.LaunchPermissionModifications{},
|
2015-04-05 17:58:48 -04:00
|
|
|
}
|
2015-06-25 20:48:38 -04:00
|
|
|
|
2015-04-05 17:58:48 -04:00
|
|
|
for i, g := range s.Groups {
|
2015-06-26 15:08:44 -04:00
|
|
|
groups[i] = aws.String(g)
|
2016-09-11 08:37:24 -04:00
|
|
|
addsImage[i] = &ec2.LaunchPermission{
|
|
|
|
Group: aws.String(g),
|
|
|
|
}
|
2013-08-15 23:28:20 -04:00
|
|
|
}
|
2015-06-25 20:48:38 -04:00
|
|
|
|
2016-09-11 08:37:24 -04:00
|
|
|
addGroups.UserGroups = groups
|
2016-12-06 03:58:17 -05:00
|
|
|
addGroups.LaunchPermission.Add = addsImage
|
2015-06-25 20:48:38 -04:00
|
|
|
options["groups"] = addGroups
|
2016-12-02 03:49:21 -05:00
|
|
|
}
|
2016-09-11 08:37:24 -04:00
|
|
|
|
2016-12-02 03:49:21 -05:00
|
|
|
if len(s.SnapshotGroups) > 0 {
|
|
|
|
groups := make([]*string, len(s.SnapshotGroups))
|
|
|
|
addsSnapshot := make([]*ec2.CreateVolumePermission, len(s.SnapshotGroups))
|
|
|
|
addSnapshotGroups := &ec2.ModifySnapshotAttributeInput{
|
|
|
|
CreateVolumePermission: &ec2.CreateVolumePermissionModifications{},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, g := range s.SnapshotGroups {
|
|
|
|
groups[i] = aws.String(g)
|
|
|
|
addsSnapshot[i] = &ec2.CreateVolumePermission{
|
|
|
|
Group: aws.String(g),
|
|
|
|
}
|
|
|
|
}
|
2016-09-11 08:37:24 -04:00
|
|
|
addSnapshotGroups.GroupNames = groups
|
|
|
|
addSnapshotGroups.CreateVolumePermission.Add = addsSnapshot
|
|
|
|
snapshotOptions["groups"] = addSnapshotGroups
|
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))
|
2016-09-11 08:37:24 -04:00
|
|
|
addsImage := make([]*ec2.LaunchPermission, len(s.Users))
|
2015-04-05 17:58:48 -04:00
|
|
|
for i, u := range s.Users {
|
2015-06-29 12:22:33 -04:00
|
|
|
users[i] = aws.String(u)
|
2016-09-11 08:37:24 -04:00
|
|
|
addsImage[i] = &ec2.LaunchPermission{UserId: aws.String(u)}
|
2015-04-05 17:58:48 -04:00
|
|
|
}
|
2016-09-11 08:37:24 -04:00
|
|
|
|
2015-04-05 17:58:48 -04:00
|
|
|
options["users"] = &ec2.ModifyImageAttributeInput{
|
2015-08-17 20:44:01 -04:00
|
|
|
UserIds: users,
|
2015-06-24 12:41:55 -04:00
|
|
|
LaunchPermission: &ec2.LaunchPermissionModifications{
|
2016-09-11 08:37:24 -04:00
|
|
|
Add: addsImage,
|
|
|
|
},
|
|
|
|
}
|
2016-12-02 03:49:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(s.SnapshotUsers) > 0 {
|
|
|
|
users := make([]*string, len(s.SnapshotUsers))
|
|
|
|
addsSnapshot := make([]*ec2.CreateVolumePermission, len(s.SnapshotUsers))
|
|
|
|
for i, u := range s.SnapshotUsers {
|
|
|
|
users[i] = aws.String(u)
|
|
|
|
addsSnapshot[i] = &ec2.CreateVolumePermission{UserId: aws.String(u)}
|
|
|
|
}
|
2016-09-11 08:37:24 -04:00
|
|
|
|
|
|
|
snapshotOptions["users"] = &ec2.ModifySnapshotAttributeInput{
|
|
|
|
UserIds: users,
|
|
|
|
CreateVolumePermission: &ec2.CreateVolumePermissionModifications{
|
|
|
|
Add: addsSnapshot,
|
2015-06-24 12:41:55 -04:00
|
|
|
},
|
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
|
|
|
}
|
|
|
|
|
2016-09-11 08:37:24 -04:00
|
|
|
// Modifying image attributes
|
2013-09-04 19:06:06 -04:00
|
|
|
for region, ami := range amis {
|
|
|
|
ui.Say(fmt.Sprintf("Modifying attributes on AMI (%s)...", ami))
|
2017-12-19 14:04:17 -05:00
|
|
|
regionConn := ec2.New(session, &aws.Config{
|
2019-03-04 19:22:52 -05:00
|
|
|
Region: aws.String(region),
|
2017-12-19 14:04:17 -05:00
|
|
|
})
|
2015-04-05 17:58:48 -04:00
|
|
|
for name, input := range options {
|
2013-09-04 19:06:06 -04:00
|
|
|
ui.Message(fmt.Sprintf("Modifying: %s", name))
|
2015-08-17 20:44:01 -04:00
|
|
|
input.ImageId = &ami
|
2017-12-19 14:04:17 -05:00
|
|
|
_, 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
|
|
|
}
|
|
|
|
|
2016-09-11 08:37:24 -04:00
|
|
|
// Modifying snapshot attributes
|
|
|
|
for region, region_snapshots := range snapshots {
|
|
|
|
for _, snapshot := range region_snapshots {
|
|
|
|
ui.Say(fmt.Sprintf("Modifying attributes on snapshot (%s)...", snapshot))
|
2017-12-19 14:04:17 -05:00
|
|
|
regionConn := ec2.New(session, &aws.Config{
|
2019-03-04 19:22:52 -05:00
|
|
|
Region: aws.String(region),
|
2017-12-19 14:04:17 -05:00
|
|
|
})
|
2016-09-11 08:37:24 -04:00
|
|
|
for name, input := range snapshotOptions {
|
|
|
|
ui.Message(fmt.Sprintf("Modifying: %s", name))
|
|
|
|
input.SnapshotId = &snapshot
|
2017-12-19 14:04:17 -05:00
|
|
|
_, err := regionConn.ModifySnapshotAttribute(input)
|
2016-09-11 08:37:24 -04:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error modify snapshot attributes: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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...
|
|
|
|
}
|