etcd.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. Copyright 2013 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 main
  14. import (
  15. "flag"
  16. "fmt"
  17. "io/ioutil"
  18. "os"
  19. "os/signal"
  20. "runtime/pprof"
  21. "github.com/coreos/etcd/log"
  22. "github.com/coreos/etcd/server"
  23. "github.com/coreos/etcd/store"
  24. "github.com/coreos/go-raft"
  25. )
  26. func main() {
  27. parseFlags()
  28. // Load configuration.
  29. var config = server.NewConfig()
  30. if err := config.Load(os.Args[1:]); err != nil {
  31. log.Fatal("Configuration error:", err)
  32. }
  33. // Turn on logging.
  34. if config.VeryVerbose {
  35. log.Verbose = true
  36. raft.SetLogLevel(raft.Debug)
  37. } else if config.Verbose {
  38. log.Verbose = true
  39. }
  40. // Create data directory if it doesn't already exist.
  41. if err := os.MkdirAll(config.DataDir, 0744); err != nil {
  42. log.Fatalf("Unable to create path: %s", err)
  43. }
  44. // Load info object.
  45. info, err := config.Info()
  46. if err != nil {
  47. log.Fatal("info:", err)
  48. }
  49. if info.Name == "" {
  50. host, err := os.Hostname()
  51. if err != nil || host == "" {
  52. log.Fatal("Machine name required and hostname not set. e.g. '-n=machine_name'")
  53. }
  54. log.Warnf("Using hostname %s as the machine name. You must ensure this name is unique among etcd machines.", host)
  55. info.Name = host
  56. }
  57. // Retrieve TLS configuration.
  58. tlsConfig, err := info.EtcdTLS.Config()
  59. if err != nil {
  60. log.Fatal("Client TLS:", err)
  61. }
  62. peerTLSConfig, err := info.RaftTLS.Config()
  63. if err != nil {
  64. log.Fatal("Peer TLS:", err)
  65. }
  66. // Create etcd key-value store and registry.
  67. store := store.New()
  68. registry := server.NewRegistry(store)
  69. // Create peer server.
  70. ps := server.NewPeerServer(info.Name, config.DataDir, info.RaftURL, info.RaftListenHost, &peerTLSConfig, &info.RaftTLS, registry, store, config.SnapCount)
  71. ps.MaxClusterSize = config.MaxClusterSize
  72. ps.RetryTimes = config.MaxRetryAttempts
  73. // Create client server.
  74. s := server.New(info.Name, info.EtcdURL, info.EtcdListenHost, &tlsConfig, &info.EtcdTLS, ps, registry, store)
  75. if err := s.AllowOrigins(config.Cors); err != nil {
  76. panic(err)
  77. }
  78. ps.SetServer(s)
  79. // Run peer server in separate thread while the client server blocks.
  80. go func() {
  81. log.Fatal(ps.ListenAndServe(config.Snapshot, config.Machines))
  82. }()
  83. log.Fatal(s.ListenAndServe())
  84. }
  85. // Parses non-configuration flags.
  86. func parseFlags() {
  87. var versionFlag bool
  88. var cpuprofile string
  89. f := flag.NewFlagSet(os.Args[0], -1)
  90. f.SetOutput(ioutil.Discard)
  91. f.BoolVar(&versionFlag, "version", false, "print the version and exit")
  92. f.StringVar(&cpuprofile, "cpuprofile", "", "write cpu profile to file")
  93. f.Parse(os.Args[1:])
  94. // Print version if necessary.
  95. if versionFlag {
  96. fmt.Println(server.ReleaseVersion)
  97. os.Exit(0)
  98. }
  99. // Begin CPU profiling if specified.
  100. if cpuprofile != "" {
  101. f, err := os.Create(cpuprofile)
  102. if err != nil {
  103. log.Fatal(err)
  104. }
  105. pprof.StartCPUProfile(f)
  106. c := make(chan os.Signal, 1)
  107. signal.Notify(c, os.Interrupt)
  108. go func() {
  109. sig := <-c
  110. log.Infof("captured %v, stopping profiler and exiting..", sig)
  111. pprof.StopCPUProfile()
  112. os.Exit(1)
  113. }()
  114. }
  115. }