2016-10-31 16:44:41 -05:00
|
|
|
package common
|
2014-06-04 14:58:11 -07:00
|
|
|
|
|
|
|
|
import (
|
2018-01-22 15:32:33 -08:00
|
|
|
"context"
|
2014-06-04 14:58:11 -07:00
|
|
|
"fmt"
|
2018-10-17 14:34:36 +01:00
|
|
|
"strings"
|
2014-06-04 14:58:11 -07:00
|
|
|
|
2015-06-03 17:13:52 -04:00
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2019-08-22 13:17:35 -07:00
|
|
|
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
|
2020-12-17 13:29:25 -08:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
|
|
|
|
confighelper "github.com/hashicorp/packer-plugin-sdk/template/config"
|
2014-06-04 14:58:11 -07:00
|
|
|
)
|
|
|
|
|
|
2016-10-31 16:44:41 -05:00
|
|
|
type StepModifyEBSBackedInstance struct {
|
2019-11-21 11:13:32 +01:00
|
|
|
Skip bool
|
2019-08-22 13:17:35 -07:00
|
|
|
EnableAMIENASupport confighelper.Trilean
|
2017-08-28 09:18:23 -07:00
|
|
|
EnableAMISriovNetSupport bool
|
2016-10-31 16:44:41 -05:00
|
|
|
}
|
2014-06-04 14:58:11 -07:00
|
|
|
|
2019-03-29 16:50:02 +01:00
|
|
|
func (s *StepModifyEBSBackedInstance) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2019-08-22 13:17:35 -07:00
|
|
|
ec2conn := state.Get("ec2").(ec2iface.EC2API)
|
2014-06-04 14:58:11 -07:00
|
|
|
instance := state.Get("instance").(*ec2.Instance)
|
2020-11-19 11:54:31 -08:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2014-06-04 14:58:11 -07:00
|
|
|
|
2019-11-21 11:13:32 +01:00
|
|
|
// Skip when it is a spot instance
|
|
|
|
|
if s.Skip {
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-25 09:14:12 -07:00
|
|
|
// Set SriovNetSupport to "simple". See http://goo.gl/icuXh5
|
|
|
|
|
// As of February 2017, this applies to C3, C4, D2, I2, R3, and M4 (excluding m4.16xlarge)
|
2017-08-28 09:18:23 -07:00
|
|
|
if s.EnableAMISriovNetSupport {
|
2017-02-21 17:46:16 -08:00
|
|
|
ui.Say("Enabling Enhanced Networking (SR-IOV)...")
|
2015-04-05 17:58:48 -04:00
|
|
|
simple := "simple"
|
2017-02-26 19:33:34 -08:00
|
|
|
_, err := ec2conn.ModifyInstanceAttribute(&ec2.ModifyInstanceAttributeInput{
|
2015-08-17 17:44:01 -07:00
|
|
|
InstanceId: instance.InstanceId,
|
|
|
|
|
SriovNetSupport: &ec2.AttributeValue{Value: &simple},
|
2015-04-05 17:58:48 -04:00
|
|
|
})
|
2017-02-26 19:33:34 -08:00
|
|
|
if err != nil {
|
|
|
|
|
err := fmt.Errorf("Error enabling Enhanced Networking (SR-IOV) on %s: %s", *instance.InstanceId, err)
|
2017-02-21 17:46:16 -08:00
|
|
|
state.Put("error", err)
|
|
|
|
|
ui.Error(err.Error())
|
|
|
|
|
return multistep.ActionHalt
|
|
|
|
|
}
|
2017-08-25 09:14:12 -07:00
|
|
|
}
|
2017-02-21 17:46:16 -08:00
|
|
|
|
2018-10-17 14:34:36 +01:00
|
|
|
// Handle EnaSupport flag.
|
2017-08-25 09:14:12 -07:00
|
|
|
// As of February 2017, this applies to C5, I3, P2, R4, X1, and m4.16xlarge
|
2019-08-22 13:17:35 -07:00
|
|
|
if s.EnableAMIENASupport != confighelper.TriUnset {
|
2018-10-19 12:21:00 +01:00
|
|
|
var prefix string
|
2019-08-22 13:17:35 -07:00
|
|
|
if s.EnableAMIENASupport.True() {
|
2018-10-19 12:21:00 +01:00
|
|
|
prefix = "En"
|
|
|
|
|
} else {
|
|
|
|
|
prefix = "Dis"
|
|
|
|
|
}
|
|
|
|
|
ui.Say(fmt.Sprintf("%sabling Enhanced Networking (ENA)...", prefix))
|
|
|
|
|
_, err := ec2conn.ModifyInstanceAttribute(&ec2.ModifyInstanceAttributeInput{
|
|
|
|
|
InstanceId: instance.InstanceId,
|
2019-08-22 13:17:35 -07:00
|
|
|
EnaSupport: &ec2.AttributeBooleanValue{Value: s.EnableAMIENASupport.ToBoolPointer()},
|
2018-10-19 12:21:00 +01:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
err := fmt.Errorf("Error %sabling Enhanced Networking (ENA) on %s: %s", strings.ToLower(prefix), *instance.InstanceId, err)
|
|
|
|
|
state.Put("error", err)
|
|
|
|
|
ui.Error(err.Error())
|
|
|
|
|
return multistep.ActionHalt
|
|
|
|
|
}
|
2014-06-04 14:58:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-31 16:44:41 -05:00
|
|
|
func (s *StepModifyEBSBackedInstance) Cleanup(state multistep.StateBag) {
|
2014-06-04 14:58:11 -07:00
|
|
|
// No cleanup...
|
|
|
|
|
}
|