2016-07-01 22:12:16 -04:00
|
|
|
package stack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/filters"
|
2016-07-01 22:12:16 -04:00
|
|
|
"github.com/docker/docker/cli"
|
2016-09-08 14:54:01 -04:00
|
|
|
"github.com/docker/docker/cli/command"
|
2017-01-26 16:08:07 -05:00
|
|
|
"github.com/docker/docker/cli/command/formatter"
|
2016-09-08 14:54:01 -04:00
|
|
|
"github.com/docker/docker/cli/command/service"
|
2016-07-01 22:12:16 -04:00
|
|
|
"github.com/docker/docker/opts"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type servicesOptions struct {
|
|
|
|
quiet bool
|
2017-01-26 16:08:07 -05:00
|
|
|
format string
|
2016-07-01 22:12:16 -04:00
|
|
|
filter opts.FilterOpt
|
|
|
|
namespace string
|
|
|
|
}
|
|
|
|
|
2016-09-08 14:54:01 -04:00
|
|
|
func newServicesCommand(dockerCli *command.DockerCli) *cobra.Command {
|
2016-07-01 22:12:16 -04:00
|
|
|
opts := servicesOptions{filter: opts.NewFilterOpt()}
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "services [OPTIONS] STACK",
|
|
|
|
Short: "List the services in the stack",
|
|
|
|
Args: cli.ExactArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
opts.namespace = args[0]
|
|
|
|
return runServices(dockerCli, opts)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
flags := cmd.Flags()
|
|
|
|
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Only display IDs")
|
2017-01-26 16:08:07 -05:00
|
|
|
flags.StringVar(&opts.format, "format", "", "Pretty-print services using a Go template")
|
2016-07-01 22:12:16 -04:00
|
|
|
flags.VarP(&opts.filter, "filter", "f", "Filter output based on conditions provided")
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2016-09-08 14:54:01 -04:00
|
|
|
func runServices(dockerCli *command.DockerCli, opts servicesOptions) error {
|
2016-07-01 22:12:16 -04:00
|
|
|
ctx := context.Background()
|
|
|
|
client := dockerCli.Client()
|
|
|
|
|
2016-11-30 17:38:40 -05:00
|
|
|
filter := getStackFilterFromOpt(opts.namespace, opts.filter)
|
2016-11-01 10:01:16 -04:00
|
|
|
services, err := client.ServiceList(ctx, types.ServiceListOptions{Filters: filter})
|
2016-07-01 22:12:16 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
out := dockerCli.Out()
|
|
|
|
|
|
|
|
// if no services in this stack, print message and exit 0
|
|
|
|
if len(services) == 0 {
|
|
|
|
fmt.Fprintf(out, "Nothing found in stack: %s\n", opts.namespace)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-26 16:08:07 -05:00
|
|
|
info := map[string]formatter.ServiceListInfo{}
|
|
|
|
if !opts.quiet {
|
2016-07-01 22:12:16 -04:00
|
|
|
taskFilter := filters.NewArgs()
|
|
|
|
for _, service := range services {
|
|
|
|
taskFilter.Add("service", service.ID)
|
|
|
|
}
|
|
|
|
|
2016-11-01 10:01:16 -04:00
|
|
|
tasks, err := client.TaskList(ctx, types.TaskListOptions{Filters: taskFilter})
|
2016-07-01 22:12:16 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-26 16:08:07 -05:00
|
|
|
|
2016-07-01 22:12:16 -04:00
|
|
|
nodes, err := client.NodeList(ctx, types.NodeListOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-26 16:08:07 -05:00
|
|
|
|
|
|
|
info = service.GetServicesStatus(services, nodes, tasks)
|
|
|
|
}
|
|
|
|
|
|
|
|
format := opts.format
|
|
|
|
if len(format) == 0 {
|
|
|
|
if len(dockerCli.ConfigFile().ServicesFormat) > 0 && !opts.quiet {
|
|
|
|
format = dockerCli.ConfigFile().ServicesFormat
|
|
|
|
} else {
|
|
|
|
format = formatter.TableFormatKey
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
servicesCtx := formatter.Context{
|
|
|
|
Output: dockerCli.Out(),
|
|
|
|
Format: formatter.NewServiceListFormat(format, opts.quiet),
|
2016-07-01 22:12:16 -04:00
|
|
|
}
|
2017-01-26 16:08:07 -05:00
|
|
|
return formatter.ServiceListWrite(servicesCtx, services, info)
|
2016-07-01 22:12:16 -04:00
|
|
|
}
|