2019-05-31 08:27:41 -04:00
|
|
|
//go:generate struct-markdown
|
|
|
|
|
2016-12-30 16:21:23 -05:00
|
|
|
// The ebssurrogate package contains a packer.Builder implementation that
|
|
|
|
// builds a new EBS-backed AMI using an ephemeral instance.
|
|
|
|
package ebssurrogate
|
|
|
|
|
|
|
|
import (
|
2019-03-22 09:53:28 -04:00
|
|
|
"context"
|
2016-12-30 16:21:23 -05:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2017-04-04 16:39:01 -04:00
|
|
|
awscommon "github.com/hashicorp/packer/builder/amazon/common"
|
|
|
|
"github.com/hashicorp/packer/common"
|
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
|
|
|
"github.com/hashicorp/packer/helper/config"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
2016-12-30 16:21:23 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const BuilderId = "mitchellh.amazon.ebssurrogate"
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
|
|
|
awscommon.AccessConfig `mapstructure:",squash"`
|
|
|
|
awscommon.RunConfig `mapstructure:",squash"`
|
|
|
|
awscommon.BlockDevices `mapstructure:",squash"`
|
|
|
|
awscommon.AMIConfig `mapstructure:",squash"`
|
2019-06-06 10:45:37 -04:00
|
|
|
// A block device mapping describing the root device of the AMI. This looks
|
|
|
|
// like the mappings in `ami_block_device_mapping`, except with an
|
|
|
|
// additional field:
|
|
|
|
//
|
|
|
|
// - `source_device_name` (string) - The device name of the block device on
|
|
|
|
// the source instance to be used as the root device for the AMI. This
|
|
|
|
// must correspond to a block device in `launch_block_device_mapping`.
|
|
|
|
RootDevice RootBlockDevice `mapstructure:"ami_root_device" required:"true"`
|
|
|
|
// Tags to apply to the volumes that are *launched* to create the AMI.
|
|
|
|
// These tags are *not* applied to the resulting AMI unless they're
|
|
|
|
// duplicated in `tags`. This is a [template
|
|
|
|
// engine](/docs/templates/engine.html), see [Build template
|
|
|
|
// data](#build-template-data) for more information.
|
2018-02-02 23:16:23 -05:00
|
|
|
VolumeRunTags awscommon.TagMap `mapstructure:"run_volume_tags"`
|
2019-05-28 11:50:58 -04:00
|
|
|
// what architecture to use when registering the
|
2019-06-06 10:29:25 -04:00
|
|
|
// final AMI; valid options are "x86_64" or "arm64". Defaults to "x86_64".
|
|
|
|
Architecture string `mapstructure:"ami_architecture" required:"false"`
|
2016-12-30 16:21:23 -05:00
|
|
|
|
|
|
|
ctx interpolate.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
type Builder struct {
|
|
|
|
config Config
|
|
|
|
runner multistep.Runner
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
|
|
|
|
b.config.ctx.Funcs = awscommon.TemplateFuncs
|
|
|
|
err := config.Decode(&b.config, &config.DecodeOpts{
|
|
|
|
Interpolate: true,
|
|
|
|
InterpolateContext: &b.config.ctx,
|
|
|
|
InterpolateFilter: &interpolate.RenderFilter{
|
|
|
|
Exclude: []string{
|
|
|
|
"ami_description",
|
|
|
|
"run_tags",
|
2017-02-27 08:51:38 -05:00
|
|
|
"run_volume_tags",
|
|
|
|
"snapshot_tags",
|
2017-10-12 17:33:01 -04:00
|
|
|
"spot_tags",
|
2016-12-30 16:21:23 -05:00
|
|
|
"tags",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, raws...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-03-09 17:24:49 -05:00
|
|
|
if b.config.PackerConfig.PackerForce {
|
|
|
|
b.config.AMIForceDeregister = true
|
|
|
|
}
|
|
|
|
|
2016-12-30 16:21:23 -05:00
|
|
|
// Accumulate any errors
|
|
|
|
var errs *packer.MultiError
|
|
|
|
errs = packer.MultiErrorAppend(errs, b.config.AccessConfig.Prepare(&b.config.ctx)...)
|
|
|
|
errs = packer.MultiErrorAppend(errs, b.config.RunConfig.Prepare(&b.config.ctx)...)
|
2017-10-30 17:17:19 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
b.config.AMIConfig.Prepare(&b.config.AccessConfig, &b.config.ctx)...)
|
2016-12-30 16:21:23 -05:00
|
|
|
errs = packer.MultiErrorAppend(errs, b.config.BlockDevices.Prepare(&b.config.ctx)...)
|
|
|
|
errs = packer.MultiErrorAppend(errs, b.config.RootDevice.Prepare(&b.config.ctx)...)
|
|
|
|
|
|
|
|
if b.config.AMIVirtType == "" {
|
|
|
|
errs = packer.MultiErrorAppend(errs, errors.New("ami_virtualization_type is required."))
|
|
|
|
}
|
|
|
|
|
|
|
|
foundRootVolume := false
|
|
|
|
for _, launchDevice := range b.config.BlockDevices.LaunchMappings {
|
|
|
|
if launchDevice.DeviceName == b.config.RootDevice.SourceDeviceName {
|
|
|
|
foundRootVolume = true
|
2019-05-03 12:39:52 -04:00
|
|
|
if launchDevice.OmitFromArtifact {
|
|
|
|
errs = packer.MultiErrorAppend(errs, fmt.Errorf("You cannot set \"omit_from_artifact\": \"true\" for the root volume."))
|
|
|
|
}
|
2016-12-30 16:21:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !foundRootVolume {
|
|
|
|
errs = packer.MultiErrorAppend(errs, fmt.Errorf("no volume with name '%s' is found", b.config.RootDevice.SourceDeviceName))
|
|
|
|
}
|
|
|
|
|
2018-10-19 07:21:00 -04:00
|
|
|
if b.config.IsSpotInstance() && ((b.config.AMIENASupport != nil && *b.config.AMIENASupport) || b.config.AMISriovNetSupport) {
|
2017-12-08 17:56:19 -05:00
|
|
|
errs = packer.MultiErrorAppend(errs,
|
|
|
|
fmt.Errorf("Spot instances do not support modification, which is required "+
|
|
|
|
"when either `ena_support` or `sriov_support` are set. Please ensure "+
|
|
|
|
"you use an AMI that already has either SR-IOV or ENA enabled."))
|
|
|
|
}
|
|
|
|
|
2019-05-07 19:38:55 -04:00
|
|
|
if b.config.Architecture == "" {
|
2019-05-07 19:44:29 -04:00
|
|
|
b.config.Architecture = "x86_64"
|
2019-05-07 19:38:55 -04:00
|
|
|
}
|
|
|
|
valid := false
|
2019-05-07 19:44:29 -04:00
|
|
|
for _, validArch := range []string{"x86_64", "arm64"} {
|
2019-05-07 19:38:55 -04:00
|
|
|
if validArch == b.config.Architecture {
|
2019-05-07 19:44:29 -04:00
|
|
|
valid = true
|
2019-05-07 19:38:55 -04:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !valid {
|
|
|
|
errs = packer.MultiErrorAppend(errs, errors.New(`The only valid ami_architecture values are "x86_64" and "arm64"`))
|
|
|
|
}
|
2016-12-30 16:21:23 -05:00
|
|
|
if errs != nil && len(errs.Errors) > 0 {
|
|
|
|
return nil, errs
|
|
|
|
}
|
|
|
|
|
2018-08-10 17:25:14 -04:00
|
|
|
packer.LogSecretFilter.Set(b.config.AccessKey, b.config.SecretKey, b.config.Token)
|
2016-12-30 16:21:23 -05:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-03-22 09:53:28 -04:00
|
|
|
func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
|
2017-03-01 19:43:09 -05:00
|
|
|
session, err := b.config.Session()
|
2016-12-30 16:21:23 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-04 19:22:52 -05:00
|
|
|
ec2conn := ec2.New(session)
|
2016-12-30 16:21:23 -05:00
|
|
|
|
|
|
|
// Setup the state bag and initial state for the steps
|
|
|
|
state := new(multistep.BasicStateBag)
|
|
|
|
state.Put("config", &b.config)
|
2019-02-05 17:07:04 -05:00
|
|
|
state.Put("access_config", &b.config.AccessConfig)
|
|
|
|
state.Put("ami_config", &b.config.AMIConfig)
|
2016-12-30 16:21:23 -05:00
|
|
|
state.Put("ec2", ec2conn)
|
2017-12-19 14:04:17 -05:00
|
|
|
state.Put("awsSession", session)
|
2016-12-30 16:21:23 -05:00
|
|
|
state.Put("hook", hook)
|
|
|
|
state.Put("ui", ui)
|
|
|
|
|
2017-10-04 05:29:38 -04:00
|
|
|
var instanceStep multistep.Step
|
|
|
|
|
2017-12-08 17:56:19 -05:00
|
|
|
if b.config.IsSpotInstance() {
|
2017-10-23 15:16:12 -04:00
|
|
|
instanceStep = &awscommon.StepRunSpotInstance{
|
2017-12-08 17:56:19 -05:00
|
|
|
AssociatePublicIpAddress: b.config.AssociatePublicIpAddress,
|
|
|
|
BlockDevices: b.config.BlockDevices,
|
2018-09-03 02:33:58 -04:00
|
|
|
BlockDurationMinutes: b.config.BlockDurationMinutes,
|
2017-12-08 17:56:19 -05:00
|
|
|
Ctx: b.config.ctx,
|
2018-09-11 21:20:14 -04:00
|
|
|
Comm: &b.config.RunConfig.Comm,
|
2017-12-08 17:56:19 -05:00
|
|
|
Debug: b.config.PackerDebug,
|
|
|
|
EbsOptimized: b.config.EbsOptimized,
|
|
|
|
ExpectedRootDevice: "ebs",
|
|
|
|
IamInstanceProfile: b.config.IamInstanceProfile,
|
2017-10-04 05:29:38 -04:00
|
|
|
InstanceInitiatedShutdownBehavior: b.config.InstanceInitiatedShutdownBehavior,
|
2017-12-08 17:56:19 -05:00
|
|
|
InstanceType: b.config.InstanceType,
|
|
|
|
SourceAMI: b.config.SourceAmi,
|
|
|
|
SpotPrice: b.config.SpotPrice,
|
|
|
|
SpotPriceProduct: b.config.SpotPriceAutoProduct,
|
2019-05-22 13:16:42 -04:00
|
|
|
SpotInstanceTypes: b.config.SpotInstanceTypes,
|
2017-10-12 17:33:01 -04:00
|
|
|
SpotTags: b.config.SpotTags,
|
2017-12-08 17:56:19 -05:00
|
|
|
Tags: b.config.RunTags,
|
|
|
|
UserData: b.config.UserData,
|
|
|
|
UserDataFile: b.config.UserDataFile,
|
|
|
|
VolumeTags: b.config.VolumeRunTags,
|
2017-10-04 05:29:38 -04:00
|
|
|
}
|
|
|
|
} else {
|
2017-10-23 15:16:12 -04:00
|
|
|
instanceStep = &awscommon.StepRunSourceInstance{
|
2017-12-08 17:56:19 -05:00
|
|
|
AssociatePublicIpAddress: b.config.AssociatePublicIpAddress,
|
|
|
|
BlockDevices: b.config.BlockDevices,
|
2018-08-30 06:50:29 -04:00
|
|
|
Comm: &b.config.RunConfig.Comm,
|
2017-12-08 17:56:19 -05:00
|
|
|
Ctx: b.config.ctx,
|
|
|
|
Debug: b.config.PackerDebug,
|
|
|
|
EbsOptimized: b.config.EbsOptimized,
|
2018-05-13 12:16:10 -04:00
|
|
|
EnableT2Unlimited: b.config.EnableT2Unlimited,
|
2017-12-08 17:56:19 -05:00
|
|
|
ExpectedRootDevice: "ebs",
|
|
|
|
IamInstanceProfile: b.config.IamInstanceProfile,
|
2017-10-04 05:29:38 -04:00
|
|
|
InstanceInitiatedShutdownBehavior: b.config.InstanceInitiatedShutdownBehavior,
|
2017-12-08 17:56:19 -05:00
|
|
|
InstanceType: b.config.InstanceType,
|
2018-02-02 23:16:23 -05:00
|
|
|
IsRestricted: b.config.IsChinaCloud() || b.config.IsGovCloud(),
|
2017-12-08 17:56:19 -05:00
|
|
|
SourceAMI: b.config.SourceAmi,
|
|
|
|
Tags: b.config.RunTags,
|
|
|
|
UserData: b.config.UserData,
|
|
|
|
UserDataFile: b.config.UserDataFile,
|
|
|
|
VolumeTags: b.config.VolumeRunTags,
|
2017-10-04 05:29:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-25 19:25:53 -04:00
|
|
|
amiDevices := b.config.BuildAMIDevices()
|
|
|
|
launchDevices := b.config.BuildLaunchDevices()
|
|
|
|
|
2016-12-30 16:21:23 -05:00
|
|
|
// Build the steps
|
|
|
|
steps := []multistep.Step{
|
2017-02-27 08:51:38 -05:00
|
|
|
&awscommon.StepPreValidate{
|
|
|
|
DestAmiName: b.config.AMIName,
|
|
|
|
ForceDeregister: b.config.AMIForceDeregister,
|
|
|
|
},
|
2016-12-30 16:21:23 -05:00
|
|
|
&awscommon.StepSourceAMIInfo{
|
2017-08-28 12:18:23 -04:00
|
|
|
SourceAmi: b.config.SourceAmi,
|
|
|
|
EnableAMISriovNetSupport: b.config.AMISriovNetSupport,
|
|
|
|
EnableAMIENASupport: b.config.AMIENASupport,
|
|
|
|
AmiFilters: b.config.SourceAmiFilter,
|
2018-09-04 21:13:18 -04:00
|
|
|
AMIVirtType: b.config.AMIVirtType,
|
2016-12-30 16:21:23 -05:00
|
|
|
},
|
2018-08-14 06:04:13 -04:00
|
|
|
&awscommon.StepNetworkInfo{
|
|
|
|
VpcId: b.config.VpcId,
|
|
|
|
VpcFilter: b.config.VpcFilter,
|
|
|
|
SecurityGroupIds: b.config.SecurityGroupIds,
|
|
|
|
SecurityGroupFilter: b.config.SecurityGroupFilter,
|
|
|
|
SubnetId: b.config.SubnetId,
|
|
|
|
SubnetFilter: b.config.SubnetFilter,
|
|
|
|
AvailabilityZone: b.config.AvailabilityZone,
|
|
|
|
},
|
2016-12-30 16:21:23 -05:00
|
|
|
&awscommon.StepKeyPair{
|
2018-08-28 11:47:02 -04:00
|
|
|
Debug: b.config.PackerDebug,
|
|
|
|
Comm: &b.config.RunConfig.Comm,
|
|
|
|
DebugKeyPath: fmt.Sprintf("ec2_%s.pem", b.config.PackerBuildName),
|
2016-12-30 16:21:23 -05:00
|
|
|
},
|
|
|
|
&awscommon.StepSecurityGroup{
|
2019-03-30 18:47:03 -04:00
|
|
|
SecurityGroupFilter: b.config.SecurityGroupFilter,
|
|
|
|
SecurityGroupIds: b.config.SecurityGroupIds,
|
|
|
|
CommConfig: &b.config.RunConfig.Comm,
|
|
|
|
TemporarySGSourceCidrs: b.config.TemporarySGSourceCidrs,
|
2016-12-30 16:21:23 -05:00
|
|
|
},
|
2018-07-26 02:30:51 -04:00
|
|
|
&awscommon.StepCleanupVolumes{
|
|
|
|
BlockDevices: b.config.BlockDevices,
|
|
|
|
},
|
2017-10-04 05:29:38 -04:00
|
|
|
instanceStep,
|
2016-12-30 16:21:23 -05:00
|
|
|
&awscommon.StepGetPassword{
|
2018-04-16 14:51:04 -04:00
|
|
|
Debug: b.config.PackerDebug,
|
|
|
|
Comm: &b.config.RunConfig.Comm,
|
|
|
|
Timeout: b.config.WindowsPasswordTimeout,
|
|
|
|
BuildName: b.config.PackerBuildName,
|
2016-12-30 16:21:23 -05:00
|
|
|
},
|
|
|
|
&communicator.StepConnect{
|
|
|
|
Config: &b.config.RunConfig.Comm,
|
|
|
|
Host: awscommon.SSHHost(
|
|
|
|
ec2conn,
|
2018-08-29 08:28:09 -04:00
|
|
|
b.config.Comm.SSHInterface),
|
2018-08-22 11:02:23 -04:00
|
|
|
SSHConfig: b.config.RunConfig.Comm.SSHConfigFunc(),
|
2016-12-30 16:21:23 -05:00
|
|
|
},
|
|
|
|
&common.StepProvision{},
|
2018-09-14 14:03:23 -04:00
|
|
|
&common.StepCleanupTempKeys{
|
|
|
|
Comm: &b.config.RunConfig.Comm,
|
|
|
|
},
|
2016-12-30 16:21:23 -05:00
|
|
|
&awscommon.StepStopEBSBackedInstance{
|
2017-12-08 17:56:19 -05:00
|
|
|
Skip: b.config.IsSpotInstance(),
|
2016-12-30 16:21:23 -05:00
|
|
|
DisableStopInstance: b.config.DisableStopInstance,
|
|
|
|
},
|
|
|
|
&awscommon.StepModifyEBSBackedInstance{
|
2017-08-28 12:18:23 -04:00
|
|
|
EnableAMISriovNetSupport: b.config.AMISriovNetSupport,
|
|
|
|
EnableAMIENASupport: b.config.AMIENASupport,
|
2016-12-30 16:21:23 -05:00
|
|
|
},
|
2018-03-25 19:25:53 -04:00
|
|
|
&StepSnapshotVolumes{
|
2019-05-03 12:39:52 -04:00
|
|
|
LaunchDevices: launchDevices,
|
|
|
|
SnapshotOmitMap: b.config.GetOmissions(),
|
2016-12-30 16:21:23 -05:00
|
|
|
},
|
2017-02-27 08:51:38 -05:00
|
|
|
&awscommon.StepDeregisterAMI{
|
2017-08-14 12:20:08 -04:00
|
|
|
AccessConfig: &b.config.AccessConfig,
|
2017-02-27 08:51:38 -05:00
|
|
|
ForceDeregister: b.config.AMIForceDeregister,
|
|
|
|
ForceDeleteSnapshot: b.config.AMIForceDeleteSnapshot,
|
|
|
|
AMIName: b.config.AMIName,
|
2017-08-14 12:20:08 -04:00
|
|
|
Regions: b.config.AMIRegions,
|
2017-02-27 08:51:38 -05:00
|
|
|
},
|
2016-12-30 16:21:23 -05:00
|
|
|
&StepRegisterAMI{
|
2017-08-28 12:18:23 -04:00
|
|
|
RootDevice: b.config.RootDevice,
|
2018-03-25 19:25:53 -04:00
|
|
|
AMIDevices: amiDevices,
|
|
|
|
LaunchDevices: launchDevices,
|
2017-08-28 12:18:23 -04:00
|
|
|
EnableAMISriovNetSupport: b.config.AMISriovNetSupport,
|
|
|
|
EnableAMIENASupport: b.config.AMIENASupport,
|
2019-05-07 19:38:55 -04:00
|
|
|
Architecture: b.config.Architecture,
|
2019-05-03 12:39:52 -04:00
|
|
|
LaunchOmitMap: b.config.GetOmissions(),
|
2016-12-30 16:21:23 -05:00
|
|
|
},
|
2017-02-27 08:51:38 -05:00
|
|
|
&awscommon.StepAMIRegionCopy{
|
2017-06-01 12:28:17 -04:00
|
|
|
AccessConfig: &b.config.AccessConfig,
|
|
|
|
Regions: b.config.AMIRegions,
|
2019-04-15 19:44:56 -04:00
|
|
|
AMIKmsKeyId: b.config.AMIKmsKeyId,
|
2017-06-01 12:28:17 -04:00
|
|
|
RegionKeyIds: b.config.AMIRegionKMSKeyIDs,
|
|
|
|
EncryptBootVolume: b.config.AMIEncryptBootVolume,
|
|
|
|
Name: b.config.AMIName,
|
2019-05-03 17:47:09 -04:00
|
|
|
OriginalRegion: *ec2conn.Config.Region,
|
2017-02-27 08:51:38 -05:00
|
|
|
},
|
|
|
|
&awscommon.StepModifyAMIAttributes{
|
|
|
|
Description: b.config.AMIDescription,
|
|
|
|
Users: b.config.AMIUsers,
|
|
|
|
Groups: b.config.AMIGroups,
|
|
|
|
ProductCodes: b.config.AMIProductCodes,
|
|
|
|
SnapshotUsers: b.config.SnapshotUsers,
|
|
|
|
SnapshotGroups: b.config.SnapshotGroups,
|
|
|
|
Ctx: b.config.ctx,
|
|
|
|
},
|
|
|
|
&awscommon.StepCreateTags{
|
|
|
|
Tags: b.config.AMITags,
|
|
|
|
SnapshotTags: b.config.SnapshotTags,
|
|
|
|
Ctx: b.config.ctx,
|
|
|
|
},
|
2016-12-30 16:21:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run!
|
|
|
|
b.runner = common.NewRunner(steps, b.config.PackerConfig, ui)
|
2019-03-22 09:53:28 -04:00
|
|
|
b.runner.Run(ctx, state)
|
2016-12-30 16:21:23 -05:00
|
|
|
|
|
|
|
// If there was an error, return that
|
|
|
|
if rawErr, ok := state.GetOk("error"); ok {
|
|
|
|
return nil, rawErr.(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
if amis, ok := state.GetOk("amis"); ok {
|
|
|
|
// Build the artifact and return it
|
|
|
|
artifact := &awscommon.Artifact{
|
|
|
|
Amis: amis.(map[string]string),
|
|
|
|
BuilderIdValue: BuilderId,
|
2017-12-19 14:04:17 -05:00
|
|
|
Session: session,
|
2016-12-30 16:21:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return artifact, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|