-
Notifications
You must be signed in to change notification settings - Fork 206
/
executor.go
122 lines (98 loc) · 2.78 KB
/
executor.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
package executor
import (
"encoding/json"
"os/exec"
"strings"
"syscall"
"time"
log "github.com/sirupsen/logrus"
"github.com/flant/shell-operator/pkg/app"
utils "github.com/flant/shell-operator/pkg/utils/labels"
)
type CmdUsage struct {
Sys time.Duration
User time.Duration
MaxRss int64
}
func Run(cmd *exec.Cmd) error {
// TODO context: hook name, hook phase, hook binding
// TODO observability
log.Debugf("Executing command '%s' in '%s' dir", strings.Join(cmd.Args, " "), cmd.Dir)
return cmd.Run()
}
func RunAndLogLines(cmd *exec.Cmd, logLabels map[string]string) (*CmdUsage, error) {
// TODO observability
logEntry := log.WithFields(utils.LabelsToLogFields(logLabels))
stdoutLogEntry := logEntry.WithField("output", "stdout")
stderrLogEntry := logEntry.WithField("output", "stderr")
logEntry.Debugf("Executing command '%s' in '%s' dir", strings.Join(cmd.Args, " "), cmd.Dir)
if app.LogProxyHookJSON {
plo := &proxyJSONLogger{stdoutLogEntry, make([]byte, 0)}
ple := &proxyJSONLogger{stderrLogEntry, make([]byte, 0)}
cmd.Stdout = plo
cmd.Stderr = ple
} else {
cmd.Stdout = stdoutLogEntry.Writer()
cmd.Stderr = stderrLogEntry.Writer()
}
err := cmd.Run()
if err != nil {
return nil, err
}
var usage *CmdUsage
if cmd.ProcessState != nil {
usage = &CmdUsage{
Sys: cmd.ProcessState.SystemTime(),
User: cmd.ProcessState.UserTime(),
}
// FIXME Maxrss is Unix specific.
sysUsage := 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, err
}
type proxyJSONLogger struct {
*log.Entry
buf []byte
}
func (pj *proxyJSONLogger) Write(p []byte) (n int, err error) {
pj.buf = append(pj.buf, p...)
var line interface{}
err = json.Unmarshal(pj.buf, &line)
if err != nil {
if err.Error() == "unexpected end of JSON input" {
return len(p), nil
}
return len(p), err
}
logMap, ok := line.(map[string]interface{})
if !ok {
pj.Debugf("json log line not map[string]interface{}: %v", line)
// fall back to using the logger
pj.Info(string(p))
return len(p), err
}
for k, v := range pj.Data {
logMap[k] = v
}
logLine, _ := json.Marshal(logMap)
logEntry := pj.WithField(app.ProxyJsonLogKey, true)
logEntry.Log(log.FatalLevel, string(logLine))
return len(p), nil
}
func Output(cmd *exec.Cmd) (output []byte, err error) {
// TODO context: hook name, hook phase, hook binding
// TODO observability
log.Debugf("Executing command '%s' in '%s' dir", strings.Join(cmd.Args, " "), cmd.Dir)
output, err = cmd.Output()
return
}
func MakeCommand(dir string, entrypoint string, args []string, envs []string) *exec.Cmd {
cmd := exec.Command(entrypoint, args...)
cmd.Env = append(cmd.Env, envs...)
cmd.Dir = dir
return cmd
}