2016-06-13 22:56:23 -04:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/filters"
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2016-06-13 22:56:23 -04:00
|
|
|
"github.com/docker/docker/cli"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/cli/command"
|
2017-01-26 16:08:07 -05:00
|
|
|
"github.com/docker/docker/cli/command/formatter"
|
2016-06-13 22:56:23 -04:00
|
|
|
"github.com/docker/docker/opts"
|
|
|
|
"github.com/spf13/cobra"
|
2016-06-15 23:58:36 -04:00
|
|
|
"golang.org/x/net/context"
|
2016-06-13 22:56:23 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type listOptions struct {
|
|
|
|
quiet bool
|
2017-01-26 16:08:07 -05:00
|
|
|
format string
|
2016-06-13 22:56:23 -04:00
|
|
|
filter opts.FilterOpt
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
func newListCommand(dockerCli *command.DockerCli) *cobra.Command {
|
2016-06-13 22:56:23 -04:00
|
|
|
opts := listOptions{filter: opts.NewFilterOpt()}
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
2016-07-16 10:44:10 -04:00
|
|
|
Use: "ls [OPTIONS]",
|
2016-06-13 22:56:23 -04:00
|
|
|
Aliases: []string{"list"},
|
|
|
|
Short: "List services",
|
|
|
|
Args: cli.NoArgs,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
return runList(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-06-13 22:56:23 -04:00
|
|
|
flags.VarP(&opts.filter, "filter", "f", "Filter output based on conditions provided")
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
func runList(dockerCli *command.DockerCli, opts listOptions) error {
|
2016-06-15 23:58:36 -04:00
|
|
|
ctx := context.Background()
|
2016-06-13 22:56:23 -04:00
|
|
|
client := dockerCli.Client()
|
|
|
|
|
2017-03-24 12:26:15 -04:00
|
|
|
serviceFilters := opts.filter.Value()
|
2017-03-24 15:19:59 -04:00
|
|
|
serviceFilters.Add("runtime", string(swarm.RuntimeContainer))
|
2017-03-24 12:26:15 -04:00
|
|
|
services, err := client.ServiceList(ctx, types.ServiceListOptions{Filters: serviceFilters})
|
2016-06-13 22:56:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-26 16:08:07 -05:00
|
|
|
info := map[string]formatter.ServiceListInfo{}
|
2016-10-08 22:29:58 -04:00
|
|
|
if len(services) > 0 && !opts.quiet {
|
|
|
|
// only non-empty services and not quiet, should we call TaskList and NodeList api
|
2016-06-15 23:58:36 -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-06-15 23:58:36 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-06-22 17:48:57 -04:00
|
|
|
nodes, err := client.NodeList(ctx, types.NodeListOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-26 16:08:07 -05:00
|
|
|
info = 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
|
|
|
|
}
|
2016-07-01 22:12:16 -04:00
|
|
|
}
|
2016-10-08 22:29:58 -04:00
|
|
|
|
2017-01-26 16:08:07 -05:00
|
|
|
servicesCtx := formatter.Context{
|
|
|
|
Output: dockerCli.Out(),
|
|
|
|
Format: formatter.NewServiceListFormat(format, opts.quiet),
|
|
|
|
}
|
|
|
|
return formatter.ServiceListWrite(servicesCtx, services, info)
|
2016-07-01 22:12:16 -04:00
|
|
|
}
|
|
|
|
|
2017-01-26 16:08:07 -05:00
|
|
|
// GetServicesStatus returns a map of mode and replicas
|
|
|
|
func GetServicesStatus(services []swarm.Service, nodes []swarm.Node, tasks []swarm.Task) map[string]formatter.ServiceListInfo {
|
|
|
|
running := map[string]int{}
|
|
|
|
tasksNoShutdown := map[string]int{}
|
|
|
|
|
2016-07-01 22:12:16 -04:00
|
|
|
activeNodes := make(map[string]struct{})
|
|
|
|
for _, n := range nodes {
|
2016-07-28 14:44:28 -04:00
|
|
|
if n.Status.State != swarm.NodeStateDown {
|
2016-07-01 22:12:16 -04:00
|
|
|
activeNodes[n.ID] = struct{}{}
|
2016-06-15 23:58:36 -04:00
|
|
|
}
|
2016-07-01 22:12:16 -04:00
|
|
|
}
|
2016-06-15 23:58:36 -04:00
|
|
|
|
2016-07-01 22:12:16 -04:00
|
|
|
for _, task := range tasks {
|
2016-10-24 23:39:53 -04:00
|
|
|
if task.DesiredState != swarm.TaskStateShutdown {
|
|
|
|
tasksNoShutdown[task.ServiceID]++
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, nodeActive := activeNodes[task.NodeID]; nodeActive && task.Status.State == swarm.TaskStateRunning {
|
2016-07-01 22:12:16 -04:00
|
|
|
running[task.ServiceID]++
|
|
|
|
}
|
2016-06-13 22:56:23 -04:00
|
|
|
}
|
2016-07-01 22:12:16 -04:00
|
|
|
|
2017-01-26 16:08:07 -05:00
|
|
|
info := map[string]formatter.ServiceListInfo{}
|
2016-06-13 22:56:23 -04:00
|
|
|
for _, service := range services {
|
2017-01-26 16:08:07 -05:00
|
|
|
info[service.ID] = formatter.ServiceListInfo{}
|
2016-06-13 22:56:23 -04:00
|
|
|
if service.Spec.Mode.Replicated != nil && service.Spec.Mode.Replicated.Replicas != nil {
|
2017-01-26 16:08:07 -05:00
|
|
|
info[service.ID] = formatter.ServiceListInfo{
|
|
|
|
Mode: "replicated",
|
|
|
|
Replicas: fmt.Sprintf("%d/%d", running[service.ID], *service.Spec.Mode.Replicated.Replicas),
|
|
|
|
}
|
2016-06-13 22:56:23 -04:00
|
|
|
} else if service.Spec.Mode.Global != nil {
|
2017-01-26 16:08:07 -05:00
|
|
|
info[service.ID] = formatter.ServiceListInfo{
|
|
|
|
Mode: "global",
|
|
|
|
Replicas: fmt.Sprintf("%d/%d", running[service.ID], tasksNoShutdown[service.ID]),
|
2016-11-17 01:21:18 -05:00
|
|
|
}
|
|
|
|
}
|
2016-06-13 22:56:23 -04:00
|
|
|
}
|
2017-01-26 16:08:07 -05:00
|
|
|
return info
|
2016-06-13 22:56:23 -04:00
|
|
|
}
|