github.com/AliyunContainerService/cli@v0.0.0-20181009023821-814ced4b30d0/cli/command/orchestrator.go (about) 1 package command 2 3 import ( 4 "fmt" 5 "io" 6 "os" 7 ) 8 9 // Orchestrator type acts as an enum describing supported orchestrators. 10 type Orchestrator string 11 12 const ( 13 // OrchestratorKubernetes orchestrator 14 OrchestratorKubernetes = Orchestrator("kubernetes") 15 // OrchestratorSwarm orchestrator 16 OrchestratorSwarm = Orchestrator("swarm") 17 // OrchestratorAll orchestrator 18 OrchestratorAll = Orchestrator("all") 19 orchestratorUnset = Orchestrator("unset") 20 21 defaultOrchestrator = OrchestratorSwarm 22 envVarDockerStackOrchestrator = "DOCKER_STACK_ORCHESTRATOR" 23 envVarDockerOrchestrator = "DOCKER_ORCHESTRATOR" 24 ) 25 26 // HasKubernetes returns true if defined orchestrator has Kubernetes capabilities. 27 func (o Orchestrator) HasKubernetes() bool { 28 return o == OrchestratorKubernetes || o == OrchestratorAll 29 } 30 31 // HasSwarm returns true if defined orchestrator has Swarm capabilities. 32 func (o Orchestrator) HasSwarm() bool { 33 return o == OrchestratorSwarm || o == OrchestratorAll 34 } 35 36 // HasAll returns true if defined orchestrator has both Swarm and Kubernetes capabilities. 37 func (o Orchestrator) HasAll() bool { 38 return o == OrchestratorAll 39 } 40 41 func normalize(value string) (Orchestrator, error) { 42 switch value { 43 case "kubernetes": 44 return OrchestratorKubernetes, nil 45 case "swarm": 46 return OrchestratorSwarm, nil 47 case "": 48 return orchestratorUnset, nil 49 case "all": 50 return OrchestratorAll, nil 51 default: 52 return defaultOrchestrator, fmt.Errorf("specified orchestrator %q is invalid, please use either kubernetes, swarm or all", value) 53 } 54 } 55 56 // GetStackOrchestrator checks DOCKER_STACK_ORCHESTRATOR environment variable and configuration file 57 // orchestrator value and returns user defined Orchestrator. 58 func GetStackOrchestrator(flagValue, value string, stderr io.Writer) (Orchestrator, error) { 59 // Check flag 60 if o, err := normalize(flagValue); o != orchestratorUnset { 61 return o, err 62 } 63 // Check environment variable 64 env := os.Getenv(envVarDockerStackOrchestrator) 65 if env == "" && os.Getenv(envVarDockerOrchestrator) != "" { 66 fmt.Fprintf(stderr, "WARNING: experimental environment variable %s is set. Please use %s instead\n", envVarDockerOrchestrator, envVarDockerStackOrchestrator) 67 } 68 if o, err := normalize(env); o != orchestratorUnset { 69 return o, err 70 } 71 // Check specified orchestrator 72 if o, err := normalize(value); o != orchestratorUnset { 73 return o, err 74 } 75 // Nothing set, use default orchestrator 76 return defaultOrchestrator, nil 77 }