github.com/kunnos/engine@v1.13.1/cli/command/node/ps.go (about)

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