config.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. Name string
  24. DiscoveryURL string
  25. ClientURLs types.URLs
  26. DataDir string
  27. SnapCount uint64
  28. Cluster *Cluster
  29. ClusterState ClusterState
  30. Transport *http.Transport
  31. }
  32. // VerifyBootstrapConfig sanity-checks the initial config and returns an error
  33. // for things that should never happen.
  34. func (c *ServerConfig) VerifyBootstrapConfig() error {
  35. m := c.Cluster.MemberByName(c.Name)
  36. // Make sure the cluster at least contains the local server.
  37. if m == nil {
  38. return fmt.Errorf("couldn't find local name %s in the initial cluster configuration", c.Name)
  39. }
  40. if m.ID == raft.None {
  41. return fmt.Errorf("cannot use %x as member id", raft.None)
  42. }
  43. if c.DiscoveryURL == "" && c.ClusterState != ClusterStateValueNew {
  44. return fmt.Errorf("initial cluster state unset and no wal or discovery URL found")
  45. }
  46. // No identical IPs in the cluster peer list
  47. urlMap := make(map[string]bool)
  48. for _, m := range c.Cluster.Members() {
  49. for _, url := range m.PeerURLs {
  50. if urlMap[url] {
  51. return fmt.Errorf("duplicate url %v in cluster config", url)
  52. }
  53. urlMap[url] = true
  54. }
  55. }
  56. return nil
  57. }
  58. func (c *ServerConfig) WALDir() string { return path.Join(c.DataDir, "wal") }
  59. func (c *ServerConfig) SnapDir() string { return path.Join(c.DataDir, "snap") }
  60. func (c *ServerConfig) ShouldDiscover() bool {
  61. return c.DiscoveryURL != ""
  62. }