Rework exec to enable splitting to retrieve exec PID

Signed-off-by: Matthew Heon <matthew.heon@gmail.com>

Closes: #412
Approved by: baude
This commit is contained in:
Matthew Heon
2018-02-27 09:50:22 -05:00
committed by Atomic Bot
parent 2a0c949b9b
commit 345bfafee2
2 changed files with 51 additions and 20 deletions

View File

@ -2,13 +2,11 @@ package libpod
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/docker/docker/daemon/caps"
"github.com/docker/docker/pkg/stringid"
"github.com/docker/docker/pkg/term"
"github.com/pkg/errors"
"github.com/projectatomic/libpod/libpod/driver"
@ -236,21 +234,8 @@ func (c *Container) Exec(tty, privileged bool, env, cmd []string, user string) e
if privileged || c.config.Privileged {
capList = caps.GetAllCapabilities()
}
globalOpts := runcGlobalOptions{
log: c.LogPath(),
}
execOpts := runcExecOptions{
capAdd: capList,
pidFile: filepath.Join(c.state.RunDir, fmt.Sprintf("%s-execpid", stringid.GenerateNonCryptoID()[:12])),
env: env,
noNewPrivs: c.config.Spec.Process.NoNewPrivileges,
user: user,
cwd: c.config.Spec.Process.Cwd,
tty: tty,
}
return c.runtime.ociRuntime.execContainer(c, cmd, globalOpts, execOpts)
return c.runtime.ociRuntime.execContainer(c, cmd, tty, user, capList, env)
}
// Attach attaches to a container

View File

@ -467,8 +467,54 @@ func (r *OCIRuntime) unpauseContainer(ctr *Container) error {
return utils.ExecCmdWithStdStreams(os.Stdin, os.Stdout, os.Stderr, r.path, "resume", ctr.ID())
}
//execContiner executes a command in a running container
func (r *OCIRuntime) execContainer(c *Container, cmd []string, globalOpts runcGlobalOptions, commandOpts runcExecOptions) error {
r.RuncExec(c, cmd, globalOpts, commandOpts)
return nil
// execContainer executes a command in a running container
// TODO: Add --detach support
// TODO: Convert to use conmon
// TODO: add --pid-file and use that to generate exec session tracking
func (r *OCIRuntime) execContainer(c *Container, cmd []string, tty bool, user string, capAdd, env []string) error {
args := []string{}
// TODO - should we maintain separate logpaths for exec sessions?
args = append(args, "--log", c.LogPath())
args = append(args, "exec")
args = append(args, "--cwd", c.config.Spec.Process.Cwd)
if tty {
args = append(args, "--tty")
}
if user != "" {
args = append(args, "--user", user)
}
if c.config.Spec.Process.NoNewPrivileges {
args = append(args, "--no-new-privs")
}
for _, cap := range capAdd {
args = append(args, "--cap", cap)
}
for _, envVar := range env {
args = append(args, "--env", envVar)
}
// Append container ID and command
args = append(args, c.ID())
args = append(args, cmd...)
logrus.Debugf("Starting runtime %s with following arguments: %v", r.path, args)
execCmd := exec.Command(r.path, args...)
execCmd.Stdout = os.Stdout
execCmd.Stderr = os.Stderr
execCmd.Stdin = os.Stdin
if err := execCmd.Start(); err != nil {
return errors.Wrapf(err, "error starting exec command for container %s", c.ID())
}
return execCmd.Wait()
}