Add exec and terminal support
Signed-off-by: Kenfe-Mickael Laventure <mickael.laventure@gmail.com>
This commit is contained in:
parent
aee6045292
commit
0aad42f5cf
10 changed files with 531 additions and 231 deletions
87
cmd/ctr/exec.go
Normal file
87
cmd/ctr/exec.go
Normal file
|
@ -0,0 +1,87 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
gocontext "context"
|
||||
|
||||
"github.com/docker/containerd/api/execution"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
var execCommand = cli.Command{
|
||||
Name: "exec",
|
||||
Usage: "exec a new process in a running container",
|
||||
Flags: []cli.Flag{
|
||||
cli.StringFlag{
|
||||
Name: "id, i",
|
||||
Usage: "target container id",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "cwd, c",
|
||||
Usage: "current working directory for the process",
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "tty, t",
|
||||
Usage: "create a terminal for the process",
|
||||
},
|
||||
cli.StringSliceFlag{
|
||||
Name: "env, e",
|
||||
Value: &cli.StringSlice{},
|
||||
Usage: "environment variables for the process",
|
||||
},
|
||||
},
|
||||
Action: func(context *cli.Context) error {
|
||||
executionService, err := getExecutionService(context)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
id := context.String("id")
|
||||
tmpDir, err := getTempDir(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer os.RemoveAll(tmpDir)
|
||||
|
||||
sOpts := &execution.StartProcessRequest{
|
||||
ContainerId: id,
|
||||
Process: &execution.Process{
|
||||
Cwd: context.String("cwd"),
|
||||
Terminal: context.Bool("tty"),
|
||||
Args: context.Args(),
|
||||
Env: context.StringSlice("env"),
|
||||
},
|
||||
Stdin: filepath.Join(tmpDir, "stdin"),
|
||||
Stdout: filepath.Join(tmpDir, "stdout"),
|
||||
Stderr: filepath.Join(tmpDir, "stderr"),
|
||||
Console: context.Bool("tty"),
|
||||
}
|
||||
|
||||
fwg, err := prepareStdio(sOpts.Stdin, sOpts.Stdout, sOpts.Stderr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sr, err := executionService.StartProcess(gocontext.Background(), sOpts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = executionService.DeleteProcess(gocontext.Background(), &execution.DeleteProcessRequest{
|
||||
Container: &execution.Container{
|
||||
ID: id,
|
||||
},
|
||||
Process: sr.Process,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Ensure we read all io
|
||||
fwg.Wait()
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
|
@ -14,12 +14,12 @@ func main() {
|
|||
app.Name = "ctr"
|
||||
app.Version = containerd.Version
|
||||
app.Usage = `
|
||||
__
|
||||
__
|
||||
_____/ /______
|
||||
/ ___/ __/ ___/
|
||||
/ /__/ /_/ /
|
||||
\___/\__/_/
|
||||
|
||||
/ /__/ /_/ /
|
||||
\___/\__/_/
|
||||
|
||||
containerd client
|
||||
`
|
||||
app.Flags = []cli.Flag{
|
||||
|
@ -35,6 +35,7 @@ containerd client
|
|||
}
|
||||
app.Commands = []cli.Command{
|
||||
runCommand,
|
||||
execCommand,
|
||||
}
|
||||
app.Before = func(context *cli.Context) error {
|
||||
if context.GlobalBool("debug") {
|
||||
|
|
115
cmd/ctr/run.go
115
cmd/ctr/run.go
|
@ -2,23 +2,13 @@ package main
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/grpclog"
|
||||
|
||||
gocontext "context"
|
||||
|
||||
"github.com/docker/containerd/api/execution"
|
||||
"github.com/tonistiigi/fifo"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
|
@ -47,6 +37,10 @@ var runCommand = cli.Command{
|
|||
Name: "bundle, b",
|
||||
Usage: "path to the container's bundle",
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "tty, t",
|
||||
Usage: "allocate a TTY for the container",
|
||||
},
|
||||
},
|
||||
Action: func(context *cli.Context) error {
|
||||
// var config runConfig
|
||||
|
@ -71,6 +65,7 @@ var runCommand = cli.Command{
|
|||
crOpts := &execution.CreateContainerRequest{
|
||||
ID: id,
|
||||
BundlePath: context.String("bundle"),
|
||||
Console: context.Bool("tty"),
|
||||
Stdin: filepath.Join(tmpDir, "stdin"),
|
||||
Stdout: filepath.Join(tmpDir, "stdout"),
|
||||
Stderr: filepath.Join(tmpDir, "stderr"),
|
||||
|
@ -118,103 +113,3 @@ var runCommand = cli.Command{
|
|||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var grpcConn *grpc.ClientConn
|
||||
|
||||
func prepareStdio(in, out, err string) (*sync.WaitGroup, error) {
|
||||
var (
|
||||
wg sync.WaitGroup
|
||||
|
||||
dst io.Writer
|
||||
src io.Reader
|
||||
close func()
|
||||
)
|
||||
|
||||
for _, f := range []struct {
|
||||
name string
|
||||
flags int
|
||||
src bool
|
||||
reader io.Reader
|
||||
writer io.Writer
|
||||
}{
|
||||
{in, syscall.O_WRONLY | syscall.O_CREAT | syscall.O_NONBLOCK, false, os.Stdin, nil},
|
||||
{out, syscall.O_RDONLY | syscall.O_CREAT | syscall.O_NONBLOCK, true, nil, os.Stdout},
|
||||
{err, syscall.O_RDONLY | syscall.O_CREAT | syscall.O_NONBLOCK, true, nil, os.Stderr},
|
||||
} {
|
||||
ff, err := fifo.OpenFifo(gocontext.Background(), f.name, f.flags, 0700)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func(c io.Closer) {
|
||||
if err != nil {
|
||||
c.Close()
|
||||
}
|
||||
}(ff)
|
||||
|
||||
if f.src {
|
||||
src = ff
|
||||
dst = f.writer
|
||||
close = func() {
|
||||
ff.Close()
|
||||
wg.Done()
|
||||
}
|
||||
wg.Add(1)
|
||||
} else {
|
||||
src = f.reader
|
||||
dst = ff
|
||||
close = func() { ff.Close() }
|
||||
}
|
||||
|
||||
go func(dst io.Writer, src io.Reader, close func()) {
|
||||
io.Copy(dst, src)
|
||||
close()
|
||||
}(dst, src, close)
|
||||
}
|
||||
|
||||
return &wg, nil
|
||||
}
|
||||
|
||||
func getGRPCConnection(context *cli.Context) (*grpc.ClientConn, error) {
|
||||
if grpcConn != nil {
|
||||
return grpcConn, nil
|
||||
}
|
||||
|
||||
bindSocket := context.GlobalString("socket")
|
||||
// reset the logger for grpc to log to dev/null so that it does not mess with our stdio
|
||||
grpclog.SetLogger(log.New(ioutil.Discard, "", log.LstdFlags))
|
||||
dialOpts := []grpc.DialOption{grpc.WithInsecure(), grpc.WithTimeout(100 * time.Second)}
|
||||
dialOpts = append(dialOpts,
|
||||
grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
|
||||
return net.DialTimeout("unix", bindSocket, timeout)
|
||||
},
|
||||
))
|
||||
|
||||
conn, err := grpc.Dial(fmt.Sprintf("unix://%s", bindSocket), dialOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
grpcConn = conn
|
||||
return grpcConn, nil
|
||||
}
|
||||
|
||||
func getExecutionService(context *cli.Context) (execution.ExecutionServiceClient, error) {
|
||||
conn, err := getGRPCConnection(context)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return execution.NewExecutionServiceClient(conn), nil
|
||||
}
|
||||
|
||||
func getTempDir(id string) (string, error) {
|
||||
err := os.MkdirAll(filepath.Join(os.TempDir(), "ctr"), 0700)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
tmpDir, err := ioutil.TempDir(filepath.Join(os.TempDir(), "ctr"), fmt.Sprintf("%s-", id))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return tmpDir, nil
|
||||
}
|
||||
|
|
122
cmd/ctr/utils.go
Normal file
122
cmd/ctr/utils.go
Normal file
|
@ -0,0 +1,122 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
gocontext "context"
|
||||
|
||||
"github.com/docker/containerd/api/execution"
|
||||
"github.com/tonistiigi/fifo"
|
||||
"github.com/urfave/cli"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/grpclog"
|
||||
)
|
||||
|
||||
var grpcConn *grpc.ClientConn
|
||||
|
||||
func prepareStdio(in, out, err string) (*sync.WaitGroup, error) {
|
||||
var (
|
||||
wg sync.WaitGroup
|
||||
|
||||
dst io.Writer
|
||||
src io.Reader
|
||||
close func()
|
||||
)
|
||||
|
||||
for _, f := range []struct {
|
||||
name string
|
||||
flags int
|
||||
src bool
|
||||
reader io.Reader
|
||||
writer io.Writer
|
||||
}{
|
||||
{in, syscall.O_WRONLY | syscall.O_CREAT | syscall.O_NONBLOCK, false, os.Stdin, nil},
|
||||
{out, syscall.O_RDONLY | syscall.O_CREAT | syscall.O_NONBLOCK, true, nil, os.Stdout},
|
||||
{err, syscall.O_RDONLY | syscall.O_CREAT | syscall.O_NONBLOCK, true, nil, os.Stderr},
|
||||
} {
|
||||
ff, err := fifo.OpenFifo(gocontext.Background(), f.name, f.flags, 0700)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func(c io.Closer) {
|
||||
if err != nil {
|
||||
c.Close()
|
||||
}
|
||||
}(ff)
|
||||
|
||||
if f.src {
|
||||
src = ff
|
||||
dst = f.writer
|
||||
close = func() {
|
||||
ff.Close()
|
||||
wg.Done()
|
||||
}
|
||||
wg.Add(1)
|
||||
} else {
|
||||
src = f.reader
|
||||
dst = ff
|
||||
close = func() { ff.Close() }
|
||||
}
|
||||
|
||||
go func(dst io.Writer, src io.Reader, close func()) {
|
||||
io.Copy(dst, src)
|
||||
close()
|
||||
}(dst, src, close)
|
||||
}
|
||||
|
||||
return &wg, nil
|
||||
}
|
||||
|
||||
func getGRPCConnection(context *cli.Context) (*grpc.ClientConn, error) {
|
||||
if grpcConn != nil {
|
||||
return grpcConn, nil
|
||||
}
|
||||
|
||||
bindSocket := context.GlobalString("socket")
|
||||
// reset the logger for grpc to log to dev/null so that it does not mess with our stdio
|
||||
grpclog.SetLogger(log.New(ioutil.Discard, "", log.LstdFlags))
|
||||
dialOpts := []grpc.DialOption{grpc.WithInsecure(), grpc.WithTimeout(100 * time.Second)}
|
||||
dialOpts = append(dialOpts,
|
||||
grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
|
||||
return net.DialTimeout("unix", bindSocket, timeout)
|
||||
},
|
||||
))
|
||||
|
||||
conn, err := grpc.Dial(fmt.Sprintf("unix://%s", bindSocket), dialOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
grpcConn = conn
|
||||
return grpcConn, nil
|
||||
}
|
||||
|
||||
func getExecutionService(context *cli.Context) (execution.ExecutionServiceClient, error) {
|
||||
conn, err := getGRPCConnection(context)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return execution.NewExecutionServiceClient(conn), nil
|
||||
}
|
||||
|
||||
func getTempDir(id string) (string, error) {
|
||||
err := os.MkdirAll(filepath.Join(os.TempDir(), "ctr"), 0700)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
tmpDir, err := ioutil.TempDir(filepath.Join(os.TempDir(), "ctr"), fmt.Sprintf("%s-", id))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return tmpDir, nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue