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-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-03-26 02:23:24 -04:00
|
|
|
if len(services)+len(networks)+len(secrets) == 0 {
|
|
|
|
fmt.Fprintf(dockerCli.Out(), "Nothing found in stack: %s\n", namespace)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
hasError := removeServices(ctx, dockerCli, services)
|
|
|
|
hasError = removeSecrets(ctx, dockerCli, secrets) || hasError
|
|
|
|
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
|
|
|
|
}
|