builder/amzon: Update port configuration for WinRM

Connectivity for WinRM works for the initial connection, but fails to upload any provisioning scripts - need to dive in deeper.
For now connectivity over SSH works as expected.
This commit is contained in:
Wilken Rivera 2020-04-01 17:33:44 -04:00
parent fbffbd1899
commit c1d2477d18
3 changed files with 28 additions and 18 deletions

View File

@ -90,9 +90,9 @@ func SSHHost(e ec2Describer, sshInterface string, host string) func(multistep.St
} }
} }
// SSHPort returns a function that can be given to the SSH communicator // Port returns a function that can be given to the communicator
// for determining the SSH port to use when connecting to an instance. // for determining the port to use when connecting to an instance.
func SSHPort(sshInterface string, port int) func(multistep.StateBag) (int, error) { func Port(sshInterface string, port int) func(multistep.StateBag) (int, error) {
return func(state multistep.StateBag) (int, error) { return func(state multistep.StateBag) (int, error) {
if sshInterface != "session_manager" { if sshInterface != "session_manager" {
return port, nil return port, nil

View File

@ -13,21 +13,23 @@ import (
"github.com/aws/aws-sdk-go/service/ssm" "github.com/aws/aws-sdk-go/service/ssm"
"github.com/hashicorp/packer/common/net" "github.com/hashicorp/packer/common/net"
"github.com/hashicorp/packer/common/retry" "github.com/hashicorp/packer/common/retry"
"github.com/hashicorp/packer/helper/communicator"
"github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer"
) )
type StepCreateSSMTunnel struct { type StepCreateSSMTunnel struct {
CommConfig *communicator.Config
AWSSession *session.Session AWSSession *session.Session
InstanceID string
DstPort int DstPort int
SSMAgentEnabled bool
instanceId string
ssmSession *ssm.StartSessionOutput ssmSession *ssm.StartSessionOutput
} }
func (s *StepCreateSSMTunnel) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { func (s *StepCreateSSMTunnel) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
if !s.SSMAgentEnabled {
return multistep.ActionContinue
}
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
// Find an available TCP port for our HTTP server // Find an available TCP port for our HTTP server
l, err := net.ListenRangeConfig{ l, err := net.ListenRangeConfig{
@ -58,26 +60,26 @@ func (s *StepCreateSSMTunnel) Run(ctx context.Context, state multistep.StateBag)
return multistep.ActionHalt return multistep.ActionHalt
} }
s.InstanceID = aws.StringValue(instance.InstanceId) s.instanceId = aws.StringValue(instance.InstanceId)
ssmconn := ssm.New(s.AWSSession) ssmconn := ssm.New(s.AWSSession)
input := ssm.StartSessionInput{ input := ssm.StartSessionInput{
DocumentName: aws.String("AWS-StartPortForwardingSession"), DocumentName: aws.String("AWS-StartPortForwardingSession"),
Parameters: params, Parameters: params,
Target: aws.String(s.InstanceID), Target: aws.String(s.instanceId),
} }
ui.Message(fmt.Sprintf("Starting PortForwarding session to instance %q on local port %q to remote port %q", s.instanceId, src, dst))
var output *ssm.StartSessionOutput var output *ssm.StartSessionOutput
err = retry.Config{ err = retry.Config{
Tries: 11,
ShouldRetry: func(err error) bool { return isAWSErr(err, "TargetNotConnected", "") }, ShouldRetry: func(err error) bool { return isAWSErr(err, "TargetNotConnected", "") },
RetryDelay: (&retry.Backoff{InitialBackoff: 200 * time.Millisecond, MaxBackoff: 30 * time.Second, Multiplier: 2}).Linear, RetryDelay: (&retry.Backoff{InitialBackoff: 200 * time.Millisecond, MaxBackoff: 60 * time.Second, Multiplier: 2}).Linear,
}.Run(ctx, func(ctx context.Context) error { }.Run(ctx, func(ctx context.Context) error {
output, err = ssmconn.StartSessionWithContext(ctx, &input) output, err = ssmconn.StartSessionWithContext(ctx, &input)
return err return err
}) })
if err != nil { if err != nil {
err = fmt.Errorf("error encountered in starting session for instance %q: %s", s.InstanceID, err) err = fmt.Errorf("error encountered in starting session for instance %q: %s", s.instanceId, err)
ui.Error(err.Error()) ui.Error(err.Error())
state.Put("error", err) state.Put("error", err)
return multistep.ActionHalt return multistep.ActionHalt
@ -110,6 +112,7 @@ func (s *StepCreateSSMTunnel) Run(ctx context.Context, state multistep.StateBag)
return multistep.ActionHalt return multistep.ActionHalt
} }
ui.Message(fmt.Sprintf("PortForwarding session to instance %q established!", s.instanceId))
state.Put("sessionPort", l.Port) state.Put("sessionPort", l.Port)
return multistep.ActionContinue return multistep.ActionContinue

View File

@ -258,16 +258,23 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
}, },
&awscommon.StepCreateSSMTunnel{ &awscommon.StepCreateSSMTunnel{
AWSSession: session, AWSSession: session,
DstPort: b.config.Comm.SSHPort, DstPort: b.config.Comm.Port(),
SSMAgentEnabled: b.config.SSHInterface == "session_manager",
}, },
&communicator.StepConnect{ &communicator.StepConnect{
// StepConnect is provided settings for WinRM and SSH, but
// the communicator will ultimately determine which port to use.
Config: &b.config.RunConfig.Comm, Config: &b.config.RunConfig.Comm,
Host: awscommon.SSHHost( Host: awscommon.SSHHost(
ec2conn, ec2conn,
b.config.SSHInterface, b.config.SSHInterface,
b.config.Comm.Host(), b.config.Comm.Host(),
), ),
SSHPort: awscommon.SSHPort( SSHPort: awscommon.Port(
b.config.SSHInterface,
b.config.Comm.Port(),
),
WinRMPort: awscommon.Port(
b.config.SSHInterface, b.config.SSHInterface,
b.config.Comm.Port(), b.config.Comm.Port(),
), ),