2016-09-08 13:11:39 -04:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"text/tabwriter"
|
|
|
|
|
2016-11-17 01:21:18 -05:00
|
|
|
distreference "github.com/docker/distribution/reference"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/filters"
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
|
|
|
"github.com/docker/docker/cli"
|
|
|
|
"github.com/docker/docker/cli/command"
|
|
|
|
"github.com/docker/docker/opts"
|
|
|
|
"github.com/docker/docker/pkg/stringid"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-10-24 23:39:53 -04:00
|
|
|
listItemFmt = "%s\t%s\t%s\t%s\t%s\n"
|
2016-09-08 13:11:39 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type listOptions struct {
|
|
|
|
quiet bool
|
|
|
|
filter opts.FilterOpt
|
|
|
|
}
|
|
|
|
|
|
|
|
func newListCommand(dockerCli *command.DockerCli) *cobra.Command {
|
|
|
|
opts := listOptions{filter: opts.NewFilterOpt()}
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "ls [OPTIONS]",
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func runList(dockerCli *command.DockerCli, opts listOptions) error {
|
|
|
|
ctx := context.Background()
|
|
|
|
client := dockerCli.Client()
|
2016-10-08 22:29:58 -04:00
|
|
|
out := dockerCli.Out()
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2016-11-01 10:01:16 -04:00
|
|
|
services, err := client.ServiceList(ctx, types.ServiceListOptions{Filters: opts.filter.Value()})
|
2016-09-08 13:11:39 -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-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
|
|
|
|
}
|
|
|
|
|
|
|
|
nodes, err := client.NodeList(ctx, types.NodeListOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
PrintNotQuiet(out, services, nodes, tasks)
|
2016-10-08 22:29:58 -04:00
|
|
|
} else if !opts.quiet {
|
2016-10-24 23:39:53 -04:00
|
|
|
// no services and not quiet, print only one line with columns ID, NAME, MODE, REPLICAS...
|
2016-10-08 22:29:58 -04:00
|
|
|
PrintNotQuiet(out, services, []swarm.Node{}, []swarm.Task{})
|
|
|
|
} else {
|
|
|
|
PrintQuiet(out, services)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2016-10-08 22:29:58 -04:00
|
|
|
|
2016-09-08 13:11:39 -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 {
|
|
|
|
if n.Status.State != swarm.NodeStateDown {
|
|
|
|
activeNodes[n.ID] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
running := map[string]int{}
|
2016-10-24 23:39:53 -04:00
|
|
|
tasksNoShutdown := map[string]int{}
|
|
|
|
|
2016-09-08 13:11:39 -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-09-08 13:11:39 -04:00
|
|
|
running[task.ServiceID]++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-24 23:39:53 -04:00
|
|
|
printTable(out, services, running, tasksNoShutdown)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2016-10-24 23:39:53 -04:00
|
|
|
func printTable(out io.Writer, services []swarm.Service, running, tasksNoShutdown map[string]int) {
|
2016-09-08 13:11:39 -04:00
|
|
|
writer := tabwriter.NewWriter(out, 0, 4, 2, ' ', 0)
|
|
|
|
|
|
|
|
// Ignore flushing errors
|
|
|
|
defer writer.Flush()
|
|
|
|
|
2016-10-24 23:39:53 -04:00
|
|
|
fmt.Fprintf(writer, listItemFmt, "ID", "NAME", "MODE", "REPLICAS", "IMAGE")
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
for _, service := range services {
|
2016-10-24 23:39:53 -04:00
|
|
|
mode := ""
|
2016-09-08 13:11:39 -04:00
|
|
|
replicas := ""
|
|
|
|
if service.Spec.Mode.Replicated != nil && service.Spec.Mode.Replicated.Replicas != nil {
|
2016-10-24 23:39:53 -04:00
|
|
|
mode = "replicated"
|
2016-09-08 13:11:39 -04:00
|
|
|
replicas = fmt.Sprintf("%d/%d", running[service.ID], *service.Spec.Mode.Replicated.Replicas)
|
|
|
|
} else if service.Spec.Mode.Global != nil {
|
2016-10-24 23:39:53 -04:00
|
|
|
mode = "global"
|
|
|
|
replicas = fmt.Sprintf("%d/%d", running[service.ID], tasksNoShutdown[service.ID])
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2016-11-17 01:21:18 -05:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
fmt.Fprintf(
|
|
|
|
writer,
|
|
|
|
listItemFmt,
|
|
|
|
stringid.TruncateID(service.ID),
|
|
|
|
service.Spec.Name,
|
2016-10-24 23:39:53 -04:00
|
|
|
mode,
|
2016-09-08 13:11:39 -04:00
|
|
|
replicas,
|
2016-11-17 01:21:18 -05:00
|
|
|
image)
|
2016-09-08 13:11:39 -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) {
|
|
|
|
for _, service := range services {
|
|
|
|
fmt.Fprintln(out, service.ID)
|
|
|
|
}
|
|
|
|
}
|