2016-06-13 22:56:23 -04:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/client"
|
|
|
|
"github.com/docker/docker/api/client/idresolver"
|
2016-06-30 09:09:03 -04:00
|
|
|
"github.com/docker/docker/api/client/node"
|
2016-06-13 22:56:23 -04:00
|
|
|
"github.com/docker/docker/api/client/task"
|
|
|
|
"github.com/docker/docker/cli"
|
|
|
|
"github.com/docker/docker/opts"
|
|
|
|
"github.com/docker/engine-api/types"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2016-07-19 17:01:31 -04:00
|
|
|
type psOptions struct {
|
2016-06-13 22:56:23 -04:00
|
|
|
serviceID string
|
|
|
|
noResolve bool
|
2016-08-02 12:25:14 -04:00
|
|
|
noTrunc bool
|
2016-06-13 22:56:23 -04:00
|
|
|
filter opts.FilterOpt
|
|
|
|
}
|
|
|
|
|
2016-07-19 17:01:31 -04:00
|
|
|
func newPSCommand(dockerCli *client.DockerCli) *cobra.Command {
|
|
|
|
opts := psOptions{filter: opts.NewFilterOpt()}
|
2016-06-13 22:56:23 -04:00
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
2016-07-19 17:01:31 -04:00
|
|
|
Use: "ps [OPTIONS] SERVICE",
|
2016-06-13 22:56:23 -04:00
|
|
|
Short: "List the tasks of a service",
|
|
|
|
Args: cli.ExactArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
opts.serviceID = args[0]
|
2016-07-19 17:01:31 -04:00
|
|
|
return runPS(dockerCli, opts)
|
2016-06-13 22:56:23 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
flags := cmd.Flags()
|
2016-08-02 12:25:14 -04:00
|
|
|
flags.BoolVar(&opts.noTrunc, "no-trunc", false, "Do not truncate output")
|
2016-07-13 12:46:17 -04:00
|
|
|
flags.BoolVar(&opts.noResolve, "no-resolve", false, "Do not map IDs to Names")
|
2016-06-13 22:56:23 -04:00
|
|
|
flags.VarP(&opts.filter, "filter", "f", "Filter output based on conditions provided")
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2016-07-19 17:01:31 -04:00
|
|
|
func runPS(dockerCli *client.DockerCli, opts psOptions) error {
|
2016-06-13 22:56:23 -04:00
|
|
|
client := dockerCli.Client()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2016-06-16 18:47:22 -04:00
|
|
|
service, _, err := client.ServiceInspectWithRaw(ctx, opts.serviceID)
|
2016-06-13 22:56:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
filter := opts.filter.Value()
|
|
|
|
filter.Add("service", service.ID)
|
2016-06-30 09:09:03 -04:00
|
|
|
if filter.Include("node") {
|
|
|
|
nodeFilters := filter.Get("node")
|
|
|
|
for _, nodeFilter := range nodeFilters {
|
|
|
|
nodeReference, err := node.Reference(client, ctx, nodeFilter)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
filter.Del("node", nodeFilter)
|
|
|
|
filter.Add("node", nodeReference)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-13 22:56:23 -04:00
|
|
|
tasks, err := client.TaskList(ctx, types.TaskListOptions{Filter: filter})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-08-02 12:25:14 -04:00
|
|
|
return task.Print(dockerCli, ctx, tasks, idresolver.New(client, opts.noResolve), opts.noTrunc)
|
2016-06-13 22:56:23 -04:00
|
|
|
}
|