Expose more source ami data in the template
This commit is contained in:
parent
883ec47a1f
commit
507b3619e7
|
@ -1,6 +1,36 @@
|
||||||
package common
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/aws/aws-sdk-go/aws"
|
||||||
|
"github.com/aws/aws-sdk-go/service/ec2"
|
||||||
|
"github.com/hashicorp/packer/helper/multistep"
|
||||||
|
)
|
||||||
|
|
||||||
type BuildInfoTemplate struct {
|
type BuildInfoTemplate struct {
|
||||||
SourceAMI string
|
BuildRegion string
|
||||||
BuildRegion string
|
SourceAMI string
|
||||||
|
SourceAMIName string
|
||||||
|
SourceAMITags map[string]string
|
||||||
|
}
|
||||||
|
|
||||||
|
func extractBuildInfo(region string, state multistep.StateBag) *BuildInfoTemplate {
|
||||||
|
rawSourceAMI, hasSourceAMI := state.GetOk("source_image")
|
||||||
|
if !hasSourceAMI {
|
||||||
|
return &BuildInfoTemplate{
|
||||||
|
BuildRegion: region,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sourceAMI := rawSourceAMI.(*ec2.Image)
|
||||||
|
sourceAMITags := make(map[string]string, len(sourceAMI.Tags))
|
||||||
|
for _, tag := range sourceAMI.Tags {
|
||||||
|
sourceAMITags[aws.StringValue(tag.Key)] = aws.StringValue(tag.Value)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &BuildInfoTemplate{
|
||||||
|
BuildRegion: region,
|
||||||
|
SourceAMI: aws.StringValue(sourceAMI.ImageId),
|
||||||
|
SourceAMIName: aws.StringValue(sourceAMI.Name),
|
||||||
|
SourceAMITags: sourceAMITags,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,13 +26,6 @@ func (s *StepCreateTags) Run(_ context.Context, state multistep.StateBag) multis
|
||||||
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)
|
||||||
|
|
||||||
var sourceAMI string
|
|
||||||
if rawSourceAMI, hasSourceAMI := state.GetOk("source_image"); hasSourceAMI {
|
|
||||||
sourceAMI = *rawSourceAMI.(*ec2.Image).ImageId
|
|
||||||
} else {
|
|
||||||
sourceAMI = ""
|
|
||||||
}
|
|
||||||
|
|
||||||
if !s.Tags.IsSet() && !s.SnapshotTags.IsSet() {
|
if !s.Tags.IsSet() && !s.SnapshotTags.IsSet() {
|
||||||
return multistep.ActionContinue
|
return multistep.ActionContinue
|
||||||
}
|
}
|
||||||
|
@ -79,7 +72,7 @@ func (s *StepCreateTags) Run(_ context.Context, state multistep.StateBag) multis
|
||||||
|
|
||||||
// Convert tags to ec2.Tag format
|
// Convert tags to ec2.Tag format
|
||||||
ui.Say("Creating AMI tags")
|
ui.Say("Creating AMI tags")
|
||||||
amiTags, err := s.Tags.EC2Tags(s.Ctx, *ec2conn.Config.Region, sourceAMI)
|
amiTags, err := s.Tags.EC2Tags(s.Ctx, *ec2conn.Config.Region, state)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
state.Put("error", err)
|
state.Put("error", err)
|
||||||
ui.Error(err.Error())
|
ui.Error(err.Error())
|
||||||
|
@ -88,7 +81,7 @@ func (s *StepCreateTags) Run(_ context.Context, state multistep.StateBag) multis
|
||||||
amiTags.Report(ui)
|
amiTags.Report(ui)
|
||||||
|
|
||||||
ui.Say("Creating snapshot tags")
|
ui.Say("Creating snapshot tags")
|
||||||
snapshotTags, err := s.SnapshotTags.EC2Tags(s.Ctx, *ec2conn.Config.Region, sourceAMI)
|
snapshotTags, err := s.SnapshotTags.EC2Tags(s.Ctx, *ec2conn.Config.Region, state)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
state.Put("error", err)
|
state.Put("error", err)
|
||||||
ui.Error(err.Error())
|
ui.Error(err.Error())
|
||||||
|
|
|
@ -27,13 +27,6 @@ func (s *StepModifyAMIAttributes) Run(_ context.Context, state multistep.StateBa
|
||||||
session := state.Get("awsSession").(*session.Session)
|
session := state.Get("awsSession").(*session.Session)
|
||||||
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)
|
||||||
|
|
||||||
var sourceAMI string
|
|
||||||
if rawSourceAMI, hasSourceAMI := state.GetOk("source_image"); hasSourceAMI {
|
|
||||||
sourceAMI = *rawSourceAMI.(*ec2.Image).ImageId
|
|
||||||
} else {
|
|
||||||
sourceAMI = ""
|
|
||||||
}
|
|
||||||
snapshots := state.Get("snapshots").(map[string][]string)
|
snapshots := state.Get("snapshots").(map[string][]string)
|
||||||
|
|
||||||
// Determine if there is any work to do.
|
// Determine if there is any work to do.
|
||||||
|
@ -50,10 +43,7 @@ func (s *StepModifyAMIAttributes) Run(_ context.Context, state multistep.StateBa
|
||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
s.Ctx.Data = &BuildInfoTemplate{
|
s.Ctx.Data = extractBuildInfo(*ec2conn.Config.Region, state)
|
||||||
SourceAMI: sourceAMI,
|
|
||||||
BuildRegion: *ec2conn.Config.Region,
|
|
||||||
}
|
|
||||||
s.Description, err = interpolate.Render(s.Description, &s.Ctx)
|
s.Description, err = interpolate.Render(s.Description, &s.Ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = fmt.Errorf("Error interpolating AMI description: %s", err)
|
err = fmt.Errorf("Error interpolating AMI description: %s", err)
|
||||||
|
|
|
@ -88,7 +88,7 @@ func (s *StepRunSourceInstance) Run(_ context.Context, state multistep.StateBag)
|
||||||
s.Tags["Name"] = "Packer Builder"
|
s.Tags["Name"] = "Packer Builder"
|
||||||
}
|
}
|
||||||
|
|
||||||
ec2Tags, err := s.Tags.EC2Tags(s.Ctx, *ec2conn.Config.Region, s.SourceAMI)
|
ec2Tags, err := s.Tags.EC2Tags(s.Ctx, *ec2conn.Config.Region, state)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err := fmt.Errorf("Error tagging source instance: %s", err)
|
err := fmt.Errorf("Error tagging source instance: %s", err)
|
||||||
state.Put("error", err)
|
state.Put("error", err)
|
||||||
|
@ -96,7 +96,7 @@ func (s *StepRunSourceInstance) Run(_ context.Context, state multistep.StateBag)
|
||||||
return multistep.ActionHalt
|
return multistep.ActionHalt
|
||||||
}
|
}
|
||||||
|
|
||||||
volTags, err := s.VolumeTags.EC2Tags(s.Ctx, *ec2conn.Config.Region, s.SourceAMI)
|
volTags, err := s.VolumeTags.EC2Tags(s.Ctx, *ec2conn.Config.Region, state)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err := fmt.Errorf("Error tagging volumes: %s", err)
|
err := fmt.Errorf("Error tagging volumes: %s", err)
|
||||||
state.Put("error", err)
|
state.Put("error", err)
|
||||||
|
@ -259,7 +259,7 @@ func (s *StepRunSourceInstance) Run(_ context.Context, state multistep.StateBag)
|
||||||
if len(volumeIds) > 0 && s.VolumeTags.IsSet() {
|
if len(volumeIds) > 0 && s.VolumeTags.IsSet() {
|
||||||
ui.Say("Adding tags to source EBS Volumes")
|
ui.Say("Adding tags to source EBS Volumes")
|
||||||
|
|
||||||
volumeTags, err := s.VolumeTags.EC2Tags(s.Ctx, *ec2conn.Config.Region, s.SourceAMI)
|
volumeTags, err := s.VolumeTags.EC2Tags(s.Ctx, *ec2conn.Config.Region, state)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err := fmt.Errorf("Error tagging source EBS Volumes on %s: %s", *instance.InstanceId, err)
|
err := fmt.Errorf("Error tagging source EBS Volumes on %s: %s", *instance.InstanceId, err)
|
||||||
state.Put("error", err)
|
state.Put("error", err)
|
||||||
|
|
|
@ -143,7 +143,7 @@ func (s *StepRunSpotInstance) Run(_ context.Context, state multistep.StateBag) m
|
||||||
s.Tags["Name"] = "Packer Builder"
|
s.Tags["Name"] = "Packer Builder"
|
||||||
}
|
}
|
||||||
|
|
||||||
ec2Tags, err := s.Tags.EC2Tags(s.Ctx, *ec2conn.Config.Region, s.SourceAMI)
|
ec2Tags, err := s.Tags.EC2Tags(s.Ctx, *ec2conn.Config.Region, state)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err := fmt.Errorf("Error tagging source instance: %s", err)
|
err := fmt.Errorf("Error tagging source instance: %s", err)
|
||||||
state.Put("error", err)
|
state.Put("error", err)
|
||||||
|
@ -287,7 +287,7 @@ func (s *StepRunSpotInstance) Run(_ context.Context, state multistep.StateBag) m
|
||||||
if len(volumeIds) > 0 && s.VolumeTags.IsSet() {
|
if len(volumeIds) > 0 && s.VolumeTags.IsSet() {
|
||||||
ui.Say("Adding tags to source EBS Volumes")
|
ui.Say("Adding tags to source EBS Volumes")
|
||||||
|
|
||||||
volumeTags, err := s.VolumeTags.EC2Tags(s.Ctx, *ec2conn.Config.Region, s.SourceAMI)
|
volumeTags, err := s.VolumeTags.EC2Tags(s.Ctx, *ec2conn.Config.Region, state)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err := fmt.Errorf("Error tagging source EBS Volumes on %s: %s", *instance.InstanceId, err)
|
err := fmt.Errorf("Error tagging source EBS Volumes on %s: %s", *instance.InstanceId, err)
|
||||||
state.Put("error", err)
|
state.Put("error", err)
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
|
|
||||||
"github.com/aws/aws-sdk-go/aws"
|
"github.com/aws/aws-sdk-go/aws"
|
||||||
"github.com/aws/aws-sdk-go/service/ec2"
|
"github.com/aws/aws-sdk-go/service/ec2"
|
||||||
|
"github.com/hashicorp/packer/helper/multistep"
|
||||||
"github.com/hashicorp/packer/packer"
|
"github.com/hashicorp/packer/packer"
|
||||||
"github.com/hashicorp/packer/template/interpolate"
|
"github.com/hashicorp/packer/template/interpolate"
|
||||||
)
|
)
|
||||||
|
@ -23,12 +24,10 @@ func (t TagMap) IsSet() bool {
|
||||||
return len(t) > 0
|
return len(t) > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t TagMap) EC2Tags(ctx interpolate.Context, region, sourceAMIID string) (EC2Tags, error) {
|
func (t TagMap) EC2Tags(ctx interpolate.Context, region string, state multistep.StateBag) (EC2Tags, error) {
|
||||||
var ec2Tags []*ec2.Tag
|
var ec2Tags []*ec2.Tag
|
||||||
ctx.Data = &BuildInfoTemplate{
|
ctx.Data = extractBuildInfo(region, state)
|
||||||
SourceAMI: sourceAMIID,
|
|
||||||
BuildRegion: region,
|
|
||||||
}
|
|
||||||
for key, value := range t {
|
for key, value := range t {
|
||||||
interpolatedKey, err := interpolate.Render(key, &ctx)
|
interpolatedKey, err := interpolate.Render(key, &ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -18,7 +18,6 @@ type stepTagEBSVolumes struct {
|
||||||
func (s *stepTagEBSVolumes) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
func (s *stepTagEBSVolumes) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
||||||
ec2conn := state.Get("ec2").(*ec2.EC2)
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
||||||
instance := state.Get("instance").(*ec2.Instance)
|
instance := state.Get("instance").(*ec2.Instance)
|
||||||
sourceAMI := state.Get("source_image").(*ec2.Image)
|
|
||||||
ui := state.Get("ui").(packer.Ui)
|
ui := state.Get("ui").(packer.Ui)
|
||||||
|
|
||||||
volumes := make(EbsVolumes)
|
volumes := make(EbsVolumes)
|
||||||
|
@ -43,7 +42,7 @@ func (s *stepTagEBSVolumes) Run(_ context.Context, state multistep.StateBag) mul
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
tags, err := mapping.Tags.EC2Tags(s.Ctx, *ec2conn.Config.Region, *sourceAMI.ImageId)
|
tags, err := mapping.Tags.EC2Tags(s.Ctx, *ec2conn.Config.Region, state)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err := fmt.Errorf("Error tagging device %s with %s", mapping.DeviceName, err)
|
err := fmt.Errorf("Error tagging device %s with %s", mapping.DeviceName, err)
|
||||||
state.Put("error", err)
|
state.Put("error", err)
|
||||||
|
|
Loading…
Reference in New Issue