2016-09-08 13:11:39 -04:00
|
|
|
package stack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2017-01-18 14:40:29 -05:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/cli"
|
2016-09-08 14:54:01 -04:00
|
|
|
"github.com/docker/docker/cli/command"
|
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 {
|
|
|
|
namespace string
|
|
|
|
}
|
|
|
|
|
2016-09-08 14:54:01 -04:00
|
|
|
func newRemoveCommand(dockerCli *command.DockerCli) *cobra.Command {
|
2016-09-08 13:11:39 -04:00
|
|
|
var opts removeOptions
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "rm STACK",
|
|
|
|
Aliases: []string{"remove", "down"},
|
|
|
|
Short: "Remove the stack",
|
|
|
|
Args: cli.ExactArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
opts.namespace = args[0]
|
|
|
|
return runRemove(dockerCli, opts)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2016-09-08 14:54:01 -04:00
|
|
|
func runRemove(dockerCli *command.DockerCli, opts removeOptions) error {
|
2016-09-08 13:11:39 -04:00
|
|
|
namespace := opts.namespace
|
|
|
|
client := dockerCli.Client()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
services, err := getServices(ctx, client, namespace)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-11-28 11:38:41 -05:00
|
|
|
networks, err := getStackNetworks(ctx, client, namespace)
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-18 14:40:29 -05:00
|
|
|
|
|
|
|
secrets, err := getStackSecrets(ctx, client, namespace)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2017-01-18 14:40:29 -05:00
|
|
|
if len(services)+len(networks)+len(secrets) == 0 {
|
2016-09-08 13:11:39 -04:00
|
|
|
fmt.Fprintf(dockerCli.Out(), "Nothing found in stack: %s\n", namespace)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-18 14:40:29 -05:00
|
|
|
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
|
|
|
if hasError {
|
2017-03-09 13:23:45 -05:00
|
|
|
return errors.Errorf("Failed to remove some resources")
|
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
|
|
|
|
}
|