-
Notifications
You must be signed in to change notification settings - Fork 2
/
process.go
323 lines (268 loc) · 7.8 KB
/
process.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
package prox
import (
"context"
"fmt"
"io"
"os/exec"
"regexp"
"strings"
"sync"
"syscall"
"time"
"unicode"
"github.com/hashicorp/go-multierror"
"github.com/pkg/errors"
"go.uber.org/zap"
)
// Process holds all information about a process that is executed by prox.
type Process struct {
Name string
Script string
Env Environment
Output StructuredOutput // optional
}
// ProcessInfo contains information about a running process.
type ProcessInfo struct {
Name string
PID int
Uptime time.Duration
}
// Validate checks if all given processes are valid and no process name is used
// multiple times. If an error is returned it will be a multierror.
func Validate(pp []Process) error {
errs := newMultiError()
seen := map[string]struct{}{}
for i, p := range pp {
if _, ok := seen[p.Name]; ok {
errs = multierror.Append(errs, errors.Errorf("process %d: name %q is already used", i+1, p.Name))
}
seen[p.Name] = struct{}{}
err := p.Validate()
if err == nil {
continue
}
id := fmt.Sprintf("%q", strings.TrimSpace(p.Name))
if id == `""` {
id = fmt.Sprint(i + 1)
}
for _, err := range err.(*multierror.Error).Errors {
errs = multierror.Append(errs, errors.Wrap(err, "process "+id))
}
}
return errs.ErrorOrNil()
}
// Validate checks that the Process configuration is complete and without errors.
// If an error is returned it will be a multierror.
func (p Process) Validate() error {
errs := newMultiError()
if strings.TrimSpace(p.Name) == "" {
errs = multierror.Append(errs, errors.New("missing name"))
}
if strings.TrimSpace(p.Script) == "" {
errs = multierror.Append(errs, errors.New("missing script"))
}
switch p.Output.Format {
case "", "auto":
// using default values, nothing to check
case "json":
if p.Output.MessageField == "" {
errs = multierror.Append(errs, errors.New(`missing log output "message" field`))
}
if p.Output.LevelField == "" {
errs = multierror.Append(errs, errors.New(`missing log output "level" field`))
}
default:
errs = multierror.Append(errs, errors.Errorf("unknown log output format %q", p.Output.Format))
}
return errs.ErrorOrNil()
}
// A process is an abstraction of a child process which is started by the
// Executor.
type process interface {
Name() string
Info() ProcessInfo
Run(context.Context) error
}
// a systemProcess is a Process implementation that uses os/exec to start shell
// processes.
type systemProcess struct {
name string
script string
env Environment
output io.Writer
logger *zap.Logger
startedAt time.Time
interruptTimeout time.Duration
mu sync.Mutex
cmd *exec.Cmd
}
// newSystemProcess creates a new process that executes the given script as a
// new system process (using os/exec).
func newSystemProcess(name, script string, env Environment, output io.Writer, logger *zap.Logger) *systemProcess {
if logger == nil {
logger = zap.NewNop()
}
return &systemProcess{
script: script,
name: name,
interruptTimeout: 5 * time.Second,
env: env,
output: output,
logger: logger,
}
}
// Name returns the human readable name of p that can be used to identify a
// specific process.
func (p *systemProcess) Name() string {
return p.name
}
func (p *systemProcess) Info() ProcessInfo {
p.mu.Lock()
defer p.mu.Unlock()
if p.cmd == nil || p.cmd.Process == nil {
return ProcessInfo{PID: -1}
}
return ProcessInfo{
PID: p.cmd.Process.Pid,
Uptime: time.Since(p.startedAt),
}
}
// Run starts the shell process and blocks until it finishes or the context is
// done. The systemProcess.output receives both the stdout and stderr output
// of the process.
func (p *systemProcess) Run(ctx context.Context) error {
p.mu.Lock()
args, err := p.parseCommandLine()
if err != nil {
return errors.Wrap(err, "failed to parse command line")
}
p.logger.Debug("Starting new shell process", zap.Strings("script", args))
p.cmd = exec.Command("env", args...)
p.cmd.Stdout = p.output
p.cmd.Stderr = p.output
p.cmd.Env = p.env.List()
p.startedAt = time.Now()
err = p.cmd.Start()
p.mu.Unlock()
if err != nil {
return fmt.Errorf("could not start shell task: %s", err)
}
return p.wait(ctx)
}
func (p *systemProcess) wait(ctx context.Context) error {
done := make(chan error)
go func() {
done <- p.cmd.Wait()
}()
// n.b. By default child processes are often started in the same
// process group as the parent. Under these circumstances the shell
// will send the signal to all processes, causing them to terminate on
// their own. We cannot rely on this behavior but we should not report
// an error if the process has already finished before we asked it to.
select {
case err := <-done:
if err != nil && strings.HasPrefix(err.Error(), "signal: ") {
// see note from above...
// TODO: this seems fishy since it also hides issues with broken output such as "signal: broken pipe"
// also matching errors based on string prefixes is pretty much an anti-pattern
err = nil
}
return err
case <-ctx.Done():
if p.cmd.ProcessState != nil && p.cmd.ProcessState.Exited() {
// There is nothing to do anymore so we can return early.
return ctx.Err()
}
p.logger.Info("Sending interrupt signal", zap.Duration("timeout", p.interruptTimeout))
/*
TODO: to kill all child processes as well try this:
group, err := os.FindProcess(-1 * p.Process.Pid)
if err == nil {
err = group.Signal(signal)
}
*/
// TODO: this results in our child processes to receive SIGINT twice, due to the process group issue (e.g. visible in redis)
err := p.cmd.Process.Signal(syscall.SIGINT)
if err != nil && err.Error() != "os: process already finished" {
p.logger.Error("Failed to send SIGINT to process", zap.Error(err))
p.cmd.Process.Kill()
return ctx.Err()
}
select {
case <-done:
p.logger.Debug("Process interrupted successfully", zap.Error(err))
case <-time.After(p.interruptTimeout):
err := p.cmd.Process.Kill()
if err != nil {
p.logger.Error("Failed to kill process", zap.Error(err))
}
}
return ctx.Err()
}
}
func (p *systemProcess) parseCommandLine() ([]string, error) {
var (
args []string
buf string
escaped bool
doubleQuoted bool
singleQuoted bool
)
for _, r := range p.script {
switch {
case escaped:
buf += string(r)
escaped = false
case r == '\\' && !singleQuoted:
escaped = true
case unicode.IsSpace(r) && (singleQuoted || doubleQuoted):
buf += string(r)
case unicode.IsSpace(r) && buf != "":
args = append(args, buf)
buf = ""
case unicode.IsSpace(r) && buf == "":
// collapse (i.e. ignore) multiple spaces between arguments
continue
case r == '"' && !singleQuoted:
doubleQuoted = !doubleQuoted
case r == '\'' && !doubleQuoted:
singleQuoted = !singleQuoted
case r == ';', r == '&', r == '|', r == '<', r == '>':
if !(escaped || singleQuoted || doubleQuoted) {
return nil, errors.Errorf("command redirection or piping is not supported(got %v)", r)
} else {
buf += string(r)
}
default:
buf += string(r)
}
}
switch {
case escaped:
return nil, errors.New("bad escape at the end")
case singleQuoted:
return nil, errors.New("unclosed single quote")
case doubleQuoted:
return nil, errors.New("unclosed double quote")
}
if buf != "" {
args = append(args, buf)
}
envRe := regexp.MustCompile(`\$({[a-zA-Z0-9_]+}|[a-zA-Z0-9_]+)`) // TODO: use os.Expand
for i := range args {
args[i] = envRe.ReplaceAllStringFunc(args[i], func(s string) string {
s = s[1:]
if s[0] == '{' {
s = s[1 : len(s)-1]
}
return p.env.Get(s, "")
})
}
return args, nil
}
// CommandLine returns the shell command line that would be executed when the
// given Process is started.
func (p Process) CommandLine() ([]string, error) {
sp := newSystemProcess(p.Name, p.Script, p.Env, nil, nil)
return sp.parseCommandLine()
}