Skip to content

Commit

Permalink
Display ssh std to streaming pipe
Browse files Browse the repository at this point in the history
  • Loading branch information
seokho-son committed Jun 11, 2024
1 parent 7a89073 commit f20a9dd
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 19 deletions.
6 changes: 3 additions & 3 deletions src/api/rest/server/middlewares/custom-middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ func ResponseBodyDump() echo.MiddlewareFunc {

// Get the request ID
reqID := c.Get("RequestID").(string)
log.Trace().Msgf("(BodyDump middleware) Request ID: %s", reqID)
//log.Trace().Msgf("(BodyDump middleware) Request ID: %s", reqID)

// Get the content type
contentType := c.Response().Header().Get(echo.HeaderContentType)
log.Trace().Msgf("contentType: %s", contentType)
//log.Trace().Msgf("contentType: %s", contentType)

// log.Debug().Msgf("Request body: %s", string(reqBody))
// log.Debug().Msgf("Response body: %s", string(resBody))
Expand All @@ -143,7 +143,7 @@ func ResponseBodyDump() echo.MiddlewareFunc {
if contentType == echo.MIMEApplicationJSONCharsetUTF8 || contentType == echo.MIMEApplicationJSON {
// Load or check the request by ID
if v, ok := common.RequestMap.Load(reqID); ok {
log.Trace().Msg("OK, common.RequestMap.Load(reqID)")
//log.Trace().Msg("OK, common.RequestMap.Load(reqID)")
details := v.(common.RequestDetails)
details.EndTime = time.Now()

Expand Down
4 changes: 2 additions & 2 deletions src/core/common/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func ExecuteHttpRequest[B any, T any](

return nil
} else {
log.Trace().Msg("Cache item expired!")
//log.Trace().Msg("Cache item expired!")
clientCache.Delete(requestKey)
}
}
Expand Down Expand Up @@ -223,7 +223,7 @@ func ExecuteHttpRequest[B any, T any](
log.Trace().Msg("Fesult is nil, not caching")
} else {
clientCache.Store(requestKey, CacheItem[T]{Response: *result, ExpiresAt: time.Now().Add(cacheDuration)})
log.Trace().Msg("Cached successfully!")
//log.Trace().Msg("Cached successfully!")
}
}

Expand Down
53 changes: 39 additions & 14 deletions src/core/mcis/remoteCommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import (
"bytes"
"encoding/json"
"fmt"
"io"
"net"
"os"
"sync"
"time"

Expand Down Expand Up @@ -510,32 +512,55 @@ func runSSH(bastionInfo sshInfo, targetInfo sshInfo, cmds []string) (map[int]str
client := ssh.NewClient(ncc, chans, reqs)
defer client.Close()

// Create a new SSH session
session, err := client.NewSession()
if err != nil {
return stdoutMap, stderrMap, err
}
defer session.Close()

// Run the commands
for i, cmd := range cmds {
// Create a new SSH session for each command
session, err := client.NewSession()
if err != nil {
return stdoutMap, stderrMap, err
}
defer session.Close()
defer session.Close() // Ensure session is closed

// Get pipes for stdout and stderr
stdoutPipe, err := session.StdoutPipe()
if err != nil {
return stdoutMap, stderrMap, err
}

stderrPipe, err := session.StderrPipe()
if err != nil {
return stdoutMap, stderrMap, err
}

// Capture the output
// Start the command
if err := session.Start(cmd); err != nil {
return stdoutMap, stderrMap, err
}

// Read stdout and stderr
var stdoutBuf, stderrBuf bytes.Buffer
session.Stdout = &stdoutBuf
session.Stderr = &stderrBuf
stdoutDone := make(chan struct{})
stderrDone := make(chan struct{})

go func() {
io.Copy(io.MultiWriter(os.Stdout, &stdoutBuf), stdoutPipe)
close(stdoutDone)
}()

go func() {
io.Copy(io.MultiWriter(os.Stderr, &stderrBuf), stderrPipe)
close(stderrDone)
}()

// Wait for the command to finish
err = session.Wait()
<-stdoutDone
<-stderrDone

// Run the command
err = session.Run(cmd)
if err != nil {
stderrMap[i] = fmt.Sprintf("(%s)\nStderr: %s", err, stderrBuf.String())
break // Stop if the command fails
stdoutMap[i] = stdoutBuf.String()
break
}

stdoutMap[i] = stdoutBuf.String()
Expand Down

0 comments on commit f20a9dd

Please sign in to comment.