2013-08-15 17:05:08 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2014-09-05 15:38:19 -04:00
|
|
|
"fmt"
|
|
|
|
|
2013-08-15 17:05:08 -04:00
|
|
|
"github.com/mitchellh/goamz/ec2"
|
2014-09-05 15:38:19 -04:00
|
|
|
"github.com/mitchellh/packer/packer"
|
2013-08-15 17:05:08 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// BlockDevice
|
|
|
|
type BlockDevice struct {
|
2014-04-30 18:43:11 -04:00
|
|
|
DeleteOnTermination bool `mapstructure:"delete_on_termination"`
|
2013-08-15 17:05:08 -04:00
|
|
|
DeviceName string `mapstructure:"device_name"`
|
2014-05-22 14:36:22 -04:00
|
|
|
Encrypted bool `mapstructure:"encrypted"`
|
2014-04-30 18:43:11 -04:00
|
|
|
IOPS int64 `mapstructure:"iops"`
|
|
|
|
NoDevice bool `mapstructure:"no_device"`
|
2013-08-15 17:05:08 -04:00
|
|
|
SnapshotId string `mapstructure:"snapshot_id"`
|
2014-04-30 18:43:11 -04:00
|
|
|
VirtualName string `mapstructure:"virtual_name"`
|
2013-08-15 17:05:08 -04:00
|
|
|
VolumeType string `mapstructure:"volume_type"`
|
|
|
|
VolumeSize int64 `mapstructure:"volume_size"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type BlockDevices struct {
|
2013-09-06 14:48:38 -04:00
|
|
|
AMIMappings []BlockDevice `mapstructure:"ami_block_device_mappings"`
|
|
|
|
LaunchMappings []BlockDevice `mapstructure:"launch_block_device_mappings"`
|
2013-08-15 17:05:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func buildBlockDevices(b []BlockDevice) []ec2.BlockDeviceMapping {
|
|
|
|
var blockDevices []ec2.BlockDeviceMapping
|
|
|
|
|
|
|
|
for _, blockDevice := range b {
|
|
|
|
blockDevices = append(blockDevices, ec2.BlockDeviceMapping{
|
|
|
|
DeviceName: blockDevice.DeviceName,
|
|
|
|
VirtualName: blockDevice.VirtualName,
|
|
|
|
SnapshotId: blockDevice.SnapshotId,
|
|
|
|
VolumeType: blockDevice.VolumeType,
|
|
|
|
VolumeSize: blockDevice.VolumeSize,
|
|
|
|
DeleteOnTermination: blockDevice.DeleteOnTermination,
|
|
|
|
IOPS: blockDevice.IOPS,
|
2013-09-06 14:44:57 -04:00
|
|
|
NoDevice: blockDevice.NoDevice,
|
2014-05-22 14:36:22 -04:00
|
|
|
Encrypted: blockDevice.Encrypted,
|
2013-08-15 17:05:08 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return blockDevices
|
|
|
|
}
|
|
|
|
|
2014-09-05 15:38:19 -04:00
|
|
|
func (b *BlockDevices) Prepare(t *packer.ConfigTemplate) []error {
|
|
|
|
if t == nil {
|
|
|
|
var err error
|
|
|
|
t, err = packer.NewConfigTemplate()
|
|
|
|
if err != nil {
|
|
|
|
return []error{err}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lists := map[string][]BlockDevice{
|
|
|
|
"ami_block_device_mappings": b.AMIMappings,
|
|
|
|
"launch_block_device_mappings": b.LaunchMappings,
|
|
|
|
}
|
|
|
|
|
|
|
|
var errs []error
|
|
|
|
for outer, bds := range lists {
|
2014-12-05 01:27:00 -05:00
|
|
|
for i := 0; i < len(bds); i++ {
|
2014-09-05 15:38:19 -04:00
|
|
|
templates := map[string]*string{
|
2014-12-05 01:27:00 -05:00
|
|
|
"device_name": &bds[i].DeviceName,
|
|
|
|
"snapshot_id": &bds[i].SnapshotId,
|
|
|
|
"virtual_name": &bds[i].VirtualName,
|
|
|
|
"volume_type": &bds[i].VolumeType,
|
2014-09-05 15:38:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
errs := make([]error, 0)
|
|
|
|
for n, ptr := range templates {
|
|
|
|
var err error
|
|
|
|
*ptr, err = t.Process(*ptr, nil)
|
|
|
|
if err != nil {
|
|
|
|
errs = append(
|
|
|
|
errs, fmt.Errorf(
|
|
|
|
"Error processing %s[%d].%s: %s",
|
|
|
|
outer, i, n, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errs) > 0 {
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-08-15 17:05:08 -04:00
|
|
|
func (b *BlockDevices) BuildAMIDevices() []ec2.BlockDeviceMapping {
|
|
|
|
return buildBlockDevices(b.AMIMappings)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BlockDevices) BuildLaunchDevices() []ec2.BlockDeviceMapping {
|
|
|
|
return buildBlockDevices(b.LaunchMappings)
|
|
|
|
}
|