2016-09-08 13:11:39 -04:00
|
|
|
package stack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
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/service"
|
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"
|
|
|
|
"github.com/docker/docker/api/types/filters"
|
|
|
|
"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 servicesOptions struct {
|
|
|
|
quiet bool
|
2017-01-26 16:08:07 -05:00
|
|
|
format string
|
2016-09-08 13:11:39 -04:00
|
|
|
filter opts.FilterOpt
|
|
|
|
namespace string
|
|
|
|
}
|
|
|
|
|
2017-06-20 14:00:01 -04:00
|
|
|
func newServicesCommand(dockerCli command.Cli) *cobra.Command {
|
2017-05-15 08:45:19 -04:00
|
|
|
options := servicesOptions{filter: opts.NewFilterOpt()}
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
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 {
|
2017-05-15 08:45:19 -04:00
|
|
|
options.namespace = args[0]
|
|
|
|
return runServices(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 IDs")
|
|
|
|
flags.StringVar(&options.format, "format", "", "Pretty-print services 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-06-20 14:00:01 -04:00
|
|
|
func runServices(dockerCli command.Cli, options servicesOptions) error {
|
2016-09-08 13:11:39 -04:00
|
|
|
ctx := context.Background()
|
|
|
|
client := dockerCli.Client()
|
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
filter := getStackFilterFromOpt(options.namespace, options.filter)
|
2016-11-01 10:01:16 -04:00
|
|
|
services, err := client.ServiceList(ctx, types.ServiceListOptions{Filters: filter})
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// if no services in this stack, print message and exit 0
|
|
|
|
if len(services) == 0 {
|
2017-07-05 13:32:54 -04:00
|
|
|
fmt.Fprintf(dockerCli.Err(), "Nothing found in stack: %s\n", options.namespace)
|
2016-09-08 13:11:39 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-26 16:08:07 -05:00
|
|
|
info := map[string]formatter.ServiceListInfo{}
|
2017-05-15 08:45:19 -04:00
|
|
|
if !options.quiet {
|
2016-09-08 13:11:39 -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-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-26 16:08:07 -05:00
|
|
|
|
2016-09-08 13:11:39 -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)
|
|
|
|
}
|
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
format := options.format
|
2017-01-26 16:08:07 -05:00
|
|
|
if len(format) == 0 {
|
2017-05-15 08:45:19 -04:00
|
|
|
if len(dockerCli.ConfigFile().ServicesFormat) > 0 && !options.quiet {
|
2017-01-26 16:08:07 -05:00
|
|
|
format = dockerCli.ConfigFile().ServicesFormat
|
|
|
|
} else {
|
|
|
|
format = formatter.TableFormatKey
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
servicesCtx := formatter.Context{
|
|
|
|
Output: dockerCli.Out(),
|
2017-05-15 08:45:19 -04:00
|
|
|
Format: formatter.NewServiceListFormat(format, options.quiet),
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2017-01-26 16:08:07 -05:00
|
|
|
return formatter.ServiceListWrite(servicesCtx, services, info)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|