2016-09-08 13:11:39 -04:00
|
|
|
package stack
|
|
|
|
|
|
|
|
import (
|
2018-06-20 08:48:50 -04:00
|
|
|
"errors"
|
2018-04-25 08:24:46 -04:00
|
|
|
"fmt"
|
2018-06-20 08:48:50 -04:00
|
|
|
"strings"
|
2018-04-25 08:24:46 -04:00
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/spf13/cobra"
|
2018-06-20 08:48:50 -04:00
|
|
|
"github.com/spf13/pflag"
|
2016-09-08 13:11:39 -04:00
|
|
|
)
|
|
|
|
|
2018-04-25 08:24:46 -04:00
|
|
|
var errUnsupportedAllOrchestrator = fmt.Errorf(`no orchestrator specified: use either "kubernetes" or "swarm"`)
|
|
|
|
|
2018-06-20 08:48:50 -04:00
|
|
|
type commonOptions struct {
|
|
|
|
orchestrator command.Orchestrator
|
|
|
|
}
|
|
|
|
|
2019-01-14 12:00:14 -05:00
|
|
|
func (o *commonOptions) Orchestrator() command.Orchestrator {
|
|
|
|
if o == nil {
|
|
|
|
return command.OrchestratorSwarm
|
|
|
|
}
|
|
|
|
return o.orchestrator
|
|
|
|
}
|
|
|
|
|
2016-10-06 10:09:54 -04:00
|
|
|
// NewStackCommand returns a cobra command for `stack` subcommands
|
2017-10-11 12:18:27 -04:00
|
|
|
func NewStackCommand(dockerCli command.Cli) *cobra.Command {
|
2018-06-20 08:48:50 -04:00
|
|
|
var opts commonOptions
|
2016-10-06 10:09:54 -04:00
|
|
|
cmd := &cobra.Command{
|
2018-06-22 02:16:27 -04:00
|
|
|
Use: "stack [OPTIONS]",
|
2018-01-11 17:42:50 -05:00
|
|
|
Short: "Manage Docker stacks",
|
|
|
|
Args: cli.NoArgs,
|
2018-06-20 08:48:50 -04:00
|
|
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
2018-12-17 05:27:07 -05:00
|
|
|
orchestrator, err := getOrchestrator(dockerCli, cmd)
|
2018-06-20 08:48:50 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
opts.orchestrator = orchestrator
|
2018-06-22 14:54:07 -04:00
|
|
|
hideOrchestrationFlags(cmd, orchestrator)
|
2018-06-20 08:48:50 -04:00
|
|
|
return checkSupportedFlag(cmd, orchestrator)
|
|
|
|
},
|
|
|
|
|
|
|
|
RunE: command.ShowHelp(dockerCli.Err()),
|
2018-01-11 17:42:50 -05:00
|
|
|
Annotations: map[string]string{
|
2018-06-20 08:48:50 -04:00
|
|
|
"version": "1.25",
|
2018-01-11 17:42:50 -05:00
|
|
|
},
|
2016-10-06 10:09:54 -04:00
|
|
|
}
|
2018-06-20 08:48:50 -04:00
|
|
|
defaultHelpFunc := cmd.HelpFunc()
|
2018-07-31 19:48:27 -04:00
|
|
|
cmd.SetHelpFunc(func(c *cobra.Command, args []string) {
|
|
|
|
if err := cmd.PersistentPreRunE(c, args); err != nil {
|
|
|
|
fmt.Fprintln(dockerCli.Err(), err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
hideOrchestrationFlags(c, opts.orchestrator)
|
|
|
|
defaultHelpFunc(c, args)
|
2018-06-20 08:48:50 -04:00
|
|
|
})
|
2017-12-04 06:30:39 -05:00
|
|
|
cmd.AddCommand(
|
2018-06-20 08:48:50 -04:00
|
|
|
newDeployCommand(dockerCli, &opts),
|
|
|
|
newListCommand(dockerCli, &opts),
|
|
|
|
newPsCommand(dockerCli, &opts),
|
|
|
|
newRemoveCommand(dockerCli, &opts),
|
|
|
|
newServicesCommand(dockerCli, &opts),
|
2017-12-04 06:30:39 -05:00
|
|
|
)
|
|
|
|
flags := cmd.PersistentFlags()
|
|
|
|
flags.String("kubeconfig", "", "Kubernetes config file")
|
|
|
|
flags.SetAnnotation("kubeconfig", "kubernetes", nil)
|
2018-06-20 08:48:50 -04:00
|
|
|
flags.String("orchestrator", "", "Orchestrator to use (swarm|kubernetes|all)")
|
2016-10-06 10:09:54 -04:00
|
|
|
return cmd
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2016-10-06 10:09:54 -04:00
|
|
|
// NewTopLevelDeployCommand returns a command for `docker deploy`
|
2017-05-03 17:58:52 -04:00
|
|
|
func NewTopLevelDeployCommand(dockerCli command.Cli) *cobra.Command {
|
2018-06-20 08:48:50 -04:00
|
|
|
cmd := newDeployCommand(dockerCli, nil)
|
2016-10-06 10:09:54 -04:00
|
|
|
// Remove the aliases at the top level
|
|
|
|
cmd.Aliases = []string{}
|
2018-01-11 17:42:50 -05:00
|
|
|
cmd.Annotations = map[string]string{
|
|
|
|
"experimental": "",
|
|
|
|
"version": "1.25",
|
|
|
|
}
|
2016-10-06 10:09:54 -04:00
|
|
|
return cmd
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2018-06-20 08:48:50 -04:00
|
|
|
|
2018-12-17 05:27:07 -05:00
|
|
|
func getOrchestrator(dockerCli command.Cli, cmd *cobra.Command) (command.Orchestrator, error) {
|
2018-06-20 08:48:50 -04:00
|
|
|
var orchestratorFlag string
|
|
|
|
if o, err := cmd.Flags().GetString("orchestrator"); err == nil {
|
|
|
|
orchestratorFlag = o
|
|
|
|
}
|
2018-12-17 05:27:07 -05:00
|
|
|
return dockerCli.StackOrchestrator(orchestratorFlag)
|
2018-06-20 08:48:50 -04:00
|
|
|
}
|
|
|
|
|
2018-06-22 14:54:07 -04:00
|
|
|
func hideOrchestrationFlags(cmd *cobra.Command, orchestrator command.Orchestrator) {
|
2018-06-20 08:48:50 -04:00
|
|
|
cmd.Flags().VisitAll(func(f *pflag.Flag) {
|
|
|
|
if _, ok := f.Annotations["kubernetes"]; ok && !orchestrator.HasKubernetes() {
|
|
|
|
f.Hidden = true
|
|
|
|
}
|
|
|
|
if _, ok := f.Annotations["swarm"]; ok && !orchestrator.HasSwarm() {
|
|
|
|
f.Hidden = true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
for _, subcmd := range cmd.Commands() {
|
2018-06-22 14:54:07 -04:00
|
|
|
hideOrchestrationFlags(subcmd, orchestrator)
|
2018-06-20 08:48:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkSupportedFlag(cmd *cobra.Command, orchestrator command.Orchestrator) error {
|
|
|
|
errs := []string{}
|
|
|
|
cmd.Flags().VisitAll(func(f *pflag.Flag) {
|
|
|
|
if !f.Changed {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, ok := f.Annotations["kubernetes"]; ok && !orchestrator.HasKubernetes() {
|
|
|
|
errs = append(errs, fmt.Sprintf(`"--%s" is only supported on a Docker cli with kubernetes features enabled`, f.Name))
|
|
|
|
}
|
|
|
|
if _, ok := f.Annotations["swarm"]; ok && !orchestrator.HasSwarm() {
|
|
|
|
errs = append(errs, fmt.Sprintf(`"--%s" is only supported on a Docker cli with swarm features enabled`, f.Name))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
for _, subcmd := range cmd.Commands() {
|
|
|
|
if err := checkSupportedFlag(subcmd, orchestrator); err != nil {
|
|
|
|
errs = append(errs, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(errs) > 0 {
|
|
|
|
return errors.New(strings.Join(errs, "\n"))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|