|
@@ -2,27 +2,21 @@ package service
|
|
|
|
|
|
import (
|
|
import (
|
|
"fmt"
|
|
"fmt"
|
|
- "io"
|
|
|
|
- "text/tabwriter"
|
|
|
|
|
|
|
|
- distreference "github.com/docker/distribution/reference"
|
|
|
|
"github.com/docker/docker/api/types"
|
|
"github.com/docker/docker/api/types"
|
|
"github.com/docker/docker/api/types/filters"
|
|
"github.com/docker/docker/api/types/filters"
|
|
"github.com/docker/docker/api/types/swarm"
|
|
"github.com/docker/docker/api/types/swarm"
|
|
"github.com/docker/docker/cli"
|
|
"github.com/docker/docker/cli"
|
|
"github.com/docker/docker/cli/command"
|
|
"github.com/docker/docker/cli/command"
|
|
|
|
+ "github.com/docker/docker/cli/command/formatter"
|
|
"github.com/docker/docker/opts"
|
|
"github.com/docker/docker/opts"
|
|
- "github.com/docker/docker/pkg/stringid"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/cobra"
|
|
"golang.org/x/net/context"
|
|
"golang.org/x/net/context"
|
|
)
|
|
)
|
|
|
|
|
|
-const (
|
|
|
|
- listItemFmt = "%s\t%s\t%s\t%s\t%s\n"
|
|
|
|
-)
|
|
|
|
-
|
|
|
|
type listOptions struct {
|
|
type listOptions struct {
|
|
quiet bool
|
|
quiet bool
|
|
|
|
+ format string
|
|
filter opts.FilterOpt
|
|
filter opts.FilterOpt
|
|
}
|
|
}
|
|
|
|
|
|
@@ -41,6 +35,7 @@ func newListCommand(dockerCli *command.DockerCli) *cobra.Command {
|
|
|
|
|
|
flags := cmd.Flags()
|
|
flags := cmd.Flags()
|
|
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Only display IDs")
|
|
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Only display IDs")
|
|
|
|
+ flags.StringVar(&opts.format, "format", "", "Pretty-print services using a Go template")
|
|
flags.VarP(&opts.filter, "filter", "f", "Filter output based on conditions provided")
|
|
flags.VarP(&opts.filter, "filter", "f", "Filter output based on conditions provided")
|
|
|
|
|
|
return cmd
|
|
return cmd
|
|
@@ -49,13 +44,13 @@ func newListCommand(dockerCli *command.DockerCli) *cobra.Command {
|
|
func runList(dockerCli *command.DockerCli, opts listOptions) error {
|
|
func runList(dockerCli *command.DockerCli, opts listOptions) error {
|
|
ctx := context.Background()
|
|
ctx := context.Background()
|
|
client := dockerCli.Client()
|
|
client := dockerCli.Client()
|
|
- out := dockerCli.Out()
|
|
|
|
|
|
|
|
services, err := client.ServiceList(ctx, types.ServiceListOptions{Filters: opts.filter.Value()})
|
|
services, err := client.ServiceList(ctx, types.ServiceListOptions{Filters: opts.filter.Value()})
|
|
if err != nil {
|
|
if err != nil {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ info := map[string]formatter.ServiceListInfo{}
|
|
if len(services) > 0 && !opts.quiet {
|
|
if len(services) > 0 && !opts.quiet {
|
|
// only non-empty services and not quiet, should we call TaskList and NodeList api
|
|
// only non-empty services and not quiet, should we call TaskList and NodeList api
|
|
taskFilter := filters.NewArgs()
|
|
taskFilter := filters.NewArgs()
|
|
@@ -73,20 +68,30 @@ func runList(dockerCli *command.DockerCli, opts listOptions) error {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
|
|
|
|
- PrintNotQuiet(out, services, nodes, tasks)
|
|
|
|
- } else if !opts.quiet {
|
|
|
|
- // no services and not quiet, print only one line with columns ID, NAME, MODE, REPLICAS...
|
|
|
|
- PrintNotQuiet(out, services, []swarm.Node{}, []swarm.Task{})
|
|
|
|
- } else {
|
|
|
|
- PrintQuiet(out, services)
|
|
|
|
|
|
+ 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
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
- return nil
|
|
|
|
|
|
+ servicesCtx := formatter.Context{
|
|
|
|
+ Output: dockerCli.Out(),
|
|
|
|
+ Format: formatter.NewServiceListFormat(format, opts.quiet),
|
|
|
|
+ }
|
|
|
|
+ return formatter.ServiceListWrite(servicesCtx, services, info)
|
|
}
|
|
}
|
|
|
|
|
|
-// PrintNotQuiet shows service list in a non-quiet way.
|
|
|
|
-// Besides this, command `docker stack services xxx` will call this, too.
|
|
|
|
-func PrintNotQuiet(out io.Writer, services []swarm.Service, nodes []swarm.Node, tasks []swarm.Task) {
|
|
|
|
|
|
+// 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{}
|
|
|
|
+
|
|
activeNodes := make(map[string]struct{})
|
|
activeNodes := make(map[string]struct{})
|
|
for _, n := range nodes {
|
|
for _, n := range nodes {
|
|
if n.Status.State != swarm.NodeStateDown {
|
|
if n.Status.State != swarm.NodeStateDown {
|
|
@@ -94,9 +99,6 @@ func PrintNotQuiet(out io.Writer, services []swarm.Service, nodes []swarm.Node,
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- running := map[string]int{}
|
|
|
|
- tasksNoShutdown := map[string]int{}
|
|
|
|
-
|
|
|
|
for _, task := range tasks {
|
|
for _, task := range tasks {
|
|
if task.DesiredState != swarm.TaskStateShutdown {
|
|
if task.DesiredState != swarm.TaskStateShutdown {
|
|
tasksNoShutdown[task.ServiceID]++
|
|
tasksNoShutdown[task.ServiceID]++
|
|
@@ -107,52 +109,20 @@ func PrintNotQuiet(out io.Writer, services []swarm.Service, nodes []swarm.Node,
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- printTable(out, services, running, tasksNoShutdown)
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func printTable(out io.Writer, services []swarm.Service, running, tasksNoShutdown map[string]int) {
|
|
|
|
- writer := tabwriter.NewWriter(out, 0, 4, 2, ' ', 0)
|
|
|
|
-
|
|
|
|
- // Ignore flushing errors
|
|
|
|
- defer writer.Flush()
|
|
|
|
-
|
|
|
|
- fmt.Fprintf(writer, listItemFmt, "ID", "NAME", "MODE", "REPLICAS", "IMAGE")
|
|
|
|
-
|
|
|
|
|
|
+ info := map[string]formatter.ServiceListInfo{}
|
|
for _, service := range services {
|
|
for _, service := range services {
|
|
- mode := ""
|
|
|
|
- replicas := ""
|
|
|
|
|
|
+ info[service.ID] = formatter.ServiceListInfo{}
|
|
if service.Spec.Mode.Replicated != nil && service.Spec.Mode.Replicated.Replicas != nil {
|
|
if service.Spec.Mode.Replicated != nil && service.Spec.Mode.Replicated.Replicas != nil {
|
|
- mode = "replicated"
|
|
|
|
- replicas = fmt.Sprintf("%d/%d", running[service.ID], *service.Spec.Mode.Replicated.Replicas)
|
|
|
|
|
|
+ info[service.ID] = formatter.ServiceListInfo{
|
|
|
|
+ Mode: "replicated",
|
|
|
|
+ Replicas: fmt.Sprintf("%d/%d", running[service.ID], *service.Spec.Mode.Replicated.Replicas),
|
|
|
|
+ }
|
|
} else if service.Spec.Mode.Global != nil {
|
|
} else if service.Spec.Mode.Global != nil {
|
|
- mode = "global"
|
|
|
|
- replicas = fmt.Sprintf("%d/%d", running[service.ID], tasksNoShutdown[service.ID])
|
|
|
|
- }
|
|
|
|
- image := service.Spec.TaskTemplate.ContainerSpec.Image
|
|
|
|
- ref, err := distreference.ParseNamed(image)
|
|
|
|
- if err == nil {
|
|
|
|
- // update image string for display
|
|
|
|
- namedTagged, ok := ref.(distreference.NamedTagged)
|
|
|
|
- if ok {
|
|
|
|
- image = namedTagged.Name() + ":" + namedTagged.Tag()
|
|
|
|
|
|
+ info[service.ID] = formatter.ServiceListInfo{
|
|
|
|
+ Mode: "global",
|
|
|
|
+ Replicas: fmt.Sprintf("%d/%d", running[service.ID], tasksNoShutdown[service.ID]),
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
- fmt.Fprintf(
|
|
|
|
- writer,
|
|
|
|
- listItemFmt,
|
|
|
|
- stringid.TruncateID(service.ID),
|
|
|
|
- service.Spec.Name,
|
|
|
|
- mode,
|
|
|
|
- replicas,
|
|
|
|
- image)
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-// PrintQuiet shows service list in a quiet way.
|
|
|
|
-// Besides this, command `docker stack services xxx` will call this, too.
|
|
|
|
-func PrintQuiet(out io.Writer, services []swarm.Service) {
|
|
|
|
- for _, service := range services {
|
|
|
|
- fmt.Fprintln(out, service.ID)
|
|
|
|
}
|
|
}
|
|
|
|
+ return info
|
|
}
|
|
}
|