Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed work ssm-plugin in non-interactive env #542

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions pkg/ssmsession/ssmsession.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package ssmsession

import (
"bytes"
"encoding/json"
"fmt"
"log"
"os"
"os/exec"
"strings"
"syscall"

"github.com/Netflix/go-expect"
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/hazelops/ize/pkg/term"
)
Expand Down Expand Up @@ -34,16 +38,33 @@ func NewSSMPluginCommand(region string) SSMPluginCommand {
}

func (s SSMPluginCommand) Start(ssmSession *ecs.Session) error {
var output bytes.Buffer
response, err := json.Marshal(ssmSession)
if err != nil {
return fmt.Errorf("marshal session response: %w", err)
}

c, err := expect.NewConsole(expect.WithStdout(&output), expect.WithStdin(os.Stdin))
if err != nil {
log.Fatal(err)
}
defer c.Close()

cmd := exec.Command(ssmPluginBinaryName, []string{string(response), s.region, startSessionAction}...)
out, _, _, err := s.runner.Run(cmd)
cmd.Stdin = c.Tty()
cmd.Stdout = c.Tty()
cmd.Stderr = c.Tty()

go func() {
c.ExpectEOF()
}()

_, _, _, err = s.Run(cmd)
if err != nil {
return fmt.Errorf("start session: %w", err)
}
if strings.Contains(out, "ERROR") {
fmt.Println(strings.TrimSpace(output.String()))
if strings.Contains(output.String(), "ERROR") {
return fmt.Errorf("exit status: 1")
}

Expand All @@ -63,3 +84,22 @@ func (s SSMPluginCommand) StartInteractive(ssmSession *ecs.Session) error {

return nil
}

func (s SSMPluginCommand) Run(cmd *exec.Cmd) (stdout, stderr string, exitCode int, err error) {

if err = cmd.Start(); err != nil {
return
}

err = cmd.Wait()

if err != nil {
if err2, ok := err.(*exec.ExitError); ok {
if s, ok := err2.Sys().(syscall.WaitStatus); ok {
err = nil
exitCode = s.ExitStatus()
}
}
}
return
}
4 changes: 2 additions & 2 deletions tests/e2e/ecs_apps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ func TestIzeExecGoblin(t *testing.T) {

ize := NewBinary(t, izeBinary, examplesRootDir)

stdout, stderr, err := ize.RunPty("--plain-text-output", "exec", "goblin", "--", "sh -c \"echo $APP_NAME\"")
stdout, stderr, err := ize.RunRaw("--plain-text-output", "exec", "goblin", "--", "sh -c \"echo $APP_NAME\"")

if err != nil {
t.Errorf("error: %s", err)
Expand Down Expand Up @@ -379,7 +379,7 @@ func TestIzeExecSquibby(t *testing.T) {

ize := NewBinary(t, izeBinary, examplesRootDir)

stdout, stderr, err := ize.RunPty("--plain-text-output", "exec", "squibby", "--", "sh -c \"echo $APP_NAME\"")
stdout, stderr, err := ize.RunRaw("--plain-text-output", "exec", "squibby", "--", "sh -c \"echo $APP_NAME\"")

if err != nil {
t.Errorf("error: %s", err)
Expand Down