|
1 | 1 | package daemon |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "encoding/json" |
5 | 4 | "io" |
6 | | - "os" |
7 | | - "sync" |
8 | | - "time" |
9 | 5 |
|
10 | | - "github.com/Sirupsen/logrus" |
11 | | - "github.com/docker/docker/pkg/jsonlog" |
12 | | - "github.com/docker/docker/pkg/promise" |
| 6 | + "github.com/docker/docker/pkg/stdcopy" |
13 | 7 | ) |
14 | 8 |
|
15 | | -func (c *Container) AttachWithLogs(stdin io.ReadCloser, stdout, stderr io.Writer, logs, stream bool) error { |
16 | | - if logs { |
17 | | - cLog, err := c.ReadLog("json") |
18 | | - if err != nil && os.IsNotExist(err) { |
19 | | - // Legacy logs |
20 | | - logrus.Debugf("Old logs format") |
21 | | - if stdout != nil { |
22 | | - cLog, err := c.ReadLog("stdout") |
23 | | - if err != nil { |
24 | | - logrus.Errorf("Error reading logs (stdout): %s", err) |
25 | | - } else if _, err := io.Copy(stdout, cLog); err != nil { |
26 | | - logrus.Errorf("Error streaming logs (stdout): %s", err) |
27 | | - } |
28 | | - } |
29 | | - if stderr != nil { |
30 | | - cLog, err := c.ReadLog("stderr") |
31 | | - if err != nil { |
32 | | - logrus.Errorf("Error reading logs (stderr): %s", err) |
33 | | - } else if _, err := io.Copy(stderr, cLog); err != nil { |
34 | | - logrus.Errorf("Error streaming logs (stderr): %s", err) |
35 | | - } |
36 | | - } |
37 | | - } else if err != nil { |
38 | | - logrus.Errorf("Error reading logs (json): %s", err) |
39 | | - } else { |
40 | | - dec := json.NewDecoder(cLog) |
41 | | - for { |
42 | | - l := &jsonlog.JSONLog{} |
43 | | - |
44 | | - if err := dec.Decode(l); err == io.EOF { |
45 | | - break |
46 | | - } else if err != nil { |
47 | | - logrus.Errorf("Error streaming logs: %s", err) |
48 | | - break |
49 | | - } |
50 | | - if l.Stream == "stdout" && stdout != nil { |
51 | | - io.WriteString(stdout, l.Log) |
52 | | - } |
53 | | - if l.Stream == "stderr" && stderr != nil { |
54 | | - io.WriteString(stderr, l.Log) |
55 | | - } |
56 | | - } |
57 | | - } |
58 | | - } |
59 | | - |
60 | | - //stream |
61 | | - if stream { |
62 | | - var stdinPipe io.ReadCloser |
63 | | - if stdin != nil { |
64 | | - r, w := io.Pipe() |
65 | | - go func() { |
66 | | - defer w.Close() |
67 | | - defer logrus.Debugf("Closing buffered stdin pipe") |
68 | | - io.Copy(w, stdin) |
69 | | - }() |
70 | | - stdinPipe = r |
71 | | - } |
72 | | - <-c.Attach(stdinPipe, stdout, stderr) |
73 | | - // If we are in stdinonce mode, wait for the process to end |
74 | | - // otherwise, simply return |
75 | | - if c.Config.StdinOnce && !c.Config.Tty { |
76 | | - c.WaitStop(-1 * time.Second) |
77 | | - } |
78 | | - } |
79 | | - return nil |
| 9 | +type ContainerAttachWithLogsConfig struct { |
| 10 | + InStream io.ReadCloser |
| 11 | + OutStream io.Writer |
| 12 | + UseStdin, UseStdout, UseStderr bool |
| 13 | + Logs, Stream bool |
| 14 | + Multiplex bool |
80 | 15 | } |
81 | 16 |
|
82 | | -func (c *Container) Attach(stdin io.ReadCloser, stdout io.Writer, stderr io.Writer) chan error { |
83 | | - return attach(&c.StreamConfig, c.Config.OpenStdin, c.Config.StdinOnce, c.Config.Tty, stdin, stdout, stderr) |
84 | | -} |
85 | | - |
86 | | -func attach(streamConfig *StreamConfig, openStdin, stdinOnce, tty bool, stdin io.ReadCloser, stdout io.Writer, stderr io.Writer) chan error { |
87 | | - var ( |
88 | | - cStdout, cStderr io.ReadCloser |
89 | | - cStdin io.WriteCloser |
90 | | - wg sync.WaitGroup |
91 | | - errors = make(chan error, 3) |
92 | | - ) |
93 | | - |
94 | | - if stdin != nil && openStdin { |
95 | | - cStdin = streamConfig.StdinPipe() |
96 | | - wg.Add(1) |
| 17 | +func (daemon *Daemon) ContainerAttachWithLogs(name string, c *ContainerAttachWithLogsConfig) error { |
| 18 | + container, err := daemon.Get(name) |
| 19 | + if err != nil { |
| 20 | + return err |
97 | 21 | } |
98 | 22 |
|
99 | | - if stdout != nil { |
100 | | - cStdout = streamConfig.StdoutPipe() |
101 | | - wg.Add(1) |
102 | | - } |
| 23 | + var errStream io.Writer |
103 | 24 |
|
104 | | - if stderr != nil { |
105 | | - cStderr = streamConfig.StderrPipe() |
106 | | - wg.Add(1) |
| 25 | + if !container.Config.Tty && c.Multiplex { |
| 26 | + errStream = stdcopy.NewStdWriter(c.OutStream, stdcopy.Stderr) |
| 27 | + c.OutStream = stdcopy.NewStdWriter(c.OutStream, stdcopy.Stdout) |
| 28 | + } else { |
| 29 | + errStream = c.OutStream |
107 | 30 | } |
108 | 31 |
|
109 | | - // Connect stdin of container to the http conn. |
110 | | - go func() { |
111 | | - if stdin == nil || !openStdin { |
112 | | - return |
113 | | - } |
114 | | - logrus.Debugf("attach: stdin: begin") |
115 | | - defer func() { |
116 | | - if stdinOnce && !tty { |
117 | | - cStdin.Close() |
118 | | - } else { |
119 | | - // No matter what, when stdin is closed (io.Copy unblock), close stdout and stderr |
120 | | - if cStdout != nil { |
121 | | - cStdout.Close() |
122 | | - } |
123 | | - if cStderr != nil { |
124 | | - cStderr.Close() |
125 | | - } |
126 | | - } |
127 | | - wg.Done() |
128 | | - logrus.Debugf("attach: stdin: end") |
129 | | - }() |
| 32 | + var stdin io.ReadCloser |
| 33 | + var stdout, stderr io.Writer |
130 | 34 |
|
131 | | - var err error |
132 | | - if tty { |
133 | | - _, err = copyEscapable(cStdin, stdin) |
134 | | - } else { |
135 | | - _, err = io.Copy(cStdin, stdin) |
136 | | - |
137 | | - } |
138 | | - if err == io.ErrClosedPipe { |
139 | | - err = nil |
140 | | - } |
141 | | - if err != nil { |
142 | | - logrus.Errorf("attach: stdin: %s", err) |
143 | | - errors <- err |
144 | | - return |
145 | | - } |
146 | | - }() |
147 | | - |
148 | | - attachStream := func(name string, stream io.Writer, streamPipe io.ReadCloser) { |
149 | | - if stream == nil { |
150 | | - return |
151 | | - } |
152 | | - defer func() { |
153 | | - // Make sure stdin gets closed |
154 | | - if stdin != nil { |
155 | | - stdin.Close() |
156 | | - } |
157 | | - streamPipe.Close() |
158 | | - wg.Done() |
159 | | - logrus.Debugf("attach: %s: end", name) |
160 | | - }() |
161 | | - |
162 | | - logrus.Debugf("attach: %s: begin", name) |
163 | | - _, err := io.Copy(stream, streamPipe) |
164 | | - if err == io.ErrClosedPipe { |
165 | | - err = nil |
166 | | - } |
167 | | - if err != nil { |
168 | | - logrus.Errorf("attach: %s: %v", name, err) |
169 | | - errors <- err |
170 | | - } |
| 35 | + if c.UseStdin { |
| 36 | + stdin = c.InStream |
| 37 | + } |
| 38 | + if c.UseStdout { |
| 39 | + stdout = c.OutStream |
| 40 | + } |
| 41 | + if c.UseStderr { |
| 42 | + stderr = errStream |
171 | 43 | } |
172 | 44 |
|
173 | | - go attachStream("stdout", stdout, cStdout) |
174 | | - go attachStream("stderr", stderr, cStderr) |
| 45 | + return container.AttachWithLogs(stdin, stdout, stderr, c.Logs, c.Stream) |
| 46 | +} |
175 | 47 |
|
176 | | - return promise.Go(func() error { |
177 | | - wg.Wait() |
178 | | - close(errors) |
179 | | - for err := range errors { |
180 | | - if err != nil { |
181 | | - return err |
182 | | - } |
183 | | - } |
184 | | - return nil |
185 | | - }) |
| 48 | +type ContainerWsAttachWithLogsConfig struct { |
| 49 | + InStream io.ReadCloser |
| 50 | + OutStream, ErrStream io.Writer |
| 51 | + Logs, Stream bool |
186 | 52 | } |
187 | 53 |
|
188 | | -// Code c/c from io.Copy() modified to handle escape sequence |
189 | | -func copyEscapable(dst io.Writer, src io.ReadCloser) (written int64, err error) { |
190 | | - buf := make([]byte, 32*1024) |
191 | | - for { |
192 | | - nr, er := src.Read(buf) |
193 | | - if nr > 0 { |
194 | | - // ---- Docker addition |
195 | | - // char 16 is C-p |
196 | | - if nr == 1 && buf[0] == 16 { |
197 | | - nr, er = src.Read(buf) |
198 | | - // char 17 is C-q |
199 | | - if nr == 1 && buf[0] == 17 { |
200 | | - if err := src.Close(); err != nil { |
201 | | - return 0, err |
202 | | - } |
203 | | - return 0, nil |
204 | | - } |
205 | | - } |
206 | | - // ---- End of docker |
207 | | - nw, ew := dst.Write(buf[0:nr]) |
208 | | - if nw > 0 { |
209 | | - written += int64(nw) |
210 | | - } |
211 | | - if ew != nil { |
212 | | - err = ew |
213 | | - break |
214 | | - } |
215 | | - if nr != nw { |
216 | | - err = io.ErrShortWrite |
217 | | - break |
218 | | - } |
219 | | - } |
220 | | - if er == io.EOF { |
221 | | - break |
222 | | - } |
223 | | - if er != nil { |
224 | | - err = er |
225 | | - break |
226 | | - } |
| 54 | +func (daemon *Daemon) ContainerWsAttachWithLogs(name string, c *ContainerWsAttachWithLogsConfig) error { |
| 55 | + container, err := daemon.Get(name) |
| 56 | + if err != nil { |
| 57 | + return err |
227 | 58 | } |
228 | | - return written, err |
| 59 | + |
| 60 | + return container.AttachWithLogs(c.InStream, c.OutStream, c.ErrStream, c.Logs, c.Stream) |
229 | 61 | } |
0 commit comments