aws: move the OmitFromArtifact field in ebssurrogat where it's being used

also, simplified a bit the usage of block device
This commit is contained in:
Adrien Delorme 2019-06-18 16:02:08 +02:00
parent a86aae1c7e
commit dcc22df609
9 changed files with 155 additions and 43 deletions

View File

@ -65,13 +65,3 @@ func (bds BlockDevices) Prepare(ctx *interpolate.Context) (errs []error) {
} }
return errs return errs
} }
func (b BlockDevices) GetOmissions() map[string]bool {
omitMap := make(map[string]bool)
for _, blockDevice := range b {
omitMap[blockDevice.DeviceName] = blockDevice.OmitFromArtifact
}
return omitMap
}

View File

@ -44,8 +44,6 @@ type BlockDevice struct {
// The size of the volume, in GiB. Required if not specifying a // The size of the volume, in GiB. Required if not specifying a
// snapshot_id. // snapshot_id.
VolumeSize int64 `mapstructure:"volume_size" required:"false"` VolumeSize int64 `mapstructure:"volume_size" required:"false"`
// ebssurrogate only
OmitFromArtifact bool `mapstructure:"omit_from_artifact"`
} }
type BlockDevices []BlockDevice type BlockDevices []BlockDevice
@ -120,13 +118,3 @@ func (bds BlockDevices) Prepare(ctx *interpolate.Context) (errs []error) {
} }
return errs return errs
} }
func (b BlockDevices) GetOmissions() map[string]bool {
omitMap := make(map[string]bool)
for _, blockDevice := range b {
omitMap[blockDevice.DeviceName] = blockDevice.OmitFromArtifact
}
return omitMap
}

View File

@ -21,7 +21,7 @@ import (
type StepRunSourceInstance struct { type StepRunSourceInstance struct {
AssociatePublicIpAddress bool AssociatePublicIpAddress bool
LaunchMappings BlockDevices LaunchMappings EC2BlockDeviceMappingsBuilder
Comm *communicator.Config Comm *communicator.Config
Ctx interpolate.Context Ctx interpolate.Context
Debug bool Debug bool

View File

@ -21,9 +21,13 @@ import (
"github.com/hashicorp/packer/template/interpolate" "github.com/hashicorp/packer/template/interpolate"
) )
type EC2BlockDeviceMappingsBuilder interface {
BuildEC2BlockDeviceMappings() []*ec2.BlockDeviceMapping
}
type StepRunSpotInstance struct { type StepRunSpotInstance struct {
AssociatePublicIpAddress bool AssociatePublicIpAddress bool
LaunchMappings BlockDevices LaunchMappings EC2BlockDeviceMappingsBuilder
BlockDurationMinutes int64 BlockDurationMinutes int64
Debug bool Debug bool
Comm *communicator.Config Comm *communicator.Config

View File

@ -82,7 +82,7 @@ func tStateSpot() multistep.StateBag {
func getBasicStep() *StepRunSpotInstance { func getBasicStep() *StepRunSpotInstance {
stepRunSpotInstance := StepRunSpotInstance{ stepRunSpotInstance := StepRunSpotInstance{
AssociatePublicIpAddress: false, AssociatePublicIpAddress: false,
LaunchMappings: []BlockDevice(nil), LaunchMappings: BlockDevices{},
BlockDurationMinutes: 0, BlockDurationMinutes: 0,
Debug: false, Debug: false,
Comm: &communicator.Config{ Comm: &communicator.Config{

View File

@ -0,0 +1,111 @@
//go:generate struct-markdown
package ebssurrogate
import (
"fmt"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
awscommon "github.com/hashicorp/packer/builder/amazon/common"
"github.com/hashicorp/packer/template/interpolate"
)
type BlockDevice struct {
awscommon.BlockDevice `mapstructure:",squash"`
// ebssurrogate only
OmitFromArtifact bool `mapstructure:"omit_from_artifact"`
}
type BlockDevices []BlockDevice
func (bds BlockDevices) Common() []awscommon.BlockDevice {
res := []awscommon.BlockDevice{}
for _, bd := range bds {
res = append(res, bd.BlockDevice)
}
return res
}
func (bds BlockDevices) BuildEC2BlockDeviceMappings() []*ec2.BlockDeviceMapping {
var blockDevices []*ec2.BlockDeviceMapping
for _, blockDevice := range bds {
blockDevices = append(blockDevices, blockDevice.BuildEC2BlockDeviceMapping())
}
return blockDevices
}
func (blockDevice BlockDevice) BuildEC2BlockDeviceMapping() *ec2.BlockDeviceMapping {
mapping := &ec2.BlockDeviceMapping{
DeviceName: aws.String(blockDevice.DeviceName),
}
if blockDevice.NoDevice {
mapping.NoDevice = aws.String("")
return mapping
} else if blockDevice.VirtualName != "" {
if strings.HasPrefix(blockDevice.VirtualName, "ephemeral") {
mapping.VirtualName = aws.String(blockDevice.VirtualName)
}
return mapping
}
ebsBlockDevice := &ec2.EbsBlockDevice{
DeleteOnTermination: aws.Bool(blockDevice.DeleteOnTermination),
}
if blockDevice.VolumeType != "" {
ebsBlockDevice.VolumeType = aws.String(blockDevice.VolumeType)
}
if blockDevice.VolumeSize > 0 {
ebsBlockDevice.VolumeSize = aws.Int64(blockDevice.VolumeSize)
}
// IOPS is only valid for io1 type
if blockDevice.VolumeType == "io1" {
ebsBlockDevice.Iops = aws.Int64(blockDevice.IOPS)
}
// You cannot specify Encrypted if you specify a Snapshot ID
if blockDevice.SnapshotId != "" {
ebsBlockDevice.SnapshotId = aws.String(blockDevice.SnapshotId)
}
ebsBlockDevice.Encrypted = blockDevice.Encrypted
mapping.Ebs = ebsBlockDevice
return mapping
}
func (b *BlockDevice) Prepare(ctx *interpolate.Context) error {
if b.DeviceName == "" {
return fmt.Errorf("The `device_name` must be specified " +
"for every device in the block device mapping.")
}
return nil
}
func (bds BlockDevices) Prepare(ctx *interpolate.Context) (errs []error) {
for _, block := range bds {
if err := block.Prepare(ctx); err != nil {
errs = append(errs, err)
}
}
return errs
}
func (b BlockDevices) GetOmissions() map[string]bool {
omitMap := make(map[string]bool)
for _, blockDevice := range b {
omitMap[blockDevice.DeviceName] = blockDevice.OmitFromArtifact
}
return omitMap
}

View File

@ -26,7 +26,7 @@ type Config struct {
awscommon.AccessConfig `mapstructure:",squash"` awscommon.AccessConfig `mapstructure:",squash"`
awscommon.RunConfig `mapstructure:",squash"` awscommon.RunConfig `mapstructure:",squash"`
AMIMappings awscommon.BlockDevices `mapstructure:"ami_block_device_mappings" required:"false"` AMIMappings awscommon.BlockDevices `mapstructure:"ami_block_device_mappings" required:"false"`
LaunchMappings awscommon.BlockDevices `mapstructure:"launch_block_device_mappings" required:"false"` LaunchMappings BlockDevices `mapstructure:"launch_block_device_mappings" required:"false"`
awscommon.AMIConfig `mapstructure:",squash"` awscommon.AMIConfig `mapstructure:",squash"`
// A block device mapping describing the root device of the AMI. This looks // A block device mapping describing the root device of the AMI. This looks
// like the mappings in `ami_block_device_mapping`, except with an // like the mappings in `ami_block_device_mapping`, except with an
@ -235,7 +235,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
TemporarySGSourceCidrs: b.config.TemporarySGSourceCidrs, TemporarySGSourceCidrs: b.config.TemporarySGSourceCidrs,
}, },
&awscommon.StepCleanupVolumes{ &awscommon.StepCleanupVolumes{
LaunchMappings: b.config.LaunchMappings, LaunchMappings: b.config.LaunchMappings.Common(),
}, },
instanceStep, instanceStep,
&awscommon.StepGetPassword{ &awscommon.StepGetPassword{

View File

@ -3,28 +3,47 @@
package ebsvolume package ebsvolume
import ( import (
"github.com/aws/aws-sdk-go/service/ec2"
awscommon "github.com/hashicorp/packer/builder/amazon/common" awscommon "github.com/hashicorp/packer/builder/amazon/common"
"github.com/hashicorp/packer/template/interpolate" "github.com/hashicorp/packer/template/interpolate"
) )
type BlockDevice struct { type BlockDevice struct {
awscommon.BlockDevice `mapstructure:"-,squash"` awscommon.BlockDevice `mapstructure:",squash"`
OmitFromArtifact bool `mapstructure:"omit_from_artifact"`
// Tags applied to the AMI. This is a // Tags applied to the AMI. This is a
// template engine, see Build template // template engine, see Build template
// data for more information. // data for more information.
Tags awscommon.TagMap `mapstructure:"tags" required:"false"` Tags awscommon.TagMap `mapstructure:"tags" required:"false"`
} }
func commonBlockDevices(mappings []BlockDevice, ctx *interpolate.Context) (awscommon.BlockDevices, error) { type BlockDevices []BlockDevice
result := make([]awscommon.BlockDevice, len(mappings))
for i, mapping := range mappings { func (bds BlockDevices) BuildEC2BlockDeviceMappings() []*ec2.BlockDeviceMapping {
interpolateBlockDev, err := interpolate.RenderInterface(&mapping.BlockDevice, ctx) var blockDevices []*ec2.BlockDeviceMapping
if err != nil {
return awscommon.BlockDevices{}, err for _, blockDevice := range bds {
blockDevices = append(blockDevices, blockDevice.BuildEC2BlockDeviceMapping())
}
return blockDevices
}
func (bds BlockDevices) Prepare(ctx *interpolate.Context) (errs []error) {
for _, block := range bds {
if err := block.Prepare(ctx); err != nil {
errs = append(errs, err)
} }
result[i] = *interpolateBlockDev.(*awscommon.BlockDevice) }
return errs
}
func (b BlockDevices) GetOmissions() map[string]bool {
omitMap := make(map[string]bool)
for _, blockDevice := range b {
omitMap[blockDevice.DeviceName] = blockDevice.OmitFromArtifact
} }
return result, nil return omitMap
} }

View File

@ -1,7 +1,7 @@
//go:generate struct-markdown //go:generate struct-markdown
// The ebsvolume package contains a packer.Builder implementation that // The ebsvolume package contains a packer.Builder implementation that builds
// builds EBS volumes for Amazon EC2 using an ephemeral instance, // EBS volumes for Amazon EC2 using an ephemeral instance,
package ebsvolume package ebsvolume
import ( import (
@ -24,9 +24,9 @@ type Config struct {
common.PackerConfig `mapstructure:",squash"` common.PackerConfig `mapstructure:",squash"`
awscommon.AccessConfig `mapstructure:",squash"` awscommon.AccessConfig `mapstructure:",squash"`
awscommon.RunConfig `mapstructure:",squash"` awscommon.RunConfig `mapstructure:",squash"`
// Add the block device // Add the block device mappings to the AMI. The block device mappings
// mappings to the AMI. The block device mappings allow for keys: // allow for keys:
VolumeMappings []BlockDevice `mapstructure:"ebs_volumes" required:"false"` VolumeMappings BlockDevices `mapstructure:"ebs_volumes" required:"false"`
// Enable enhanced networking (ENA but not SriovNetSupport) on // Enable enhanced networking (ENA but not SriovNetSupport) on
// HVM-compatible AMIs. If set, add ec2:ModifyInstanceAttribute to your AWS // HVM-compatible AMIs. If set, add ec2:ModifyInstanceAttribute to your AWS
// IAM policy. If false, this will disable enhanced networking in the final // IAM policy. If false, this will disable enhanced networking in the final
@ -42,7 +42,7 @@ type Config struct {
// Default `false`. // Default `false`.
AMISriovNetSupport bool `mapstructure:"sriov_support" required:"false"` AMISriovNetSupport bool `mapstructure:"sriov_support" required:"false"`
launchBlockDevices awscommon.BlockDevices launchBlockDevices BlockDevices
ctx interpolate.Context ctx interpolate.Context
} }
@ -84,7 +84,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
} }
} }
b.config.launchBlockDevices, err = commonBlockDevices(b.config.VolumeMappings, &b.config.ctx) b.config.launchBlockDevices = b.config.VolumeMappings
if err != nil { if err != nil {
errs = packer.MultiErrorAppend(errs, err) errs = packer.MultiErrorAppend(errs, err)
} }