2016-09-08 13:11:39 -04:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
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/formatter"
|
|
|
|
"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"
|
2017-05-15 08:45:19 -04:00
|
|
|
"golang.org/x/net/context"
|
2016-09-08 13:11:39 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
return runPS(dockerCli, options)
|
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
|
|
|
|
}
|
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
func runPS(dockerCli command.Cli, options psOptions) error {
|
2016-09-08 13:11:39 -04:00
|
|
|
client := dockerCli.Client()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2017-06-01 11:47:43 -04:00
|
|
|
filter, notfound, err := createFilter(ctx, client, options)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tasks, err := client.TaskList(ctx, types.TaskListOptions{Filters: filter})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
format := options.format
|
|
|
|
if len(format) == 0 {
|
|
|
|
if len(dockerCli.ConfigFile().TasksFormat) > 0 && !options.quiet {
|
|
|
|
format = dockerCli.ConfigFile().TasksFormat
|
|
|
|
} else {
|
|
|
|
format = formatter.TableFormatKey
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := task.Print(ctx, dockerCli, tasks, idresolver.New(client, options.noResolve), !options.noTrunc, options.quiet, format); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(notfound) != 0 {
|
|
|
|
return errors.New(strings.Join(notfound, "\n"))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func createFilter(ctx context.Context, client 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)
|
|
|
|
}
|
|
|
|
serviceByIDList, err := client.ServiceList(ctx, types.ServiceListOptions{Filters: serviceIDFilter})
|
|
|
|
if err != nil {
|
2017-06-01 11:47:43 -04:00
|
|
|
return filter, nil, err
|
2016-07-29 11:20:03 -04:00
|
|
|
}
|
|
|
|
serviceByNameList, err := client.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
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
if filter.Include("node") {
|
|
|
|
nodeFilters := filter.Get("node")
|
|
|
|
for _, nodeFilter := range nodeFilters {
|
|
|
|
nodeReference, err := node.Reference(ctx, client, nodeFilter)
|
|
|
|
if err != nil {
|
2017-06-01 11:47:43 -04:00
|
|
|
return filter, nil, err
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
filter.Del("node", nodeFilter)
|
|
|
|
filter.Add("node", nodeReference)
|
|
|
|
}
|
|
|
|
}
|
2017-06-01 11:47:43 -04:00
|
|
|
return filter, notfound, err
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|