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"
|
2018-05-28 06:21:41 -04:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"unicode"
|
2018-05-03 21:02:44 -04:00
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli/compose/convert"
|
2017-05-15 08:45:19 -04:00
|
|
|
"github.com/docker/cli/opts"
|
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"
|
2017-05-08 13:51:30 -04:00
|
|
|
"github.com/docker/docker/client"
|
2016-09-08 13:11:39 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func getStackFilter(namespace string) filters.Args {
|
|
|
|
filter := filters.NewArgs()
|
2016-12-05 16:14:08 -05:00
|
|
|
filter.Add("label", convert.LabelNamespace+"="+namespace)
|
2016-09-08 13:11:39 -04:00
|
|
|
return filter
|
|
|
|
}
|
|
|
|
|
2018-05-28 06:26:14 -04:00
|
|
|
func getStackServiceFilter(namespace string) filters.Args {
|
2017-04-26 09:09:54 -04:00
|
|
|
return getStackFilter(namespace)
|
2017-03-24 12:26:15 -04:00
|
|
|
}
|
|
|
|
|
2016-11-30 17:38:40 -05:00
|
|
|
func getStackFilterFromOpt(namespace string, opt opts.FilterOpt) filters.Args {
|
|
|
|
filter := opt.Value()
|
2016-12-05 16:14:08 -05:00
|
|
|
filter.Add("label", convert.LabelNamespace+"="+namespace)
|
2016-11-30 17:38:40 -05:00
|
|
|
return filter
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAllStacksFilter() filters.Args {
|
|
|
|
filter := filters.NewArgs()
|
2016-12-05 16:14:08 -05:00
|
|
|
filter.Add("label", convert.LabelNamespace)
|
2016-11-30 17:38:40 -05:00
|
|
|
return filter
|
|
|
|
}
|
|
|
|
|
2018-05-28 06:26:14 -04:00
|
|
|
func getStackServices(ctx context.Context, apiclient client.APIClient, namespace string) ([]swarm.Service, error) {
|
|
|
|
return apiclient.ServiceList(ctx, types.ServiceListOptions{Filters: getStackServiceFilter(namespace)})
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2018-05-28 06:26:14 -04:00
|
|
|
func getStackNetworks(ctx context.Context, apiclient client.APIClient, namespace string) ([]types.NetworkResource, error) {
|
|
|
|
return apiclient.NetworkList(ctx, types.NetworkListOptions{Filters: getStackFilter(namespace)})
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2017-01-18 14:40:29 -05:00
|
|
|
|
2018-05-28 06:26:14 -04:00
|
|
|
func getStackSecrets(ctx context.Context, apiclient client.APIClient, namespace string) ([]swarm.Secret, error) {
|
|
|
|
return apiclient.SecretList(ctx, types.SecretListOptions{Filters: getStackFilter(namespace)})
|
2017-01-18 14:40:29 -05:00
|
|
|
}
|
2017-05-26 20:30:33 -04:00
|
|
|
|
2018-05-28 06:26:14 -04:00
|
|
|
func getStackConfigs(ctx context.Context, apiclient client.APIClient, namespace string) ([]swarm.Config, error) {
|
|
|
|
return apiclient.ConfigList(ctx, types.ConfigListOptions{Filters: getStackFilter(namespace)})
|
2017-05-26 20:30:33 -04:00
|
|
|
}
|
2018-05-28 06:21:41 -04:00
|
|
|
|
|
|
|
// validateStackName checks if the provided string is a valid stack name (namespace).
|
|
|
|
//
|
|
|
|
// It currently only does a rudimentary check if the string is empty, or consists
|
|
|
|
// of only whitespace and quoting characters.
|
|
|
|
func validateStackName(namespace string) error {
|
|
|
|
v := strings.TrimFunc(namespace, quotesOrWhitespace)
|
|
|
|
if len(v) == 0 {
|
|
|
|
return fmt.Errorf("invalid stack name: %q", namespace)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateStackNames(namespaces []string) error {
|
|
|
|
for _, ns := range namespaces {
|
|
|
|
if err := validateStackName(ns); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func quotesOrWhitespace(r rune) bool {
|
|
|
|
return unicode.IsSpace(r) || r == '"' || r == '\''
|
|
|
|
}
|