/
command.go
146 lines (114 loc) · 2.97 KB
/
command.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
package command
import (
"context"
"errors"
"fmt"
"io"
"os"
"os/exec"
"strconv"
"syscall"
"time"
"github.com/sirupsen/logrus"
"gitlab.com/gitlab-org/gitlab-runner/common"
"gitlab.com/gitlab-org/gitlab-runner/executors/custom/api"
"gitlab.com/gitlab-org/gitlab-runner/executors/custom/process"
)
const (
BuildFailureExitCode = 1
SystemFailureExitCode = 2
)
type CreateOptions struct {
Dir string
Env []string
Stdout io.Writer
Stderr io.Writer
Logger common.BuildLogger
GracefulKillTimeout time.Duration
ForceKillTimeout time.Duration
}
type Command interface {
Run() error
}
type command struct {
context context.Context
cmd commander
waitCh chan error
logger common.BuildLogger
gracefulKillTimeout time.Duration
forceKillTimeout time.Duration
}
func New(ctx context.Context, executable string, args []string, options CreateOptions) Command {
defaultVariables := map[string]string{
"TMPDIR": options.Dir,
api.BuildFailureExitCodeVariable: strconv.Itoa(BuildFailureExitCode),
api.SystemFailureExitCodeVariable: strconv.Itoa(SystemFailureExitCode),
}
env := os.Environ()
for key, value := range defaultVariables {
env = append(env, fmt.Sprintf("%s=%s", key, value))
}
options.Env = append(env, options.Env...)
return &command{
context: ctx,
cmd: newCmd(executable, args, options),
waitCh: make(chan error),
logger: options.Logger,
gracefulKillTimeout: options.GracefulKillTimeout,
forceKillTimeout: options.ForceKillTimeout,
}
}
func (c *command) Run() error {
err := c.cmd.Start()
if err != nil {
return fmt.Errorf("failed to start command: %v", err)
}
go c.waitForCommand()
select {
case err = <-c.waitCh:
return err
case <-c.context.Done():
return c.killAndWait()
}
}
var getExitStatus = func(err *exec.ExitError) int {
// TODO: simplify when we will update to Go 1.12. ExitStatus()
// is available there directly from err.Sys().
return err.Sys().(syscall.WaitStatus).ExitStatus()
}
func (c *command) waitForCommand() {
err := c.cmd.Wait()
eerr, ok := err.(*exec.ExitError)
if ok {
exitCode := getExitStatus(eerr)
if exitCode == BuildFailureExitCode {
err = &common.BuildError{Inner: eerr}
} else if exitCode != SystemFailureExitCode {
err = &ErrUnknownFailure{Inner: eerr, ExitCode: exitCode}
}
}
c.waitCh <- err
}
var newProcessKiller = process.NewKiller
func (c *command) killAndWait() error {
if c.cmd.Process() == nil {
return errors.New("process not started yet")
}
logger := c.logger.WithFields(logrus.Fields{
"PID": c.cmd.Process().Pid,
})
processKiller := newProcessKiller(logger, c.cmd.Process())
processKiller.Terminate()
select {
case err := <-c.waitCh:
return err
case <-time.After(c.gracefulKillTimeout):
processKiller.ForceKill()
select {
case err := <-c.waitCh:
return err
case <-time.After(c.forceKillTimeout):
return errors.New("failed to kill process, likely process is dormant")
}
}
}