2016-09-08 13:11:39 -04:00
|
|
|
package stack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-03-26 02:23:24 -04:00
|
|
|
"strings"
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2017-01-18 14:40:29 -05:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2017-06-12 16:14:05 -04:00
|
|
|
"github.com/docker/docker/api/types/versions"
|
2017-03-09 13:23:45 -05:00
|
|
|
"github.com/pkg/errors"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/spf13/cobra"
|
2017-01-18 14:40:29 -05:00
|
|
|
"golang.org/x/net/context"
|
2016-09-08 13:11:39 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type removeOptions struct {
|
2017-03-26 02:23:24 -04:00
|
|
|
namespaces []string
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2017-03-26 02:23:24 -04:00
|
|
|
func newRemoveCommand(dockerCli command.Cli) *cobra.Command {
|
2016-09-08 13:11:39 -04:00
|
|
|
var opts removeOptions
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
2017-03-26 02:23:24 -04:00
|
|
|
Use: "rm STACK [STACK...]",
|
2016-09-08 13:11:39 -04:00
|
|
|
Aliases: []string{"remove", "down"},
|
2017-03-26 02:23:24 -04:00
|
|
|
Short: "Remove one or more stacks",
|
|
|
|
Args: cli.RequiresMinArgs(1),
|
2016-09-08 13:11:39 -04:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2017-03-26 02:23:24 -04:00
|
|
|
opts.namespaces = args
|
2016-09-08 13:11:39 -04:00
|
|
|
return runRemove(dockerCli, opts)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2017-03-26 02:23:24 -04:00
|
|
|
func runRemove(dockerCli command.Cli, opts removeOptions) error {
|
|
|
|
namespaces := opts.namespaces
|
2016-09-08 13:11:39 -04:00
|
|
|
client := dockerCli.Client()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2017-03-26 02:23:24 -04:00
|
|
|
var errs []string
|
|
|
|
for _, namespace := range namespaces {
|
|
|
|
services, err := getServices(ctx, client, namespace)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2017-03-26 02:23:24 -04:00
|
|
|
networks, err := getStackNetworks(ctx, client, namespace)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-18 14:40:29 -05:00
|
|
|
|
2017-03-26 02:23:24 -04:00
|
|
|
secrets, err := getStackSecrets(ctx, client, namespace)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2017-06-12 16:14:05 -04:00
|
|
|
var configs []swarm.Config
|
|
|
|
|
|
|
|
version, err := client.ServerVersion(ctx)
|
2017-05-26 20:30:33 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-06-12 16:14:05 -04:00
|
|
|
if versions.LessThan(version.APIVersion, "1.30") {
|
|
|
|
fmt.Fprintf(dockerCli.Err(), `WARNING: ignoring "configs" (requires API version 1.30, but the Docker daemon API version is %s)`, version.APIVersion)
|
|
|
|
} else {
|
|
|
|
configs, err = getStackConfigs(ctx, client, namespace)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-05-26 20:30:33 -04:00
|
|
|
|
|
|
|
if len(services)+len(networks)+len(secrets)+len(configs) == 0 {
|
2017-03-26 02:23:24 -04:00
|
|
|
fmt.Fprintf(dockerCli.Out(), "Nothing found in stack: %s\n", namespace)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
hasError := removeServices(ctx, dockerCli, services)
|
|
|
|
hasError = removeSecrets(ctx, dockerCli, secrets) || hasError
|
2017-05-26 20:30:33 -04:00
|
|
|
hasError = removeConfigs(ctx, dockerCli, configs) || hasError
|
2017-03-26 02:23:24 -04:00
|
|
|
hasError = removeNetworks(ctx, dockerCli, networks) || hasError
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2017-03-26 02:23:24 -04:00
|
|
|
if hasError {
|
|
|
|
errs = append(errs, fmt.Sprintf("Failed to remove some resources from stack: %s", namespace))
|
|
|
|
}
|
|
|
|
}
|
2017-01-18 14:40:29 -05:00
|
|
|
|
2017-03-26 02:23:24 -04:00
|
|
|
if len(errs) > 0 {
|
|
|
|
return errors.Errorf(strings.Join(errs, "\n"))
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-01-18 14:40:29 -05:00
|
|
|
|
|
|
|
func removeServices(
|
|
|
|
ctx context.Context,
|
2017-02-22 15:43:13 -05:00
|
|
|
dockerCli command.Cli,
|
2017-01-18 14:40:29 -05:00
|
|
|
services []swarm.Service,
|
|
|
|
) bool {
|
|
|
|
var err error
|
|
|
|
for _, service := range services {
|
|
|
|
fmt.Fprintf(dockerCli.Err(), "Removing service %s\n", service.Spec.Name)
|
|
|
|
if err = dockerCli.Client().ServiceRemove(ctx, service.ID); err != nil {
|
|
|
|
fmt.Fprintf(dockerCli.Err(), "Failed to remove service %s: %s", service.ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeNetworks(
|
|
|
|
ctx context.Context,
|
2017-02-22 15:43:13 -05:00
|
|
|
dockerCli command.Cli,
|
2017-01-18 14:40:29 -05:00
|
|
|
networks []types.NetworkResource,
|
|
|
|
) bool {
|
|
|
|
var err error
|
|
|
|
for _, network := range networks {
|
|
|
|
fmt.Fprintf(dockerCli.Err(), "Removing network %s\n", network.Name)
|
|
|
|
if err = dockerCli.Client().NetworkRemove(ctx, network.ID); err != nil {
|
|
|
|
fmt.Fprintf(dockerCli.Err(), "Failed to remove network %s: %s", network.ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeSecrets(
|
|
|
|
ctx context.Context,
|
2017-02-22 15:43:13 -05:00
|
|
|
dockerCli command.Cli,
|
2017-01-18 14:40:29 -05:00
|
|
|
secrets []swarm.Secret,
|
|
|
|
) bool {
|
|
|
|
var err error
|
|
|
|
for _, secret := range secrets {
|
|
|
|
fmt.Fprintf(dockerCli.Err(), "Removing secret %s\n", secret.Spec.Name)
|
|
|
|
if err = dockerCli.Client().SecretRemove(ctx, secret.ID); err != nil {
|
|
|
|
fmt.Fprintf(dockerCli.Err(), "Failed to remove secret %s: %s", secret.ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err != nil
|
|
|
|
}
|
2017-05-26 20:30:33 -04:00
|
|
|
|
|
|
|
func removeConfigs(
|
|
|
|
ctx context.Context,
|
|
|
|
dockerCli command.Cli,
|
|
|
|
configs []swarm.Config,
|
|
|
|
) bool {
|
|
|
|
var err error
|
|
|
|
for _, config := range configs {
|
|
|
|
fmt.Fprintf(dockerCli.Err(), "Removing config %s\n", config.Spec.Name)
|
|
|
|
if err = dockerCli.Client().ConfigRemove(ctx, config.ID); err != nil {
|
|
|
|
fmt.Fprintf(dockerCli.Err(), "Failed to remove config %s: %s", config.ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err != nil
|
|
|
|
}
|