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/service"
|
2018-10-23 11:05:44 -04:00
|
|
|
"github.com/docker/cli/cli/command/stack/formatter"
|
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"
|
|
|
|
)
|
|
|
|
|
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 {
|
2019-10-23 18:16:35 -04:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
ctx = context.Background()
|
|
|
|
client = dockerCli.Client()
|
|
|
|
)
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2019-10-23 18:16:35 -04:00
|
|
|
listOpts := types.ServiceListOptions{
|
|
|
|
Filters: getStackFilterFromOpt(opts.Namespace, opts.Filter),
|
|
|
|
// When not running "quiet", also get service status (number of running
|
|
|
|
// and desired tasks). Note that this is only supported on API v1.41 and
|
|
|
|
// up; older API versions ignore this option, and we will have to collect
|
|
|
|
// the information manually below.
|
|
|
|
Status: !opts.Quiet,
|
|
|
|
}
|
|
|
|
|
|
|
|
services, err := client.ServiceList(ctx, listOpts)
|
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 {
|
2019-10-23 18:16:35 -04:00
|
|
|
_, _ = fmt.Fprintf(dockerCli.Err(), "Nothing found in stack: %s\n", opts.Namespace)
|
2016-09-08 13:11:39 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-23 18:16:35 -04:00
|
|
|
if listOpts.Status {
|
|
|
|
// Now that a request was made, we know what API version was used (either
|
|
|
|
// through configuration, or after client and daemon negotiated a version).
|
|
|
|
// If API version v1.41 or up was used; the daemon should already have done
|
|
|
|
// the legwork for us, and we don't have to calculate the number of desired
|
|
|
|
// and running tasks. On older API versions, we need to do some extra requests
|
|
|
|
// to get that information.
|
|
|
|
//
|
|
|
|
// So theoretically, this step can be skipped based on API version, however,
|
|
|
|
// some of our unit tests don't set the API version, and there may be other
|
|
|
|
// situations where the client uses the "default" version. To account for
|
|
|
|
// these situations, we do a quick check for services that do not have
|
|
|
|
// a ServiceStatus set, and perform a lookup for those.
|
|
|
|
services, err = service.AppendServiceStatus(ctx, client, services)
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-26 16:08:07 -05:00
|
|
|
}
|
|
|
|
|
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(),
|
2018-10-23 11:05:44 -04:00
|
|
|
Format: service.NewListFormat(format, opts.Quiet),
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2019-10-23 18:16:35 -04:00
|
|
|
return service.ListFormatWrite(servicesCtx, services)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|