2017-01-19 23:21:25 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-01-26 23:09:59 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2017-01-19 23:21:25 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net"
|
2017-01-26 23:09:59 +00:00
|
|
|
"os"
|
|
|
|
"strconv"
|
2017-01-19 23:21:25 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
gocontext "context"
|
|
|
|
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/grpclog"
|
|
|
|
|
2017-01-26 23:09:59 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2017-01-26 19:29:19 +00:00
|
|
|
"github.com/crosbymichael/console"
|
2017-02-09 07:31:26 +00:00
|
|
|
"github.com/docker/containerd/api/services/shim"
|
2017-01-19 23:21:25 +00:00
|
|
|
"github.com/urfave/cli"
|
2017-02-10 04:32:11 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-01-19 23:21:25 +00:00
|
|
|
)
|
|
|
|
|
2017-01-25 21:27:48 +00:00
|
|
|
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",
|
|
|
|
},
|
2017-01-26 19:29:19 +00:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "tty,t",
|
|
|
|
Usage: "enable tty support",
|
|
|
|
},
|
2017-01-25 21:27:48 +00:00
|
|
|
}
|
|
|
|
|
2017-01-19 23:21:25 +00:00
|
|
|
var shimCommand = cli.Command{
|
|
|
|
Name: "shim",
|
|
|
|
Usage: "interact with a shim directly",
|
|
|
|
Subcommands: []cli.Command{
|
|
|
|
shimCreateCommand,
|
|
|
|
shimStartCommand,
|
|
|
|
shimDeleteCommand,
|
2017-01-26 19:29:19 +00:00
|
|
|
shimEventsCommand,
|
2017-01-26 23:09:59 +00:00
|
|
|
shimStateCommand,
|
|
|
|
shimExecCommand,
|
2017-01-19 23:21:25 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var shimCreateCommand = cli.Command{
|
|
|
|
Name: "create",
|
|
|
|
Usage: "create a container with a shim",
|
2017-01-25 21:27:48 +00:00
|
|
|
Flags: append(fifoFlags,
|
2017-01-19 23:21:25 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "bundle",
|
|
|
|
Usage: "bundle path for the container",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "runtime",
|
|
|
|
Value: "runc",
|
|
|
|
Usage: "runtime to use for the container",
|
|
|
|
},
|
2017-01-25 21:27:48 +00:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "attach,a",
|
|
|
|
Usage: "stay attached to the container and open the fifos",
|
|
|
|
},
|
|
|
|
),
|
2017-01-19 23:21:25 +00:00
|
|
|
Action: func(context *cli.Context) error {
|
|
|
|
id := context.Args().First()
|
|
|
|
if id == "" {
|
2017-02-10 04:32:11 +00:00
|
|
|
return errors.New("container id must be provided")
|
2017-01-19 23:21:25 +00:00
|
|
|
}
|
|
|
|
service, err := getShimService()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-26 19:29:19 +00:00
|
|
|
tty := context.Bool("tty")
|
|
|
|
wg, err := prepareStdio(context.String("stdin"), context.String("stdout"), context.String("stderr"), tty)
|
2017-01-25 21:27:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-19 23:21:25 +00:00
|
|
|
r, err := service.Create(gocontext.Background(), &shim.CreateRequest{
|
2017-01-26 19:29:19 +00:00
|
|
|
ID: id,
|
|
|
|
Bundle: context.String("bundle"),
|
|
|
|
Runtime: context.String("runtime"),
|
|
|
|
Stdin: context.String("stdin"),
|
|
|
|
Stdout: context.String("stdout"),
|
|
|
|
Stderr: context.String("stderr"),
|
|
|
|
Terminal: tty,
|
2017-01-19 23:21:25 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Printf("container created with id %s and pid %d\n", id, r.Pid)
|
2017-01-25 21:27:48 +00:00
|
|
|
if context.Bool("attach") {
|
2017-01-26 19:29:19 +00:00
|
|
|
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.Pty(gocontext.Background(), &shim.PtyRequest{
|
|
|
|
Pid: r.Pid,
|
|
|
|
Width: uint32(size.Width),
|
|
|
|
Height: uint32(size.Height),
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-01-25 21:27:48 +00:00
|
|
|
wg.Wait()
|
|
|
|
}
|
2017-01-19 23:21:25 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var shimStartCommand = cli.Command{
|
|
|
|
Name: "start",
|
|
|
|
Usage: "start a container with a shim",
|
|
|
|
Action: func(context *cli.Context) error {
|
|
|
|
service, err := getShimService()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = service.Start(gocontext.Background(), &shim.StartRequest{})
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var shimDeleteCommand = cli.Command{
|
|
|
|
Name: "delete",
|
|
|
|
Usage: "delete a container with a shim",
|
|
|
|
Action: func(context *cli.Context) error {
|
|
|
|
service, err := getShimService()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-26 23:09:59 +00:00
|
|
|
pid, err := strconv.Atoi(context.Args().First())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
r, err := service.Delete(gocontext.Background(), &shim.DeleteRequest{
|
|
|
|
Pid: uint32(pid),
|
|
|
|
})
|
2017-01-19 23:21:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-25 21:27:48 +00:00
|
|
|
fmt.Printf("container deleted and returned exit status %d\n", r.ExitStatus)
|
2017-01-19 23:21:25 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-01-26 23:09:59 +00:00
|
|
|
var shimStateCommand = cli.Command{
|
|
|
|
Name: "state",
|
|
|
|
Usage: "get the state of all the processes of the shim",
|
|
|
|
Action: func(context *cli.Context) error {
|
|
|
|
service, err := getShimService()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
r, err := service.State(gocontext.Background(), &shim.StateRequest{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
data, err := json.Marshal(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
if err := json.Indent(buf, data, " ", " "); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
buf.WriteTo(os.Stdout)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var shimExecCommand = 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",
|
|
|
|
},
|
|
|
|
),
|
|
|
|
Action: func(context *cli.Context) error {
|
|
|
|
service, err := getShimService()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tty := context.Bool("tty")
|
|
|
|
wg, err := prepareStdio(context.String("stdin"), context.String("stdout"), context.String("stderr"), tty)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rq := &shim.ExecRequest{
|
|
|
|
Args: []string(context.Args()),
|
|
|
|
Env: context.StringSlice("env"),
|
|
|
|
Cwd: context.String("cwd"),
|
|
|
|
Stdin: context.String("stdin"),
|
|
|
|
Stdout: context.String("stdout"),
|
|
|
|
Stderr: context.String("stderr"),
|
|
|
|
Terminal: tty,
|
|
|
|
}
|
|
|
|
r, err := service.Exec(gocontext.Background(), rq)
|
|
|
|
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.Pty(gocontext.Background(), &shim.PtyRequest{
|
|
|
|
Pid: r.Pid,
|
|
|
|
Width: uint32(size.Width),
|
|
|
|
Height: uint32(size.Height),
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-01-26 19:29:19 +00:00
|
|
|
var shimEventsCommand = cli.Command{
|
|
|
|
Name: "events",
|
|
|
|
Usage: "get events for a shim",
|
|
|
|
Action: func(context *cli.Context) error {
|
|
|
|
service, err := getShimService()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
events, err := service.Events(gocontext.Background(), &shim.EventsRequest{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
e, err := events.Recv()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Printf("type=%s id=%s pid=%d status=%d\n", e.Type, e.ID, e.Pid, e.ExitStatus)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-01-24 22:45:18 +00:00
|
|
|
func getShimService() (shim.ShimClient, error) {
|
2017-01-19 23:21:25 +00:00
|
|
|
bindSocket := "shim.sock"
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2017-01-24 22:45:18 +00:00
|
|
|
return shim.NewShimClient(conn), nil
|
2017-01-19 23:21:25 +00:00
|
|
|
}
|