2016-09-08 13:11:39 -04:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2016-09-04 03:38:50 -04:00
|
|
|
"strings"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2022-03-30 09:27:25 -04:00
|
|
|
"github.com/docker/cli/cli/command/completion"
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli/command/idresolver"
|
|
|
|
"github.com/docker/cli/cli/command/task"
|
2017-05-15 08:45:19 -04:00
|
|
|
"github.com/docker/cli/opts"
|
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"
|
2017-03-09 13:23:45 -05:00
|
|
|
"github.com/pkg/errors"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
2017-05-15 08:45:19 -04:00
|
|
|
options := psOptions{filter: opts.NewFilterOpt()}
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
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 {
|
2017-05-15 08:45:19 -04:00
|
|
|
options.nodeIDs = []string{"self"}
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
if len(args) != 0 {
|
2017-05-15 08:45:19 -04:00
|
|
|
options.nodeIDs = args
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2023-09-09 18:27:44 -04:00
|
|
|
return runPs(cmd.Context(), dockerCli, options)
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
2022-03-30 09:27:25 -04:00
|
|
|
ValidArgsFunction: completion.NoComplete,
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
flags := cmd.Flags()
|
2017-05-15 08:45:19 -04:00
|
|
|
flags.BoolVar(&options.noTrunc, "no-trunc", false, "Do not truncate output")
|
|
|
|
flags.BoolVar(&options.noResolve, "no-resolve", false, "Do not map IDs to Names")
|
|
|
|
flags.VarP(&options.filter, "filter", "f", "Filter output based on conditions provided")
|
|
|
|
flags.StringVar(&options.format, "format", "", "Pretty-print tasks using a Go template")
|
|
|
|
flags.BoolVarP(&options.quiet, "quiet", "q", false, "Only display task IDs")
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2023-09-09 18:27:44 -04:00
|
|
|
func runPs(ctx context.Context, dockerCli command.Cli, options psOptions) error {
|
2016-09-08 13:11:39 -04:00
|
|
|
client := dockerCli.Client()
|
|
|
|
|
2016-09-04 03:38:50 -04:00
|
|
|
var (
|
|
|
|
errs []string
|
|
|
|
tasks []swarm.Task
|
|
|
|
)
|
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
for _, nodeID := range options.nodeIDs {
|
2016-09-04 03:38:50 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
filter := options.filter.Value()
|
2016-09-04 03:38:50 -04:00
|
|
|
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
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
format := options.format
|
2016-11-07 00:54:40 -05:00
|
|
|
if len(format) == 0 {
|
2017-07-05 13:40:08 -04:00
|
|
|
format = task.DefaultFormat(dockerCli.ConfigFile(), options.quiet)
|
2016-11-07 00:54:40 -05:00
|
|
|
}
|
|
|
|
|
2017-03-21 19:19:59 -04:00
|
|
|
if len(errs) == 0 || len(tasks) != 0 {
|
2017-05-15 08:45:19 -04:00
|
|
|
if err := task.Print(ctx, dockerCli, tasks, idresolver.New(client, options.noResolve), !options.noTrunc, options.quiet, format); err != nil {
|
2017-03-21 19:19:59 -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 {
|
2017-03-09 13:23:45 -05:00
|
|
|
return errors.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
|
|
|
}
|