-
Notifications
You must be signed in to change notification settings - Fork 21
/
runner.go
73 lines (65 loc) · 1.7 KB
/
runner.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
package main
import (
"fmt"
"os"
"os/signal"
"strings"
"syscall"
log "github.com/sirupsen/logrus"
)
type CommandFailedError struct {
ExitCode int
}
func (e *CommandFailedError) Error() string {
return fmt.Sprintf("Command failed with exit code %d", e.ExitCode)
}
func RunCommand(command string, args []string, envVars []string) error {
log.Infof("PID %v running %s %s", os.Getpid(), command,
strings.Join(args[:], " "))
procAttr := new(os.ProcAttr)
procAttr.Env = envVars
procAttr.Files = []*os.File{os.Stdin, os.Stdout, os.Stderr}
// prefix args with the command, as per https://golang.org/pkg/os/#StartProcess
// The argv slice will become os.Args in the new process, so it normally starts
// with the program name.
args = append([]string{command}, args...)
proc, err := os.StartProcess(command, args, procAttr)
if err != nil {
return err
}
sigc := make(chan os.Signal, 1)
signal.Notify(sigc,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT)
go func() {
sigv := <-sigc
switch sigv {
case syscall.SIGHUP:
err = syscall.Kill(-os.Getpid(), syscall.SIGHUP)
case syscall.SIGINT:
err = syscall.Kill(-os.Getpid(), syscall.SIGINT)
case syscall.SIGTERM:
err = syscall.Kill(-os.Getpid(), syscall.SIGTERM)
case syscall.SIGQUIT:
err = syscall.Kill(-os.Getpid(), syscall.SIGQUIT)
default:
err = syscall.Kill(-os.Getpid(), syscall.SIGTERM)
}
log.WithFields(log.Fields{
"err": err,
"proc": proc,
"pid": -proc.Pid,
"signal": sigv},
).Info("Caught signal, sent to child")
}()
procState, err := proc.Wait()
if err != nil {
return err
}
if procState.ExitCode() != 0 {
return &CommandFailedError{procState.ExitCode()}
}
return nil
}