ps.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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/idresolver"
  10. "github.com/docker/docker/cli/command/task"
  11. "github.com/docker/docker/opts"
  12. "github.com/spf13/cobra"
  13. "golang.org/x/net/context"
  14. )
  15. type psOptions struct {
  16. nodeIDs []string
  17. all bool
  18. noResolve bool
  19. noTrunc bool
  20. filter opts.FilterOpt
  21. }
  22. func newPsCommand(dockerCli *command.DockerCli) *cobra.Command {
  23. opts := psOptions{filter: opts.NewFilterOpt()}
  24. cmd := &cobra.Command{
  25. Use: "ps [OPTIONS] [NODE...]",
  26. Short: "List tasks running on one or more nodes, defaults to current node",
  27. Args: cli.RequiresMinArgs(0),
  28. RunE: func(cmd *cobra.Command, args []string) error {
  29. opts.nodeIDs = []string{"self"}
  30. if len(args) != 0 {
  31. opts.nodeIDs = args
  32. }
  33. return runPs(dockerCli, opts)
  34. },
  35. }
  36. flags := cmd.Flags()
  37. flags.BoolVar(&opts.noTrunc, "no-trunc", false, "Do not truncate output")
  38. flags.BoolVar(&opts.noResolve, "no-resolve", false, "Do not map IDs to Names")
  39. flags.VarP(&opts.filter, "filter", "f", "Filter output based on conditions provided")
  40. flags.BoolVarP(&opts.all, "all", "a", false, "Show all tasks (default shows tasks that are or will be running)")
  41. return cmd
  42. }
  43. func runPs(dockerCli *command.DockerCli, opts psOptions) error {
  44. client := dockerCli.Client()
  45. ctx := context.Background()
  46. var (
  47. errs []string
  48. tasks []swarm.Task
  49. )
  50. for _, nodeID := range opts.nodeIDs {
  51. nodeRef, err := Reference(ctx, client, nodeID)
  52. if err != nil {
  53. errs = append(errs, err.Error())
  54. continue
  55. }
  56. node, _, err := client.NodeInspectWithRaw(ctx, nodeRef)
  57. if err != nil {
  58. errs = append(errs, err.Error())
  59. continue
  60. }
  61. filter := opts.filter.Value()
  62. filter.Add("node", node.ID)
  63. if !opts.all && !filter.Include("desired-state") {
  64. filter.Add("desired-state", string(swarm.TaskStateRunning))
  65. filter.Add("desired-state", string(swarm.TaskStateAccepted))
  66. }
  67. nodeTasks, err := client.TaskList(ctx, types.TaskListOptions{Filters: filter})
  68. if err != nil {
  69. errs = append(errs, err.Error())
  70. continue
  71. }
  72. tasks = append(tasks, nodeTasks...)
  73. }
  74. if err := task.Print(dockerCli, ctx, tasks, idresolver.New(client, opts.noResolve), opts.noTrunc); err != nil {
  75. errs = append(errs, err.Error())
  76. }
  77. if len(errs) > 0 {
  78. return fmt.Errorf("%s", strings.Join(errs, "\n"))
  79. }
  80. return nil
  81. }