Skip to content

Commit 36a1e84

Browse files
committed
[supervisor] Forward terminal output to stdout
1 parent a147fa7 commit 36a1e84

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

Diff for: components/docker-up/docker-up/main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ func main() {
7373
cmd = args[0]
7474
}
7575

76-
if _, err := os.Stat(dockerSocketFN); err == nil || !os.IsNotExist(err) {
76+
listenFD := os.Getenv("LISTEN_FDS") != ""
77+
if _, err := os.Stat(dockerSocketFN); !listenFD && (err == nil || !os.IsNotExist(err)) {
7778
logger.Fatalf("Docker socket already exists at %s.\nIn a Gitpod workspace Docker will start automatically when used.\nIf all else fails, please remove %s and try again.", dockerSocketFN, dockerSocketFN)
7879
}
7980

Diff for: components/supervisor/pkg/supervisor/supervisor.go

+1
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,7 @@ func socketActivationForDocker(ctx context.Context, wg *sync.WaitGroup, term *te
795795
Annotations: map[string]string{
796796
"supervisor": "true",
797797
},
798+
LogToStdout: true,
798799
})
799800
return err
800801
})

Diff for: components/supervisor/pkg/terminal/terminal.go

+20-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717

1818
"github.com/creack/pty"
1919
"github.com/google/uuid"
20+
"github.com/sirupsen/logrus"
2021
"golang.org/x/sys/unix"
2122
"golang.org/x/xerrors"
2223

@@ -62,7 +63,7 @@ func (m *Mux) Start(cmd *exec.Cmd, options TermOptions) (alias string, err error
6263
}
6364
alias = uid.String()
6465

65-
term, err := newTerm(pty, cmd, options)
66+
term, err := newTerm(alias, pty, cmd, options)
6667
if err != nil {
6768
pty.Close()
6869
return "", err
@@ -188,7 +189,7 @@ func (term *Term) shutdownProcessImmediately() error {
188189
// For now we assume an average of five terminals per workspace, which makes this consume 1MiB of RAM.
189190
const terminalBacklogSize = 256 << 10
190191

191-
func newTerm(pty *os.File, cmd *exec.Cmd, options TermOptions) (*Term, error) {
192+
func newTerm(alias string, pty *os.File, cmd *exec.Cmd, options TermOptions) (*Term, error) {
192193
token, err := uuid.NewRandom()
193194
if err != nil {
194195
return nil, err
@@ -207,9 +208,11 @@ func newTerm(pty *os.File, cmd *exec.Cmd, options TermOptions) (*Term, error) {
207208
PTY: pty,
208209
Command: cmd,
209210
Stdout: &multiWriter{
210-
timeout: timeout,
211-
listener: make(map[*multiWriterListener]struct{}),
212-
recorder: recorder,
211+
timeout: timeout,
212+
listener: make(map[*multiWriterListener]struct{}),
213+
recorder: recorder,
214+
logStdout: options.LogToStdout,
215+
logLabel: alias,
213216
},
214217
Annotations: options.Annotations,
215218
title: options.Title,
@@ -245,6 +248,9 @@ type TermOptions struct {
245248

246249
// Title describes the terminal title.
247250
Title string
251+
252+
// LogToStdout forwards the terminal's stdout to supervisor's stdout
253+
LogToStdout bool
248254
}
249255

250256
// Term is a pseudo-terminal
@@ -312,6 +318,9 @@ type multiWriter struct {
312318
// ring buffer to record last 256kb of pty output
313319
// new listener is initialized with the latest recodring first
314320
recorder *RingBuffer
321+
322+
logStdout bool
323+
logLabel string
315324
}
316325

317326
var (
@@ -424,6 +433,12 @@ func (mw *multiWriter) Write(p []byte) (n int, err error) {
424433
defer mw.mu.Unlock()
425434

426435
mw.recorder.Write(p)
436+
if mw.logStdout {
437+
log.WithFields(logrus.Fields{
438+
"terminalOutput": true,
439+
"label": mw.logLabel,
440+
}).Info(string(p))
441+
}
427442

428443
for lstr := range mw.listener {
429444
if lstr.closed {

0 commit comments

Comments
 (0)