ps.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package node
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/docker/docker/api/types"
  6. "github.com/docker/docker/api/types/swarm"
  7. "github.com/docker/docker/cli"
  8. "github.com/docker/docker/cli/command"
  9. "github.com/docker/docker/cli/command/formatter"
  10. "github.com/docker/docker/cli/command/idresolver"
  11. "github.com/docker/docker/cli/command/task"
  12. "github.com/docker/docker/opts"
  13. "github.com/spf13/cobra"
  14. "golang.org/x/net/context"
  15. )
  16. type psOptions struct {
  17. nodeIDs []string
  18. noResolve bool
  19. noTrunc bool
  20. quiet bool
  21. format string
  22. filter opts.FilterOpt
  23. }
  24. func newPsCommand(dockerCli command.Cli) *cobra.Command {
  25. opts := psOptions{filter: opts.NewFilterOpt()}
  26. cmd := &cobra.Command{
  27. Use: "ps [OPTIONS] [NODE...]",
  28. Short: "List tasks running on one or more nodes, defaults to current node",
  29. Args: cli.RequiresMinArgs(0),
  30. RunE: func(cmd *cobra.Command, args []string) error {
  31. opts.nodeIDs = []string{"self"}
  32. if len(args) != 0 {
  33. opts.nodeIDs = args
  34. }
  35. return runPs(dockerCli, opts)
  36. },
  37. }
  38. flags := cmd.Flags()
  39. flags.BoolVar(&opts.noTrunc, "no-trunc", false, "Do not truncate output")
  40. flags.BoolVar(&opts.noResolve, "no-resolve", false, "Do not map IDs to Names")
  41. flags.VarP(&opts.filter, "filter", "f", "Filter output based on conditions provided")
  42. flags.StringVar(&opts.format, "format", "", "Pretty-print tasks using a Go template")
  43. flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Only display task IDs")
  44. return cmd
  45. }
  46. func runPs(dockerCli command.Cli, opts psOptions) error {
  47. client := dockerCli.Client()
  48. ctx := context.Background()
  49. var (
  50. errs []string
  51. tasks []swarm.Task
  52. )
  53. for _, nodeID := range opts.nodeIDs {
  54. nodeRef, err := Reference(ctx, client, nodeID)
  55. if err != nil {
  56. errs = append(errs, err.Error())
  57. continue
  58. }
  59. node, _, err := client.NodeInspectWithRaw(ctx, nodeRef)
  60. if err != nil {
  61. errs = append(errs, err.Error())
  62. continue
  63. }
  64. filter := opts.filter.Value()
  65. filter.Add("node", node.ID)
  66. nodeTasks, err := client.TaskList(ctx, types.TaskListOptions{Filters: filter})
  67. if err != nil {
  68. errs = append(errs, err.Error())
  69. continue
  70. }
  71. tasks = append(tasks, nodeTasks...)
  72. }
  73. format := opts.format
  74. if len(format) == 0 {
  75. if dockerCli.ConfigFile() != nil && len(dockerCli.ConfigFile().TasksFormat) > 0 && !opts.quiet {
  76. format = dockerCli.ConfigFile().TasksFormat
  77. } else {
  78. format = formatter.TableFormatKey
  79. }
  80. }
  81. if err := task.Print(dockerCli, ctx, tasks, idresolver.New(client, opts.noResolve), !opts.noTrunc, opts.quiet, format); err != nil {
  82. errs = append(errs, err.Error())
  83. }
  84. if len(errs) > 0 {
  85. return fmt.Errorf("%s", strings.Join(errs, "\n"))
  86. }
  87. return nil
  88. }