etcd.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "strings"
  8. "github.com/coreos/etcd/log"
  9. "github.com/coreos/etcd/server"
  10. "github.com/coreos/etcd/store"
  11. "github.com/coreos/go-raft"
  12. )
  13. //------------------------------------------------------------------------------
  14. //
  15. // Initialization
  16. //
  17. //------------------------------------------------------------------------------
  18. var (
  19. veryVerbose bool
  20. machines string
  21. machinesFile string
  22. cluster []string
  23. argInfo Info
  24. dirPath string
  25. force bool
  26. printVersion bool
  27. maxSize int
  28. snapshot bool
  29. retryTimes int
  30. maxClusterSize int
  31. cpuprofile string
  32. cors string
  33. )
  34. func init() {
  35. flag.BoolVar(&printVersion, "version", false, "print the version and exit")
  36. flag.BoolVar(&log.Verbose, "v", false, "verbose logging")
  37. flag.BoolVar(&veryVerbose, "vv", false, "very verbose logging")
  38. flag.StringVar(&machines, "C", "", "the ip address and port of a existing machines in the cluster, sepearate by comma")
  39. flag.StringVar(&machinesFile, "CF", "", "the file contains a list of existing machines in the cluster, seperate by comma")
  40. flag.StringVar(&argInfo.Name, "n", "default-name", "the node name (required)")
  41. flag.StringVar(&argInfo.EtcdURL, "c", "127.0.0.1:4001", "the advertised public hostname:port for etcd client communication")
  42. flag.StringVar(&argInfo.RaftURL, "s", "127.0.0.1:7001", "the advertised public hostname:port for raft server communication")
  43. flag.StringVar(&argInfo.EtcdListenHost, "cl", "", "the listening hostname for etcd client communication (defaults to advertised ip)")
  44. flag.StringVar(&argInfo.RaftListenHost, "sl", "", "the listening hostname for raft server communication (defaults to advertised ip)")
  45. flag.StringVar(&argInfo.WebURL, "w", "", "the hostname:port of web interface")
  46. flag.StringVar(&argInfo.RaftTLS.CAFile, "serverCAFile", "", "the path of the CAFile")
  47. flag.StringVar(&argInfo.RaftTLS.CertFile, "serverCert", "", "the cert file of the server")
  48. flag.StringVar(&argInfo.RaftTLS.KeyFile, "serverKey", "", "the key file of the server")
  49. flag.StringVar(&argInfo.EtcdTLS.CAFile, "clientCAFile", "", "the path of the client CAFile")
  50. flag.StringVar(&argInfo.EtcdTLS.CertFile, "clientCert", "", "the cert file of the client")
  51. flag.StringVar(&argInfo.EtcdTLS.KeyFile, "clientKey", "", "the key file of the client")
  52. flag.StringVar(&dirPath, "d", ".", "the directory to store log and snapshot")
  53. flag.BoolVar(&force, "f", false, "force new node configuration if existing is found (WARNING: data loss!)")
  54. flag.BoolVar(&snapshot, "snapshot", false, "open or close snapshot")
  55. flag.IntVar(&maxSize, "m", 1024, "the max size of result buffer")
  56. flag.IntVar(&retryTimes, "r", 3, "the max retry attempts when trying to join a cluster")
  57. flag.IntVar(&maxClusterSize, "maxsize", 9, "the max size of the cluster")
  58. flag.StringVar(&cpuprofile, "cpuprofile", "", "write cpu profile to file")
  59. flag.StringVar(&cors, "cors", "", "whitelist origins for cross-origin resource sharing (e.g. '*' or 'http://localhost:8001,etc')")
  60. }
  61. //------------------------------------------------------------------------------
  62. //
  63. // Typedefs
  64. //
  65. //------------------------------------------------------------------------------
  66. type Info struct {
  67. Name string `json:"name"`
  68. RaftURL string `json:"raftURL"`
  69. EtcdURL string `json:"etcdURL"`
  70. WebURL string `json:"webURL"`
  71. RaftListenHost string `json:"raftListenHost"`
  72. EtcdListenHost string `json:"etcdListenHost"`
  73. RaftTLS server.TLSInfo `json:"raftTLS"`
  74. EtcdTLS server.TLSInfo `json:"etcdTLS"`
  75. }
  76. //------------------------------------------------------------------------------
  77. //
  78. // Functions
  79. //
  80. //------------------------------------------------------------------------------
  81. //--------------------------------------
  82. // Main
  83. //--------------------------------------
  84. func main() {
  85. flag.Parse()
  86. if printVersion {
  87. fmt.Println(server.ReleaseVersion)
  88. os.Exit(0)
  89. }
  90. if cpuprofile != "" {
  91. runCPUProfile()
  92. }
  93. if veryVerbose {
  94. log.Verbose = true
  95. raft.SetLogLevel(raft.Debug)
  96. }
  97. if machines != "" {
  98. cluster = strings.Split(machines, ",")
  99. } else if machinesFile != "" {
  100. b, err := ioutil.ReadFile(machinesFile)
  101. if err != nil {
  102. log.Fatalf("Unable to read the given machines file: %s", err)
  103. }
  104. cluster = strings.Split(string(b), ",")
  105. }
  106. // Check TLS arguments
  107. raftTLSConfig, ok := tlsConfigFromInfo(argInfo.RaftTLS)
  108. if !ok {
  109. log.Fatal("Please specify cert and key file or cert and key file and CAFile or none of the three")
  110. }
  111. etcdTLSConfig, ok := tlsConfigFromInfo(argInfo.EtcdTLS)
  112. if !ok {
  113. log.Fatal("Please specify cert and key file or cert and key file and CAFile or none of the three")
  114. }
  115. argInfo.Name = strings.TrimSpace(argInfo.Name)
  116. if argInfo.Name == "" {
  117. log.Fatal("ERROR: server name required. e.g. '-n=server_name'")
  118. }
  119. // Check host name arguments
  120. argInfo.RaftURL = sanitizeURL(argInfo.RaftURL, raftTLSConfig.Scheme)
  121. argInfo.EtcdURL = sanitizeURL(argInfo.EtcdURL, etcdTLSConfig.Scheme)
  122. argInfo.WebURL = sanitizeURL(argInfo.WebURL, "http")
  123. argInfo.RaftListenHost = sanitizeListenHost(argInfo.RaftListenHost, argInfo.RaftURL)
  124. argInfo.EtcdListenHost = sanitizeListenHost(argInfo.EtcdListenHost, argInfo.EtcdURL)
  125. // Read server info from file or grab it from user.
  126. if err := os.MkdirAll(dirPath, 0744); err != nil {
  127. log.Fatalf("Unable to create path: %s", err)
  128. }
  129. info := getInfo(dirPath)
  130. // Create etcd key-value store
  131. store := store.New()
  132. // Create a shared node registry.
  133. registry := server.NewRegistry(store)
  134. // Create peer server.
  135. ps := server.NewPeerServer(info.Name, dirPath, info.RaftURL, info.RaftListenHost, &raftTLSConfig, &info.RaftTLS, registry, store)
  136. ps.MaxClusterSize = maxClusterSize
  137. ps.RetryTimes = retryTimes
  138. s := server.New(info.Name, info.EtcdURL, info.EtcdListenHost, &etcdTLSConfig, &info.EtcdTLS, ps, registry, store)
  139. if err := s.AllowOrigins(cors); err != nil {
  140. panic(err)
  141. }
  142. ps.SetServer(s)
  143. ps.ListenAndServe(snapshot, cluster)
  144. s.ListenAndServe()
  145. }