2016-09-08 13:11:39 -04:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
2016-09-04 03:38:50 -04:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-09-04 03:38:50 -04:00
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/cli"
|
|
|
|
"github.com/docker/docker/cli/command"
|
2016-11-07 00:54:40 -05:00
|
|
|
"github.com/docker/docker/cli/command/formatter"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/cli/command/idresolver"
|
|
|
|
"github.com/docker/docker/cli/command/task"
|
|
|
|
"github.com/docker/docker/opts"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
type psOptions struct {
|
2016-09-04 03:38:50 -04:00
|
|
|
nodeIDs []string
|
2016-09-08 13:11:39 -04:00
|
|
|
noResolve bool
|
|
|
|
noTrunc bool
|
2016-11-07 00:54:40 -05:00
|
|
|
quiet bool
|
|
|
|
format string
|
2016-09-08 13:11:39 -04:00
|
|
|
filter opts.FilterOpt
|
|
|
|
}
|
|
|
|
|
2016-12-25 16:23:35 -05:00
|
|
|
func newPsCommand(dockerCli command.Cli) *cobra.Command {
|
2016-09-08 13:11:39 -04:00
|
|
|
opts := psOptions{filter: opts.NewFilterOpt()}
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
2016-09-04 03:38:50 -04:00
|
|
|
Use: "ps [OPTIONS] [NODE...]",
|
|
|
|
Short: "List tasks running on one or more nodes, defaults to current node",
|
|
|
|
Args: cli.RequiresMinArgs(0),
|
2016-09-08 13:11:39 -04:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2016-09-04 03:38:50 -04:00
|
|
|
opts.nodeIDs = []string{"self"}
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
if len(args) != 0 {
|
2016-09-04 03:38:50 -04:00
|
|
|
opts.nodeIDs = args
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return runPs(dockerCli, opts)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
flags := cmd.Flags()
|
|
|
|
flags.BoolVar(&opts.noTrunc, "no-trunc", false, "Do not truncate output")
|
|
|
|
flags.BoolVar(&opts.noResolve, "no-resolve", false, "Do not map IDs to Names")
|
|
|
|
flags.VarP(&opts.filter, "filter", "f", "Filter output based on conditions provided")
|
2016-11-07 00:54:40 -05:00
|
|
|
flags.StringVar(&opts.format, "format", "", "Pretty-print tasks using a Go template")
|
|
|
|
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Only display task IDs")
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2016-12-25 16:23:35 -05:00
|
|
|
func runPs(dockerCli command.Cli, opts psOptions) error {
|
2016-09-08 13:11:39 -04:00
|
|
|
client := dockerCli.Client()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2016-09-04 03:38:50 -04:00
|
|
|
var (
|
|
|
|
errs []string
|
|
|
|
tasks []swarm.Task
|
|
|
|
)
|
|
|
|
|
|
|
|
for _, nodeID := range opts.nodeIDs {
|
|
|
|
nodeRef, err := Reference(ctx, client, nodeID)
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
node, _, err := client.NodeInspectWithRaw(ctx, nodeRef)
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
filter := opts.filter.Value()
|
|
|
|
filter.Add("node", node.ID)
|
|
|
|
|
2016-11-01 10:01:16 -04:00
|
|
|
nodeTasks, err := client.TaskList(ctx, types.TaskListOptions{Filters: filter})
|
2016-09-04 03:38:50 -04:00
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
tasks = append(tasks, nodeTasks...)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2016-09-04 03:38:50 -04:00
|
|
|
|
2016-11-07 00:54:40 -05:00
|
|
|
format := opts.format
|
|
|
|
if len(format) == 0 {
|
|
|
|
if dockerCli.ConfigFile() != nil && len(dockerCli.ConfigFile().TasksFormat) > 0 && !opts.quiet {
|
|
|
|
format = dockerCli.ConfigFile().TasksFormat
|
|
|
|
} else {
|
|
|
|
format = formatter.TableFormatKey
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := task.Print(dockerCli, ctx, tasks, idresolver.New(client, opts.noResolve), !opts.noTrunc, opts.quiet, format); err != nil {
|
2016-09-04 03:38:50 -04:00
|
|
|
errs = append(errs, err.Error())
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2016-09-04 03:38:50 -04:00
|
|
|
if len(errs) > 0 {
|
|
|
|
return fmt.Errorf("%s", strings.Join(errs, "\n"))
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2016-09-04 03:38:50 -04:00
|
|
|
return nil
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|