Merge pull request #1299 from crosbymichael/ctr

Update ctr containers and tasks command
This commit is contained in:
Derek McGowan 2017-08-07 13:32:03 -07:00 committed by GitHub
commit 738c22a756
15 changed files with 188 additions and 168 deletions

View File

@ -9,7 +9,7 @@ import (
"github.com/urfave/cli"
)
var attachCommand = cli.Command{
var taskAttachCommand = cli.Command{
Name: "attach",
Usage: "attach to the IO of a running container",
ArgsUsage: "CONTAINER",

View File

@ -8,7 +8,7 @@ import (
"github.com/urfave/cli"
)
var checkpointCommand = cli.Command{
var taskCheckpointCommand = cli.Command{
Name: "checkpoint",
Usage: "checkpoint a container",
ArgsUsage: "CONTAINER",

77
cmd/ctr/containers.go Normal file
View File

@ -0,0 +1,77 @@
package main
import (
"fmt"
"os"
"strings"
"text/tabwriter"
"github.com/urfave/cli"
)
var containersCommand = cli.Command{
Name: "containers",
Usage: "manage containers (metadata)",
Aliases: []string{"c"},
Flags: []cli.Flag{
cli.BoolFlag{
Name: "quiet, q",
Usage: "print only the container id",
},
},
Subcommands: []cli.Command{
containersDeleteCommand,
containersSetLabelsCommand,
containerInfoCommand,
},
ArgsUsage: "[filter, ...]",
Action: func(context *cli.Context) error {
var (
filters = context.Args()
quiet = context.Bool("quiet")
ctx, cancel = appContext(context)
)
defer cancel()
client, err := newClient(context)
if err != nil {
return err
}
containers, err := client.Containers(ctx, filters...)
if err != nil {
return err
}
if quiet {
for _, c := range containers {
fmt.Printf("%s\n", c.ID())
}
return nil
}
w := tabwriter.NewWriter(os.Stdout, 4, 8, 4, ' ', 0)
fmt.Fprintln(w, "CONTAINER\tIMAGE\tRUNTIME\tLABELS\t")
for _, c := range containers {
var labelStrings []string
for k, v := range c.Info().Labels {
labelStrings = append(labelStrings, strings.Join([]string{k, v}, "="))
}
labels := strings.Join(labelStrings, ",")
if labels == "" {
labels = "-"
}
imageName := c.Info().Image
if imageName == "" {
imageName = "-"
}
record := c.Info()
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%v\t\n",
c.ID(),
imageName,
record.Runtime.Name,
labels,
); err != nil {
return err
}
}
return w.Flush()
},
}

View File

@ -9,7 +9,7 @@ import (
"github.com/urfave/cli"
)
var execCommand = cli.Command{
var taskExecCommand = cli.Command{
Name: "exec",
Usage: "execute additional processes in an existing container",
ArgsUsage: "CONTAINER CMD [ARG...]",

View File

@ -9,7 +9,7 @@ import (
"github.com/urfave/cli"
)
var infoCommand = cli.Command{
var containerInfoCommand = cli.Command{
Name: "info",
Usage: "get info about a container",
ArgsUsage: "CONTAINER",

View File

@ -5,7 +5,7 @@ import (
"github.com/urfave/cli"
)
var killCommand = cli.Command{
var taskKillCommand = cli.Command{
Name: "kill",
Usage: "signal a container (default: SIGTERM)",
ArgsUsage: "CONTAINER",

View File

@ -1,138 +0,0 @@
package main
import (
"fmt"
"os"
"strings"
"text/tabwriter"
tasks "github.com/containerd/containerd/api/services/tasks/v1"
"github.com/urfave/cli"
)
var taskListCommand = cli.Command{
Name: "tasks",
Usage: "manage tasks",
Aliases: []string{"t"},
Subcommands: []cli.Command{
{
Name: "list",
Usage: "list tasks",
Aliases: []string{"ls"},
Flags: []cli.Flag{
cli.BoolFlag{
Name: "quiet, q",
Usage: "print only the task id & pid",
},
},
Action: taskListFn,
},
},
}
var containersListCommand = cli.Command{
Name: "list",
Usage: "list all tasks or those that match a filter",
ArgsUsage: "[filter, ...]",
Aliases: []string{"ls"},
Flags: []cli.Flag{
cli.BoolFlag{
Name: "quiet, q",
Usage: "print only the container id",
},
},
Action: containerListFn,
}
func taskListFn(context *cli.Context) error {
var (
quiet = context.Bool("quiet")
ctx, cancel = appContext(context)
)
defer cancel()
client, err := newClient(context)
if err != nil {
return err
}
s := client.TaskService()
response, err := s.List(ctx, &tasks.ListTasksRequest{})
if err != nil {
return err
}
if quiet {
for _, task := range response.Tasks {
fmt.Println(task.ID)
}
} else {
w := tabwriter.NewWriter(os.Stdout, 4, 8, 4, ' ', 0)
fmt.Fprintln(w, "TASK\tPID\tSTATUS\t")
for _, task := range response.Tasks {
if _, err := fmt.Fprintf(w, "%s\t%d\t%s\n",
task.ID,
task.Pid,
task.Status.String(),
); err != nil {
return err
}
}
return w.Flush()
}
return nil
}
func containerListFn(context *cli.Context) error {
var (
filters = context.Args()
quiet = context.Bool("quiet")
ctx, cancel = appContext(context)
)
defer cancel()
client, err := newClient(context)
if err != nil {
return err
}
containers, err := client.Containers(ctx, filters...)
if err != nil {
return err
}
if quiet {
for _, c := range containers {
fmt.Printf("%s\n", c.ID())
}
} else {
w := tabwriter.NewWriter(os.Stdout, 4, 8, 4, ' ', 0)
fmt.Fprintln(w, "CONTAINER\tIMAGE\tRUNTIME\tLABELS\t")
for _, c := range containers {
var labelStrings []string
for k, v := range c.Info().Labels {
labelStrings = append(labelStrings, strings.Join([]string{k, v}, "="))
}
labels := strings.Join(labelStrings, ",")
if labels == "" {
labels = "-"
}
imageName := c.Info().Image
if imageName == "" {
imageName = "-"
}
record := c.Info()
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%v\t\n",
c.ID(),
imageName,
record.Runtime.Name,
labels,
); err != nil {
return err
}
}
return w.Flush()
}
return nil
}

View File

@ -65,30 +65,21 @@ containerd CLI
}
app.Commands = append([]cli.Command{
applyCommand,
attachCommand,
checkpointCommand,
containersCommand,
contentCommand,
eventsCommand,
execCommand,
fetchCommand,
fetchObjectCommand,
imageCommand,
infoCommand,
killCommand,
namespacesCommand,
pauseCommand,
pprofCommand,
psCommand,
pullCommand,
pushCommand,
pushObjectCommand,
resumeCommand,
rootfsCommand,
runCommand,
snapshotCommand,
startCommand,
taskListCommand,
tasksCommand,
versionCommand,
}, extraCmds...)
app.Before = func(context *cli.Context) error {
@ -102,14 +93,3 @@ containerd CLI
os.Exit(1)
}
}
var containersCommand = cli.Command{
Name: "containers",
Usage: "manage containers (metadata)",
Aliases: []string{"c"},
Subcommands: []cli.Command{
containersListCommand,
containersDeleteCommand,
containersSetLabelsCommand,
},
}

View File

@ -2,7 +2,7 @@ package main
import "github.com/urfave/cli"
var pauseCommand = cli.Command{
var taskPauseCommand = cli.Command{
Name: "pause",
Usage: "pause an existing container",
ArgsUsage: "CONTAINER",

View File

@ -9,7 +9,7 @@ import (
"github.com/urfave/cli"
)
var psCommand = cli.Command{
var taskPsCommand = cli.Command{
Name: "ps",
Usage: "list processes for container",
ArgsUsage: "CONTAINER",

View File

@ -2,7 +2,7 @@ package main
import "github.com/urfave/cli"
var resumeCommand = cli.Command{
var taskResumeCommand = cli.Command{
Name: "resume",
Usage: "resume a paused container",
ArgsUsage: "CONTAINER",

View File

@ -8,7 +8,7 @@ import (
"github.com/urfave/cli"
)
var startCommand = cli.Command{
var taskStartCommand = cli.Command{
Name: "start",
Usage: "start a container that have been created",
ArgsUsage: "CONTAINER",

68
cmd/ctr/task.go Normal file
View File

@ -0,0 +1,68 @@
package main
import (
"fmt"
"os"
"text/tabwriter"
tasks "github.com/containerd/containerd/api/services/tasks/v1"
"github.com/urfave/cli"
)
var tasksCommand = cli.Command{
Name: "tasks",
Usage: "manage tasks",
Aliases: []string{"t"},
Flags: []cli.Flag{
cli.BoolFlag{
Name: "quiet, q",
Usage: "print only the task id & pid",
},
},
Subcommands: []cli.Command{
taskAttachCommand,
taskCheckpointCommand,
taskExecCommand,
taskKillCommand,
taskPauseCommand,
taskPsCommand,
taskResumeCommand,
taskStartCommand,
taskDeleteCommand,
},
Action: func(context *cli.Context) error {
var (
quiet = context.Bool("quiet")
ctx, cancel = appContext(context)
)
defer cancel()
client, err := newClient(context)
if err != nil {
return err
}
s := client.TaskService()
response, err := s.List(ctx, &tasks.ListTasksRequest{})
if err != nil {
return err
}
if quiet {
for _, task := range response.Tasks {
fmt.Println(task.ID)
}
return nil
}
w := tabwriter.NewWriter(os.Stdout, 4, 8, 4, ' ', 0)
fmt.Fprintln(w, "TASK\tPID\tSTATUS\t")
for _, task := range response.Tasks {
if _, err := fmt.Fprintf(w, "%s\t%d\t%s\n",
task.ID,
task.Pid,
task.Status.String(),
); err != nil {
return err
}
}
return w.Flush()
},
}

33
cmd/ctr/task_delete.go Normal file
View File

@ -0,0 +1,33 @@
package main
import "github.com/urfave/cli"
var taskDeleteCommand = cli.Command{
Name: "delete",
Usage: "delete a task",
ArgsUsage: "CONTAINER",
Action: func(context *cli.Context) error {
ctx, cancel := appContext(context)
defer cancel()
client, err := newClient(context)
if err != nil {
return err
}
container, err := client.LoadContainer(ctx, context.Args().First())
if err != nil {
return err
}
task, err := container.Task(ctx, nil)
if err != nil {
return err
}
status, err := task.Delete(ctx)
if err != nil {
return err
}
if status != 0 {
return cli.NewExitError("", int(status))
}
return nil
},
}