mirror of
https://github.com/k3s-io/k3s.git
synced 2024-06-07 19:41:36 +00:00
11ef43011a
Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
55 lines
968 B
Go
55 lines
968 B
Go
package common
|
|
|
|
import (
|
|
"io"
|
|
"os/exec"
|
|
"syscall"
|
|
|
|
"github.com/pkg/errors"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// ErrorWithSys is implemented by *exec.ExitError and *child.reaperErr
|
|
type ErrorWithSys interface {
|
|
error
|
|
Sys() interface{}
|
|
}
|
|
|
|
func GetExecExitStatus(err error) (int, bool) {
|
|
err = errors.Cause(err)
|
|
if err == nil {
|
|
return 0, false
|
|
}
|
|
exitErr, ok := err.(ErrorWithSys)
|
|
if !ok {
|
|
return 0, false
|
|
}
|
|
status, ok := exitErr.Sys().(syscall.WaitStatus)
|
|
if !ok {
|
|
return 0, false
|
|
}
|
|
return status.ExitStatus(), true
|
|
}
|
|
|
|
func Execs(o io.Writer, env []string, cmds [][]string) error {
|
|
for _, cmd := range cmds {
|
|
var args []string
|
|
if len(cmd) > 1 {
|
|
args = cmd[1:]
|
|
}
|
|
x := exec.Command(cmd[0], args...)
|
|
x.Stdin = nil
|
|
x.Stdout = o
|
|
x.Stderr = o
|
|
x.Env = env
|
|
x.SysProcAttr = &syscall.SysProcAttr{
|
|
Pdeathsig: syscall.SIGKILL,
|
|
}
|
|
logrus.Debugf("executing %v", cmd)
|
|
if err := x.Run(); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|