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"
|
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")
|
|
|
|
orchestratorUnset = Orchestrator("unset")
|
2017-11-20 09:30:52 -05:00
|
|
|
|
2018-01-02 17:56:07 -05:00
|
|
|
defaultOrchestrator = OrchestratorSwarm
|
|
|
|
envVarDockerOrchestrator = "DOCKER_ORCHESTRATOR"
|
2017-11-20 09:30:52 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func normalize(flag string) Orchestrator {
|
2018-01-02 17:56:07 -05:00
|
|
|
switch flag {
|
2017-11-20 09:30:52 -05:00
|
|
|
case "kubernetes", "k8s":
|
2017-12-04 03:53:03 -05:00
|
|
|
return OrchestratorKubernetes
|
2017-11-20 09:30:52 -05:00
|
|
|
case "swarm", "swarmkit":
|
2017-12-04 03:53:03 -05:00
|
|
|
return OrchestratorSwarm
|
2017-11-20 09:30:52 -05:00
|
|
|
default:
|
2017-12-04 03:53:03 -05:00
|
|
|
return orchestratorUnset
|
2017-11-20 09:30:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetOrchestrator checks DOCKER_ORCHESTRATOR environment variable and configuration file
|
|
|
|
// orchestrator value and returns user defined Orchestrator.
|
2017-12-26 09:40:17 -05:00
|
|
|
func GetOrchestrator(isExperimental bool, flagValue, value string) Orchestrator {
|
|
|
|
// Non experimental CLI has kubernetes disabled
|
|
|
|
if !isExperimental {
|
|
|
|
return defaultOrchestrator
|
|
|
|
}
|
2017-12-20 13:06:15 -05:00
|
|
|
// Check flag
|
|
|
|
if o := normalize(flagValue); o != orchestratorUnset {
|
|
|
|
return o
|
|
|
|
}
|
2017-11-20 09:30:52 -05:00
|
|
|
// Check environment variable
|
2018-01-02 17:56:07 -05:00
|
|
|
env := os.Getenv(envVarDockerOrchestrator)
|
2017-12-04 03:53:03 -05:00
|
|
|
if o := normalize(env); o != orchestratorUnset {
|
2017-11-20 09:30:52 -05:00
|
|
|
return o
|
|
|
|
}
|
2017-12-04 06:30:39 -05:00
|
|
|
// Check specified orchestrator
|
2017-12-20 13:06:15 -05:00
|
|
|
if o := normalize(value); o != orchestratorUnset {
|
2017-12-04 06:30:39 -05:00
|
|
|
return o
|
2017-11-20 09:30:52 -05:00
|
|
|
}
|
|
|
|
|
2017-12-20 13:06:15 -05:00
|
|
|
if value != "" {
|
|
|
|
fmt.Fprintf(os.Stderr, "Specified orchestrator %q is invalid. Please use either kubernetes or swarm\n", value)
|
2017-12-05 05:13:38 -05:00
|
|
|
}
|
2017-11-20 09:30:52 -05:00
|
|
|
// Nothing set, use default orchestrator
|
|
|
|
return defaultOrchestrator
|
|
|
|
}
|