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:
parent
fbffbd1899
commit
c1d2477d18
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
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
|
||||
|
|
|
@ -258,16 +258,23 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
|
|||
},
|
||||
&awscommon.StepCreateSSMTunnel{
|
||||
AWSSession: session,
|
||||
DstPort: b.config.Comm.SSHPort,
|
||||
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(),
|
||||
),
|
||||
|
|
Loading…
Reference in New Issue