config.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package etcdserver
  14. import (
  15. "fmt"
  16. "net/http"
  17. "path"
  18. "github.com/coreos/etcd/pkg/types"
  19. "github.com/coreos/etcd/raft"
  20. )
  21. // ServerConfig holds the configuration of etcd as taken from the command line or discovery.
  22. type ServerConfig struct {
  23. NodeID uint64
  24. Name string
  25. DiscoveryURL string
  26. ClientURLs types.URLs
  27. DataDir string
  28. SnapCount uint64
  29. Cluster *Cluster
  30. ClusterState ClusterState
  31. Transport *http.Transport
  32. }
  33. // VerifyBootstrapConfig sanity-checks the initial config and returns an error
  34. // for things that should never happen.
  35. func (c *ServerConfig) VerifyBootstrapConfig() error {
  36. if c.NodeID == raft.None {
  37. return fmt.Errorf("could not use %x as member id", raft.None)
  38. }
  39. if c.DiscoveryURL == "" && c.ClusterState != ClusterStateValueNew {
  40. return fmt.Errorf("initial cluster state unset and no wal or discovery URL found")
  41. }
  42. // Make sure the cluster at least contains the local server.
  43. m := c.Cluster.FindID(c.NodeID)
  44. if m == nil {
  45. return fmt.Errorf("couldn't find local ID in cluster config")
  46. }
  47. // No identical IPs in the cluster peer list
  48. urlMap := make(map[string]bool)
  49. for _, m := range c.Cluster.Members() {
  50. for _, url := range m.PeerURLs {
  51. if urlMap[url] {
  52. return fmt.Errorf("duplicate url %v in cluster config", url)
  53. }
  54. urlMap[url] = true
  55. }
  56. }
  57. return nil
  58. }
  59. func (c *ServerConfig) WALDir() string { return path.Join(c.DataDir, "wal") }
  60. func (c *ServerConfig) SnapDir() string { return path.Join(c.DataDir, "snap") }
  61. func (c *ServerConfig) ShouldDiscover() bool {
  62. return c.DiscoveryURL != ""
  63. }