builder/amazon: Re-ordered steps for AMI region copying so tags and
launch permissions are also applied to copied AMIs
This commit is contained in:
parent
e5350ce573
commit
5bca569fa4
|
@ -189,15 +189,14 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
|
||||||
&StepEarlyCleanup{},
|
&StepEarlyCleanup{},
|
||||||
&StepSnapshot{},
|
&StepSnapshot{},
|
||||||
&StepRegisterAMI{},
|
&StepRegisterAMI{},
|
||||||
|
&awscommon.StepAMIRegionCopy{
|
||||||
|
Regions: b.config.AMIRegions,
|
||||||
|
},
|
||||||
&awscommon.StepModifyAMIAttributes{
|
&awscommon.StepModifyAMIAttributes{
|
||||||
Description: b.config.AMIDescription,
|
Description: b.config.AMIDescription,
|
||||||
Users: b.config.AMIUsers,
|
Users: b.config.AMIUsers,
|
||||||
Groups: b.config.AMIGroups,
|
Groups: b.config.AMIGroups,
|
||||||
},
|
},
|
||||||
&awscommon.StepAMIRegionCopy{
|
|
||||||
Regions: b.config.AMIRegions,
|
|
||||||
Tags: b.config.AMITags,
|
|
||||||
},
|
|
||||||
&awscommon.StepCreateTags{
|
&awscommon.StepCreateTags{
|
||||||
Tags: b.config.AMITags,
|
Tags: b.config.AMITags,
|
||||||
},
|
},
|
||||||
|
|
|
@ -10,7 +10,6 @@ import (
|
||||||
|
|
||||||
type StepAMIRegionCopy struct {
|
type StepAMIRegionCopy struct {
|
||||||
Regions []string
|
Regions []string
|
||||||
Tags map[string]string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StepAMIRegionCopy) Run(state multistep.StateBag) multistep.StepAction {
|
func (s *StepAMIRegionCopy) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
|
@ -49,25 +48,6 @@ func (s *StepAMIRegionCopy) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
return multistep.ActionHalt
|
return multistep.ActionHalt
|
||||||
}
|
}
|
||||||
|
|
||||||
// Need to re-apply Tags since they are not copied with the AMI
|
|
||||||
if len(s.Tags) > 0 {
|
|
||||||
ui.Say(fmt.Sprintf("Adding tags to AMI (%s)...", resp.ImageId))
|
|
||||||
|
|
||||||
var ec2Tags []ec2.Tag
|
|
||||||
for key, value := range s.Tags {
|
|
||||||
ui.Message(fmt.Sprintf("Adding tag: \"%s\": \"%s\"", key, value))
|
|
||||||
ec2Tags = append(ec2Tags, ec2.Tag{key, value})
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err := regionconn.CreateTags([]string{resp.ImageId}, ec2Tags)
|
|
||||||
if err != nil {
|
|
||||||
err := fmt.Errorf("Error adding tags to AMI (%s): %s", resp.ImageId, err)
|
|
||||||
state.Put("error", err)
|
|
||||||
ui.Error(err.Error())
|
|
||||||
return multistep.ActionHalt
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
amis[region] = resp.ImageId
|
amis[region] = resp.ImageId
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/mitchellh/goamz/aws"
|
||||||
"github.com/mitchellh/goamz/ec2"
|
"github.com/mitchellh/goamz/ec2"
|
||||||
"github.com/mitchellh/multistep"
|
"github.com/mitchellh/multistep"
|
||||||
"github.com/mitchellh/packer/packer"
|
"github.com/mitchellh/packer/packer"
|
||||||
|
@ -15,9 +16,9 @@ func (s *StepCreateTags) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
ec2conn := state.Get("ec2").(*ec2.EC2)
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
||||||
ui := state.Get("ui").(packer.Ui)
|
ui := state.Get("ui").(packer.Ui)
|
||||||
amis := state.Get("amis").(map[string]string)
|
amis := state.Get("amis").(map[string]string)
|
||||||
ami := amis[ec2conn.Region.Name]
|
|
||||||
|
|
||||||
if len(s.Tags) > 0 {
|
if len(s.Tags) > 0 {
|
||||||
|
for region, ami := range amis {
|
||||||
ui.Say(fmt.Sprintf("Adding tags to AMI (%s)...", ami))
|
ui.Say(fmt.Sprintf("Adding tags to AMI (%s)...", ami))
|
||||||
|
|
||||||
var ec2Tags []ec2.Tag
|
var ec2Tags []ec2.Tag
|
||||||
|
@ -26,7 +27,8 @@ func (s *StepCreateTags) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
ec2Tags = append(ec2Tags, ec2.Tag{key, value})
|
ec2Tags = append(ec2Tags, ec2.Tag{key, value})
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := ec2conn.CreateTags([]string{ami}, ec2Tags)
|
regionconn := ec2.New(ec2conn.Auth, aws.Regions[region])
|
||||||
|
_, err := regionconn.CreateTags([]string{ami}, ec2Tags)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err := fmt.Errorf("Error adding tags to AMI (%s): %s", ami, err)
|
err := fmt.Errorf("Error adding tags to AMI (%s): %s", ami, err)
|
||||||
state.Put("error", err)
|
state.Put("error", err)
|
||||||
|
@ -34,6 +36,7 @@ func (s *StepCreateTags) Run(state multistep.StateBag) multistep.StepAction {
|
||||||
return multistep.ActionHalt
|
return multistep.ActionHalt
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return multistep.ActionContinue
|
return multistep.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/mitchellh/goamz/aws"
|
||||||
"github.com/mitchellh/goamz/ec2"
|
"github.com/mitchellh/goamz/ec2"
|
||||||
"github.com/mitchellh/multistep"
|
"github.com/mitchellh/multistep"
|
||||||
"github.com/mitchellh/packer/packer"
|
"github.com/mitchellh/packer/packer"
|
||||||
|
@ -18,7 +19,6 @@ func (s *StepModifyAMIAttributes) Run(state multistep.StateBag) multistep.StepAc
|
||||||
ec2conn := state.Get("ec2").(*ec2.EC2)
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
||||||
ui := state.Get("ui").(packer.Ui)
|
ui := state.Get("ui").(packer.Ui)
|
||||||
amis := state.Get("amis").(map[string]string)
|
amis := state.Get("amis").(map[string]string)
|
||||||
ami := amis[ec2conn.Region.Name]
|
|
||||||
|
|
||||||
// Determine if there is any work to do.
|
// Determine if there is any work to do.
|
||||||
valid := false
|
valid := false
|
||||||
|
@ -59,10 +59,12 @@ func (s *StepModifyAMIAttributes) Run(state multistep.StateBag) multistep.StepAc
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ui.Say("Modifying AMI attributes...")
|
for region, ami := range amis {
|
||||||
|
ui.Say(fmt.Sprintf("Modifying attributes on AMI (%s)...", ami))
|
||||||
|
regionconn := ec2.New(ec2conn.Auth, aws.Regions[region])
|
||||||
for name, opts := range options {
|
for name, opts := range options {
|
||||||
ui.Message(fmt.Sprintf("Modifying: %s", name))
|
ui.Message(fmt.Sprintf("Modifying: %s", name))
|
||||||
_, err := ec2conn.ModifyImageAttribute(ami, opts)
|
_, err := regionconn.ModifyImageAttribute(ami, opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err := fmt.Errorf("Error modify AMI attributes: %s", err)
|
err := fmt.Errorf("Error modify AMI attributes: %s", err)
|
||||||
state.Put("error", err)
|
state.Put("error", err)
|
||||||
|
@ -70,6 +72,7 @@ func (s *StepModifyAMIAttributes) Run(state multistep.StateBag) multistep.StepAc
|
||||||
return multistep.ActionHalt
|
return multistep.ActionHalt
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return multistep.ActionContinue
|
return multistep.ActionContinue
|
||||||
}
|
}
|
||||||
|
|
|
@ -109,15 +109,14 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
|
||||||
&common.StepProvision{},
|
&common.StepProvision{},
|
||||||
&stepStopInstance{},
|
&stepStopInstance{},
|
||||||
&stepCreateAMI{},
|
&stepCreateAMI{},
|
||||||
|
&awscommon.StepAMIRegionCopy{
|
||||||
|
Regions: b.config.AMIRegions,
|
||||||
|
},
|
||||||
&awscommon.StepModifyAMIAttributes{
|
&awscommon.StepModifyAMIAttributes{
|
||||||
Description: b.config.AMIDescription,
|
Description: b.config.AMIDescription,
|
||||||
Users: b.config.AMIUsers,
|
Users: b.config.AMIUsers,
|
||||||
Groups: b.config.AMIGroups,
|
Groups: b.config.AMIGroups,
|
||||||
},
|
},
|
||||||
&awscommon.StepAMIRegionCopy{
|
|
||||||
Regions: b.config.AMIRegions,
|
|
||||||
Tags: b.config.AMITags,
|
|
||||||
},
|
|
||||||
&awscommon.StepCreateTags{
|
&awscommon.StepCreateTags{
|
||||||
Tags: b.config.AMITags,
|
Tags: b.config.AMITags,
|
||||||
},
|
},
|
||||||
|
|
|
@ -214,16 +214,15 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
|
||||||
&StepBundleVolume{},
|
&StepBundleVolume{},
|
||||||
&StepUploadBundle{},
|
&StepUploadBundle{},
|
||||||
&StepRegisterAMI{},
|
&StepRegisterAMI{},
|
||||||
|
&awscommon.StepAMIRegionCopy{
|
||||||
|
Regions: b.config.AMIRegions,
|
||||||
|
},
|
||||||
&awscommon.StepModifyAMIAttributes{
|
&awscommon.StepModifyAMIAttributes{
|
||||||
Description: b.config.AMIDescription,
|
Description: b.config.AMIDescription,
|
||||||
Users: b.config.AMIUsers,
|
Users: b.config.AMIUsers,
|
||||||
Groups: b.config.AMIGroups,
|
Groups: b.config.AMIGroups,
|
||||||
ProductCodes: b.config.AMIProductCodes,
|
ProductCodes: b.config.AMIProductCodes,
|
||||||
},
|
},
|
||||||
&awscommon.StepAMIRegionCopy{
|
|
||||||
Regions: b.config.AMIRegions,
|
|
||||||
Tags: b.config.AMITags,
|
|
||||||
},
|
|
||||||
&awscommon.StepCreateTags{
|
&awscommon.StepCreateTags{
|
||||||
Tags: b.config.AMITags,
|
Tags: b.config.AMITags,
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in New Issue