From c1d2477d189266f85706d29715c6e1c284efc318 Mon Sep 17 00:00:00 2001 From: Wilken Rivera Date: Wed, 1 Apr 2020 17:33:44 -0400 Subject: [PATCH] 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. --- builder/amazon/common/ssh.go | 6 ++--- .../amazon/common/step_create_ssm_tunnel.go | 27 ++++++++++--------- builder/amazon/ebs/builder.go | 13 ++++++--- 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/builder/amazon/common/ssh.go b/builder/amazon/common/ssh.go index 55b1a7883..436d126c1 100644 --- a/builder/amazon/common/ssh.go +++ b/builder/amazon/common/ssh.go @@ -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 -// for determining the SSH port to use when connecting to an instance. -func SSHPort(sshInterface string, port int) func(multistep.StateBag) (int, error) { +// Port returns a function that can be given to the communicator +// for determining the port to use when connecting to an instance. +func Port(sshInterface string, port int) func(multistep.StateBag) (int, error) { return func(state multistep.StateBag) (int, error) { if sshInterface != "session_manager" { return port, nil diff --git a/builder/amazon/common/step_create_ssm_tunnel.go b/builder/amazon/common/step_create_ssm_tunnel.go index 28eaa1c24..213cd0c2c 100644 --- a/builder/amazon/common/step_create_ssm_tunnel.go +++ b/builder/amazon/common/step_create_ssm_tunnel.go @@ -13,21 +13,23 @@ import ( "github.com/aws/aws-sdk-go/service/ssm" "github.com/hashicorp/packer/common/net" "github.com/hashicorp/packer/common/retry" - "github.com/hashicorp/packer/helper/communicator" "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" ) type StepCreateSSMTunnel struct { - CommConfig *communicator.Config - AWSSession *session.Session - InstanceID string - DstPort int - - ssmSession *ssm.StartSessionOutput + AWSSession *session.Session + DstPort int + SSMAgentEnabled bool + instanceId string + ssmSession *ssm.StartSessionOutput } func (s *StepCreateSSMTunnel) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { + if !s.SSMAgentEnabled { + return multistep.ActionContinue + } + ui := state.Get("ui").(packer.Ui) // Find an available TCP port for our HTTP server l, err := net.ListenRangeConfig{ @@ -58,26 +60,26 @@ func (s *StepCreateSSMTunnel) Run(ctx context.Context, state multistep.StateBag) return multistep.ActionHalt } - s.InstanceID = aws.StringValue(instance.InstanceId) + s.instanceId = aws.StringValue(instance.InstanceId) ssmconn := ssm.New(s.AWSSession) input := ssm.StartSessionInput{ DocumentName: aws.String("AWS-StartPortForwardingSession"), 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 err = retry.Config{ - Tries: 11, 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 { output, err = ssmconn.StartSessionWithContext(ctx, &input) return err }) 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()) state.Put("error", err) return multistep.ActionHalt @@ -110,6 +112,7 @@ func (s *StepCreateSSMTunnel) Run(ctx context.Context, state multistep.StateBag) return multistep.ActionHalt } + ui.Message(fmt.Sprintf("PortForwarding session to instance %q established!", s.instanceId)) state.Put("sessionPort", l.Port) return multistep.ActionContinue diff --git a/builder/amazon/ebs/builder.go b/builder/amazon/ebs/builder.go index ac56da0ac..b8aafd4c8 100644 --- a/builder/amazon/ebs/builder.go +++ b/builder/amazon/ebs/builder.go @@ -257,17 +257,24 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack BuildName: b.config.PackerBuildName, }, &awscommon.StepCreateSSMTunnel{ - AWSSession: session, - DstPort: b.config.Comm.SSHPort, + AWSSession: session, + DstPort: b.config.Comm.Port(), + SSMAgentEnabled: b.config.SSHInterface == "session_manager", }, &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, Host: awscommon.SSHHost( ec2conn, b.config.SSHInterface, 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.Comm.Port(), ),