/
base.go
440 lines (364 loc) · 10.8 KB
/
base.go
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
package executor
import (
"context"
"fmt"
"io"
"os"
"os/exec"
"strings"
"time"
pio "github.com/1xyz/pryrite/internal/io"
"github.com/1xyz/pryrite/tools"
pseudoTY "github.com/creack/pty"
"github.com/mattn/go-shellwords"
"github.com/pkg/errors"
"github.com/rs/zerolog"
"golang.org/x/term"
)
type BaseExecutor struct {
name string
contentType *ContentType
command string
commandArgs []string
isRunning bool
execCmd *exec.Cmd
execDone chan error
execErr error
isExecuting bool
skipCount uint
stdin *CommandFeeder
stdout *readWriterProxy
stderr *readWriterProxy
inFile *os.File
// callbacks for replacing parts of the base logic
prepareCmd func(stdout, stderr io.WriteCloser, usePty bool) (execReadyCh, error)
prepareIO func(*ExecRequest, bool) (resultReadyCh, error)
cancel func()
clearIO func(bool)
cleanup func(bool)
inputRdr *pio.CancelableReadCloser
}
type execReadyCh chan error
type resultReadyCh chan collectorResult
var expectExitResultReady = make(resultReadyCh, 1)
type collectorResult struct {
err error
exitStatus int
}
func (be *BaseExecutor) Name() string { return be.name }
func (be *BaseExecutor) ContentType() *ContentType { return be.contentType }
func (be *BaseExecutor) Execute(ctx context.Context, req *ExecRequest) *ExecResponse {
resp := &ExecResponse{
Hdr: &ResponseHdr{req.Hdr.ID},
ExitStatus: -1,
}
if be.execErr != nil {
resp.Err = be.execErr
return resp
}
// on by default but may be globally disabled or per commend through the content-type
usePty := true
if disablePTY {
usePty = false
} else {
if disPTY, ok := be.contentType.Params["disable-pty"]; ok {
usePty = strings.ToLower(disPTY) == "true"
}
}
resp.Err = be.ensureRunning(req.Stdout, req.Stderr, usePty)
if resp.Err != nil {
be.execErr = resp.Err
return resp
}
if be.isExecuting {
resp.Err = ErrExecInProgress
return resp
}
be.isExecuting = true
defer func() {
be.isExecuting = false
if be.skipCount > 0 {
be.skipCount--
}
}()
canceler := &Canceler{}
canceler.OnCancel(be.cancel)
defer canceler.Stop()
tools.Log.Debug().
Str("name", be.Name()).
Str("id", req.Hdr.ID).
Str("command", string(req.Content)).
Str("contentType", req.ContentType.String()).
Int("skip", int(be.skipCount)).
Bool("pty", usePty).
Int("In-fd", int(req.In.Fd())).
Msg("Executing")
var resultReady resultReadyCh
resultReady, resp.Err = be.prepareIO(req, be.skipCount > 0)
if resp.Err != nil {
return resp
}
be.inputRdr = pio.NewCancelableReadCloser(ctx)
if err := be.inputRdr.Start(be.inFile); err != nil {
resp.Err = fmt.Errorf("cancelableReaderCloser.Start err = %v", err)
return resp
}
go be.startInputReading(req.In)
select {
case result := <-resultReady: // got a result
tools.Log.Info().Msgf("Execute: Received a result %+v", result)
resp.ExitStatus = result.exitStatus
resp.Err = result.err
be.stopInputReading()
case <-ctx.Done(): // interrupted by the context
resp.Err = ctx.Err()
// it's difficult to kill the current command and its children so we have to error out and reset ourselves
// TODO: have our REPL above run every command in its own process group and report that back to us
alreadyDone := false
be.stopInputReading()
be.cleanup(alreadyDone)
case resp.Err = <-be.execDone: // the underlying process exited!
resp.ExitStatus = be.execCmd.ProcessState.ExitCode()
alreadyDone := true
be.stopInputReading()
be.cleanup(alreadyDone)
if resp.Err == nil && resultReady != expectExitResultReady {
resp.Err = fmt.Errorf("%s terminated unexpectedly (status:%d)",
be.execCmd.Path, resp.ExitStatus)
}
}
be.clearIO(be.skipCount > 0)
tools.Log.Debug().Str("name", be.Name()).Interface("response", resp).Msg("Execution complete")
return resp
}
func (be *BaseExecutor) Cleanup() {
alreadyDone := false
be.cleanup(alreadyDone)
}
// startInputReading starts reading from the input file descriptor (typically os.stdin)
// and copies the bytes to upstream writer.
func (be *BaseExecutor) startInputReading(in *os.File) {
fd := int(in.Fd())
oldState, err := term.MakeRaw(fd)
if err != nil {
tools.Log.Err(err).Msgf("startInputReading: term.MakeRaw(fd=%d)", fd)
return
}
defer func() {
if err := term.Restore(fd, oldState); err != nil {
tools.Log.Err(err).Msgf("startInputReading: term.Restore(fd=%d)", fd)
}
}()
for {
_, err := io.Copy(be.stdin, be.inputRdr)
if err != nil {
if err != io.EOF {
tools.Log.Err(err).Msgf("startInputReading: io.Copy(be.stdin <- be.inputRdr)")
}
break
}
}
tools.Log.Info().Msg("startInputReading: io-loop done")
}
// stopInputReading stops reading from the input file descriptor
func (be *BaseExecutor) stopInputReading() {
if err := be.inputRdr.Close(); err != nil {
tools.Log.Warn().Msgf("stopInputReading: inputrdr.close err = %v", err)
}
be.inputRdr = nil
}
//--------------------------------------------------------------------------------
func (be *BaseExecutor) setDefaults() {
be.prepareCmd = be.defaultPrepareCmd
be.prepareIO = be.defaultPrepareIO
be.cancel = be.defaultCancel
be.clearIO = be.defaultClearIO
be.cleanup = be.defaultCleanup
}
func (be *BaseExecutor) processContentType(content []byte, myContentType, wantContentType *ContentType) error {
if !myContentType.ParentOf(wantContentType, nil) {
return ErrUnsupportedContentType
}
if _, ok := wantContentType.Params["prompt"]; ok {
// this happens if there wasn't a prompt-assign found associated with the same content-type
return fmt.Errorf("prompt requested but none found running: %s", wantContentType)
}
be.execDone = make(chan error, 1)
be.name = myContentType.Subtype + "-executor"
be.contentType = myContentType.Clone()
prompt := wantContentType.Params["prompt-assign"]
if prompt != "" {
// restrict matching this executor to only prompted request content-types
be.contentType.Params["prompt"] = prompt
commandLine := wantContentType.Params["command"]
if commandLine == "" {
commandLine = string(content)
}
args, err := shellwords.Parse(commandLine)
if err != nil {
return err
}
be.setExecCommand(args[0], args[1:])
// since we use this command for the executor itself, we need to skip it when requested
be.skipCount = 1
}
return nil
}
func (be *BaseExecutor) setExecCommand(cmd string, args []string) {
// allow folks to specify exactly which binary to run for a given command type
overrideCommand := os.Getenv("AARDY_" + strings.ToUpper(cmd) + "_PATH")
if overrideCommand == "" {
be.command = cmd
} else {
tools.Trace("exec", "overriding command", cmd, "=>", overrideCommand)
be.command = overrideCommand
}
if args != nil {
be.commandArgs = args
}
}
func (be *BaseExecutor) getCommandFrom(content []byte, contentType *ContentType) ([]byte, error) {
if _, ok := contentType.Params["prompt"]; ok {
var start, stop int
n, _ := fmt.Sscanf(contentType.Params["command"], "%d:%d", &start, &stop)
if n != 1 && n != 2 {
return content, nil
}
if n == 1 {
return content[start:], nil
}
return content[start:stop], nil
}
return content, nil
}
func (be *BaseExecutor) ensureRunning(stdout, stderr io.WriteCloser, usePty bool) error {
if be.isRunning {
return nil
}
tools.Trace("exec", "command is not running: about to start")
execReady, err := be.prepareCmd(stdout, stderr, usePty)
if err != nil {
return err
}
tools.Trace("exec", "command ready to run", be.execCmd.Args)
if err := be.execCmd.Start(); err != nil {
return err
}
go func() {
err := be.execCmd.Wait()
tools.Trace("exec", "command stopped", err)
be.execDone <- err
}()
select {
case err = <-execReady:
if err != nil {
return err
}
case err = <-be.execDone:
if err != nil {
return err
}
case <-time.After(10 * time.Second):
be.stdin.Put(nil)
stopKill(be.execCmd.Process)
return errors.New("gave up waiting for executor to be ready")
}
be.isRunning = true
if tools.Log.GetLevel() <= zerolog.DebugLevel {
args := zerolog.Arr()
for _, arg := range be.commandArgs {
args = args.Str(arg)
}
var pid int
if be.execCmd.Process == nil {
pid = -1
} else {
pid = be.execCmd.Process.Pid
}
tools.Log.Debug().Str("execType", be.contentType.String()).Str("name", be.Name()).
Str("command", be.command).Array("args", args).Int("pid", pid).
Msg("Executor is running")
}
return nil
}
func (be *BaseExecutor) defaultPrepareCmd(stdout, stderr io.WriteCloser, usePty bool) (execReadyCh, error) {
be.execCmd = exec.Command(be.command, be.commandArgs...)
var outPTY, errPTY *os.File
if usePty {
var err error
var outTTY, errTTY *os.File
outPTY, outTTY, err = pseudoTY.Open()
if err != nil {
return nil, errors.Wrap(err, "Unable to open a Pseudo terminal (PTY) for stdin/out")
}
errPTY, errTTY, err = pseudoTY.Open()
if err != nil {
return nil, errors.Wrap(err, "Unable to open a Pseudo terminal (PTY) for stderr")
}
tools.Trace("exec", "PTYs are open")
be.setProcAttr()
be.execCmd.Stdin = outTTY
be.execCmd.Stdout = outTTY
be.execCmd.Stderr = errTTY
}
be.stdin = NewCommandFeeder(outPTY)
// proxy out/err to allow for dynamic reassignment for each execution
be.stdout = &readWriterProxy{name: "stdout", writer: stdout}
be.stderr = &readWriterProxy{name: "stderr", writer: stderr}
if outPTY != nil {
be.stdout.Monitor(outPTY)
be.stderr.Monitor(errPTY)
}
// default is to be ready immediately (but executors can/will override this)
execReady := make(execReadyCh, 1)
execReady <- nil
return execReady, nil
}
func (be *BaseExecutor) defaultCancel() {
if be.isRunning {
stopKill(be.execCmd.Process)
}
}
func (be *BaseExecutor) defaultPrepareIO(req *ExecRequest, isExecCmd bool) (resultReadyCh, error) {
if isExecCmd {
// just a test connection so provide EOF to the input to have the command exit
be.stdin.Put(nil)
} else {
command, err := be.getCommandFrom(req.Content, req.ContentType)
if err != nil {
return nil, err
}
be.stdin.Put(command)
// for now we can only support one command per execution for the basic input feeder executors
if err := be.stdin.Close(); err != nil {
tools.Log.Err(err).Msgf("defaultPrepareIO: be.stdin.close()")
}
}
// this will never "fire"--instead, we expect the command to exit once done processing
ready := expectExitResultReady
return ready, nil
}
func (be *BaseExecutor) defaultClearIO(isExecCmd bool) {
// update proxies to avoid confusing caller if more junk comes in
be.stdout.SetWriter(nil)
be.stderr.SetWriter(nil)
}
func (be *BaseExecutor) defaultCleanup(alreadyDone bool) {
if be.isRunning {
if !alreadyDone {
be.stdin.Close()
stopKill(be.execCmd.Process)
<-be.execDone // wait for our goroutine to exit
}
be.isRunning = false
}
}
func stopKill(proc *os.Process) {
if proc == nil {
return
}
// Refer WAR-295
if err := proc.Kill(); err != nil {
tools.Log.Warn().Msgf("stopKill: syscall.Kill(SIGKILL) err = %v", err)
}
}