amazon/ssm_driver: Update to support command context

* Defer the release of the found port to prevent possible Packer build collisions.
This commit is contained in:
Wilken Rivera 2020-04-24 09:05:13 -04:00
parent 5e14089851
commit e53d6aea66
2 changed files with 9 additions and 4 deletions

View File

@ -2,6 +2,7 @@ package common
import (
"bytes"
"context"
"fmt"
"log"
"os/exec"
@ -12,7 +13,8 @@ import (
const SessionManagerPluginName string = "session-manager-plugin"
type SSMDriver struct {
Ui packer.Ui
Ui packer.Ui
Ctx context.Context
// Provided for testing purposes; if not specified it defaults to SessionManagerPluginName
PluginName string
}
@ -40,7 +42,7 @@ func (s *SSMDriver) StartSession(sessionData, region, profile, params, endpoint
}
log.Printf("Attempting to start session with the following args: %v", args)
cmd := exec.Command(s.PluginName, args...)
cmd := exec.CommandContext(s.Ctx, s.PluginName, args...)
cmd.Stdout = &stdout
cmd.Stderr = &stderr

View File

@ -50,7 +50,6 @@ func (s *StepCreateSSMTunnel) Run(ctx context.Context, state multistep.StateBag)
"portNumber": []*string{aws.String(dst)},
"localPortNumber": []*string{aws.String(src)},
}
l.Close()
instance, ok := state.Get("instance").(*ec2.Instance)
if !ok {
@ -101,7 +100,11 @@ func (s *StepCreateSSMTunnel) Run(ctx context.Context, state multistep.StateBag)
return multistep.ActionHalt
}
driver := SSMDriver{Ui: ui}
// Stop listening on selected port so that the AWS session-manager-plugin can use it.
// The port is closed right before we start the session to avoid two Packer builds from getting the same port - fingers-crossed
l.Close()
driver := SSMDriver{Ui: ui, Ctx: ctx}
// sessionDetails, region, "StartSession", profile, paramJson, endpoint
region := aws.StringValue(s.AWSSession.Config.Region)
// how to best get Profile name