From b12a508e4e0a82741ab6eef6be029c2a411f559b Mon Sep 17 00:00:00 2001 From: Mrunal Patel Date: Thu, 17 Nov 2016 16:40:52 -0800 Subject: [PATCH] Add method to exec a command sync in a container Signed-off-by: Mrunal Patel --- oci/oci.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/oci/oci.go b/oci/oci.go index abfa558b..d4e480f4 100644 --- a/oci/oci.go +++ b/oci/oci.go @@ -148,6 +148,30 @@ func (r *Runtime) StartContainer(c *Container) error { return nil } +// ExecSync execs a command in a container and returns it's stdout, stderr and return code. +func (r *Runtime) ExecSync(c *Container, command []string, timeout int64) (stdout []byte, stderr []byte, exitCode int32, err error) { + args := []string{"exec", c.name} + args = append(args, command...) + cmd := exec.Command(r.Path(), args...) + logrus.Debugf("Command: +v\n", cmd) + var stdoutBuf, stderrBuf bytes.Buffer + cmd.Stdout = &stdoutBuf + cmd.Stderr = &stderrBuf + err = cmd.Start() + if err != nil { + return stdoutBuf.Bytes(), stderrBuf.Bytes(), -1, err + } + err = cmd.Wait() + if err != nil { + if exitErr, ok := err.(*exec.ExitError); ok { + if status, ok := exitErr.Sys().(syscall.WaitStatus); ok { + return stdoutBuf.Bytes(), stderrBuf.Bytes(), int32(status.ExitStatus()), err + } + } + } + return stdoutBuf.Bytes(), stderrBuf.Bytes(), 0, nil +} + // StopContainer stops a container. func (r *Runtime) StopContainer(c *Container) error { if err := utils.ExecCmdWithStdStreams(os.Stdin, os.Stdout, os.Stderr, r.path, "kill", c.name); err != nil {