2016-09-08 13:11:39 -04:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2016-07-29 11:20:03 -04:00
|
|
|
"strings"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
|
|
|
"github.com/docker/cli/cli/command/idresolver"
|
|
|
|
"github.com/docker/cli/cli/command/node"
|
|
|
|
"github.com/docker/cli/cli/command/task"
|
2017-05-15 08:45:19 -04:00
|
|
|
"github.com/docker/cli/opts"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-07-29 11:20:03 -04:00
|
|
|
"github.com/docker/docker/api/types/filters"
|
2017-06-01 11:47:43 -04:00
|
|
|
"github.com/docker/docker/client"
|
2017-03-09 13:23:45 -05:00
|
|
|
"github.com/pkg/errors"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type psOptions struct {
|
2016-07-29 11:20:03 -04:00
|
|
|
services []string
|
2016-10-27 20:02:57 -04:00
|
|
|
quiet bool
|
2016-09-08 13:11:39 -04:00
|
|
|
noResolve bool
|
|
|
|
noTrunc bool
|
2016-11-07 00:54:40 -05:00
|
|
|
format string
|
2016-09-08 13:11:39 -04:00
|
|
|
filter opts.FilterOpt
|
|
|
|
}
|
|
|
|
|
2017-05-03 17:58:52 -04:00
|
|
|
func newPsCommand(dockerCli command.Cli) *cobra.Command {
|
2017-05-15 08:45:19 -04:00
|
|
|
options := psOptions{filter: opts.NewFilterOpt()}
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
2016-07-29 11:20:03 -04:00
|
|
|
Use: "ps [OPTIONS] SERVICE [SERVICE...]",
|
|
|
|
Short: "List the tasks of one or more services",
|
|
|
|
Args: cli.RequiresMinArgs(1),
|
2016-09-08 13:11:39 -04:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2017-05-15 08:45:19 -04:00
|
|
|
options.services = args
|
2023-09-09 18:27:44 -04:00
|
|
|
return runPS(cmd.Context(), dockerCli, options)
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
2022-03-30 09:27:25 -04:00
|
|
|
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
|
|
|
return CompletionFn(dockerCli)(cmd, args, toComplete)
|
|
|
|
},
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
flags := cmd.Flags()
|
2017-05-15 08:45:19 -04:00
|
|
|
flags.BoolVarP(&options.quiet, "quiet", "q", false, "Only display task IDs")
|
|
|
|
flags.BoolVar(&options.noTrunc, "no-trunc", false, "Do not truncate output")
|
|
|
|
flags.BoolVar(&options.noResolve, "no-resolve", false, "Do not map IDs to Names")
|
|
|
|
flags.StringVar(&options.format, "format", "", "Pretty-print tasks using a Go template")
|
|
|
|
flags.VarP(&options.filter, "filter", "f", "Filter output based on conditions provided")
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2023-09-09 18:27:44 -04:00
|
|
|
func runPS(ctx context.Context, dockerCli command.Cli, options psOptions) error {
|
2023-11-20 11:38:50 -05:00
|
|
|
apiClient := dockerCli.Client()
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2023-11-20 11:38:50 -05:00
|
|
|
filter, notfound, err := createFilter(ctx, apiClient, options)
|
2017-06-01 11:47:43 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-11-20 11:38:50 -05:00
|
|
|
if err := updateNodeFilter(ctx, apiClient, filter); err != nil {
|
2017-05-09 18:35:25 -04:00
|
|
|
return err
|
|
|
|
}
|
2017-06-01 11:47:43 -04:00
|
|
|
|
2023-11-20 11:38:50 -05:00
|
|
|
tasks, err := apiClient.TaskList(ctx, types.TaskListOptions{Filters: filter})
|
2017-06-01 11:47:43 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
format := options.format
|
|
|
|
if len(format) == 0 {
|
2017-07-05 13:40:08 -04:00
|
|
|
format = task.DefaultFormat(dockerCli.ConfigFile(), options.quiet)
|
2017-06-01 11:47:43 -04:00
|
|
|
}
|
2017-09-30 16:19:34 -04:00
|
|
|
if options.quiet {
|
|
|
|
options.noTrunc = true
|
|
|
|
}
|
2023-11-20 11:38:50 -05:00
|
|
|
if err := task.Print(ctx, dockerCli, tasks, idresolver.New(apiClient, options.noResolve), !options.noTrunc, options.quiet, format); err != nil {
|
2017-06-01 11:47:43 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(notfound) != 0 {
|
|
|
|
return errors.New(strings.Join(notfound, "\n"))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-20 11:38:50 -05:00
|
|
|
func createFilter(ctx context.Context, apiClient client.APIClient, options psOptions) (filters.Args, []string, error) {
|
2017-05-15 08:45:19 -04:00
|
|
|
filter := options.filter.Value()
|
2016-07-29 11:20:03 -04:00
|
|
|
|
|
|
|
serviceIDFilter := filters.NewArgs()
|
|
|
|
serviceNameFilter := filters.NewArgs()
|
2017-05-15 08:45:19 -04:00
|
|
|
for _, service := range options.services {
|
2016-07-29 11:20:03 -04:00
|
|
|
serviceIDFilter.Add("id", service)
|
|
|
|
serviceNameFilter.Add("name", service)
|
|
|
|
}
|
2023-11-20 11:38:50 -05:00
|
|
|
serviceByIDList, err := apiClient.ServiceList(ctx, types.ServiceListOptions{Filters: serviceIDFilter})
|
2016-07-29 11:20:03 -04:00
|
|
|
if err != nil {
|
2017-06-01 11:47:43 -04:00
|
|
|
return filter, nil, err
|
2016-07-29 11:20:03 -04:00
|
|
|
}
|
2023-11-20 11:38:50 -05:00
|
|
|
serviceByNameList, err := apiClient.ServiceList(ctx, types.ServiceListOptions{Filters: serviceNameFilter})
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
2017-06-01 11:47:43 -04:00
|
|
|
return filter, nil, err
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2017-06-01 11:47:43 -04:00
|
|
|
var notfound []string
|
2017-05-31 15:43:56 -04:00
|
|
|
serviceCount := 0
|
|
|
|
loop:
|
|
|
|
// Match services by 1. Full ID, 2. Full name, 3. ID prefix. An error is returned if the ID-prefix match is ambiguous
|
2017-05-15 08:45:19 -04:00
|
|
|
for _, service := range options.services {
|
2017-05-31 15:43:56 -04:00
|
|
|
for _, s := range serviceByIDList {
|
|
|
|
if s.ID == service {
|
|
|
|
filter.Add("service", s.ID)
|
2016-07-29 11:20:03 -04:00
|
|
|
serviceCount++
|
2017-05-31 15:43:56 -04:00
|
|
|
continue loop
|
2016-07-29 11:20:03 -04:00
|
|
|
}
|
|
|
|
}
|
2017-05-31 15:43:56 -04:00
|
|
|
for _, s := range serviceByNameList {
|
|
|
|
if s.Spec.Annotations.Name == service {
|
|
|
|
filter.Add("service", s.ID)
|
2016-07-29 11:20:03 -04:00
|
|
|
serviceCount++
|
2017-05-31 15:43:56 -04:00
|
|
|
continue loop
|
2016-07-29 11:20:03 -04:00
|
|
|
}
|
|
|
|
}
|
2017-05-31 15:43:56 -04:00
|
|
|
found := false
|
|
|
|
for _, s := range serviceByIDList {
|
|
|
|
if strings.HasPrefix(s.ID, service) {
|
|
|
|
if found {
|
2017-06-01 11:47:43 -04:00
|
|
|
return filter, nil, errors.New("multiple services found with provided prefix: " + service)
|
2017-05-31 15:43:56 -04:00
|
|
|
}
|
|
|
|
filter.Add("service", s.ID)
|
|
|
|
serviceCount++
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
2017-06-01 11:47:43 -04:00
|
|
|
notfound = append(notfound, "no such service: "+service)
|
2016-07-29 11:20:03 -04:00
|
|
|
}
|
|
|
|
}
|
2017-05-31 15:43:56 -04:00
|
|
|
if serviceCount == 0 {
|
2017-06-01 11:47:43 -04:00
|
|
|
return filter, nil, errors.New(strings.Join(notfound, "\n"))
|
2017-05-31 15:43:56 -04:00
|
|
|
}
|
2017-05-09 18:35:25 -04:00
|
|
|
return filter, notfound, err
|
|
|
|
}
|
|
|
|
|
2023-11-20 11:38:50 -05:00
|
|
|
func updateNodeFilter(ctx context.Context, apiClient client.APIClient, filter filters.Args) error {
|
2018-07-27 09:37:43 -04:00
|
|
|
if filter.Contains("node") {
|
2016-09-08 13:11:39 -04:00
|
|
|
nodeFilters := filter.Get("node")
|
|
|
|
for _, nodeFilter := range nodeFilters {
|
2023-11-20 11:38:50 -05:00
|
|
|
nodeReference, err := node.Reference(ctx, apiClient, nodeFilter)
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
2017-05-09 18:35:25 -04:00
|
|
|
return err
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
filter.Del("node", nodeFilter)
|
|
|
|
filter.Add("node", nodeReference)
|
|
|
|
}
|
|
|
|
}
|
2017-05-09 18:35:25 -04:00
|
|
|
return nil
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|