2017-11-20 09:30:52 -05:00
|
|
|
package swarm
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2016-09-08 13:11:39 -04:00
|
|
|
"fmt"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli/command"
|
|
|
|
"github.com/docker/cli/cli/command/formatter"
|
|
|
|
"github.com/docker/cli/cli/command/service"
|
2017-12-04 06:30:39 -05:00
|
|
|
"github.com/docker/cli/cli/command/stack/options"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/filters"
|
|
|
|
)
|
|
|
|
|
2017-12-04 06:30:39 -05:00
|
|
|
// RunServices is the swarm implementation of docker stack services
|
|
|
|
func RunServices(dockerCli command.Cli, opts options.Services) error {
|
2018-05-28 06:21:41 -04:00
|
|
|
if err := validateStackName(opts.Namespace); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
ctx := context.Background()
|
|
|
|
client := dockerCli.Client()
|
|
|
|
|
2017-12-04 06:30:39 -05:00
|
|
|
filter := getStackFilterFromOpt(opts.Namespace, opts.Filter)
|
2016-11-01 10:01:16 -04:00
|
|
|
services, err := client.ServiceList(ctx, types.ServiceListOptions{Filters: filter})
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// if no services in this stack, print message and exit 0
|
|
|
|
if len(services) == 0 {
|
2017-12-04 06:30:39 -05:00
|
|
|
fmt.Fprintf(dockerCli.Err(), "Nothing found in stack: %s\n", opts.Namespace)
|
2016-09-08 13:11:39 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-26 16:08:07 -05:00
|
|
|
info := map[string]formatter.ServiceListInfo{}
|
2017-12-04 06:30:39 -05:00
|
|
|
if !opts.Quiet {
|
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
|
|
|
|
}
|
2017-01-26 16:08:07 -05:00
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
nodes, err := client.NodeList(ctx, types.NodeListOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-26 16:08:07 -05:00
|
|
|
|
|
|
|
info = service.GetServicesStatus(services, nodes, tasks)
|
|
|
|
}
|
|
|
|
|
2017-12-04 06:30:39 -05:00
|
|
|
format := opts.Format
|
2017-01-26 16:08:07 -05:00
|
|
|
if len(format) == 0 {
|
2017-12-04 06:30:39 -05:00
|
|
|
if len(dockerCli.ConfigFile().ServicesFormat) > 0 && !opts.Quiet {
|
2017-01-26 16:08:07 -05:00
|
|
|
format = dockerCli.ConfigFile().ServicesFormat
|
|
|
|
} else {
|
|
|
|
format = formatter.TableFormatKey
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
servicesCtx := formatter.Context{
|
|
|
|
Output: dockerCli.Out(),
|
2017-12-04 06:30:39 -05:00
|
|
|
Format: formatter.NewServiceListFormat(format, opts.Quiet),
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2017-01-26 16:08:07 -05:00
|
|
|
return formatter.ServiceListWrite(servicesCtx, services, info)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|