1// Copyright 2015 CoreOS, Inc. 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15// Every change should be reflected on help.go as well. 16 17package etcdmain 18 19import ( 20 "flag" 21 "fmt" 22 "net/url" 23 "os" 24 "runtime" 25 "strings" 26 27 "github.com/coreos/etcd/etcdserver" 28 "github.com/coreos/etcd/pkg/cors" 29 "github.com/coreos/etcd/pkg/flags" 30 "github.com/coreos/etcd/pkg/transport" 31 "github.com/coreos/etcd/version" 32) 33 34const ( 35 proxyFlagOff = "off" 36 proxyFlagReadonly = "readonly" 37 proxyFlagOn = "on" 38 39 fallbackFlagExit = "exit" 40 fallbackFlagProxy = "proxy" 41 42 clusterStateFlagNew = "new" 43 clusterStateFlagExisting = "existing" 44 45 defaultName = "default" 46 defaultInitialAdvertisePeerURLs = "http://localhost:2380,http://localhost:7001" 47 48 // maxElectionMs specifies the maximum value of election timeout. 49 // More details are listed in ../Documentation/tuning.md#time-parameters. 50 maxElectionMs = 50000 51) 52 53var ( 54 ignored = []string{ 55 "cluster-active-size", 56 "cluster-remove-delay", 57 "cluster-sync-interval", 58 "config", 59 "force", 60 "max-result-buffer", 61 "max-retry-attempts", 62 "peer-heartbeat-interval", 63 "peer-election-timeout", 64 "retry-interval", 65 "snapshot", 66 "v", 67 "vv", 68 } 69 70 ErrConflictBootstrapFlags = fmt.Errorf("multiple discovery or bootstrap flags are set. " + 71 "Choose one of \"initial-cluster\", \"discovery\" or \"discovery-srv\"") 72 errUnsetAdvertiseClientURLsFlag = fmt.Errorf("-advertise-client-urls is required when -listen-client-urls is set explicitly") 73) 74 75type config struct { 76 *flag.FlagSet 77 78 // member 79 corsInfo *cors.CORSInfo 80 dir string 81 walDir string 82 lpurls, lcurls []url.URL 83 maxSnapFiles uint 84 maxWalFiles uint 85 name string 86 snapCount uint64 87 // TickMs is the number of milliseconds between heartbeat ticks. 88 // TODO: decouple tickMs and heartbeat tick (current heartbeat tick = 1). 89 // make ticks a cluster wide configuration. 90 TickMs uint 91 ElectionMs uint 92 93 // clustering 94 apurls, acurls []url.URL 95 clusterState *flags.StringsFlag 96 dnsCluster string 97 dproxy string 98 durl string 99 fallback *flags.StringsFlag 100 initialCluster string 101 initialClusterToken string 102 strictReconfigCheck bool 103 104 // proxy 105 proxy *flags.StringsFlag 106 proxyFailureWaitMs uint 107 proxyRefreshIntervalMs uint 108 proxyDialTimeoutMs uint 109 proxyWriteTimeoutMs uint 110 proxyReadTimeoutMs uint 111 112 // security 113 clientTLSInfo, peerTLSInfo transport.TLSInfo 114 115 // logging 116 debug bool 117 logPkgLevels string 118 119 // unsafe 120 forceNewCluster bool 121 122 printVersion bool 123 124 v3demo bool 125 gRPCAddr string 126 autoCompactionRetention int 127 128 enablePprof bool 129 130 ignored []string 131} 132 133func NewConfig() *config { 134 cfg := &config{ 135 corsInfo: &cors.CORSInfo{}, 136 clusterState: flags.NewStringsFlag( 137 clusterStateFlagNew, 138 clusterStateFlagExisting, 139 ), 140 fallback: flags.NewStringsFlag( 141 fallbackFlagExit, 142 fallbackFlagProxy, 143 ), 144 ignored: ignored, 145 proxy: flags.NewStringsFlag( 146 proxyFlagOff, 147 proxyFlagReadonly, 148 proxyFlagOn, 149 ), 150 } 151 152 cfg.FlagSet = flag.NewFlagSet("etcd", flag.ContinueOnError) 153 fs := cfg.FlagSet 154 fs.Usage = func() { 155 fmt.Println(usageline) 156 } 157 158 // member 159 fs.Var(cfg.corsInfo, "cors", "Comma-separated white list of origins for CORS (cross-origin resource sharing).") 160 fs.StringVar(&cfg.dir, "data-dir", "", "Path to the data directory.") 161 fs.StringVar(&cfg.walDir, "wal-dir", "", "Path to the dedicated wal directory.") 162 fs.Var(flags.NewURLsValue("http://localhost:2380,http://localhost:7001"), "listen-peer-urls", "List of URLs to listen on for peer traffic.") 163 fs.Var(flags.NewURLsValue("http://localhost:2379,http://localhost:4001"), "listen-client-urls", "List of URLs to listen on for client traffic.") 164 fs.UintVar(&cfg.maxSnapFiles, "max-snapshots", defaultMaxSnapshots, "Maximum number of snapshot files to retain (0 is unlimited).") 165 fs.UintVar(&cfg.maxWalFiles, "max-wals", defaultMaxWALs, "Maximum number of wal files to retain (0 is unlimited).") 166 fs.StringVar(&cfg.name, "name", defaultName, "Human-readable name for this member.") 167 fs.Uint64Var(&cfg.snapCount, "snapshot-count", etcdserver.DefaultSnapCount, "Number of committed transactions to trigger a snapshot to disk.") 168 fs.UintVar(&cfg.TickMs, "heartbeat-interval", 100, "Time (in milliseconds) of a heartbeat interval.") 169 fs.UintVar(&cfg.ElectionMs, "election-timeout", 1000, "Time (in milliseconds) for an election to timeout.") 170 171 // clustering 172 fs.Var(flags.NewURLsValue(defaultInitialAdvertisePeerURLs), "initial-advertise-peer-urls", "List of this member's peer URLs to advertise to the rest of the cluster.") 173 fs.Var(flags.NewURLsValue("http://localhost:2379,http://localhost:4001"), "advertise-client-urls", "List of this member's client URLs to advertise to the public.") 174 fs.StringVar(&cfg.durl, "discovery", "", "Discovery URL used to bootstrap the cluster.") 175 fs.Var(cfg.fallback, "discovery-fallback", fmt.Sprintf("Valid values include %s", strings.Join(cfg.fallback.Values, ", "))) 176 if err := cfg.fallback.Set(fallbackFlagProxy); err != nil { 177 // Should never happen. 178 plog.Panicf("unexpected error setting up discovery-fallback flag: %v", err) 179 } 180 fs.StringVar(&cfg.dproxy, "discovery-proxy", "", "HTTP proxy to use for traffic to discovery service.") 181 fs.StringVar(&cfg.dnsCluster, "discovery-srv", "", "DNS domain used to bootstrap initial cluster.") 182 fs.StringVar(&cfg.initialCluster, "initial-cluster", initialClusterFromName(defaultName), "Initial cluster configuration for bootstrapping.") 183 fs.StringVar(&cfg.initialClusterToken, "initial-cluster-token", "etcd-cluster", "Initial cluster token for the etcd cluster during bootstrap.") 184 fs.Var(cfg.clusterState, "initial-cluster-state", "Initial cluster state ('new' or 'existing').") 185 if err := cfg.clusterState.Set(clusterStateFlagNew); err != nil { 186 // Should never happen. 187 plog.Panicf("unexpected error setting up clusterStateFlag: %v", err) 188 } 189 fs.BoolVar(&cfg.strictReconfigCheck, "strict-reconfig-check", false, "Reject reconfiguration requests that would cause quorum loss.") 190 191 // proxy 192 fs.Var(cfg.proxy, "proxy", fmt.Sprintf("Valid values include %s", strings.Join(cfg.proxy.Values, ", "))) 193 if err := cfg.proxy.Set(proxyFlagOff); err != nil { 194 // Should never happen. 195 plog.Panicf("unexpected error setting up proxyFlag: %v", err) 196 } 197 fs.UintVar(&cfg.proxyFailureWaitMs, "proxy-failure-wait", 5000, "Time (in milliseconds) an endpoint will be held in a failed state.") 198 fs.UintVar(&cfg.proxyRefreshIntervalMs, "proxy-refresh-interval", 30000, "Time (in milliseconds) of the endpoints refresh interval.") 199 fs.UintVar(&cfg.proxyDialTimeoutMs, "proxy-dial-timeout", 1000, "Time (in milliseconds) for a dial to timeout.") 200 fs.UintVar(&cfg.proxyWriteTimeoutMs, "proxy-write-timeout", 5000, "Time (in milliseconds) for a write to timeout.") 201 fs.UintVar(&cfg.proxyReadTimeoutMs, "proxy-read-timeout", 0, "Time (in milliseconds) for a read to timeout.") 202 203 // security 204 fs.StringVar(&cfg.clientTLSInfo.CAFile, "ca-file", "", "DEPRECATED: Path to the client server TLS CA file.") 205 fs.StringVar(&cfg.clientTLSInfo.CertFile, "cert-file", "", "Path to the client server TLS cert file.") 206 fs.StringVar(&cfg.clientTLSInfo.KeyFile, "key-file", "", "Path to the client server TLS key file.") 207 fs.BoolVar(&cfg.clientTLSInfo.ClientCertAuth, "client-cert-auth", false, "Enable client cert authentication.") 208 fs.StringVar(&cfg.clientTLSInfo.TrustedCAFile, "trusted-ca-file", "", "Path to the client server TLS trusted CA key file.") 209 fs.StringVar(&cfg.peerTLSInfo.CAFile, "peer-ca-file", "", "DEPRECATED: Path to the peer server TLS CA file.") 210 fs.StringVar(&cfg.peerTLSInfo.CertFile, "peer-cert-file", "", "Path to the peer server TLS cert file.") 211 fs.StringVar(&cfg.peerTLSInfo.KeyFile, "peer-key-file", "", "Path to the peer server TLS key file.") 212 fs.BoolVar(&cfg.peerTLSInfo.ClientCertAuth, "peer-client-cert-auth", false, "Enable peer client cert authentication.") 213 fs.StringVar(&cfg.peerTLSInfo.TrustedCAFile, "peer-trusted-ca-file", "", "Path to the peer server TLS trusted CA file.") 214 215 // logging 216 fs.BoolVar(&cfg.debug, "debug", false, "Enable debug-level logging for etcd.") 217 fs.StringVar(&cfg.logPkgLevels, "log-package-levels", "", "Specify a particular log level for each etcd package (eg: 'etcdmain=CRITICAL,etcdserver=DEBUG').") 218 219 // unsafe 220 fs.BoolVar(&cfg.forceNewCluster, "force-new-cluster", false, "Force to create a new one member cluster.") 221 222 // version 223 fs.BoolVar(&cfg.printVersion, "version", false, "Print the version and exit.") 224 225 // demo flag 226 fs.BoolVar(&cfg.v3demo, "experimental-v3demo", false, "Enable experimental v3 demo API.") 227 fs.StringVar(&cfg.gRPCAddr, "experimental-gRPC-addr", "127.0.0.1:2378", "gRPC address for experimental v3 demo API.") 228 fs.IntVar(&cfg.autoCompactionRetention, "experimental-auto-compaction-retention", 0, "Auto compaction retention in hour. 0 means disable auto compaction.") 229 230 // backwards-compatibility with v0.4.6 231 fs.Var(&flags.IPAddressPort{}, "addr", "DEPRECATED: Use -advertise-client-urls instead.") 232 fs.Var(&flags.IPAddressPort{}, "bind-addr", "DEPRECATED: Use -listen-client-urls instead.") 233 fs.Var(&flags.IPAddressPort{}, "peer-addr", "DEPRECATED: Use -initial-advertise-peer-urls instead.") 234 fs.Var(&flags.IPAddressPort{}, "peer-bind-addr", "DEPRECATED: Use -listen-peer-urls instead.") 235 fs.Var(&flags.DeprecatedFlag{Name: "peers"}, "peers", "DEPRECATED: Use -initial-cluster instead.") 236 fs.Var(&flags.DeprecatedFlag{Name: "peers-file"}, "peers-file", "DEPRECATED: Use -initial-cluster instead.") 237 238 // pprof profiler via HTTP 239 fs.BoolVar(&cfg.enablePprof, "enable-pprof", false, "Enable runtime profiling data via HTTP server. Address is at client URL + \"/debug/pprof\"") 240 241 // ignored 242 for _, f := range cfg.ignored { 243 fs.Var(&flags.IgnoredFlag{Name: f}, f, "") 244 } 245 return cfg 246} 247 248func (cfg *config) Parse(arguments []string) error { 249 perr := cfg.FlagSet.Parse(arguments) 250 switch perr { 251 case nil: 252 case flag.ErrHelp: 253 fmt.Println(flagsline) 254 os.Exit(0) 255 default: 256 os.Exit(2) 257 } 258 if len(cfg.FlagSet.Args()) != 0 { 259 return fmt.Errorf("'%s' is not a valid flag", cfg.FlagSet.Arg(0)) 260 } 261 262 if cfg.printVersion { 263 fmt.Printf("etcd Version: %s\n", version.Version) 264 fmt.Printf("Git SHA: %s\n", version.GitSHA) 265 fmt.Printf("Go Version: %s\n", runtime.Version()) 266 fmt.Printf("Go OS/Arch: %s/%s\n", runtime.GOOS, runtime.GOARCH) 267 os.Exit(0) 268 } 269 270 err := flags.SetFlagsFromEnv(cfg.FlagSet) 271 if err != nil { 272 plog.Fatalf("%v", err) 273 } 274 275 set := make(map[string]bool) 276 cfg.FlagSet.Visit(func(f *flag.Flag) { 277 set[f.Name] = true 278 }) 279 nSet := 0 280 for _, v := range []bool{set["discovery"], set["initial-cluster"], set["discovery-srv"]} { 281 if v { 282 nSet += 1 283 } 284 } 285 if nSet > 1 { 286 return ErrConflictBootstrapFlags 287 } 288 289 flags.SetBindAddrFromAddr(cfg.FlagSet, "peer-bind-addr", "peer-addr") 290 flags.SetBindAddrFromAddr(cfg.FlagSet, "bind-addr", "addr") 291 292 cfg.lpurls, err = flags.URLsFromFlags(cfg.FlagSet, "listen-peer-urls", "peer-bind-addr", cfg.peerTLSInfo) 293 if err != nil { 294 return err 295 } 296 cfg.apurls, err = flags.URLsFromFlags(cfg.FlagSet, "initial-advertise-peer-urls", "peer-addr", cfg.peerTLSInfo) 297 if err != nil { 298 return err 299 } 300 cfg.lcurls, err = flags.URLsFromFlags(cfg.FlagSet, "listen-client-urls", "bind-addr", cfg.clientTLSInfo) 301 if err != nil { 302 return err 303 } 304 cfg.acurls, err = flags.URLsFromFlags(cfg.FlagSet, "advertise-client-urls", "addr", cfg.clientTLSInfo) 305 if err != nil { 306 return err 307 } 308 309 // when etcd runs in member mode user needs to set -advertise-client-urls if -listen-client-urls is set. 310 // TODO(yichengq): check this for joining through discovery service case 311 mayFallbackToProxy := flags.IsSet(cfg.FlagSet, "discovery") && cfg.fallback.String() == fallbackFlagProxy 312 mayBeProxy := cfg.proxy.String() != proxyFlagOff || mayFallbackToProxy 313 if !mayBeProxy { 314 if flags.IsSet(cfg.FlagSet, "listen-client-urls") && !flags.IsSet(cfg.FlagSet, "advertise-client-urls") { 315 return errUnsetAdvertiseClientURLsFlag 316 } 317 } 318 319 if 5*cfg.TickMs > cfg.ElectionMs { 320 return fmt.Errorf("-election-timeout[%vms] should be at least as 5 times as -heartbeat-interval[%vms]", cfg.ElectionMs, cfg.TickMs) 321 } 322 if cfg.ElectionMs > maxElectionMs { 323 return fmt.Errorf("-election-timeout[%vms] is too long, and should be set less than %vms", cfg.ElectionMs, maxElectionMs) 324 } 325 326 return nil 327} 328 329func initialClusterFromName(name string) string { 330 n := name 331 if name == "" { 332 n = defaultName 333 } 334 return fmt.Sprintf("%s=http://localhost:2380,%s=http://localhost:7001", n, n) 335} 336 337func (cfg config) isNewCluster() bool { return cfg.clusterState.String() == clusterStateFlagNew } 338func (cfg config) isProxy() bool { return cfg.proxy.String() != proxyFlagOff } 339func (cfg config) isReadonlyProxy() bool { return cfg.proxy.String() == proxyFlagReadonly } 340func (cfg config) shouldFallbackToProxy() bool { return cfg.fallback.String() == fallbackFlagProxy } 341 342func (cfg config) electionTicks() int { return int(cfg.ElectionMs / cfg.TickMs) } 343