Merge pull request #1207 from runcom/fix-exec-termianl
container_exec: fix terminal true process json
This commit is contained in:
commit
a85ea609db
2 changed files with 30 additions and 39 deletions
47
oci/oci.go
47
oci/oci.go
|
@ -423,15 +423,6 @@ func (r *Runtime) ExecSync(c *Container, command []string, timeout int64) (resp
|
||||||
os.RemoveAll(logPath)
|
os.RemoveAll(logPath)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
f, err := ioutil.TempFile("", "exec-sync-process")
|
|
||||||
if err != nil {
|
|
||||||
return nil, ExecSyncError{
|
|
||||||
ExitCode: -1,
|
|
||||||
Err: err,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
defer os.RemoveAll(f.Name())
|
|
||||||
|
|
||||||
var args []string
|
var args []string
|
||||||
args = append(args, "-c", c.id)
|
args = append(args, "-c", c.id)
|
||||||
args = append(args, "-r", r.Path(c))
|
args = append(args, "-r", r.Path(c))
|
||||||
|
@ -447,24 +438,16 @@ func (r *Runtime) ExecSync(c *Container, command []string, timeout int64) (resp
|
||||||
args = append(args, "-l", logPath)
|
args = append(args, "-l", logPath)
|
||||||
args = append(args, "--socket-dir-path", ContainerAttachSocketDir)
|
args = append(args, "--socket-dir-path", ContainerAttachSocketDir)
|
||||||
|
|
||||||
pspec := c.Spec().Process
|
processFile, err := PrepareProcessExec(c, command, false)
|
||||||
pspec.Args = command
|
|
||||||
processJSON, err := json.Marshal(pspec)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, ExecSyncError{
|
return nil, ExecSyncError{
|
||||||
ExitCode: -1,
|
ExitCode: -1,
|
||||||
Err: err,
|
Err: err,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
defer os.RemoveAll(processFile.Name())
|
||||||
|
|
||||||
if err := ioutil.WriteFile(f.Name(), processJSON, 0644); err != nil {
|
args = append(args, "--exec-process-spec", processFile.Name())
|
||||||
return nil, ExecSyncError{
|
|
||||||
ExitCode: -1,
|
|
||||||
Err: err,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
args = append(args, "--exec-process-spec", f.Name())
|
|
||||||
|
|
||||||
cmd := exec.Command(r.conmonPath, args...)
|
cmd := exec.Command(r.conmonPath, args...)
|
||||||
|
|
||||||
|
@ -772,3 +755,27 @@ func (r *Runtime) UnpauseContainer(c *Container) error {
|
||||||
_, err := utils.ExecCmd(r.Path(c), "resume", c.id)
|
_, err := utils.ExecCmd(r.Path(c), "resume", c.id)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PrepareProcessExec returns the path of the process.json used in runc exec -p
|
||||||
|
// caller is responsible to close the returned *os.File if needed.
|
||||||
|
func PrepareProcessExec(c *Container, cmd []string, tty bool) (*os.File, error) {
|
||||||
|
f, err := ioutil.TempFile("", "exec-process-")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
pspec := c.Spec().Process
|
||||||
|
pspec.Args = cmd
|
||||||
|
if tty {
|
||||||
|
pspec.Terminal = true
|
||||||
|
}
|
||||||
|
processJSON, err := json.Marshal(pspec)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := ioutil.WriteFile(f.Name(), processJSON, 0644); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return f, nil
|
||||||
|
}
|
||||||
|
|
|
@ -1,10 +1,8 @@
|
||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"time"
|
"time"
|
||||||
|
@ -55,28 +53,14 @@ func (ss streamService) Exec(containerID string, cmd []string, stdin io.Reader,
|
||||||
return fmt.Errorf("container is not created or running")
|
return fmt.Errorf("container is not created or running")
|
||||||
}
|
}
|
||||||
|
|
||||||
f, err := ioutil.TempFile("", "exec-process")
|
processFile, err := oci.PrepareProcessExec(c, cmd, tty)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer os.RemoveAll(f.Name())
|
defer os.RemoveAll(processFile.Name())
|
||||||
|
|
||||||
pspec := c.Spec().Process
|
|
||||||
pspec.Args = cmd
|
|
||||||
processJSON, err := json.Marshal(pspec)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := ioutil.WriteFile(f.Name(), processJSON, 0644); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
args := []string{"exec"}
|
args := []string{"exec"}
|
||||||
if tty {
|
args = append(args, "--process", processFile.Name())
|
||||||
args = append(args, "-t")
|
|
||||||
}
|
|
||||||
args = append(args, "-p", f.Name())
|
|
||||||
args = append(args, c.ID())
|
args = append(args, c.ID())
|
||||||
execCmd := exec.Command(ss.runtimeServer.Runtime().Path(c), args...)
|
execCmd := exec.Command(ss.runtimeServer.Runtime().Path(c), args...)
|
||||||
var cmdErr error
|
var cmdErr error
|
||||||
|
|
Loading…
Reference in a new issue