Add -a option to service/node ps

Signed-off-by: Josh Horwitz <horwitzja@gmail.com>
This commit is contained in:
Josh Horwitz 2016-11-03 09:58:45 -04:00
parent aa90a531c5
commit 139fff2bf0
5 changed files with 99 additions and 1 deletions

View file

@ -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())

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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