From 7891d349b377b5fdc0869bf6f24828e46bb4b9ac Mon Sep 17 00:00:00 2001 From: allencloud Date: Tue, 25 Oct 2016 11:39:53 +0800 Subject: [PATCH] support show numbers of global service in service ls command Signed-off-by: allencloud --- command/service/list.go | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/command/service/list.go b/command/service/list.go index 05b425a45e..f758808d1f 100644 --- a/command/service/list.go +++ b/command/service/list.go @@ -17,7 +17,7 @@ import ( ) const ( - listItemFmt = "%s\t%s\t%s\t%s\n" + listItemFmt = "%s\t%s\t%s\t%s\t%s\n" ) type listOptions struct { @@ -74,7 +74,7 @@ func runList(dockerCli *command.DockerCli, opts listOptions) error { PrintNotQuiet(out, services, nodes, tasks) } else if !opts.quiet { - // no services and not quiet, print only one line with columns ID, NAME, REPLICAS... + // 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) @@ -94,34 +94,45 @@ func PrintNotQuiet(out io.Writer, services []swarm.Service, nodes []swarm.Node, } running := map[string]int{} + tasksNoShutdown := map[string]int{} + for _, task := range tasks { - if _, nodeActive := activeNodes[task.NodeID]; nodeActive && task.Status.State == "running" { + if task.DesiredState != swarm.TaskStateShutdown { + tasksNoShutdown[task.ServiceID]++ + } + + if _, nodeActive := activeNodes[task.NodeID]; nodeActive && task.Status.State == swarm.TaskStateRunning { running[task.ServiceID]++ } } - printTable(out, services, running) + printTable(out, services, running, tasksNoShutdown) } -func printTable(out io.Writer, services []swarm.Service, running map[string]int) { +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", "REPLICAS", "IMAGE") + fmt.Fprintf(writer, listItemFmt, "ID", "NAME", "MODE", "REPLICAS", "IMAGE") + for _, service := range services { + mode := "" replicas := "" 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) } else if service.Spec.Mode.Global != nil { - replicas = "global" + 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, service.Spec.TaskTemplate.ContainerSpec.Image) }