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