2015-12-02 22:41:49 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-12-15 18:51:12 +00:00
|
|
|
"encoding/json"
|
2015-12-02 22:41:49 +00:00
|
|
|
"fmt"
|
2015-12-14 22:15:26 +00:00
|
|
|
"io"
|
2015-12-14 23:54:11 +00:00
|
|
|
"net"
|
2015-12-02 22:41:49 +00:00
|
|
|
"os"
|
2015-12-14 22:15:26 +00:00
|
|
|
"path/filepath"
|
2016-01-06 21:32:46 +00:00
|
|
|
"strings"
|
2015-12-14 22:15:26 +00:00
|
|
|
"syscall"
|
2015-12-02 22:41:49 +00:00
|
|
|
"text/tabwriter"
|
2015-12-14 23:54:11 +00:00
|
|
|
"time"
|
2015-12-02 22:41:49 +00:00
|
|
|
|
|
|
|
"github.com/codegangsta/cli"
|
2015-12-09 23:37:04 +00:00
|
|
|
"github.com/docker/containerd/api/grpc/types"
|
2015-12-15 00:47:42 +00:00
|
|
|
"github.com/docker/docker/pkg/term"
|
2015-12-15 18:51:12 +00:00
|
|
|
"github.com/opencontainers/specs"
|
2015-12-09 23:37:04 +00:00
|
|
|
netcontext "golang.org/x/net/context"
|
|
|
|
"google.golang.org/grpc"
|
2015-12-02 22:41:49 +00:00
|
|
|
)
|
|
|
|
|
2015-12-09 23:37:04 +00:00
|
|
|
// TODO: parse flags and pass opts
|
2015-12-14 23:54:11 +00:00
|
|
|
func getClient(ctx *cli.Context) types.APIClient {
|
|
|
|
dialOpts := []grpc.DialOption{grpc.WithInsecure()}
|
|
|
|
dialOpts = append(dialOpts,
|
|
|
|
grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
|
|
|
|
return net.DialTimeout("unix", addr, timeout)
|
|
|
|
},
|
|
|
|
))
|
|
|
|
conn, err := grpc.Dial(ctx.GlobalString("address"), dialOpts...)
|
2015-12-09 23:37:04 +00:00
|
|
|
if err != nil {
|
|
|
|
fatal(err.Error(), 1)
|
|
|
|
}
|
2016-01-05 21:07:36 +00:00
|
|
|
return types.NewAPIClient(conn)
|
2015-12-09 23:37:04 +00:00
|
|
|
}
|
|
|
|
|
2016-01-23 22:40:55 +00:00
|
|
|
var containersCommand = cli.Command{
|
2015-12-02 22:41:49 +00:00
|
|
|
Name: "containers",
|
|
|
|
Usage: "interact with running containers",
|
|
|
|
Subcommands: []cli.Command{
|
2016-01-23 22:40:55 +00:00
|
|
|
execCommand,
|
|
|
|
killCommand,
|
|
|
|
listCommand,
|
|
|
|
startCommand,
|
|
|
|
statsCommand,
|
2016-01-06 21:32:46 +00:00
|
|
|
attachCommand,
|
2015-12-02 22:41:49 +00:00
|
|
|
},
|
|
|
|
Action: listContainers,
|
|
|
|
}
|
|
|
|
|
2016-01-23 22:40:55 +00:00
|
|
|
var listCommand = cli.Command{
|
2015-12-02 22:41:49 +00:00
|
|
|
Name: "list",
|
|
|
|
Usage: "list all running containers",
|
|
|
|
Action: listContainers,
|
|
|
|
}
|
|
|
|
|
|
|
|
func listContainers(context *cli.Context) {
|
2015-12-14 23:54:11 +00:00
|
|
|
c := getClient(context)
|
2015-12-10 19:03:36 +00:00
|
|
|
resp, err := c.State(netcontext.Background(), &types.StateRequest{})
|
2015-12-02 22:41:49 +00:00
|
|
|
if err != nil {
|
|
|
|
fatal(err.Error(), 1)
|
|
|
|
}
|
|
|
|
w := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0)
|
2016-01-06 21:32:46 +00:00
|
|
|
fmt.Fprint(w, "ID\tPATH\tSTATUS\tPROCESSES\n")
|
2015-12-09 23:37:04 +00:00
|
|
|
for _, c := range resp.Containers {
|
2016-01-06 21:32:46 +00:00
|
|
|
procs := []string{}
|
|
|
|
for _, p := range c.Processes {
|
|
|
|
procs = append(procs, p.Pid)
|
|
|
|
}
|
|
|
|
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", c.Id, c.BundlePath, c.Status, strings.Join(procs, ","))
|
2015-12-02 22:41:49 +00:00
|
|
|
}
|
|
|
|
if err := w.Flush(); err != nil {
|
2015-12-10 01:03:53 +00:00
|
|
|
fatal(err.Error(), 1)
|
2015-12-02 22:41:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-06 21:32:46 +00:00
|
|
|
var attachCommand = cli.Command{
|
|
|
|
Name: "attach",
|
|
|
|
Usage: "attach to a running container",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "state-dir",
|
|
|
|
Value: "/run/containerd",
|
|
|
|
Usage: "runtime state directory",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "pid,p",
|
|
|
|
Value: "init",
|
|
|
|
Usage: "specify the process id to attach to",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: func(context *cli.Context) {
|
|
|
|
var (
|
|
|
|
id = context.Args().First()
|
|
|
|
pid = context.String("pid")
|
|
|
|
)
|
|
|
|
if id == "" {
|
|
|
|
fatal("container id cannot be empty", 1)
|
|
|
|
}
|
|
|
|
c := getClient(context)
|
|
|
|
events, err := c.Events(netcontext.Background(), &types.EventsRequest{})
|
|
|
|
if err != nil {
|
|
|
|
fatal(err.Error(), 1)
|
|
|
|
}
|
|
|
|
type bundleState struct {
|
|
|
|
Bundle string `json:"bundle"`
|
|
|
|
}
|
|
|
|
f, err := os.Open(filepath.Join(context.String("state-dir"), id, "state.json"))
|
|
|
|
if err != nil {
|
|
|
|
fatal(err.Error(), 1)
|
|
|
|
}
|
|
|
|
var s bundleState
|
|
|
|
err = json.NewDecoder(f).Decode(&s)
|
|
|
|
f.Close()
|
|
|
|
if err != nil {
|
|
|
|
fatal(err.Error(), 1)
|
|
|
|
}
|
|
|
|
mkterm, err := readTermSetting(s.Bundle)
|
|
|
|
if err != nil {
|
|
|
|
fatal(err.Error(), 1)
|
|
|
|
}
|
|
|
|
if mkterm {
|
|
|
|
s, err := term.SetRawTerminal(os.Stdin.Fd())
|
|
|
|
if err != nil {
|
|
|
|
fatal(err.Error(), 1)
|
|
|
|
}
|
|
|
|
state = s
|
|
|
|
}
|
|
|
|
if err := attachStdio(
|
|
|
|
filepath.Join(context.String("state-dir"), id, pid, "stdin"),
|
|
|
|
filepath.Join(context.String("state-dir"), id, pid, "stdout"),
|
|
|
|
filepath.Join(context.String("state-dir"), id, pid, "stderr"),
|
|
|
|
); err != nil {
|
|
|
|
fatal(err.Error(), 1)
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
io.Copy(stdin, os.Stdin)
|
|
|
|
if state != nil {
|
|
|
|
term.RestoreTerminal(os.Stdin.Fd(), state)
|
|
|
|
}
|
|
|
|
stdin.Close()
|
|
|
|
}()
|
|
|
|
for {
|
|
|
|
e, err := events.Recv()
|
|
|
|
if err != nil {
|
|
|
|
fatal(err.Error(), 1)
|
|
|
|
}
|
|
|
|
if e.Id == id && e.Type == "exit" && e.Pid == pid {
|
|
|
|
os.Exit(int(e.Status))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-01-23 22:40:55 +00:00
|
|
|
var startCommand = cli.Command{
|
2015-12-02 22:41:49 +00:00
|
|
|
Name: "start",
|
|
|
|
Usage: "start a container",
|
|
|
|
Flags: []cli.Flag{
|
2015-12-08 21:31:20 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "checkpoint,c",
|
|
|
|
Value: "",
|
|
|
|
Usage: "checkpoint to start the container from",
|
|
|
|
},
|
2015-12-14 22:15:26 +00:00
|
|
|
cli.BoolFlag{
|
2015-12-15 18:51:12 +00:00
|
|
|
Name: "attach,a",
|
2015-12-14 22:15:26 +00:00
|
|
|
Usage: "connect to the stdio of the container",
|
|
|
|
},
|
2015-12-02 22:41:49 +00:00
|
|
|
},
|
|
|
|
Action: func(context *cli.Context) {
|
2015-12-10 01:03:53 +00:00
|
|
|
var (
|
|
|
|
id = context.Args().Get(0)
|
|
|
|
path = context.Args().Get(1)
|
|
|
|
)
|
2015-12-02 22:41:49 +00:00
|
|
|
if path == "" {
|
|
|
|
fatal("bundle path cannot be empty", 1)
|
|
|
|
}
|
|
|
|
if id == "" {
|
|
|
|
fatal("container id cannot be empty", 1)
|
|
|
|
}
|
2016-01-26 03:36:17 +00:00
|
|
|
bpath, err := filepath.Abs(path)
|
|
|
|
if err != nil {
|
|
|
|
fatal(fmt.Sprintf("cannot get the absolute path of the bundle: %v", err), 1)
|
|
|
|
}
|
2015-12-14 23:54:11 +00:00
|
|
|
c := getClient(context)
|
2015-12-15 00:47:42 +00:00
|
|
|
events, err := c.Events(netcontext.Background(), &types.EventsRequest{})
|
|
|
|
if err != nil {
|
|
|
|
fatal(err.Error(), 1)
|
|
|
|
}
|
2015-12-14 22:15:26 +00:00
|
|
|
r := &types.CreateContainerRequest{
|
2015-12-09 23:37:04 +00:00
|
|
|
Id: id,
|
2016-01-26 03:36:17 +00:00
|
|
|
BundlePath: bpath,
|
2015-12-08 21:31:20 +00:00
|
|
|
Checkpoint: context.String("checkpoint"),
|
2015-12-14 22:15:26 +00:00
|
|
|
}
|
2016-01-06 21:32:46 +00:00
|
|
|
resp, err := c.CreateContainer(netcontext.Background(), r)
|
|
|
|
if err != nil {
|
|
|
|
fatal(err.Error(), 1)
|
|
|
|
}
|
2015-12-15 18:51:12 +00:00
|
|
|
if context.Bool("attach") {
|
2016-01-26 03:36:17 +00:00
|
|
|
mkterm, err := readTermSetting(bpath)
|
2015-12-15 18:51:12 +00:00
|
|
|
if err != nil {
|
2015-12-14 22:15:26 +00:00
|
|
|
fatal(err.Error(), 1)
|
|
|
|
}
|
2015-12-15 18:51:12 +00:00
|
|
|
if mkterm {
|
2016-01-06 21:32:46 +00:00
|
|
|
s, err := term.SetRawTerminal(os.Stdin.Fd())
|
|
|
|
if err != nil {
|
2015-12-15 18:51:12 +00:00
|
|
|
fatal(err.Error(), 1)
|
|
|
|
}
|
2016-01-06 21:32:46 +00:00
|
|
|
state = s
|
|
|
|
}
|
|
|
|
if err := attachStdio(resp.Stdin, resp.Stdout, resp.Stderr); err != nil {
|
|
|
|
fatal(err.Error(), 1)
|
2015-12-15 00:47:42 +00:00
|
|
|
}
|
2015-12-02 22:41:49 +00:00
|
|
|
}
|
2015-12-15 18:51:12 +00:00
|
|
|
if context.Bool("attach") {
|
2016-01-06 08:25:38 +00:00
|
|
|
restoreAndCloseStdin := func() {
|
2015-12-15 00:47:42 +00:00
|
|
|
if state != nil {
|
|
|
|
term.RestoreTerminal(os.Stdin.Fd(), state)
|
|
|
|
}
|
2015-12-17 21:42:02 +00:00
|
|
|
stdin.Close()
|
2016-01-06 08:25:38 +00:00
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
io.Copy(stdin, os.Stdin)
|
|
|
|
restoreAndCloseStdin()
|
2015-12-15 00:47:42 +00:00
|
|
|
}()
|
|
|
|
for {
|
|
|
|
e, err := events.Recv()
|
|
|
|
if err != nil {
|
2016-01-06 08:25:38 +00:00
|
|
|
restoreAndCloseStdin()
|
2015-12-15 00:47:42 +00:00
|
|
|
fatal(err.Error(), 1)
|
|
|
|
}
|
2016-01-06 21:32:46 +00:00
|
|
|
if e.Id == id && e.Type == "exit" && e.Pid == "init" {
|
2016-01-06 08:25:38 +00:00
|
|
|
restoreAndCloseStdin()
|
2015-12-15 00:47:42 +00:00
|
|
|
os.Exit(int(e.Status))
|
|
|
|
}
|
|
|
|
}
|
2015-12-14 22:40:50 +00:00
|
|
|
}
|
2015-12-02 22:41:49 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-12-15 00:47:42 +00:00
|
|
|
var (
|
|
|
|
stdin io.WriteCloser
|
|
|
|
state *term.State
|
|
|
|
)
|
|
|
|
|
2015-12-15 19:43:51 +00:00
|
|
|
// readTermSetting reads the Terminal option out of the specs configuration
|
|
|
|
// to know if ctr should allocate a pty
|
2015-12-15 18:51:12 +00:00
|
|
|
func readTermSetting(path string) (bool, error) {
|
|
|
|
f, err := os.Open(filepath.Join(path, "config.json"))
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
var spec specs.Spec
|
|
|
|
if err := json.NewDecoder(f).Decode(&spec); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return spec.Process.Terminal, nil
|
|
|
|
}
|
|
|
|
|
2016-01-06 21:32:46 +00:00
|
|
|
func attachStdio(stdins, stdout, stderr string) error {
|
|
|
|
stdinf, err := os.OpenFile(stdins, syscall.O_RDWR, 0)
|
2015-12-15 00:47:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-06 21:32:46 +00:00
|
|
|
stdin = stdinf
|
|
|
|
|
|
|
|
stdoutf, err := os.OpenFile(stdout, syscall.O_RDWR, 0)
|
2015-12-15 00:47:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-06 21:32:46 +00:00
|
|
|
go io.Copy(os.Stdout, stdoutf)
|
2015-12-14 22:40:50 +00:00
|
|
|
|
2016-01-06 21:32:46 +00:00
|
|
|
stderrf, err := os.OpenFile(stderr, syscall.O_RDWR, 0)
|
2015-12-14 22:15:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-06 21:32:46 +00:00
|
|
|
go io.Copy(os.Stderr, stderrf)
|
2015-12-14 22:15:26 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-23 22:40:55 +00:00
|
|
|
var killCommand = cli.Command{
|
2015-12-02 22:41:49 +00:00
|
|
|
Name: "kill",
|
2016-01-23 22:32:56 +00:00
|
|
|
Usage: "send a signal to a container or its processes",
|
2015-12-02 22:41:49 +00:00
|
|
|
Flags: []cli.Flag{
|
2016-01-06 21:32:46 +00:00
|
|
|
cli.StringFlag{
|
2015-12-02 22:41:49 +00:00
|
|
|
Name: "pid,p",
|
|
|
|
Usage: "pid of the process to signal within the container",
|
|
|
|
},
|
|
|
|
cli.IntFlag{
|
|
|
|
Name: "signal,s",
|
|
|
|
Value: 15,
|
|
|
|
Usage: "signal to send to the container",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: func(context *cli.Context) {
|
|
|
|
id := context.Args().First()
|
|
|
|
if id == "" {
|
|
|
|
fatal("container id cannot be empty", 1)
|
|
|
|
}
|
2015-12-14 23:54:11 +00:00
|
|
|
c := getClient(context)
|
2015-12-09 23:37:04 +00:00
|
|
|
if _, err := c.Signal(netcontext.Background(), &types.SignalRequest{
|
|
|
|
Id: id,
|
2016-01-06 21:32:46 +00:00
|
|
|
Pid: context.String("pid"),
|
2015-12-09 23:37:04 +00:00
|
|
|
Signal: uint32(context.Int("signal")),
|
|
|
|
}); err != nil {
|
2015-12-02 22:41:49 +00:00
|
|
|
fatal(err.Error(), 1)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
2015-12-10 19:03:36 +00:00
|
|
|
|
2016-01-23 22:40:55 +00:00
|
|
|
var execCommand = cli.Command{
|
2015-12-10 19:03:36 +00:00
|
|
|
Name: "exec",
|
|
|
|
Usage: "exec another process in an existing container",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "id",
|
|
|
|
Usage: "container id to add the process to",
|
|
|
|
},
|
2015-12-15 19:43:51 +00:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "attach,a",
|
|
|
|
Usage: "connect to the stdio of the container",
|
|
|
|
},
|
2015-12-10 19:03:36 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "cwd",
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
cli.IntFlag{
|
|
|
|
Name: "uid,u",
|
|
|
|
Usage: "user id of the user for the process",
|
|
|
|
},
|
|
|
|
cli.IntFlag{
|
|
|
|
Name: "gid,g",
|
|
|
|
Usage: "group id of the user for the process",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: func(context *cli.Context) {
|
2016-01-06 21:32:46 +00:00
|
|
|
panic("not implemented")
|
|
|
|
/*
|
|
|
|
p := &types.AddProcessRequest{
|
|
|
|
Args: context.Args(),
|
|
|
|
Cwd: context.String("cwd"),
|
|
|
|
Terminal: context.Bool("tty"),
|
|
|
|
Id: context.String("id"),
|
|
|
|
Env: context.StringSlice("env"),
|
|
|
|
User: &types.User{
|
|
|
|
Uid: uint32(context.Int("uid")),
|
|
|
|
Gid: uint32(context.Int("gid")),
|
|
|
|
},
|
2015-12-15 19:43:51 +00:00
|
|
|
}
|
2016-01-06 21:32:46 +00:00
|
|
|
c := getClient(context)
|
|
|
|
events, err := c.Events(netcontext.Background(), &types.EventsRequest{})
|
|
|
|
if err != nil {
|
|
|
|
fatal(err.Error(), 1)
|
|
|
|
}
|
|
|
|
if context.Bool("attach") {
|
|
|
|
if p.Terminal {
|
|
|
|
if err := attachTty(&p.Console); err != nil {
|
|
|
|
fatal(err.Error(), 1)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := attachStdio(&p.Stdin, &p.Stdout, &p.Stderr); err != nil {
|
|
|
|
fatal(err.Error(), 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r, err := c.AddProcess(netcontext.Background(), p)
|
2015-12-15 19:43:51 +00:00
|
|
|
if err != nil {
|
|
|
|
fatal(err.Error(), 1)
|
|
|
|
}
|
2016-01-06 21:32:46 +00:00
|
|
|
if context.Bool("attach") {
|
|
|
|
go func() {
|
|
|
|
io.Copy(stdin, os.Stdin)
|
|
|
|
if state != nil {
|
|
|
|
term.RestoreTerminal(os.Stdin.Fd(), state)
|
|
|
|
}
|
|
|
|
stdin.Close()
|
|
|
|
}()
|
|
|
|
for {
|
|
|
|
e, err := events.Recv()
|
|
|
|
if err != nil {
|
|
|
|
fatal(err.Error(), 1)
|
|
|
|
}
|
|
|
|
if e.Pid == r.Pid && e.Type == "exit" {
|
|
|
|
os.Exit(int(e.Status))
|
|
|
|
}
|
|
|
|
}
|
2015-12-15 19:43:51 +00:00
|
|
|
}
|
2016-01-06 21:32:46 +00:00
|
|
|
*/
|
2015-12-10 19:03:36 +00:00
|
|
|
},
|
|
|
|
}
|
2015-12-14 22:43:00 +00:00
|
|
|
|
2016-01-23 22:40:55 +00:00
|
|
|
var statsCommand = cli.Command{
|
2015-12-14 22:43:00 +00:00
|
|
|
Name: "stats",
|
|
|
|
Usage: "get stats for running container",
|
|
|
|
Action: func(context *cli.Context) {
|
|
|
|
req := &types.StatsRequest{
|
2015-12-15 00:06:27 +00:00
|
|
|
Id: context.Args().First(),
|
2015-12-14 22:43:00 +00:00
|
|
|
}
|
2015-12-14 23:54:11 +00:00
|
|
|
c := getClient(context)
|
2015-12-14 22:43:00 +00:00
|
|
|
stream, err := c.GetStats(netcontext.Background(), req)
|
|
|
|
if err != nil {
|
|
|
|
fatal(err.Error(), 1)
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
stats, err := stream.Recv()
|
|
|
|
if err != nil {
|
|
|
|
fatal(err.Error(), 1)
|
|
|
|
}
|
|
|
|
fmt.Println(stats)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|