2016-06-13 22:56:23 -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-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 (
|
2016-10-24 23:39:53 -04:00
|
|
|
listItemFmt = "%s\t%s\t%s\t%s\t%s\n"
|
2016-06-13 22:56:23 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
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-11-01 10:01:16 -04:00
|
|
|
services, err := client.ServiceList(ctx, types.ServiceListOptions{Filters: 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)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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 {
|
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-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{}
|
2016-10-24 23:39:53 -04:00
|
|
|
tasksNoShutdown := map[string]int{}
|
|
|
|
|
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
|
|
|
|
2016-10-24 23:39:53 -04:00
|
|
|
printTable(out, services, running, tasksNoShutdown)
|
2016-06-13 22:56:23 -04:00
|
|
|
}
|
|
|
|
|
2016-10-24 23:39:53 -04:00
|
|
|
func printTable(out io.Writer, services []swarm.Service, running, tasksNoShutdown 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-10-24 23:39:53 -04:00
|
|
|
fmt.Fprintf(writer, listItemFmt, "ID", "NAME", "MODE", "REPLICAS", "IMAGE")
|
|
|
|
|
2016-06-13 22:56:23 -04:00
|
|
|
for _, service := range services {
|
2016-10-24 23:39:53 -04:00
|
|
|
mode := ""
|
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-10-24 23:39:53 -04:00
|
|
|
mode = "replicated"
|
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-10-24 23:39:53 -04:00
|
|
|
mode = "global"
|
|
|
|
replicas = fmt.Sprintf("%d/%d", running[service.ID], tasksNoShutdown[service.ID])
|
2016-06-13 22:56:23 -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-06-13 22:56:23 -04:00
|
|
|
fmt.Fprintf(
|
|
|
|
writer,
|
|
|
|
listItemFmt,
|
|
|
|
stringid.TruncateID(service.ID),
|
|
|
|
service.Spec.Name,
|
2016-10-24 23:39:53 -04:00
|
|
|
mode,
|
2016-06-15 23:58:36 -04:00
|
|
|
replicas,
|
2016-11-17 01:21:18 -05:00
|
|
|
image)
|
2016-06-13 22:56:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|