config.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. "reflect"
  19. "sort"
  20. "github.com/coreos/etcd/pkg/types"
  21. "github.com/coreos/etcd/raft"
  22. )
  23. // ServerConfig holds the configuration of etcd as taken from the command line or discovery.
  24. type ServerConfig struct {
  25. Name string
  26. DiscoveryURL string
  27. DiscoveryProxy string
  28. ClientURLs types.URLs
  29. PeerURLs types.URLs
  30. DataDir string
  31. SnapCount uint64
  32. Cluster *Cluster
  33. NewCluster bool
  34. ForceNewCluster bool
  35. Transport *http.Transport
  36. }
  37. // VerifyBootstrapConfig sanity-checks the initial config and returns an error
  38. // for things that should never happen.
  39. func (c *ServerConfig) VerifyBootstrapConfig() error {
  40. m := c.Cluster.MemberByName(c.Name)
  41. // Make sure the cluster at least contains the local server.
  42. if m == nil {
  43. return fmt.Errorf("couldn't find local name %q in the initial cluster configuration", c.Name)
  44. }
  45. if uint64(m.ID) == raft.None {
  46. return fmt.Errorf("cannot use %x as member id", raft.None)
  47. }
  48. if c.DiscoveryURL == "" && !c.NewCluster {
  49. return fmt.Errorf("initial cluster state unset and no wal or discovery URL found")
  50. }
  51. // No identical IPs in the cluster peer list
  52. urlMap := make(map[string]bool)
  53. for _, m := range c.Cluster.Members() {
  54. for _, url := range m.PeerURLs {
  55. if urlMap[url] {
  56. return fmt.Errorf("duplicate url %v in cluster config", url)
  57. }
  58. urlMap[url] = true
  59. }
  60. }
  61. // Advertised peer URLs must match those in the cluster peer list
  62. apurls := c.PeerURLs.StringSlice()
  63. sort.Strings(apurls)
  64. if !reflect.DeepEqual(apurls, m.PeerURLs) {
  65. return fmt.Errorf("%s has different advertised URLs in the cluster and advertised peer URLs list", c.Name)
  66. }
  67. return nil
  68. }
  69. func (c *ServerConfig) WALDir() string { return path.Join(c.DataDir, "wal") }
  70. func (c *ServerConfig) SnapDir() string { return path.Join(c.DataDir, "snap") }
  71. func (c *ServerConfig) ShouldDiscover() bool {
  72. return c.DiscoveryURL != ""
  73. }