Merge pull request #4464 from rickard-von-essen/ebsvolume-interpolate

builder/amazon-ebsvolume: Fix interpolation of block_device
This commit is contained in:
Rickard von Essen 2017-01-26 21:34:19 +01:00 committed by GitHub
commit bc6b4ab8b9
2 changed files with 17 additions and 7 deletions

View File

@ -2,6 +2,7 @@ package ebsvolume
import ( import (
awscommon "github.com/mitchellh/packer/builder/amazon/common" awscommon "github.com/mitchellh/packer/builder/amazon/common"
"github.com/mitchellh/packer/template/interpolate"
) )
type BlockDevice struct { type BlockDevice struct {
@ -9,15 +10,20 @@ type BlockDevice struct {
Tags map[string]string `mapstructure:"tags"` Tags map[string]string `mapstructure:"tags"`
} }
func commonBlockDevices(mappings []BlockDevice) awscommon.BlockDevices { func commonBlockDevices(mappings []BlockDevice, ctx *interpolate.Context) (awscommon.BlockDevices, error) {
result := make([]awscommon.BlockDevice, len(mappings)) result := make([]awscommon.BlockDevice, len(mappings))
for i, mapping := range mappings { for i, mapping := range mappings {
result[i] = mapping.BlockDevice interpolateBlockDev, err := interpolate.RenderInterface(&mapping.BlockDevice, ctx)
if err != nil {
return awscommon.BlockDevices{}, err
}
result[i] = *interpolateBlockDev.(*awscommon.BlockDevice)
} }
return awscommon.BlockDevices{ return awscommon.BlockDevices{
LaunchBlockDevices: awscommon.LaunchBlockDevices{ LaunchBlockDevices: awscommon.LaunchBlockDevices{
LaunchMappings: result, LaunchMappings: result,
}, },
} }, nil
} }

View File

@ -28,6 +28,7 @@ type Config struct {
VolumeMappings []BlockDevice `mapstructure:"ebs_volumes"` VolumeMappings []BlockDevice `mapstructure:"ebs_volumes"`
AMIEnhancedNetworking bool `mapstructure:"enhanced_networking"` AMIEnhancedNetworking bool `mapstructure:"enhanced_networking"`
launchBlockDevices awscommon.BlockDevices
ctx interpolate.Context ctx interpolate.Context
} }
@ -57,6 +58,11 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
errs = packer.MultiErrorAppend(errs, b.config.AccessConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend(errs, b.config.AccessConfig.Prepare(&b.config.ctx)...)
errs = packer.MultiErrorAppend(errs, b.config.RunConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend(errs, b.config.RunConfig.Prepare(&b.config.ctx)...)
b.config.launchBlockDevices, err = commonBlockDevices(b.config.VolumeMappings, &b.config.ctx)
if err != nil {
errs = packer.MultiErrorAppend(errs, err)
}
if errs != nil && len(errs.Errors) > 0 { if errs != nil && len(errs.Errors) > 0 {
return nil, errs return nil, errs
} }
@ -96,8 +102,6 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
state.Put("hook", hook) state.Put("hook", hook)
state.Put("ui", ui) state.Put("ui", ui)
launchBlockDevices := commonBlockDevices(b.config.VolumeMappings)
// Build the steps // Build the steps
steps := []multistep.Step{ steps := []multistep.Step{
&awscommon.StepSourceAMIInfo{ &awscommon.StepSourceAMIInfo{
@ -132,7 +136,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
AssociatePublicIpAddress: b.config.AssociatePublicIpAddress, AssociatePublicIpAddress: b.config.AssociatePublicIpAddress,
EbsOptimized: b.config.EbsOptimized, EbsOptimized: b.config.EbsOptimized,
AvailabilityZone: b.config.AvailabilityZone, AvailabilityZone: b.config.AvailabilityZone,
BlockDevices: launchBlockDevices, BlockDevices: b.config.launchBlockDevices,
Tags: b.config.RunTags, Tags: b.config.RunTags,
Ctx: b.config.ctx, Ctx: b.config.ctx,
InstanceInitiatedShutdownBehavior: b.config.InstanceInitiatedShutdownBehavior, InstanceInitiatedShutdownBehavior: b.config.InstanceInitiatedShutdownBehavior,