-
Notifications
You must be signed in to change notification settings - Fork 251
Expand file tree
/
Copy pathexecutor.go
More file actions
424 lines (339 loc) · 9.84 KB
/
Copy pathexecutor.go
File metadata and controls
424 lines (339 loc) · 9.84 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
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
package executor
import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"io"
"log/slog"
"os/exec"
"strings"
"sync"
"syscall"
"time"
"github.com/deckhouse/deckhouse/pkg/log"
"go.opentelemetry.io/otel"
pkg "github.com/flant/shell-operator/pkg"
json "github.com/flant/shell-operator/pkg/utils/json"
utils "github.com/flant/shell-operator/pkg/utils/labels"
)
const (
serviceName = "executor"
)
// ProcessRegistry tracks PIDs of processes started by the executor so that
// a PID-1 zombie reaper can skip them (their parent already calls wait).
// This prevents the reaper from stealing a child that cmd.Wait expects to reap.
type ProcessRegistry struct {
mu sync.RWMutex
activePIDs map[int32]struct{}
}
// NewProcessRegistry creates a new ProcessRegistry.
func NewProcessRegistry() *ProcessRegistry {
return &ProcessRegistry{
activePIDs: make(map[int32]struct{}),
}
}
// Register adds pid to the set of active PIDs.
func (r *ProcessRegistry) Register(pid int) {
r.mu.Lock()
r.activePIDs[int32(pid)] = struct{}{}
r.mu.Unlock()
}
// Unregister removes pid from the set of active PIDs.
func (r *ProcessRegistry) Unregister(pid int) {
r.mu.Lock()
delete(r.activePIDs, int32(pid))
r.mu.Unlock()
}
// IsActive reports whether pid is currently tracked as an active process.
func (r *ProcessRegistry) IsActive(pid int) bool {
r.mu.RLock()
_, ok := r.activePIDs[int32(pid)]
r.mu.RUnlock()
return ok
}
// Registry is the global process registry shared between the executor and
// the PID-1 zombie reaper. All executor methods that spawn child processes
// register their PIDs here so the reaper can skip them.
var Registry = NewProcessRegistry()
// Run starts the command, waits for it to complete, and returns the error.
// The child PID is registered in the global Registry while the process is
// running so that a PID-1 zombie reaper does not steal it.
func Run(cmd *exec.Cmd) error {
// TODO context: hook name, hook phase, hook binding
// TODO observability
log.Debug("Executing command", slog.String(pkg.LogKeyCommand, strings.Join(cmd.Args, " ")), slog.String(pkg.LogKeyDir, cmd.Dir))
if err := cmd.Start(); err != nil {
return err
}
Registry.Register(cmd.Process.Pid)
defer Registry.Unregister(cmd.Process.Pid)
return cmd.Wait()
}
// StderrError is returned by RunAndLogLines when a command fails and produces
// output on stderr. Callers can use errors.As to access the raw stderr content.
type StderrError struct {
Message string
}
func (e *StderrError) Error() string {
return e.Message
}
type Executor struct {
cmd *exec.Cmd
logProxyHookJSON bool
proxyJsonKey string
logger *log.Logger
}
func (e *Executor) WithLogProxyHookJSON(logProxyHookJSON bool) *Executor {
e.logProxyHookJSON = logProxyHookJSON
return e
}
func (e *Executor) WithLogProxyHookJSONKey(logProxyHookJSONKey string) *Executor {
if logProxyHookJSONKey == "" {
return e
}
e.proxyJsonKey = logProxyHookJSONKey
return e
}
func (e *Executor) WithLogger(logger *log.Logger) *Executor {
e.logger = logger
return e
}
func (e *Executor) WithChroot(path string) *Executor {
if len(path) > 0 {
e.cmd.SysProcAttr = &syscall.SysProcAttr{
Chroot: path,
}
e.cmd.Path = strings.TrimPrefix(e.cmd.Path, path)
e.cmd.Dir = "/"
}
return e
}
func (e *Executor) WithCMDStdout(w io.Writer) *Executor {
e.cmd.Stdout = w
return e
}
func (e *Executor) WithCMDStderr(w io.Writer) *Executor {
e.cmd.Stderr = w
return e
}
func NewExecutor(dir string, entrypoint string, args []string, envs []string) *Executor {
cmd := exec.Command(entrypoint, args...)
cmd.Env = append(cmd.Env, envs...)
cmd.Dir = dir
ex := &Executor{
cmd: cmd,
proxyJsonKey: "proxyJsonLog",
logger: log.NewLogger().Named("auto-executor"),
}
return ex
}
func (e *Executor) Output() ([]byte, error) {
e.logger.Debug("Executing command",
slog.String(pkg.LogKeyCommand, strings.Join(e.cmd.Args, " ")),
slog.String(pkg.LogKeyDir, e.cmd.Dir))
// Reproduce cmd.Output() but interleave PID registration so that the
// PID-1 zombie reaper skips this process.
if e.cmd.Stdout != nil {
return nil, errors.New("exec: Stdout already set")
}
var stdout bytes.Buffer
e.cmd.Stdout = &stdout
captureErr := e.cmd.Stderr == nil
var stderrBuf bytes.Buffer
if captureErr {
e.cmd.Stderr = &stderrBuf
}
if err := e.cmd.Start(); err != nil {
return nil, err
}
Registry.Register(e.cmd.Process.Pid)
defer Registry.Unregister(e.cmd.Process.Pid)
err := e.cmd.Wait()
if err != nil && captureErr {
if ee, ok := err.(*exec.ExitError); ok {
ee.Stderr = stderrBuf.Bytes()
}
}
return stdout.Bytes(), err
}
type CmdUsage struct {
Sys time.Duration
User time.Duration
MaxRss int64
}
func (e *Executor) RunAndLogLines(ctx context.Context, logLabels map[string]string) (*CmdUsage, error) {
ctx, span := otel.Tracer(serviceName).Start(ctx, "RunAndLogLines")
defer span.End()
stdErr := bytes.NewBuffer(nil)
logEntry := utils.EnrichLoggerWithLabels(e.logger, logLabels)
stdoutLogEntry := logEntry.With(pkg.LogKeyOutput, "stdout")
stderrLogEntry := logEntry.With(pkg.LogKeyOutput, "stderr")
log.Debug("Executing command",
slog.String(pkg.LogKeyCommand, strings.Join(e.cmd.Args, " ")),
slog.String(pkg.LogKeyDir, e.cmd.Dir))
plo := &proxyLogger{
ctx: ctx,
logProxyHookJSON: e.logProxyHookJSON,
proxyJsonLogKey: e.proxyJsonKey,
logger: stdoutLogEntry,
buf: make([]byte, 0),
}
ple := &proxyLogger{
ctx: ctx,
logProxyHookJSON: e.logProxyHookJSON,
proxyJsonLogKey: e.proxyJsonKey,
logger: stderrLogEntry,
buf: make([]byte, 0),
}
e.cmd.Stdout = plo
e.cmd.Stderr = io.MultiWriter(ple, stdErr)
if err := e.cmd.Start(); err != nil {
return nil, fmt.Errorf("cmd start: %w", err)
}
Registry.Register(e.cmd.Process.Pid)
defer Registry.Unregister(e.cmd.Process.Pid)
err := e.cmd.Wait()
if err != nil {
if len(stdErr.Bytes()) > 0 {
return nil, &StderrError{Message: stdErr.String()}
}
return nil, fmt.Errorf("cmd run: %w", err)
}
var usage *CmdUsage
if e.cmd.ProcessState != nil {
usage = &CmdUsage{
Sys: e.cmd.ProcessState.SystemTime(),
User: e.cmd.ProcessState.UserTime(),
}
// FIXME Maxrss is Unix specific.
sysUsage := e.cmd.ProcessState.SysUsage()
if v, ok := sysUsage.(*syscall.Rusage); ok {
// v.Maxrss is int32 on arm/v7
usage.MaxRss = int64(v.Maxrss) //nolint:unconvert
}
}
return usage, nil
}
type proxyLogger struct {
ctx context.Context
logProxyHookJSON bool
proxyJsonLogKey string
logger *log.Logger
buf []byte
}
func (pl *proxyLogger) Write(p []byte) (int, error) {
if !pl.logProxyHookJSON {
pl.writerScanner(p)
return len(p), nil
}
// join all parts of json
pl.buf = append(pl.buf, p...)
var line interface{}
err := json.Unmarshal(pl.buf, &line)
if err != nil {
if err.Error() == "unexpected end of JSON input" {
return len(p), nil
}
pl.logger.Debug("output is not json", log.Err(err))
pl.writerScanner(p)
return len(p), nil
}
logMap, ok := line.(map[string]interface{})
defer func() {
pl.buf = pl.buf[:0]
}()
if !ok {
pl.logger.Debug("json log line not map[string]interface{}", slog.Any(pkg.LogKeyLine, line))
// fall back to using the logger
pl.logger.Info(string(p))
return len(p), nil
}
pl.mergeAndLogInputLog(pl.ctx, logMap, "hook")
return len(p), nil
}
func (pl *proxyLogger) writerScanner(p []byte) {
scanner := bufio.NewScanner(bytes.NewReader(p))
scanner.Buffer(make([]byte, bufio.MaxScanTokenSize), bufio.MaxScanTokenSize)
var jsonBuf []string
// Split large entries into chunks
chunkSize := bufio.MaxScanTokenSize // 64KB
splitFunc := func(data []byte, atEOF bool) (int, []byte, error) {
if len(data) >= chunkSize {
return chunkSize, data[:chunkSize], nil
}
return bufio.ScanLines(data, atEOF)
}
scanner.Split(splitFunc)
// Scan the input and write it to the logger using the specified print function
for scanner.Scan() {
// Prevent empty logging
line := strings.TrimSpace(scanner.Text())
if line == "" {
continue
}
if len(jsonBuf) > 0 || strings.HasPrefix(line, "{") {
jsonBuf = append(jsonBuf, line)
joined := strings.Join(jsonBuf, "\n")
if err := pl.tryLogJSON(joined); err == nil {
jsonBuf = nil
}
continue
}
if len(line) > 10000 {
line = fmt.Sprintf("%s:truncated", line[:10000])
}
if err := pl.tryLogJSON(line); err == nil {
continue
}
pl.logger.Info(line)
}
// If there was an error while scanning the input, log an error
if err := scanner.Err(); err != nil {
pl.logger.Error("reading from scanner", log.Err(err))
}
}
// tryLogJSON tries to parse the string as JSON and log it if it succeeds
func (pl *proxyLogger) tryLogJSON(s string) error {
var m map[string]interface{}
err := json.Unmarshal([]byte(s), &m)
if err == nil {
pl.mergeAndLogInputLog(pl.ctx, m, "hook")
return nil
}
return fmt.Errorf("failed to parse json: %w", err)
}
// level = level
// msg = msg
// prefix for all fields hook_
// source = hook_source
// stacktrace = hook_stacktrace
func (pl *proxyLogger) mergeAndLogInputLog(ctx context.Context, inputLog map[string]interface{}, prefix string) {
var lvl log.Level
lvlRaw, ok := inputLog[slog.LevelKey].(string)
if ok {
lvl = log.LogLevelFromStr(lvlRaw)
delete(inputLog, slog.LevelKey)
}
msg, ok := inputLog[slog.MessageKey].(string)
if !ok {
msg = "hook result"
}
delete(inputLog, slog.MessageKey)
delete(inputLog, slog.TimeKey)
logLineRaw, _ := json.Marshal(inputLog)
logLine := string(logLineRaw)
logger := pl.logger.With(pl.proxyJsonLogKey, true)
if len(logLine) > 10000 {
logLine = fmt.Sprintf("%s:truncated", logLine[:10000])
logger.Log(ctx, lvl.Level(), msg, slog.Any(pkg.LogKeyHook, map[string]any{
"truncated": logLine,
}))
return
}
for key, val := range inputLog {
logger = logger.With(slog.Any(prefix+"_"+key, val))
}
logger.Log(ctx, lvl.Level(), msg)
}