/
process.go
323 lines (284 loc) · 8.53 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
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package process
import (
"bytes"
"fmt"
"io"
"log"
"os"
"os/exec"
"os/signal"
"strings"
"sync"
"syscall"
"time"
)
// Control can commands until a timeout is reached, at which point it signals and then terminates them.
type Control struct {
termLock *sync.RWMutex
terminated bool
intLock *sync.RWMutex
interrupted bool
Timeout time.Duration
Interrupt *time.Timer
Terminate *time.Timer
verbose bool
}
// NewControl constructs a Control with the specified arguments, instiating other necessary fields.
func NewControl(timeout time.Duration, interrupt, terminate *time.Timer, verbose bool) *Control {
return &Control{
termLock: new(sync.RWMutex),
terminated: false,
intLock: new(sync.RWMutex),
interrupted: false,
Timeout: timeout,
Interrupt: interrupt,
Terminate: terminate,
verbose: verbose,
}
}
func (c *Control) isTerminated() bool {
c.termLock.RLock()
t := c.terminated
c.termLock.RUnlock()
return t
}
func (c *Control) isInterrupted() bool {
c.intLock.RLock()
i := c.interrupted
c.intLock.RUnlock()
return i
}
// FinishRunning returns cmd.Wait() and/or times out.
func (c *Control) FinishRunning(cmd *exec.Cmd) error {
stepName := strings.Join(cmd.Args, " ")
if c.isTerminated() {
return fmt.Errorf("skipped %s (kubetest is terminated)", stepName)
}
if cmd.Stdout == nil && c.verbose {
cmd.Stdout = os.Stdout
}
if cmd.Stderr == nil && c.verbose {
cmd.Stderr = os.Stderr
}
log.Printf("Running: %v", stepName)
defer func(start time.Time) {
log.Printf("Step '%s' finished in %s", stepName, time.Since(start))
}(time.Now())
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
if err := cmd.Start(); err != nil {
return fmt.Errorf("error starting %v: %v", stepName, err)
}
finished := make(chan error)
sigChannel := make(chan os.Signal, 1)
signal.Notify(sigChannel, os.Interrupt)
go func() {
finished <- cmd.Wait()
}()
for {
select {
case <-sigChannel:
log.Printf("Killing %v(%v) after receiving signal", stepName, -cmd.Process.Pid)
pgid := getGroupPid(cmd.Process.Pid)
if err := syscall.Kill(-pgid, syscall.SIGKILL); err != nil {
log.Printf("Failed to kill %v: %v", stepName, err)
}
case <-c.Terminate.C:
c.termLock.Lock()
c.terminated = true
c.termLock.Unlock()
c.Terminate.Reset(time.Duration(-1)) // Kill subsequent processes immediately.
pgid := getGroupPid(cmd.Process.Pid)
if err := syscall.Kill(-pgid, syscall.SIGKILL); err != nil {
log.Printf("Failed to kill %v: %v", stepName, err)
}
if err := cmd.Process.Kill(); err != nil {
log.Printf("Failed to terminate %s (terminated 15m after interrupt): %v", stepName, err)
}
case <-c.Interrupt.C:
c.intLock.Lock()
c.interrupted = true
c.intLock.Unlock()
log.Printf("Interrupt after %s timeout during %s. Will terminate in another 15m", c.Timeout, stepName)
c.Terminate.Reset(15 * time.Minute)
pgid := getGroupPid(cmd.Process.Pid)
if err := syscall.Kill(-pgid, syscall.SIGINT); err != nil {
log.Printf("Failed to interrupt %s. Will terminate immediately: %v", stepName, err)
syscall.Kill(-pgid, syscall.SIGTERM)
cmd.Process.Kill()
}
case err := <-finished:
if err != nil {
var suffix string
if c.isTerminated() {
suffix = " (terminated)"
} else if c.isInterrupted() {
suffix = " (interrupted)"
}
return fmt.Errorf("error during %s%s: %v", stepName, suffix, err)
}
return err
}
}
}
type cmdExecResult struct {
stepName string
output string
execTime time.Duration
err error
}
// executeParallelCommand executes a given command and send output and error via channel
func (c *Control) executeParallelCommand(cmd *exec.Cmd, resChan chan cmdExecResult, termChan, intChan chan struct{}) {
stepName := strings.Join(cmd.Args, " ")
stdout := bytes.Buffer{}
cmd.Stdout = &stdout
cmd.Stderr = &stdout
start := time.Now()
log.Printf("Running: %v in parallel", stepName)
if c.isTerminated() {
resChan <- cmdExecResult{stepName: stepName, output: stdout.String(), execTime: time.Since(start), err: fmt.Errorf("skipped %s (kubetest is terminated)", stepName)}
return
}
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
if err := cmd.Start(); err != nil {
resChan <- cmdExecResult{stepName: stepName, output: stdout.String(), execTime: time.Since(start), err: fmt.Errorf("error starting %v: %v", stepName, err)}
return
}
finished := make(chan error)
go func() {
finished <- cmd.Wait()
}()
for {
select {
case err := <-finished:
if err != nil {
var suffix string
if c.isTerminated() {
suffix = " (terminated)"
} else if c.isInterrupted() {
suffix = " (interrupted)"
}
err = fmt.Errorf("error during %s%s: %v", stepName, suffix, err)
}
resChan <- cmdExecResult{stepName: stepName, output: stdout.String(), execTime: time.Since(start), err: err}
return
case <-termChan:
pgid := getGroupPid(cmd.Process.Pid)
syscall.Kill(-pgid, syscall.SIGKILL)
if err := cmd.Process.Kill(); err != nil {
log.Printf("Failed to terminate %s (terminated 15m after interrupt): %v", strings.Join(cmd.Args, " "), err)
}
case <-intChan:
log.Printf("Abort after %s timeout during %s. Will terminate in another 15m", c.Timeout, strings.Join(cmd.Args, " "))
pgid := getGroupPid(cmd.Process.Pid)
if err := syscall.Kill(-pgid, syscall.SIGABRT); err != nil {
log.Printf("Failed to abort %s. Will terminate immediately: %v", strings.Join(cmd.Args, " "), err)
syscall.Kill(-pgid, syscall.SIGTERM)
cmd.Process.Kill()
}
}
}
}
// FinishRunningParallel executes multiple commands in parallel
func (c *Control) FinishRunningParallel(cmds ...*exec.Cmd) error {
var wg sync.WaitGroup
resultChan := make(chan cmdExecResult, len(cmds))
termChan := make(chan struct{}, len(cmds))
intChan := make(chan struct{}, len(cmds))
for _, cmd := range cmds {
wg.Add(1)
go func(cmd *exec.Cmd) {
defer wg.Done()
c.executeParallelCommand(cmd, resultChan, termChan, intChan)
}(cmd)
}
go func() {
wg.Wait()
close(resultChan)
}()
cmdFailed := false
for {
select {
case <-c.Terminate.C:
c.termLock.Lock()
c.terminated = true
c.termLock.Unlock()
c.Terminate.Reset(time.Duration(0))
select {
case <-termChan:
default:
close(termChan)
}
case <-c.Interrupt.C:
c.intLock.Lock()
c.interrupted = true
c.intLock.Unlock()
c.Terminate.Reset(15 * time.Minute)
close(intChan)
case result, ok := <-resultChan:
if !ok {
if cmdFailed {
return fmt.Errorf("one or more commands failed")
}
return nil
}
log.Print(result.output)
if result.err != nil {
cmdFailed = true
}
log.Printf("Step '%s' finished in %s", result.stepName, result.execTime)
}
}
}
// InputCommand returns exec.Command(cmd, args...) while calling .StdinPipe().WriteString(input)
func (c *Control) InputCommand(input, cmd string, args ...string) (*exec.Cmd, error) {
command := exec.Command(cmd, args...)
w, e := command.StdinPipe()
if e != nil {
return nil, e
}
go func() {
if _, e = io.WriteString(w, input); e != nil {
log.Printf("Failed to write all %d chars to %s: %v", len(input), cmd, e)
}
if e = w.Close(); e != nil {
log.Printf("Failed to close stdin for %s: %v", cmd, e)
}
}()
return command, nil
}
// Output returns cmd.Output(), potentially timing out in the process.
func (c *Control) Output(cmd *exec.Cmd) ([]byte, error) {
var stdout bytes.Buffer
cmd.Stdout = &stdout
err := c.FinishRunning(cmd)
return stdout.Bytes(), err
}
// NoOutput ignores all output from the command, potentially timing out in the process.
func (c *Control) NoOutput(cmd *exec.Cmd) error {
var void bytes.Buffer
cmd.Stdout = &void
cmd.Stderr = &void
return c.FinishRunning(cmd)
}
// getGroupPid gets the process group to kill the entire main/child process
// if Getpgid return error use the current process Pid
func getGroupPid(pid int) int {
pgid, err := syscall.Getpgid(pid)
if err != nil {
log.Printf("Failed to get the group process from %v: %v", pid, err)
return pid
}
return pgid
}