2017-12-04 03:53:03 -05:00
|
|
|
package command
|
2017-11-20 09:30:52 -05:00
|
|
|
|
|
|
|
import (
|
2017-12-05 05:13:38 -05:00
|
|
|
"fmt"
|
2018-06-22 10:26:18 -04:00
|
|
|
"io"
|
2017-11-20 09:30:52 -05:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Orchestrator type acts as an enum describing supported orchestrators.
|
|
|
|
type Orchestrator string
|
|
|
|
|
|
|
|
const (
|
2017-12-04 06:30:39 -05:00
|
|
|
// OrchestratorKubernetes orchestrator
|
2017-12-04 03:53:03 -05:00
|
|
|
OrchestratorKubernetes = Orchestrator("kubernetes")
|
2017-12-04 06:30:39 -05:00
|
|
|
// OrchestratorSwarm orchestrator
|
2017-12-04 03:53:03 -05:00
|
|
|
OrchestratorSwarm = Orchestrator("swarm")
|
2018-04-25 08:24:46 -04:00
|
|
|
// OrchestratorAll orchestrator
|
|
|
|
OrchestratorAll = Orchestrator("all")
|
2018-11-09 09:10:41 -05:00
|
|
|
orchestratorUnset = Orchestrator("")
|
2017-11-20 09:30:52 -05:00
|
|
|
|
2018-06-20 08:48:50 -04:00
|
|
|
defaultOrchestrator = OrchestratorSwarm
|
|
|
|
envVarDockerStackOrchestrator = "DOCKER_STACK_ORCHESTRATOR"
|
2018-06-22 10:26:18 -04:00
|
|
|
envVarDockerOrchestrator = "DOCKER_ORCHESTRATOR"
|
2017-11-20 09:30:52 -05:00
|
|
|
)
|
|
|
|
|
2018-06-20 08:48:50 -04:00
|
|
|
// HasKubernetes returns true if defined orchestrator has Kubernetes capabilities.
|
|
|
|
func (o Orchestrator) HasKubernetes() bool {
|
|
|
|
return o == OrchestratorKubernetes || o == OrchestratorAll
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasSwarm returns true if defined orchestrator has Swarm capabilities.
|
|
|
|
func (o Orchestrator) HasSwarm() bool {
|
|
|
|
return o == OrchestratorSwarm || o == OrchestratorAll
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasAll returns true if defined orchestrator has both Swarm and Kubernetes capabilities.
|
|
|
|
func (o Orchestrator) HasAll() bool {
|
|
|
|
return o == OrchestratorAll
|
|
|
|
}
|
|
|
|
|
2018-05-11 11:19:55 -04:00
|
|
|
func normalize(value string) (Orchestrator, error) {
|
|
|
|
switch value {
|
2018-01-11 15:16:37 -05:00
|
|
|
case "kubernetes":
|
2018-05-11 11:19:55 -04:00
|
|
|
return OrchestratorKubernetes, nil
|
2018-01-11 15:16:37 -05:00
|
|
|
case "swarm":
|
2018-05-11 11:19:55 -04:00
|
|
|
return OrchestratorSwarm, nil
|
2018-11-09 09:10:41 -05:00
|
|
|
case "", "unset": // unset is the old value for orchestratorUnset. Keep accepting this for backward compat
|
2018-05-11 11:19:55 -04:00
|
|
|
return orchestratorUnset, nil
|
2018-04-25 08:24:46 -04:00
|
|
|
case "all":
|
|
|
|
return OrchestratorAll, nil
|
2017-11-20 09:30:52 -05:00
|
|
|
default:
|
2018-04-25 08:24:46 -04:00
|
|
|
return defaultOrchestrator, fmt.Errorf("specified orchestrator %q is invalid, please use either kubernetes, swarm or all", value)
|
2017-11-20 09:30:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-17 05:27:07 -05:00
|
|
|
// NormalizeOrchestrator parses an orchestrator value and checks if it is valid
|
|
|
|
func NormalizeOrchestrator(value string) (Orchestrator, error) {
|
|
|
|
return normalize(value)
|
|
|
|
}
|
|
|
|
|
2018-06-20 08:48:50 -04:00
|
|
|
// GetStackOrchestrator checks DOCKER_STACK_ORCHESTRATOR environment variable and configuration file
|
2017-11-20 09:30:52 -05:00
|
|
|
// orchestrator value and returns user defined Orchestrator.
|
2018-12-17 05:27:07 -05:00
|
|
|
func GetStackOrchestrator(flagValue, contextValue, globalDefault string, stderr io.Writer) (Orchestrator, error) {
|
2017-12-20 13:06:15 -05:00
|
|
|
// Check flag
|
2018-05-11 11:19:55 -04:00
|
|
|
if o, err := normalize(flagValue); o != orchestratorUnset {
|
|
|
|
return o, err
|
2017-12-20 13:06:15 -05:00
|
|
|
}
|
2017-11-20 09:30:52 -05:00
|
|
|
// Check environment variable
|
2018-06-20 08:48:50 -04:00
|
|
|
env := os.Getenv(envVarDockerStackOrchestrator)
|
2018-06-22 10:26:18 -04:00
|
|
|
if env == "" && os.Getenv(envVarDockerOrchestrator) != "" {
|
|
|
|
fmt.Fprintf(stderr, "WARNING: experimental environment variable %s is set. Please use %s instead\n", envVarDockerOrchestrator, envVarDockerStackOrchestrator)
|
|
|
|
}
|
2018-05-11 11:19:55 -04:00
|
|
|
if o, err := normalize(env); o != orchestratorUnset {
|
|
|
|
return o, err
|
2017-11-20 09:30:52 -05:00
|
|
|
}
|
2018-12-17 05:27:07 -05:00
|
|
|
if o, err := normalize(contextValue); o != orchestratorUnset {
|
|
|
|
return o, err
|
|
|
|
}
|
|
|
|
if o, err := normalize(globalDefault); o != orchestratorUnset {
|
2018-05-11 11:19:55 -04:00
|
|
|
return o, err
|
2017-12-05 05:13:38 -05:00
|
|
|
}
|
2017-11-20 09:30:52 -05:00
|
|
|
// Nothing set, use default orchestrator
|
2018-05-11 11:19:55 -04:00
|
|
|
return defaultOrchestrator, nil
|
2017-11-20 09:30:52 -05:00
|
|
|
}
|