ctr: move shim to commands
Signed-off-by: Jess Valarezo <valarezo.jessica@gmail.com>
This commit is contained in:
68
cmd/ctr/commands/shim/io_unix.go
Normal file
68
cmd/ctr/commands/shim/io_unix.go
Normal file
@@ -0,0 +1,68 @@
|
||||
// +build !windows
|
||||
|
||||
package shim
|
||||
|
||||
import (
|
||||
gocontext "context"
|
||||
"io"
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
"github.com/containerd/fifo"
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func prepareStdio(stdin, stdout, stderr string, console bool) (wg *sync.WaitGroup, err error) {
|
||||
wg = &sync.WaitGroup{}
|
||||
ctx := gocontext.Background()
|
||||
|
||||
f, err := fifo.OpenFifo(ctx, stdin, unix.O_WRONLY|unix.O_CREAT|unix.O_NONBLOCK, 0700)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func(c io.Closer) {
|
||||
if err != nil {
|
||||
c.Close()
|
||||
}
|
||||
}(f)
|
||||
go func(w io.WriteCloser) {
|
||||
io.Copy(w, os.Stdin)
|
||||
w.Close()
|
||||
}(f)
|
||||
|
||||
f, err = fifo.OpenFifo(ctx, stdout, unix.O_RDONLY|unix.O_CREAT|unix.O_NONBLOCK, 0700)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func(c io.Closer) {
|
||||
if err != nil {
|
||||
c.Close()
|
||||
}
|
||||
}(f)
|
||||
wg.Add(1)
|
||||
go func(r io.ReadCloser) {
|
||||
io.Copy(os.Stdout, r)
|
||||
r.Close()
|
||||
wg.Done()
|
||||
}(f)
|
||||
|
||||
f, err = fifo.OpenFifo(ctx, stderr, unix.O_RDONLY|unix.O_CREAT|unix.O_NONBLOCK, 0700)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func(c io.Closer) {
|
||||
if err != nil {
|
||||
c.Close()
|
||||
}
|
||||
}(f)
|
||||
if !console {
|
||||
wg.Add(1)
|
||||
go func(r io.ReadCloser) {
|
||||
io.Copy(os.Stderr, r)
|
||||
r.Close()
|
||||
wg.Done()
|
||||
}(f)
|
||||
}
|
||||
|
||||
return wg, nil
|
||||
}
|
91
cmd/ctr/commands/shim/io_windows.go
Normal file
91
cmd/ctr/commands/shim/io_windows.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package shim
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net"
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
"github.com/Microsoft/go-winio"
|
||||
clog "github.com/containerd/containerd/log"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func prepareStdio(stdin, stdout, stderr string, console bool) (*sync.WaitGroup, error) {
|
||||
var wg sync.WaitGroup
|
||||
|
||||
if stdin != "" {
|
||||
l, err := winio.ListenPipe(stdin, nil)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "failed to create stdin pipe %s", stdin)
|
||||
}
|
||||
defer func(l net.Listener) {
|
||||
if err != nil {
|
||||
l.Close()
|
||||
}
|
||||
}(l)
|
||||
|
||||
go func() {
|
||||
c, err := l.Accept()
|
||||
if err != nil {
|
||||
clog.L.WithError(err).Errorf("failed to accept stdin connection on %s", stdin)
|
||||
return
|
||||
}
|
||||
io.Copy(c, os.Stdin)
|
||||
c.Close()
|
||||
l.Close()
|
||||
}()
|
||||
}
|
||||
|
||||
if stdout != "" {
|
||||
l, err := winio.ListenPipe(stdout, nil)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "failed to create stdin pipe %s", stdout)
|
||||
}
|
||||
defer func(l net.Listener) {
|
||||
if err != nil {
|
||||
l.Close()
|
||||
}
|
||||
}(l)
|
||||
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
c, err := l.Accept()
|
||||
if err != nil {
|
||||
clog.L.WithError(err).Errorf("failed to accept stdout connection on %s", stdout)
|
||||
return
|
||||
}
|
||||
io.Copy(os.Stdout, c)
|
||||
c.Close()
|
||||
l.Close()
|
||||
}()
|
||||
}
|
||||
|
||||
if !console && stderr != "" {
|
||||
l, err := winio.ListenPipe(stderr, nil)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "failed to create stderr pipe %s", stderr)
|
||||
}
|
||||
defer func(l net.Listener) {
|
||||
if err != nil {
|
||||
l.Close()
|
||||
}
|
||||
}(l)
|
||||
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
c, err := l.Accept()
|
||||
if err != nil {
|
||||
clog.L.WithError(err).Errorf("failed to accept stderr connection on %s", stderr)
|
||||
return
|
||||
}
|
||||
io.Copy(os.Stderr, c)
|
||||
c.Close()
|
||||
l.Close()
|
||||
}()
|
||||
}
|
||||
|
||||
return &wg, nil
|
||||
}
|
233
cmd/ctr/commands/shim/shim.go
Normal file
233
cmd/ctr/commands/shim/shim.go
Normal file
@@ -0,0 +1,233 @@
|
||||
// +build !windows
|
||||
|
||||
package shim
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
gocontext "context"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
|
||||
"github.com/containerd/console"
|
||||
"github.com/containerd/containerd/cmd/ctr/commands"
|
||||
shim "github.com/containerd/containerd/linux/shim/v1"
|
||||
"github.com/containerd/typeurl"
|
||||
protobuf "github.com/gogo/protobuf/types"
|
||||
google_protobuf "github.com/golang/protobuf/ptypes/empty"
|
||||
"github.com/opencontainers/runtime-spec/specs-go"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
var empty = &google_protobuf.Empty{}
|
||||
|
||||
var fifoFlags = []cli.Flag{
|
||||
cli.StringFlag{
|
||||
Name: "stdin",
|
||||
Usage: "specify the path to the stdin fifo",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "stdout",
|
||||
Usage: "specify the path to the stdout fifo",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "stderr",
|
||||
Usage: "specify the path to the stderr fifo",
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "tty,t",
|
||||
Usage: "enable tty support",
|
||||
},
|
||||
}
|
||||
|
||||
// Command is the cli command for interacting with a shim
|
||||
var Command = cli.Command{
|
||||
Name: "shim",
|
||||
Usage: "interact with a shim directly",
|
||||
Flags: []cli.Flag{
|
||||
cli.StringFlag{
|
||||
Name: "socket",
|
||||
Usage: "socket on which to connect to the shim",
|
||||
},
|
||||
},
|
||||
Subcommands: []cli.Command{
|
||||
deleteCommand,
|
||||
execCommand,
|
||||
startCommand,
|
||||
stateCommand,
|
||||
},
|
||||
}
|
||||
|
||||
var startCommand = cli.Command{
|
||||
Name: "start",
|
||||
Usage: "start a container with a shim",
|
||||
Action: func(context *cli.Context) error {
|
||||
service, err := getShimService(context)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = service.Start(gocontext.Background(), &shim.StartRequest{
|
||||
ID: context.Args().First(),
|
||||
})
|
||||
return err
|
||||
},
|
||||
}
|
||||
|
||||
var deleteCommand = cli.Command{
|
||||
Name: "delete",
|
||||
Usage: "delete a container with a shim",
|
||||
Action: func(context *cli.Context) error {
|
||||
service, err := getShimService(context)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r, err := service.Delete(gocontext.Background(), empty)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("container deleted and returned exit status %d\n", r.ExitStatus)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var stateCommand = cli.Command{
|
||||
Name: "state",
|
||||
Usage: "get the state of all the processes of the shim",
|
||||
Action: func(context *cli.Context) error {
|
||||
service, err := getShimService(context)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r, err := service.State(gocontext.Background(), &shim.StateRequest{
|
||||
ID: context.Args().First(),
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
commands.PrintAsJSON(r)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var execCommand = cli.Command{
|
||||
Name: "exec",
|
||||
Usage: "exec a new process in the shim's container",
|
||||
Flags: append(fifoFlags,
|
||||
cli.BoolFlag{
|
||||
Name: "attach,a",
|
||||
Usage: "stay attached to the container and open the fifos",
|
||||
},
|
||||
cli.StringSliceFlag{
|
||||
Name: "env,e",
|
||||
Usage: "add environment vars",
|
||||
Value: &cli.StringSlice{},
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "cwd",
|
||||
Usage: "current working directory",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "spec",
|
||||
Usage: "runtime spec",
|
||||
},
|
||||
),
|
||||
Action: func(context *cli.Context) error {
|
||||
service, err := getShimService(context)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var (
|
||||
id = context.Args().First()
|
||||
ctx = gocontext.Background()
|
||||
)
|
||||
|
||||
if id == "" {
|
||||
return errors.New("exec id must be provided")
|
||||
}
|
||||
|
||||
tty := context.Bool("tty")
|
||||
wg, err := prepareStdio(context.String("stdin"), context.String("stdout"), context.String("stderr"), tty)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// read spec file and extract Any object
|
||||
spec, err := ioutil.ReadFile(context.String("spec"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
url, err := typeurl.TypeURL(specs.Process{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rq := &shim.ExecProcessRequest{
|
||||
ID: id,
|
||||
Spec: &protobuf.Any{
|
||||
TypeUrl: url,
|
||||
Value: spec,
|
||||
},
|
||||
Stdin: context.String("stdin"),
|
||||
Stdout: context.String("stdout"),
|
||||
Stderr: context.String("stderr"),
|
||||
Terminal: tty,
|
||||
}
|
||||
if _, err := service.Exec(ctx, rq); err != nil {
|
||||
return err
|
||||
}
|
||||
r, err := service.Start(ctx, &shim.StartRequest{
|
||||
ID: id,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("exec running with pid %d\n", r.Pid)
|
||||
if context.Bool("attach") {
|
||||
logrus.Info("attaching")
|
||||
if tty {
|
||||
current := console.Current()
|
||||
defer current.Reset()
|
||||
if err := current.SetRaw(); err != nil {
|
||||
return err
|
||||
}
|
||||
size, err := current.Size()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := service.ResizePty(ctx, &shim.ResizePtyRequest{
|
||||
ID: id,
|
||||
Width: uint32(size.Width),
|
||||
Height: uint32(size.Height),
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func getShimService(context *cli.Context) (shim.ShimClient, error) {
|
||||
bindSocket := context.GlobalString("socket")
|
||||
if bindSocket == "" {
|
||||
return nil, errors.New("socket path must be specified")
|
||||
}
|
||||
|
||||
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", "\x00"+bindSocket, timeout)
|
||||
},
|
||||
))
|
||||
conn, err := grpc.Dial(fmt.Sprintf("unix://%s", bindSocket), dialOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return shim.NewShimClient(conn), nil
|
||||
}
|
Reference in New Issue
Block a user