2016-06-13 22:56:23 -04:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
"text/tabwriter"
|
|
|
|
|
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"
|
2016-06-13 22:56:23 -04:00
|
|
|
"github.com/docker/docker/opts"
|
|
|
|
"github.com/docker/docker/pkg/stringid"
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
listItemFmt = "%s\t%s\t%s\t%s\t%s\n"
|
|
|
|
)
|
|
|
|
|
|
|
|
type listOptions struct {
|
|
|
|
quiet bool
|
|
|
|
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")
|
|
|
|
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()
|
2016-10-08 22:29:58 -04:00
|
|
|
out := dockerCli.Out()
|
2016-06-13 22:56:23 -04:00
|
|
|
|
2016-06-15 23:58:36 -04:00
|
|
|
services, err := client.ServiceList(ctx, types.ServiceListOptions{Filter: opts.filter.Value()})
|
2016-06-13 22:56:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
tasks, err := client.TaskList(ctx, types.TaskListOptions{Filter: taskFilter})
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-07-01 22:12:16 -04:00
|
|
|
PrintNotQuiet(out, services, nodes, tasks)
|
2016-10-08 22:29:58 -04:00
|
|
|
} else if !opts.quiet {
|
|
|
|
// no services and not quiet, print only one line with columns ID, NAME, REPLICAS...
|
|
|
|
PrintNotQuiet(out, services, []swarm.Node{}, []swarm.Task{})
|
|
|
|
} else {
|
|
|
|
PrintQuiet(out, services)
|
2016-07-01 22:12:16 -04:00
|
|
|
}
|
2016-10-08 22:29:58 -04:00
|
|
|
|
2016-07-01 22:12:16 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
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
|
|
|
running := map[string]int{}
|
|
|
|
for _, task := range tasks {
|
|
|
|
if _, nodeActive := activeNodes[task.NodeID]; nodeActive && task.Status.State == "running" {
|
|
|
|
running[task.ServiceID]++
|
|
|
|
}
|
2016-06-13 22:56:23 -04:00
|
|
|
}
|
2016-07-01 22:12:16 -04:00
|
|
|
|
|
|
|
printTable(out, services, running)
|
2016-06-13 22:56:23 -04:00
|
|
|
}
|
|
|
|
|
2016-06-15 23:58:36 -04:00
|
|
|
func printTable(out io.Writer, services []swarm.Service, running map[string]int) {
|
2016-06-13 22:56:23 -04:00
|
|
|
writer := tabwriter.NewWriter(out, 0, 4, 2, ' ', 0)
|
|
|
|
|
|
|
|
// Ignore flushing errors
|
|
|
|
defer writer.Flush()
|
|
|
|
|
2016-06-15 17:57:59 -04:00
|
|
|
fmt.Fprintf(writer, listItemFmt, "ID", "NAME", "REPLICAS", "IMAGE", "COMMAND")
|
2016-06-13 22:56:23 -04:00
|
|
|
for _, service := range services {
|
2016-06-15 23:58:36 -04:00
|
|
|
replicas := ""
|
2016-06-13 22:56:23 -04:00
|
|
|
if service.Spec.Mode.Replicated != nil && service.Spec.Mode.Replicated.Replicas != nil {
|
2016-06-15 23:58:36 -04:00
|
|
|
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 {
|
2016-06-15 23:58:36 -04:00
|
|
|
replicas = "global"
|
2016-06-13 22:56:23 -04:00
|
|
|
}
|
|
|
|
fmt.Fprintf(
|
|
|
|
writer,
|
|
|
|
listItemFmt,
|
|
|
|
stringid.TruncateID(service.ID),
|
|
|
|
service.Spec.Name,
|
2016-06-15 23:58:36 -04:00
|
|
|
replicas,
|
2016-06-13 22:56:23 -04:00
|
|
|
service.Spec.TaskTemplate.ContainerSpec.Image,
|
|
|
|
strings.Join(service.Spec.TaskTemplate.ContainerSpec.Args, " "))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-01 22:12:16 -04:00
|
|
|
// 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) {
|
2016-06-13 22:56:23 -04:00
|
|
|
for _, service := range services {
|
|
|
|
fmt.Fprintln(out, service.ID)
|
|
|
|
}
|
|
|
|
}
|