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 (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"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"`
|
|
|
|
|
2018-02-02 23:16:23 -05:00
|
|
|
RootDevice RootBlockDevice `mapstructure:"ami_root_device"`
|
|
|
|
VolumeRunTags awscommon.TagMap `mapstructure:"run_volume_tags"`
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !foundRootVolume {
|
|
|
|
errs = packer.MultiErrorAppend(errs, fmt.Errorf("no volume with name '%s' is found", b.config.RootDevice.SourceDeviceName))
|
|
|
|
}
|
|
|
|
|
2017-12-08 17:56:19 -05:00
|
|
|
if b.config.IsSpotInstance() && (b.config.AMIENASupport || b.config.AMISriovNetSupport) {
|
|
|
|
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."))
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (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
|
|
|
|
}
|
2017-03-01 19:43:09 -05:00
|
|
|
ec2conn := ec2.New(session)
|
2016-12-30 16:21:23 -05:00
|
|
|
|
2017-05-11 20:46:35 -04:00
|
|
|
// If the subnet is specified but not the VpcId or AZ, try to determine them automatically
|
|
|
|
if b.config.SubnetId != "" && (b.config.AvailabilityZone == "" || b.config.VpcId == "") {
|
|
|
|
log.Printf("[INFO] Finding AZ and VpcId for the given subnet '%s'", b.config.SubnetId)
|
2016-12-30 16:21:23 -05:00
|
|
|
resp, err := ec2conn.DescribeSubnets(&ec2.DescribeSubnetsInput{SubnetIds: []*string{&b.config.SubnetId}})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-05-11 20:46:35 -04:00
|
|
|
if b.config.AvailabilityZone == "" {
|
|
|
|
b.config.AvailabilityZone = *resp.Subnets[0].AvailabilityZone
|
|
|
|
log.Printf("[INFO] AvailabilityZone found: '%s'", b.config.AvailabilityZone)
|
|
|
|
}
|
|
|
|
if b.config.VpcId == "" {
|
|
|
|
b.config.VpcId = *resp.Subnets[0].VpcId
|
|
|
|
log.Printf("[INFO] VpcId found: '%s'", b.config.VpcId)
|
|
|
|
}
|
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)
|
|
|
|
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,
|
|
|
|
AvailabilityZone: b.config.AvailabilityZone,
|
|
|
|
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,
|
2017-10-12 17:33:01 -04:00
|
|
|
SpotTags: b.config.SpotTags,
|
2017-12-08 17:56:19 -05:00
|
|
|
SubnetId: b.config.SubnetId,
|
|
|
|
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,
|
|
|
|
AvailabilityZone: b.config.AvailabilityZone,
|
|
|
|
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,
|
|
|
|
SubnetId: b.config.SubnetId,
|
|
|
|
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
|
|
|
},
|
|
|
|
&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{
|
2018-08-24 18:56:44 -04:00
|
|
|
SecurityGroupIds: b.config.SecurityGroupIds,
|
|
|
|
CommConfig: &b.config.RunConfig.Comm,
|
|
|
|
VpcId: b.config.VpcId,
|
2017-10-12 20:05:31 -04:00
|
|
|
TemporarySGSourceCidr: b.config.TemporarySGSourceCidr,
|
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{
|
|
|
|
LaunchDevices: launchDevices,
|
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,
|
2016-12-30 16:21:23 -05:00
|
|
|
},
|
2017-02-27 08:51:38 -05:00
|
|
|
&awscommon.StepCreateEncryptedAMICopy{
|
|
|
|
KeyID: b.config.AMIKmsKeyId,
|
|
|
|
EncryptBootVolume: b.config.AMIEncryptBootVolume,
|
|
|
|
Name: b.config.AMIName,
|
|
|
|
},
|
|
|
|
&awscommon.StepAMIRegionCopy{
|
2017-06-01 12:28:17 -04:00
|
|
|
AccessConfig: &b.config.AccessConfig,
|
|
|
|
Regions: b.config.AMIRegions,
|
|
|
|
RegionKeyIds: b.config.AMIRegionKMSKeyIDs,
|
|
|
|
EncryptBootVolume: b.config.AMIEncryptBootVolume,
|
|
|
|
Name: b.config.AMIName,
|
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)
|
|
|
|
b.runner.Run(state)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Builder) Cancel() {
|
|
|
|
if b.runner != nil {
|
|
|
|
log.Println("Cancelling the step runner...")
|
|
|
|
b.runner.Cancel()
|
|
|
|
}
|
|
|
|
}
|