From 139fff2bf0ebe12b61871ba8ec8be8d51c2338db Mon Sep 17 00:00:00 2001 From: Josh Horwitz Date: Thu, 3 Nov 2016 09:58:45 -0400 Subject: [PATCH] Add -a option to service/node ps Signed-off-by: Josh Horwitz --- cli/command/node/ps.go | 7 ++ cli/command/service/ps.go | 8 +++ docs/reference/commandline/node_ps.md | 2 +- docs/reference/commandline/service_ps.md | 1 + integration-cli/docker_cli_swarm_test.go | 82 ++++++++++++++++++++++++ 5 files changed, 99 insertions(+), 1 deletion(-) diff --git a/cli/command/node/ps.go b/cli/command/node/ps.go index a034721d24..8591f04669 100644 --- a/cli/command/node/ps.go +++ b/cli/command/node/ps.go @@ -17,6 +17,7 @@ import ( type psOptions struct { nodeIDs []string + all bool noResolve bool noTrunc bool filter opts.FilterOpt @@ -43,6 +44,7 @@ func newPsCommand(dockerCli *command.DockerCli) *cobra.Command { 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") + flags.BoolVarP(&opts.all, "all", "a", false, "Show all tasks (default shows tasks that are or will be running)") return cmd } @@ -72,6 +74,11 @@ func runPs(dockerCli *command.DockerCli, opts psOptions) error { filter := opts.filter.Value() filter.Add("node", node.ID) + if !opts.all && !filter.Include("desired-state") { + filter.Add("desired-state", string(swarm.TaskStateRunning)) + filter.Add("desired-state", string(swarm.TaskStateAccepted)) + } + nodeTasks, err := client.TaskList(ctx, types.TaskListOptions{Filters: filter}) if err != nil { errs = append(errs, err.Error()) diff --git a/cli/command/service/ps.go b/cli/command/service/ps.go index cf94ad7374..0028507c22 100644 --- a/cli/command/service/ps.go +++ b/cli/command/service/ps.go @@ -2,6 +2,7 @@ package service import ( "github.com/docker/docker/api/types" + "github.com/docker/docker/api/types/swarm" "github.com/docker/docker/cli" "github.com/docker/docker/cli/command" "github.com/docker/docker/cli/command/idresolver" @@ -14,6 +15,7 @@ import ( type psOptions struct { serviceID string + all bool quiet bool noResolve bool noTrunc bool @@ -37,6 +39,7 @@ func newPsCommand(dockerCli *command.DockerCli) *cobra.Command { 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") + flags.BoolVarP(&opts.all, "all", "a", false, "Show all tasks (default shows tasks that are or will be running)") return cmd } @@ -64,6 +67,11 @@ func runPS(dockerCli *command.DockerCli, opts psOptions) error { } } + if !opts.all && !filter.Include("desired-state") { + filter.Add("desired-state", string(swarm.TaskStateRunning)) + filter.Add("desired-state", string(swarm.TaskStateAccepted)) + } + tasks, err := client.TaskList(ctx, types.TaskListOptions{Filters: filter}) if err != nil { return err diff --git a/docs/reference/commandline/node_ps.md b/docs/reference/commandline/node_ps.md index 0c54c9cfb0..7a4054bebe 100644 --- a/docs/reference/commandline/node_ps.md +++ b/docs/reference/commandline/node_ps.md @@ -22,7 +22,7 @@ Usage: docker node ps [OPTIONS] [NODE...] List tasks running on one or more nodes, defaults to current node. Options: - -a, --all Display all instances + -a, --all Show all tasks (default shows tasks that are or will be running) -f, --filter value Filter output based on conditions provided --help Print usage --no-resolve Do not map IDs to Names diff --git a/docs/reference/commandline/service_ps.md b/docs/reference/commandline/service_ps.md index d1d14633ed..8a5a5f565c 100644 --- a/docs/reference/commandline/service_ps.md +++ b/docs/reference/commandline/service_ps.md @@ -22,6 +22,7 @@ Usage: docker service ps [OPTIONS] SERVICE List the tasks of a service Options: + -a, --all Show all tasks (default shows tasks that are or will be running) -f, --filter filter Filter output based on conditions provided --help Print usage --no-resolve Do not map IDs to Names diff --git a/integration-cli/docker_cli_swarm_test.go b/integration-cli/docker_cli_swarm_test.go index d4c1e97f3d..d551fe543a 100644 --- a/integration-cli/docker_cli_swarm_test.go +++ b/integration-cli/docker_cli_swarm_test.go @@ -203,6 +203,88 @@ func (s *DockerSwarmSuite) TestSwarmNodeTaskListFilter(c *check.C) { c.Assert(out, checker.Not(checker.Contains), name+".1") c.Assert(out, checker.Not(checker.Contains), name+".2") c.Assert(out, checker.Not(checker.Contains), name+".3") + + out, err = d.Cmd("node", "ps", "--filter", "desired-state=running", "self") + c.Assert(err, checker.IsNil) + c.Assert(out, checker.Contains, name+".1") + c.Assert(out, checker.Contains, name+".2") + c.Assert(out, checker.Contains, name+".3") + + out, err = d.Cmd("node", "ps", "--filter", "desired-state=shutdown", "self") + c.Assert(err, checker.IsNil) + c.Assert(out, checker.Not(checker.Contains), name+".1") + c.Assert(out, checker.Not(checker.Contains), name+".2") + c.Assert(out, checker.Not(checker.Contains), name+".3") +} + +func (s *DockerSwarmSuite) TestSwarmServiceTaskListAll(c *check.C) { + d := s.AddDaemon(c, true, true) + + name := "service-task-list-1" + out, err := d.Cmd("service", "create", "--name", name, "--replicas=3", "busybox", "top") + c.Assert(err, checker.IsNil) + c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "") + + // make sure task has been deployed. + waitAndAssert(c, defaultReconciliationTimeout, d.checkActiveContainerCount, checker.Equals, 3) + + out, err = d.Cmd("service", "ps", name) + c.Assert(err, checker.IsNil) + c.Assert(out, checker.Contains, name+".1") + c.Assert(out, checker.Contains, name+".2") + c.Assert(out, checker.Contains, name+".3") + + // Get the last container id so we can restart it to cause a task error in the history + containerID, err := d.Cmd("ps", "-q", "-l") + c.Assert(err, checker.IsNil) + + _, err = d.Cmd("stop", strings.TrimSpace(containerID)) + c.Assert(err, checker.IsNil) + + waitAndAssert(c, defaultReconciliationTimeout, d.checkActiveContainerCount, checker.Equals, 3) + + out, err = d.Cmd("service", "ps", name) + c.Assert(err, checker.IsNil) + c.Assert(out, checker.Count, name, 3) + + out, err = d.Cmd("service", "ps", name, "-a") + c.Assert(err, checker.IsNil) + c.Assert(out, checker.Count, name, 4) +} + +func (s *DockerSwarmSuite) TestSwarmNodeTaskListAll(c *check.C) { + d := s.AddDaemon(c, true, true) + + name := "node-task-list" + out, err := d.Cmd("service", "create", "--name", name, "--replicas=3", "busybox", "top") + c.Assert(err, checker.IsNil) + c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "") + + // make sure task has been deployed. + waitAndAssert(c, defaultReconciliationTimeout, d.checkActiveContainerCount, checker.Equals, 3) + + out, err = d.Cmd("service", "ps", name) + c.Assert(err, checker.IsNil) + c.Assert(out, checker.Contains, name+".1") + c.Assert(out, checker.Contains, name+".2") + c.Assert(out, checker.Contains, name+".3") + + // Get the last container id so we can restart it to cause a task error in the history + containerID, err := d.Cmd("ps", "-q", "-l") + c.Assert(err, checker.IsNil) + + _, err = d.Cmd("stop", strings.TrimSpace(containerID)) + c.Assert(err, checker.IsNil) + + waitAndAssert(c, defaultReconciliationTimeout, d.checkActiveContainerCount, checker.Equals, 3) + + out, err = d.Cmd("node", "ps", "self") + c.Assert(err, checker.IsNil) + c.Assert(out, checker.Count, name, 3) + + out, err = d.Cmd("node", "ps", "self", "-a") + c.Assert(err, checker.IsNil) + c.Assert(out, checker.Count, name, 4) } // Test case for #25375