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-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-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 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
serviceByNameList, err := client.ServiceList(ctx, types.ServiceListOptions{Filters: serviceNameFilter})
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
for _, service := range options.services {
|
2016-07-29 11:20:03 -04:00
|
|
|
serviceCount := 0
|
|
|
|
// Lookup by ID/Prefix
|
|
|
|
for _, serviceEntry := range serviceByIDList {
|
|
|
|
if strings.HasPrefix(serviceEntry.ID, service) {
|
|
|
|
filter.Add("service", serviceEntry.ID)
|
|
|
|
serviceCount++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup by Name/Prefix
|
|
|
|
for _, serviceEntry := range serviceByNameList {
|
|
|
|
if strings.HasPrefix(serviceEntry.Spec.Annotations.Name, service) {
|
|
|
|
filter.Add("service", serviceEntry.ID)
|
|
|
|
serviceCount++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If nothing has been found, return immediately.
|
|
|
|
if serviceCount == 0 {
|
2017-03-09 13:23:45 -05:00
|
|
|
return errors.Errorf("no such services: %s", service)
|
2016-07-29 11:20:03 -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 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
filter.Del("node", nodeFilter)
|
|
|
|
filter.Add("node", nodeReference)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-01 10:01:16 -04:00
|
|
|
tasks, err := client.TaskList(ctx, types.TaskListOptions{Filters: filter})
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
format := options.format
|
2016-11-07 00:54:40 -05:00
|
|
|
if len(format) == 0 {
|
2017-05-15 08:45:19 -04:00
|
|
|
if len(dockerCli.ConfigFile().TasksFormat) > 0 && !options.quiet {
|
2016-11-07 00:54:40 -05:00
|
|
|
format = dockerCli.ConfigFile().TasksFormat
|
|
|
|
} else {
|
|
|
|
format = formatter.TableFormatKey
|
|
|
|
}
|
2016-10-27 20:02:57 -04:00
|
|
|
}
|
2016-11-07 00:54:40 -05:00
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
return task.Print(ctx, dockerCli, tasks, idresolver.New(client, options.noResolve), !options.noTrunc, options.quiet, format)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|