forked from argoproj/argo-cd
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexec.go
More file actions
378 lines (337 loc) · 11 KB
/
exec.go
File metadata and controls
378 lines (337 loc) · 11 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
package exec
import (
"bytes"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"syscall"
"time"
"unicode"
"github.com/argoproj/gitops-engine/pkg/utils/tracing"
"github.com/sirupsen/logrus"
"github.com/argoproj/argo-cd/v3/util/log"
"github.com/argoproj/argo-cd/v3/util/rand"
)
var (
timeout time.Duration
fatalTimeout time.Duration
Unredacted = Redact(nil)
)
type ExecRunOpts struct {
// Redactor redacts tokens from the output
Redactor func(text string) string
// TimeoutBehavior configures what to do in case of timeout
TimeoutBehavior TimeoutBehavior
// SkipErrorLogging determines whether to skip logging of execution errors (rc > 0)
SkipErrorLogging bool
// CaptureStderr determines whether to capture stderr in addition to stdout
CaptureStderr bool
}
func init() {
initTimeout()
}
func initTimeout() {
var err error
timeout, err = time.ParseDuration(os.Getenv("ARGOCD_EXEC_TIMEOUT"))
if err != nil {
timeout = 90 * time.Second
}
fatalTimeout, err = time.ParseDuration(os.Getenv("ARGOCD_EXEC_FATAL_TIMEOUT"))
if err != nil {
fatalTimeout = 10 * time.Second
}
}
func Run(cmd *exec.Cmd) (string, error) {
return RunWithRedactor(cmd, nil)
}
func RunWithRedactor(cmd *exec.Cmd, redactor func(text string) string) (string, error) {
opts := ExecRunOpts{Redactor: redactor}
return RunWithExecRunOpts(cmd, opts)
}
func RunWithExecRunOpts(cmd *exec.Cmd, opts ExecRunOpts) (string, error) {
cmdOpts := CmdOpts{Timeout: timeout, FatalTimeout: fatalTimeout, Redactor: opts.Redactor, TimeoutBehavior: opts.TimeoutBehavior, SkipErrorLogging: opts.SkipErrorLogging}
span := tracing.NewLoggingTracer(log.NewLogrusLogger(log.NewWithCurrentConfig())).StartSpan(fmt.Sprintf("exec %v", cmd.Args[0]))
span.SetBaggageItem("dir", cmd.Dir)
if cmdOpts.Redactor != nil {
span.SetBaggageItem("args", opts.Redactor(fmt.Sprintf("%v", cmd.Args)))
} else {
span.SetBaggageItem("args", fmt.Sprintf("%v", cmd.Args))
}
defer span.Finish()
return RunCommandExt(cmd, cmdOpts)
}
// GetCommandArgsToLog represents the given command in a way that we can copy-and-paste into a terminal
func GetCommandArgsToLog(cmd *exec.Cmd) string {
var argsToLog []string
for _, arg := range cmd.Args {
if arg == "" {
argsToLog = append(argsToLog, `""`)
continue
}
containsSpace := false
for _, r := range arg {
if unicode.IsSpace(r) {
containsSpace = true
break
}
}
if containsSpace {
// add quotes and escape any internal quotes
argsToLog = append(argsToLog, strconv.Quote(arg))
} else {
argsToLog = append(argsToLog, arg)
}
}
args := strings.Join(argsToLog, " ")
return args
}
type CmdError struct {
Args string
Stderr string
Cause error
}
func (ce *CmdError) Error() string {
res := fmt.Sprintf("`%v` failed %v", ce.Args, ce.Cause)
if ce.Stderr != "" {
res = fmt.Sprintf("%s: %s", res, ce.Stderr)
}
return res
}
func (ce *CmdError) String() string {
return ce.Error()
}
func newCmdError(args string, cause error, stderr string) *CmdError {
return &CmdError{Args: args, Stderr: stderr, Cause: cause}
}
// TimeoutBehavior defines behavior for when the command takes longer than the passed in timeout to exit
// By default, SIGKILL is sent to the process and it is not waited upon
type TimeoutBehavior struct {
// Signal determines the signal to send to the process
Signal syscall.Signal
// ShouldWait determines whether to wait for the command to exit once timeout is reached
ShouldWait bool
}
type CmdOpts struct {
// Timeout determines how long to wait for the command to exit
Timeout time.Duration
// FatalTimeout is the amount of additional time to wait after Timeout before fatal SIGKILL
FatalTimeout time.Duration
// Redactor redacts tokens from the output
Redactor func(text string) string
// TimeoutBehavior configures what to do in case of timeout
TimeoutBehavior TimeoutBehavior
// SkipErrorLogging defines whether to skip logging of execution errors (rc > 0)
SkipErrorLogging bool
// CaptureStderr defines whether to capture stderr in addition to stdout
CaptureStderr bool
}
var DefaultCmdOpts = CmdOpts{
Timeout: time.Duration(0),
FatalTimeout: time.Duration(0),
Redactor: Unredacted,
TimeoutBehavior: TimeoutBehavior{syscall.SIGKILL, false},
SkipErrorLogging: false,
CaptureStderr: false,
}
func Redact(items []string) func(text string) string {
return func(text string) string {
for _, item := range items {
text = strings.ReplaceAll(text, item, "******")
}
return text
}
}
// RunCommandExt is a convenience function to run/log a command and return/log stderr in an error upon
// failure.
func RunCommandExt(cmd *exec.Cmd, opts CmdOpts) (string, error) {
execId, err := rand.RandHex(5)
if err != nil {
return "", err
}
logCtx := logrus.WithFields(logrus.Fields{"execID": execId})
redactor := DefaultCmdOpts.Redactor
if opts.Redactor != nil {
redactor = opts.Redactor
}
// log in a way we can copy-and-paste into a terminal
args := strings.Join(cmd.Args, " ")
logCtx.WithFields(logrus.Fields{"dir": cmd.Dir}).Info(redactor(args))
// Best-effort cleanup of a stale HEAD.lock after the command finishes.
defer func() {
if cmd.Dir == "" {
return
}
lockPath := filepath.Join(cmd.Dir, ".git", "HEAD.lock")
logCtx.WithFields(logrus.Fields{"headLockPath": lockPath}).Info("Checking HEAD.lock presence post-exec")
if _, err := os.Stat(lockPath); err == nil {
// Log and attempt removal; ignore ENOENT races
logCtx.WithFields(logrus.Fields{"headLockPath": lockPath}).Warn("HEAD.lock present post-exec, removing it")
if rmErr := os.Remove(lockPath); rmErr != nil && !os.IsNotExist(rmErr) {
logCtx.WithFields(logrus.Fields{"headLockPath": lockPath}).Warnf("Failed to remove HEAD.lock: %v", rmErr)
}
} else if !os.IsNotExist(err) {
logCtx.WithFields(logrus.Fields{"headLockPath": lockPath}).Warnf("Failed to stat HEAD.lock: %v", err)
} else {
logCtx.WithFields(logrus.Fields{"headLockPath": lockPath}).Info("HEAD.lock not present post-exec")
}
}()
// Helper: debug whether HEAD.lock exists under the current working directory
logHeadLockStatus := func(where string) {
if cmd.Dir == "" {
return
}
lockPath := filepath.Join(cmd.Dir, ".git", "HEAD.lock")
fileInfo, statErr := os.Stat(lockPath)
exists := statErr == nil
fields := logrus.Fields{
"headLockPath": lockPath,
"headLockExists": exists,
"where": where,
}
pgid, pgErr := syscall.Getpgid(cmd.Process.Pid)
if pgErr == nil && pgid > 0 {
// Portable ps: list all processes, print needed columns without headers, then filter by PGID in Go.
out, _ := exec.Command(
"ps",
"-ax",
"-o", "pid=,ppid=,pgid=,etime=,comm=,args=",
).Output()
if len(out) > 0 {
var b strings.Builder
want := strconv.Itoa(pgid)
for _, line := range strings.Split(string(out), "\n") {
line = strings.TrimSpace(line)
if line == "" {
continue
}
fieldsSlice := strings.Fields(line)
if len(fieldsSlice) < 3 {
continue
}
if fieldsSlice[2] == want {
b.WriteString(line)
b.WriteByte('\n')
}
}
fields["gitProcsInGroup"] = strings.TrimSpace(b.String())
}
}
if exists {
fields["headLockSize"] = fileInfo.Size()
fields["headLockMode"] = fileInfo.Mode().String()
fields["headLockModTime"] = fileInfo.ModTime()
fields["headLockIsDir"] = fileInfo.IsDir()
}
logCtx.WithFields(fields).Info("HEAD.lock status")
}
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
// Configure the child to run in its own process group so we can signal the whole group on timeout/cancel.
// On Unix this sets Setpgid; on Windows this is a no-op.
if cmd.SysProcAttr == nil {
cmd.SysProcAttr = newSysProcAttr(true)
}
start := time.Now()
err = cmd.Start()
if err != nil {
return "", err
}
logHeadLockStatus("start-exec")
done := make(chan error)
go func() { done <- cmd.Wait() }()
// Start timers for timeout
timeout := DefaultCmdOpts.Timeout
fatalTimeout := DefaultCmdOpts.FatalTimeout
if opts.Timeout != time.Duration(0) {
timeout = opts.Timeout
}
if opts.FatalTimeout != time.Duration(0) {
fatalTimeout = opts.FatalTimeout
}
var timoutCh <-chan time.Time
if timeout != 0 {
timoutCh = time.NewTimer(timeout).C
}
var fatalTimeoutCh <-chan time.Time
if fatalTimeout != 0 {
fatalTimeoutCh = time.NewTimer(timeout + fatalTimeout).C
}
timeoutBehavior := DefaultCmdOpts.TimeoutBehavior
fatalTimeoutBehaviour := syscall.SIGKILL
if opts.TimeoutBehavior.Signal != syscall.Signal(0) {
timeoutBehavior = opts.TimeoutBehavior
}
select {
// noinspection ALL
case <-timoutCh:
// send timeout signal
// signal the process group (negative PID) so children are terminated as well
if cmd.Process != nil {
_ = sysCallSignal(-cmd.Process.Pid, timeoutBehavior.Signal)
}
// wait on timeout signal and fallback to fatal timeout signal
if timeoutBehavior.ShouldWait {
select {
case <-done:
logHeadLockStatus("timeout-waited-done")
case <-fatalTimeoutCh:
// upgrades to fatal signal (default SIGKILL) if cmd does not respect the initial signal
if cmd.Process != nil {
_ = sysCallSignal(-cmd.Process.Pid, fatalTimeoutBehaviour)
}
// now original cmd should exit immediately after SIGKILL
<-done
// return error with a marker indicating that cmd exited only after fatal SIGKILL
output := stdout.String()
if opts.CaptureStderr {
output += stderr.String()
}
logCtx.WithFields(logrus.Fields{"duration": time.Since(start)}).Debug(redactor(output))
logHeadLockStatus("fatal-timeout")
err = newCmdError(redactor(args), fmt.Errorf("fatal timeout after %v", timeout+fatalTimeout), "")
logCtx.Error(err.Error())
return strings.TrimSuffix(output, "\n"), err
}
}
// either did not wait for timeout or cmd did respect SIGTERM
output := stdout.String()
if opts.CaptureStderr {
output += stderr.String()
}
logCtx.WithFields(logrus.Fields{"duration": time.Since(start)}).Debug(redactor(output))
logHeadLockStatus("timeout")
err = newCmdError(redactor(args), fmt.Errorf("timeout after %v", timeout), "")
logCtx.Error(err.Error())
return strings.TrimSuffix(output, "\n"), err
case err := <-done:
if err != nil {
output := stdout.String()
if opts.CaptureStderr {
output += stderr.String()
}
logCtx.WithFields(logrus.Fields{"duration": time.Since(start)}).Debug(redactor(output))
err := newCmdError(redactor(args), errors.New(redactor(err.Error())), strings.TrimSpace(redactor(stderr.String())))
if !opts.SkipErrorLogging {
logCtx.Error(err.Error())
}
logHeadLockStatus("done-error")
return strings.TrimSuffix(output, "\n"), err
}
}
output := stdout.String()
if opts.CaptureStderr {
output += stderr.String()
}
logCtx.WithFields(logrus.Fields{"duration": time.Since(start)}).Debug(redactor(output))
logHeadLockStatus("done-success")
return strings.TrimSuffix(output, "\n"), nil
}
func RunCommand(name string, opts CmdOpts, arg ...string) (string, error) {
return RunCommandExt(exec.Command(name, arg...), opts)
}