2016-06-13 19:56:23 -07:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/client"
|
|
|
|
"github.com/docker/docker/api/client/idresolver"
|
|
|
|
"github.com/docker/docker/api/client/task"
|
|
|
|
"github.com/docker/docker/cli"
|
|
|
|
"github.com/docker/docker/opts"
|
|
|
|
"github.com/docker/engine-api/types"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2016-07-19 14:01:31 -07:00
|
|
|
type psOptions struct {
|
2016-06-13 19:56:23 -07:00
|
|
|
nodeID string
|
|
|
|
noResolve bool
|
|
|
|
filter opts.FilterOpt
|
|
|
|
}
|
|
|
|
|
2016-07-19 14:01:31 -07:00
|
|
|
func newPSCommand(dockerCli *client.DockerCli) *cobra.Command {
|
|
|
|
opts := psOptions{filter: opts.NewFilterOpt()}
|
2016-06-13 19:56:23 -07:00
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
2016-07-19 14:01:31 -07:00
|
|
|
Use: "ps [OPTIONS] self|NODE",
|
2016-06-13 19:56:23 -07:00
|
|
|
Short: "List tasks running on a node",
|
|
|
|
Args: cli.ExactArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
opts.nodeID = args[0]
|
2016-07-19 14:01:31 -07:00
|
|
|
return runPS(dockerCli, opts)
|
2016-06-13 19:56:23 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
flags := cmd.Flags()
|
2016-07-13 18:46:17 +02:00
|
|
|
flags.BoolVar(&opts.noResolve, "no-resolve", false, "Do not map IDs to Names")
|
2016-06-13 19:56:23 -07:00
|
|
|
flags.VarP(&opts.filter, "filter", "f", "Filter output based on conditions provided")
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2016-07-19 14:01:31 -07:00
|
|
|
func runPS(dockerCli *client.DockerCli, opts psOptions) error {
|
2016-06-13 19:56:23 -07:00
|
|
|
client := dockerCli.Client()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2016-06-30 15:09:03 +02:00
|
|
|
nodeRef, err := Reference(client, ctx, opts.nodeID)
|
2016-06-13 19:56:23 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2016-06-30 14:04:02 -07:00
|
|
|
node, _, err := client.NodeInspectWithRaw(ctx, nodeRef)
|
2016-06-13 19:56:23 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
filter := opts.filter.Value()
|
|
|
|
filter.Add("node", node.ID)
|
|
|
|
tasks, err := client.TaskList(
|
|
|
|
ctx,
|
|
|
|
types.TaskListOptions{Filter: filter})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return task.Print(dockerCli, ctx, tasks, idresolver.New(client, opts.noResolve))
|
|
|
|
}
|