2016-09-08 13:11:39 -04:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2016-07-29 11:20:03 -04:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
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"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/cli"
|
|
|
|
"github.com/docker/docker/cli/command"
|
2016-11-07 00:54:40 -05:00
|
|
|
"github.com/docker/docker/cli/command/formatter"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/cli/command/idresolver"
|
|
|
|
"github.com/docker/docker/cli/command/node"
|
|
|
|
"github.com/docker/docker/cli/command/task"
|
|
|
|
"github.com/docker/docker/opts"
|
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
|
|
|
func newPsCommand(dockerCli *command.DockerCli) *cobra.Command {
|
|
|
|
opts := psOptions{filter: opts.NewFilterOpt()}
|
|
|
|
|
|
|
|
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 {
|
2016-07-29 11:20:03 -04:00
|
|
|
opts.services = args
|
2016-09-08 13:11:39 -04:00
|
|
|
return runPS(dockerCli, opts)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
flags := cmd.Flags()
|
2016-10-27 20:02:57 -04:00
|
|
|
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Only display task IDs")
|
2016-09-08 13:11:39 -04:00
|
|
|
flags.BoolVar(&opts.noTrunc, "no-trunc", false, "Do not truncate output")
|
|
|
|
flags.BoolVar(&opts.noResolve, "no-resolve", false, "Do not map IDs to Names")
|
2016-11-07 00:54:40 -05:00
|
|
|
flags.StringVar(&opts.format, "format", "", "Pretty-print tasks using a Go template")
|
2016-09-08 13:11:39 -04:00
|
|
|
flags.VarP(&opts.filter, "filter", "f", "Filter output based on conditions provided")
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func runPS(dockerCli *command.DockerCli, opts psOptions) error {
|
|
|
|
client := dockerCli.Client()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2016-07-29 11:20:03 -04:00
|
|
|
filter := opts.filter.Value()
|
|
|
|
|
|
|
|
serviceIDFilter := filters.NewArgs()
|
|
|
|
serviceNameFilter := filters.NewArgs()
|
|
|
|
for _, service := range opts.services {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-07-29 11:20:03 -04:00
|
|
|
for _, service := range opts.services {
|
|
|
|
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 {
|
|
|
|
return fmt.Errorf("no such services: %s", service)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-11-07 00:54:40 -05:00
|
|
|
format := opts.format
|
|
|
|
if len(format) == 0 {
|
|
|
|
if len(dockerCli.ConfigFile().TasksFormat) > 0 && !opts.quiet {
|
|
|
|
format = dockerCli.ConfigFile().TasksFormat
|
|
|
|
} else {
|
|
|
|
format = formatter.TableFormatKey
|
|
|
|
}
|
2016-10-27 20:02:57 -04:00
|
|
|
}
|
2016-11-07 00:54:40 -05:00
|
|
|
|
|
|
|
return task.Print(dockerCli, ctx, tasks, idresolver.New(client, opts.noResolve), !opts.noTrunc, opts.quiet, format)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|