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"
|
2016-01-29 22:13:58 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2015-12-14 23:54:11 +00:00
|
|
|
"net"
|
2015-12-02 22:41:49 +00:00
|
|
|
"os"
|
2016-02-02 22:31:06 +00:00
|
|
|
"os/signal"
|
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"
|
2016-02-29 18:48:39 +00:00
|
|
|
"github.com/docker/containerd/specs"
|
2015-12-15 00:47:42 +00:00
|
|
|
"github.com/docker/docker/pkg/term"
|
2015-12-09 23:37:04 +00:00
|
|
|
netcontext "golang.org/x/net/context"
|
|
|
|
"google.golang.org/grpc"
|
2016-01-29 22:13:58 +00:00
|
|
|
"google.golang.org/grpc/grpclog"
|
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 {
|
2016-04-01 11:09:02 +00:00
|
|
|
// Parse proto://address form addresses.
|
|
|
|
bindSpec := ctx.GlobalString("address")
|
|
|
|
bindParts := strings.SplitN(bindSpec, "://", 2)
|
|
|
|
if len(bindParts) != 2 {
|
|
|
|
fatal(fmt.Sprintf("bad bind address format %s, expected proto://address", bindSpec), 1)
|
|
|
|
}
|
|
|
|
|
2016-01-29 22:13:58 +00:00
|
|
|
// 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))
|
2016-03-28 17:09:40 +00:00
|
|
|
dialOpts := []grpc.DialOption{grpc.WithInsecure(), grpc.WithTimeout(ctx.GlobalDuration("conn-timeout"))}
|
2015-12-14 23:54:11 +00:00
|
|
|
dialOpts = append(dialOpts,
|
|
|
|
grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
|
2016-04-01 11:09:02 +00:00
|
|
|
return net.DialTimeout(bindParts[0], bindParts[1], timeout)
|
2015-12-14 23:54:11 +00:00
|
|
|
},
|
|
|
|
))
|
2016-04-01 11:09:02 +00:00
|
|
|
conn, err := grpc.Dial(bindSpec, 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-04-13 07:03:53 +00:00
|
|
|
var contSubCmds = []cli.Command{
|
|
|
|
execCommand,
|
|
|
|
killCommand,
|
|
|
|
listCommand,
|
|
|
|
pauseCommand,
|
|
|
|
resumeCommand,
|
|
|
|
startCommand,
|
2016-04-29 23:08:11 +00:00
|
|
|
stateCommand,
|
2016-04-13 07:03:53 +00:00
|
|
|
statsCommand,
|
|
|
|
watchCommand,
|
|
|
|
updateCommand,
|
|
|
|
}
|
|
|
|
|
2016-01-23 22:40:55 +00:00
|
|
|
var containersCommand = cli.Command{
|
2016-04-13 07:03:53 +00:00
|
|
|
Name: "containers",
|
|
|
|
Usage: "interact with running containers",
|
|
|
|
ArgsUsage: "COMMAND [arguments...]",
|
|
|
|
Subcommands: contSubCmds,
|
|
|
|
Description: func() string {
|
|
|
|
desc := "\n COMMAND:\n"
|
|
|
|
for _, command := range contSubCmds {
|
|
|
|
desc += fmt.Sprintf(" %-10.10s%s\n", command.Name, command.Usage)
|
|
|
|
}
|
|
|
|
return desc
|
|
|
|
}(),
|
2015-12-02 22:41:49 +00:00
|
|
|
Action: listContainers,
|
|
|
|
}
|
|
|
|
|
2016-02-03 22:30:45 +00:00
|
|
|
var stateCommand = cli.Command{
|
|
|
|
Name: "state",
|
|
|
|
Usage: "get a raw dump of the containerd state",
|
|
|
|
Action: func(context *cli.Context) {
|
|
|
|
c := getClient(context)
|
2016-02-11 20:20:29 +00:00
|
|
|
resp, err := c.State(netcontext.Background(), &types.StateRequest{
|
|
|
|
Id: context.Args().First(),
|
|
|
|
})
|
2016-02-03 22:30:45 +00:00
|
|
|
if err != nil {
|
|
|
|
fatal(err.Error(), 1)
|
|
|
|
}
|
|
|
|
data, err := json.Marshal(resp)
|
|
|
|
if err != nil {
|
|
|
|
fatal(err.Error(), 1)
|
|
|
|
}
|
|
|
|
fmt.Print(string(data))
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
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)
|
2016-02-11 20:20:29 +00:00
|
|
|
resp, err := c.State(netcontext.Background(), &types.StateRequest{
|
|
|
|
Id: context.Args().First(),
|
|
|
|
})
|
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")
|
2016-02-25 20:59:34 +00:00
|
|
|
sortContainers(resp.Containers)
|
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-23 22:40:55 +00:00
|
|
|
var startCommand = cli.Command{
|
2016-04-13 07:03:53 +00:00
|
|
|
Name: "start",
|
|
|
|
Usage: "start a container",
|
|
|
|
ArgsUsage: "ID BundlePath",
|
2015-12-02 22:41:49 +00:00
|
|
|
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",
|
|
|
|
},
|
2016-02-11 21:44:25 +00:00
|
|
|
cli.StringSliceFlag{
|
|
|
|
Name: "label,l",
|
|
|
|
Value: &cli.StringSlice{},
|
|
|
|
Usage: "set labels for the container",
|
|
|
|
},
|
2016-03-30 21:25:42 +00:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "no-pivot",
|
|
|
|
Usage: "do not use pivot root",
|
|
|
|
},
|
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 == "" {
|
2016-04-22 15:08:42 +00:00
|
|
|
fatal("bundle path cannot be empty", ExitStatusMissingArg)
|
2015-12-02 22:41:49 +00:00
|
|
|
}
|
|
|
|
if id == "" {
|
2016-04-22 15:08:42 +00:00
|
|
|
fatal("container id cannot be empty", ExitStatusMissingArg)
|
2015-12-02 22:41:49 +00:00
|
|
|
}
|
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)
|
|
|
|
}
|
2016-02-03 21:56:15 +00:00
|
|
|
s, err := createStdio()
|
2016-01-06 21:32:46 +00:00
|
|
|
if err != nil {
|
|
|
|
fatal(err.Error(), 1)
|
|
|
|
}
|
2016-02-03 21:56:15 +00:00
|
|
|
var (
|
2016-03-22 16:47:35 +00:00
|
|
|
restoreAndCloseStdin func()
|
|
|
|
tty bool
|
|
|
|
c = getClient(context)
|
|
|
|
r = &types.CreateContainerRequest{
|
2016-03-30 21:25:42 +00:00
|
|
|
Id: id,
|
|
|
|
BundlePath: bpath,
|
|
|
|
Checkpoint: context.String("checkpoint"),
|
|
|
|
Stdin: s.stdin,
|
|
|
|
Stdout: s.stdout,
|
|
|
|
Stderr: s.stderr,
|
|
|
|
Labels: context.StringSlice("label"),
|
|
|
|
NoPivotRoot: context.Bool("no-pivot"),
|
2016-02-03 21:56:15 +00:00
|
|
|
}
|
|
|
|
)
|
2016-03-22 16:47:35 +00:00
|
|
|
restoreAndCloseStdin = func() {
|
|
|
|
if state != nil {
|
|
|
|
term.RestoreTerminal(os.Stdin.Fd(), state)
|
|
|
|
}
|
|
|
|
if stdin != nil {
|
|
|
|
stdin.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defer restoreAndCloseStdin()
|
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)
|
|
|
|
}
|
2016-02-02 22:31:06 +00:00
|
|
|
tty = mkterm
|
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
|
|
|
|
}
|
2016-02-03 21:56:15 +00:00
|
|
|
if err := attachStdio(s); err != nil {
|
2016-01-06 21:32:46 +00:00
|
|
|
fatal(err.Error(), 1)
|
2015-12-15 00:47:42 +00:00
|
|
|
}
|
2015-12-02 22:41:49 +00:00
|
|
|
}
|
2016-02-03 21:56:15 +00:00
|
|
|
events, err := c.Events(netcontext.Background(), &types.EventsRequest{})
|
|
|
|
if err != nil {
|
|
|
|
fatal(err.Error(), 1)
|
|
|
|
}
|
|
|
|
if _, err := c.CreateContainer(netcontext.Background(), r); err != nil {
|
|
|
|
fatal(err.Error(), 1)
|
|
|
|
}
|
2015-12-15 18:51:12 +00:00
|
|
|
if context.Bool("attach") {
|
2016-01-06 08:25:38 +00:00
|
|
|
go func() {
|
|
|
|
io.Copy(stdin, os.Stdin)
|
2016-02-02 22:21:25 +00:00
|
|
|
if _, err := c.UpdateProcess(netcontext.Background(), &types.UpdateProcessRequest{
|
|
|
|
Id: id,
|
|
|
|
Pid: "init",
|
|
|
|
CloseStdin: true,
|
|
|
|
}); err != nil {
|
|
|
|
fatal(err.Error(), 1)
|
|
|
|
}
|
2016-01-06 08:25:38 +00:00
|
|
|
restoreAndCloseStdin()
|
2015-12-15 00:47:42 +00:00
|
|
|
}()
|
2016-02-02 22:31:06 +00:00
|
|
|
if tty {
|
|
|
|
resize(id, "init", c)
|
|
|
|
go func() {
|
|
|
|
s := make(chan os.Signal, 64)
|
|
|
|
signal.Notify(s, syscall.SIGWINCH)
|
|
|
|
for range s {
|
|
|
|
if err := resize(id, "init", c); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2016-03-22 16:47:03 +00:00
|
|
|
waitForExit(c, events, id, "init", restoreAndCloseStdin)
|
2015-12-14 22:40:50 +00:00
|
|
|
}
|
2015-12-02 22:41:49 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-02-02 22:31:06 +00:00
|
|
|
func resize(id, pid string, c types.APIClient) error {
|
|
|
|
ws, err := term.GetWinsize(os.Stdin.Fd())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := c.UpdateProcess(netcontext.Background(), &types.UpdateProcessRequest{
|
|
|
|
Id: id,
|
|
|
|
Pid: "init",
|
|
|
|
Width: uint32(ws.Width),
|
|
|
|
Height: uint32(ws.Height),
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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-02-03 21:56:15 +00:00
|
|
|
func attachStdio(s stdio) error {
|
|
|
|
stdinf, err := os.OpenFile(s.stdin, syscall.O_RDWR, 0)
|
2015-12-15 00:47:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-03 21:56:15 +00:00
|
|
|
// FIXME: assign to global
|
2016-01-06 21:32:46 +00:00
|
|
|
stdin = stdinf
|
2016-02-03 21:56:15 +00:00
|
|
|
stdoutf, err := os.OpenFile(s.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)
|
2016-02-03 21:56:15 +00:00
|
|
|
stderrf, err := os.OpenFile(s.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-02-25 18:10:22 +00:00
|
|
|
var watchCommand = cli.Command{
|
|
|
|
Name: "watch",
|
|
|
|
Usage: "print container events",
|
|
|
|
Action: func(context *cli.Context) {
|
|
|
|
c := getClient(context)
|
|
|
|
id := context.Args().First()
|
|
|
|
if id != "" {
|
|
|
|
resp, err := c.State(netcontext.Background(), &types.StateRequest{Id: id})
|
|
|
|
if err != nil {
|
|
|
|
fatal(err.Error(), 1)
|
|
|
|
}
|
|
|
|
for _, c := range resp.Containers {
|
|
|
|
if c.Id == id {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if id == "" {
|
|
|
|
fatal("Invalid container id", 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
events, reqErr := c.Events(netcontext.Background(), &types.EventsRequest{})
|
|
|
|
if reqErr != nil {
|
|
|
|
fatal(reqErr.Error(), 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
e, err := events.Recv()
|
|
|
|
if err != nil {
|
|
|
|
fatal(err.Error(), 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if id == "" || e.Id == id {
|
|
|
|
fmt.Printf("%#v\n", e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-02-25 18:09:22 +00:00
|
|
|
var pauseCommand = cli.Command{
|
|
|
|
Name: "pause",
|
|
|
|
Usage: "pause a container",
|
|
|
|
Action: func(context *cli.Context) {
|
|
|
|
id := context.Args().First()
|
|
|
|
if id == "" {
|
2016-04-22 15:08:42 +00:00
|
|
|
fatal("container id cannot be empty", ExitStatusMissingArg)
|
2016-02-25 18:09:22 +00:00
|
|
|
}
|
|
|
|
c := getClient(context)
|
|
|
|
_, err := c.UpdateContainer(netcontext.Background(), &types.UpdateContainerRequest{
|
|
|
|
Id: id,
|
|
|
|
Pid: "init",
|
|
|
|
Status: "paused",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
fatal(err.Error(), 1)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-02-25 18:10:05 +00:00
|
|
|
var resumeCommand = cli.Command{
|
|
|
|
Name: "resume",
|
|
|
|
Usage: "resume a paused container",
|
|
|
|
Action: func(context *cli.Context) {
|
|
|
|
id := context.Args().First()
|
|
|
|
if id == "" {
|
2016-04-22 15:08:42 +00:00
|
|
|
fatal("container id cannot be empty", ExitStatusMissingArg)
|
2016-02-25 18:10:05 +00:00
|
|
|
}
|
|
|
|
c := getClient(context)
|
|
|
|
_, err := c.UpdateContainer(netcontext.Background(), &types.UpdateContainerRequest{
|
|
|
|
Id: id,
|
|
|
|
Pid: "init",
|
|
|
|
Status: "running",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
fatal(err.Error(), 1)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
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",
|
2016-02-02 22:21:25 +00:00
|
|
|
Value: "init",
|
2015-12-02 22:41:49 +00:00
|
|
|
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 == "" {
|
2016-04-22 15:08:42 +00:00
|
|
|
fatal("container id cannot be empty", ExitStatusMissingArg)
|
2015-12-02 22:41:49 +00:00
|
|
|
}
|
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",
|
|
|
|
},
|
2016-02-01 19:02:41 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "pid",
|
|
|
|
Usage: "process id for the new process",
|
|
|
|
},
|
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-03-22 16:47:35 +00:00
|
|
|
var restoreAndCloseStdin func()
|
|
|
|
|
2016-02-01 19:02:41 +00:00
|
|
|
p := &types.AddProcessRequest{
|
2016-02-03 21:56:15 +00:00
|
|
|
Id: context.String("id"),
|
2016-02-01 19:02:41 +00:00
|
|
|
Pid: context.String("pid"),
|
|
|
|
Args: context.Args(),
|
|
|
|
Cwd: context.String("cwd"),
|
|
|
|
Terminal: context.Bool("tty"),
|
|
|
|
Env: context.StringSlice("env"),
|
|
|
|
User: &types.User{
|
|
|
|
Uid: uint32(context.Int("uid")),
|
|
|
|
Gid: uint32(context.Int("gid")),
|
|
|
|
},
|
|
|
|
}
|
2016-02-03 21:56:15 +00:00
|
|
|
s, err := createStdio()
|
2016-02-01 19:02:41 +00:00
|
|
|
if err != nil {
|
|
|
|
fatal(err.Error(), 1)
|
|
|
|
}
|
2016-02-03 21:56:15 +00:00
|
|
|
p.Stdin = s.stdin
|
|
|
|
p.Stdout = s.stdout
|
|
|
|
p.Stderr = s.stderr
|
2016-03-22 16:47:35 +00:00
|
|
|
restoreAndCloseStdin = func() {
|
|
|
|
if state != nil {
|
|
|
|
term.RestoreTerminal(os.Stdin.Fd(), state)
|
|
|
|
}
|
|
|
|
if stdin != nil {
|
|
|
|
stdin.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defer restoreAndCloseStdin()
|
2016-02-01 19:02:41 +00:00
|
|
|
if context.Bool("attach") {
|
|
|
|
if context.Bool("tty") {
|
|
|
|
s, err := term.SetRawTerminal(os.Stdin.Fd())
|
2015-12-15 19:43:51 +00:00
|
|
|
if err != nil {
|
|
|
|
fatal(err.Error(), 1)
|
|
|
|
}
|
2016-02-01 19:02:41 +00:00
|
|
|
state = s
|
|
|
|
}
|
2016-02-03 21:56:15 +00:00
|
|
|
if err := attachStdio(s); err != nil {
|
2016-02-01 19:02:41 +00:00
|
|
|
fatal(err.Error(), 1)
|
|
|
|
}
|
|
|
|
}
|
2016-02-03 21:56:15 +00:00
|
|
|
c := getClient(context)
|
|
|
|
events, err := c.Events(netcontext.Background(), &types.EventsRequest{})
|
|
|
|
if err != nil {
|
|
|
|
fatal(err.Error(), 1)
|
|
|
|
}
|
|
|
|
if _, err := c.AddProcess(netcontext.Background(), p); err != nil {
|
|
|
|
fatal(err.Error(), 1)
|
|
|
|
}
|
2016-02-01 19:02:41 +00:00
|
|
|
if context.Bool("attach") {
|
|
|
|
go func() {
|
|
|
|
io.Copy(stdin, os.Stdin)
|
2016-02-03 21:56:15 +00:00
|
|
|
if _, err := c.UpdateProcess(netcontext.Background(), &types.UpdateProcessRequest{
|
|
|
|
Id: p.Id,
|
|
|
|
Pid: p.Pid,
|
|
|
|
CloseStdin: true,
|
|
|
|
}); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
2016-02-01 19:02:41 +00:00
|
|
|
restoreAndCloseStdin()
|
|
|
|
}()
|
2016-02-03 21:56:15 +00:00
|
|
|
if context.Bool("tty") {
|
|
|
|
resize(p.Id, p.Pid, c)
|
|
|
|
go func() {
|
|
|
|
s := make(chan os.Signal, 64)
|
|
|
|
signal.Notify(s, syscall.SIGWINCH)
|
|
|
|
for range s {
|
|
|
|
if err := resize(p.Id, p.Pid, c); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2016-03-22 16:47:03 +00:00
|
|
|
waitForExit(c, events, context.String("id"), context.String("pid"), restoreAndCloseStdin)
|
2016-02-01 19:02:41 +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)
|
2016-02-11 22:36:32 +00:00
|
|
|
stats, err := c.Stats(netcontext.Background(), req)
|
2015-12-14 22:43:00 +00:00
|
|
|
if err != nil {
|
|
|
|
fatal(err.Error(), 1)
|
|
|
|
}
|
2016-02-11 22:36:32 +00:00
|
|
|
data, err := json.Marshal(stats)
|
|
|
|
if err != nil {
|
|
|
|
fatal(err.Error(), 1)
|
2015-12-14 22:43:00 +00:00
|
|
|
}
|
2016-02-11 22:36:32 +00:00
|
|
|
fmt.Print(string(data))
|
2015-12-14 22:43:00 +00:00
|
|
|
},
|
|
|
|
}
|
2016-01-29 22:13:58 +00:00
|
|
|
|
2016-03-07 23:23:52 +00:00
|
|
|
var updateCommand = cli.Command{
|
|
|
|
Name: "update",
|
|
|
|
Usage: "update a containers resources",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.IntFlag{
|
|
|
|
Name: "memory-limit",
|
|
|
|
},
|
|
|
|
cli.IntFlag{
|
|
|
|
Name: "memory-reservation",
|
|
|
|
},
|
|
|
|
cli.IntFlag{
|
|
|
|
Name: "memory-swap",
|
|
|
|
},
|
|
|
|
cli.IntFlag{
|
|
|
|
Name: "cpu-quota",
|
|
|
|
},
|
|
|
|
cli.IntFlag{
|
|
|
|
Name: "cpu-period",
|
|
|
|
},
|
|
|
|
cli.IntFlag{
|
|
|
|
Name: "kernel-limit",
|
|
|
|
},
|
|
|
|
cli.IntFlag{
|
|
|
|
Name: "blkio-weight",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "cpuset-cpus",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "cpuset-mems",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: func(context *cli.Context) {
|
|
|
|
req := &types.UpdateContainerRequest{
|
|
|
|
Id: context.Args().First(),
|
|
|
|
}
|
|
|
|
req.Resources = &types.UpdateResource{}
|
|
|
|
req.Resources.MemoryLimit = uint32(context.Int("memory-limit"))
|
|
|
|
req.Resources.MemoryReservation = uint32(context.Int("memory-reservation"))
|
|
|
|
req.Resources.MemorySwap = uint32(context.Int("memory-swap"))
|
|
|
|
req.Resources.BlkioWeight = uint32(context.Int("blkio-weight"))
|
|
|
|
req.Resources.CpuPeriod = uint32(context.Int("cpu-period"))
|
|
|
|
req.Resources.CpuQuota = uint32(context.Int("cpu-quota"))
|
|
|
|
req.Resources.CpuShares = uint32(context.Int("cpu-shares"))
|
|
|
|
req.Resources.CpusetCpus = context.String("cpuset-cpus")
|
|
|
|
req.Resources.CpusetMems = context.String("cpuset-mems")
|
|
|
|
c := getClient(context)
|
|
|
|
if _, err := c.UpdateContainer(netcontext.Background(), req); err != nil {
|
|
|
|
fatal(err.Error(), 1)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-03-22 16:47:03 +00:00
|
|
|
func waitForExit(c types.APIClient, events types.API_EventsClient, id, pid string, closer func()) {
|
2016-03-22 17:38:47 +00:00
|
|
|
timestamp := uint64(time.Now().Unix())
|
2016-01-29 22:13:58 +00:00
|
|
|
for {
|
|
|
|
e, err := events.Recv()
|
|
|
|
if err != nil {
|
|
|
|
time.Sleep(1 * time.Second)
|
2016-03-22 17:38:47 +00:00
|
|
|
events, _ = c.Events(netcontext.Background(), &types.EventsRequest{Timestamp: timestamp})
|
2016-01-29 22:13:58 +00:00
|
|
|
continue
|
|
|
|
}
|
2016-03-22 17:38:47 +00:00
|
|
|
timestamp = e.Timestamp
|
2016-02-01 19:02:41 +00:00
|
|
|
if e.Id == id && e.Type == "exit" && e.Pid == pid {
|
2016-01-29 22:13:58 +00:00
|
|
|
closer()
|
|
|
|
os.Exit(int(e.Status))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-03 21:56:15 +00:00
|
|
|
|
|
|
|
type stdio struct {
|
|
|
|
stdin string
|
|
|
|
stdout string
|
|
|
|
stderr string
|
|
|
|
}
|
2016-04-26 20:29:35 +00:00
|
|
|
|
|
|
|
func createStdio() (s stdio, err error) {
|
|
|
|
tmp, err := ioutil.TempDir("", "ctr-")
|
|
|
|
if err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
// create fifo's for the process
|
|
|
|
for name, fd := range map[string]*string{
|
|
|
|
"stdin": &s.stdin,
|
|
|
|
"stdout": &s.stdout,
|
|
|
|
"stderr": &s.stderr,
|
|
|
|
} {
|
|
|
|
path := filepath.Join(tmp, name)
|
|
|
|
if err := syscall.Mkfifo(path, 0755); err != nil && !os.IsExist(err) {
|
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
*fd = path
|
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}
|