-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexec.go
More file actions
295 lines (253 loc) · 6.61 KB
/
Copy pathexec.go
File metadata and controls
295 lines (253 loc) · 6.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package docker
import (
"bytes"
"context"
"fmt"
"io"
"net/http/httputil"
"strings"
"github.com/cenkalti/backoff"
"github.com/docker/docker/api/types"
shellwords "github.com/junegunn/go-shellwords"
"github.com/pkg/errors"
"github.com/rai-project/utils/promise"
)
type Execution struct {
container *Container
context context.Context
// Path is the path or name of the command in the container.
Path string
// Arguments to the command in the container, excluding the command
// name as the first argument.
Args []string
// Env is environment variables to the command. If Env is nil, Run will use
// Env specified on Method or pre-built container image.
Env []string
// Dir specifies the working directory of the command. If Dir is the empty
// string, Run uses Dir specified on Method or pre-built container image.
Dir string
// Stdin specifies the process's standard input.
// If Stdin is nil, the process reads from the null device (os.DevNull).
//
// Run will not close the underlying handle if the Reader is an *os.File
// differently than os/exec.
Stdin io.ReadCloser
// Stdout and Stderr specify the process's standard output and error.
// If either is nil, they will be redirected to the null device (os.DevNull).
//
// Run will not close the underlying handles if they are *os.File differently
// than os/exec.
Stdout io.Writer
Stderr io.Writer
isStarted bool
execID string
wc chan error
closeAfterWait []io.Closer
}
func NewExecution(container *Container, args ...string) (*Execution, error) {
ctx := context.WithValue(container.options.context, "cmd", strings.Join(args, " "))
var cmd string
var cmdArgs []string
if len(args) > 0 {
cmd = args[0]
}
if len(args) > 1 {
cmdArgs = args[1:]
}
return &Execution{
container: container,
Path: cmd,
Args: cmdArgs,
context: ctx,
isStarted: false,
}, nil
}
func NewExecutionFromString(container *Container, shell string) (*Execution, error) {
args, err := shellwords.Parse(shell)
if err != nil {
log.WithError(err).WithField("cmd", shell).Error("Failed to parse command line")
return nil, errors.Wrapf(err, "Docker execution:: failed to parse command line %v", shell)
}
return NewExecution(container, args...)
}
func (e *Execution) CombinedOutput() ([]byte, error) {
if e.Stdout != nil {
return nil, errors.New("Docker execution:: Stdout already set")
}
if e.Stderr != nil {
return nil, errors.New("Docker execution:: Stderr already set")
}
var b bytes.Buffer
e.Stdout, e.Stderr = &b, &b
err := e.Run()
return b.Bytes(), err
}
func (e *Execution) Output() ([]byte, error) {
if e.Stdout != nil {
return nil, errors.New("Docker execution: Stdout already set")
}
var stdout, stderr bytes.Buffer
e.Stdout = &stdout
captureErr := e.Stderr == nil
if captureErr {
e.Stderr = &stderr
}
err := e.Run()
if err != nil && captureErr {
if ee, ok := err.(*ExitError); ok {
ee.Stderr = stderr.Bytes()
}
}
return stdout.Bytes(), err
}
func (e *Execution) Run() error {
defer closeFds(e)
if err := e.Start(); err != nil {
return err
}
return e.Wait()
}
func (e *Execution) Start() error {
container := e.container
client := container.client
//if e.Stdin == nil {
// e.Stdin = client.options.stdin
//}
if e.Stdout == nil {
e.Stdout = client.options.stdout
}
if e.Stderr == nil {
e.Stderr = client.options.stderr
}
isTty := container.options.containerConfig.Tty
env := e.Env
if len(env) == 0 {
env = container.options.containerConfig.Env
}
cmd := append([]string{e.Path}, e.Args...)
execOpts := types.ExecConfig{
AttachStdin: e.Stdin != nil,
AttachStdout: true,
AttachStderr: true,
Detach: true,
Tty: isTty,
Cmd: cmd,
User: container.options.containerConfig.User,
Privileged: container.options.hostConfig.Privileged,
Env: env,
}
execID, err := client.ContainerExecCreate(
e.context,
container.ID,
execOpts,
)
if err != nil {
return errors.Wrapf(err,
"cannot create execution %v in container", strings.Join(cmd, " "))
}
e.execID = execID.ID
resp, errAttach := client.ContainerExecAttach(
e.context,
e.execID,
types.ExecStartCheck{
Detach: false,
Tty: isTty,
},
)
if errAttach != nil && errAttach != httputil.ErrPersistEOF {
// ContainerAttach returns an ErrPersistEOF (connection closed)
// means server met an error and put it in Hijacked connection
// keep the error and read detailed error message from hijacked connection later
return errors.Wrap(errAttach, "cannot attach to container")
}
strm := &stream{
stdin: e.Stdin,
stdout: e.Stdout,
stderr: e.Stderr,
}
cErr := promise.Go(func() error {
defer resp.Close()
errHijack := holdHijackedConnection(
e.context,
strm,
isTty,
e.Stdin,
e.Stdout,
e.Stderr,
resp,
)
if errHijack == nil {
return errAttach
}
return errHijack
})
e.wc = cErr
e.isStarted = true
return err
}
func (e *Execution) StdinPipe() (io.WriteCloser, error) {
if e.Stdin != nil {
return nil, errors.New("Docker execution:: Stdin already set")
}
pr, pw := io.Pipe()
e.Stdin = pr
return pw, nil
}
func (e *Execution) StderrPipe() (io.ReadCloser, error) {
if e.Stderr != nil {
return nil, errors.New("Docker execution Stderr already set")
}
pr, pw := io.Pipe()
e.Stderr = pw
e.closeAfterWait = append(e.closeAfterWait, pw)
return pr, nil
}
func (e *Execution) StdoutPipe() (io.ReadCloser, error) {
if e.Stderr != nil {
return nil, errors.New("Docker execution stdout already set")
}
pr, pw := io.Pipe()
e.Stdout = pw
e.closeAfterWait = append(e.closeAfterWait, pw)
return pr, nil
}
func (e *Execution) Wait() error {
defer closeFds(e)
if !e.isStarted {
return nil
}
if err := <-e.wc; err != nil {
return errors.Wrap(err, "failed to wait for hijacked connection")
}
client := e.container.client
inspect := func() error {
info, err := client.ContainerExecInspect(e.context, e.execID)
if err != nil {
return err
}
if !info.Running {
return nil
}
return errors.New("container is running")
}
return backoff.Retry(inspect, backoff.NewExponentialBackOff())
}
func closeFds(e *Execution) {
fds := e.closeAfterWait
for _, fd := range fds {
fd.Close()
}
e.closeAfterWait = []io.Closer{}
}
// ExitError reports an unsuccessful exit by a command.
type ExitError struct {
// ExitCode holds the non-zero exit code of the container
ExitCode int
// Stderr holds the standard error output from the command
// if it *Cmd executed through Output() and Cmd.Stderr was not
// set.
Stderr []byte
}
func (e *ExitError) Error() string {
return fmt.Sprintf("Docker execution: exit status: %d", e.ExitCode)
}