mirror of https://github.com/docker/cli.git
Merge pull request #26299 from allencloud/support-docker-node-ps-multi-nodes
support docker node ps multiNodes
This commit is contained in:
commit
e7e32f0465
|
@ -1,7 +1,11 @@
|
||||||
package node
|
package node
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/docker/docker/api/types"
|
||||||
|
"github.com/docker/docker/api/types/swarm"
|
||||||
"github.com/docker/docker/cli"
|
"github.com/docker/docker/cli"
|
||||||
"github.com/docker/docker/cli/command"
|
"github.com/docker/docker/cli/command"
|
||||||
"github.com/docker/docker/cli/command/idresolver"
|
"github.com/docker/docker/cli/command/idresolver"
|
||||||
|
@ -12,7 +16,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type psOptions struct {
|
type psOptions struct {
|
||||||
nodeID string
|
nodeIDs []string
|
||||||
noResolve bool
|
noResolve bool
|
||||||
noTrunc bool
|
noTrunc bool
|
||||||
filter opts.FilterOpt
|
filter opts.FilterOpt
|
||||||
|
@ -22,14 +26,14 @@ func newPsCommand(dockerCli *command.DockerCli) *cobra.Command {
|
||||||
opts := psOptions{filter: opts.NewFilterOpt()}
|
opts := psOptions{filter: opts.NewFilterOpt()}
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "ps [OPTIONS] [NODE]",
|
Use: "ps [OPTIONS] [NODE...]",
|
||||||
Short: "List tasks running on a node, defaults to current node",
|
Short: "List tasks running on one or more nodes, defaults to current node",
|
||||||
Args: cli.RequiresRangeArgs(0, 1),
|
Args: cli.RequiresMinArgs(0),
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
opts.nodeID = "self"
|
opts.nodeIDs = []string{"self"}
|
||||||
|
|
||||||
if len(args) != 0 {
|
if len(args) != 0 {
|
||||||
opts.nodeID = args[0]
|
opts.nodeIDs = args
|
||||||
}
|
}
|
||||||
|
|
||||||
return runPs(dockerCli, opts)
|
return runPs(dockerCli, opts)
|
||||||
|
@ -47,23 +51,43 @@ func runPs(dockerCli *command.DockerCli, opts psOptions) error {
|
||||||
client := dockerCli.Client()
|
client := dockerCli.Client()
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
nodeRef, err := Reference(ctx, client, opts.nodeID)
|
var (
|
||||||
if err != nil {
|
errs []string
|
||||||
return nil
|
tasks []swarm.Task
|
||||||
}
|
)
|
||||||
node, _, err := client.NodeInspectWithRaw(ctx, nodeRef)
|
|
||||||
if err != nil {
|
for _, nodeID := range opts.nodeIDs {
|
||||||
return err
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
filter := opts.filter.Value()
|
||||||
|
filter.Add("node", node.ID)
|
||||||
|
|
||||||
|
nodeTasks, err := client.TaskList(ctx, types.TaskListOptions{Filter: filter})
|
||||||
|
if err != nil {
|
||||||
|
errs = append(errs, err.Error())
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks = append(tasks, nodeTasks...)
|
||||||
}
|
}
|
||||||
|
|
||||||
filter := opts.filter.Value()
|
if err := task.Print(dockerCli, ctx, tasks, idresolver.New(client, opts.noResolve), opts.noTrunc); err != nil {
|
||||||
filter.Add("node", node.ID)
|
errs = append(errs, err.Error())
|
||||||
tasks, err := client.TaskList(
|
|
||||||
ctx,
|
|
||||||
types.TaskListOptions{Filter: filter})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return task.Print(dockerCli, ctx, tasks, idresolver.New(client, opts.noResolve), opts.noTrunc)
|
if len(errs) > 0 {
|
||||||
|
return fmt.Errorf("%s", strings.Join(errs, "\n"))
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue