etcd.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package main
  2. import (
  3. "crypto/tls"
  4. "flag"
  5. "github.com/coreos/etcd/store"
  6. "github.com/coreos/go-raft"
  7. "io/ioutil"
  8. "net/http"
  9. "net/url"
  10. "os"
  11. "strings"
  12. "time"
  13. )
  14. //------------------------------------------------------------------------------
  15. //
  16. // Initialization
  17. //
  18. //------------------------------------------------------------------------------
  19. var verbose bool
  20. var veryVerbose bool
  21. var machines string
  22. var machinesFile string
  23. var cluster []string
  24. var argInfo Info
  25. var dirPath string
  26. var force bool
  27. var maxSize int
  28. var snapshot bool
  29. var retryTimes int
  30. var maxClusterSize int
  31. var cpuprofile string
  32. func init() {
  33. flag.BoolVar(&verbose, "v", false, "verbose logging")
  34. flag.BoolVar(&veryVerbose, "vv", false, "very verbose logging")
  35. flag.StringVar(&machines, "C", "", "the ip address and port of a existing machines in the cluster, sepearate by comma")
  36. flag.StringVar(&machinesFile, "CF", "", "the file contains a list of existing machines in the cluster, seperate by comma")
  37. flag.StringVar(&argInfo.Name, "n", "default-name", "the node name (required)")
  38. flag.StringVar(&argInfo.EtcdURL, "c", "127.0.0.1:4001", "the hostname:port for etcd client communication")
  39. flag.StringVar(&argInfo.RaftURL, "s", "127.0.0.1:7001", "the hostname:port for raft server communication")
  40. flag.StringVar(&argInfo.WebURL, "w", "", "the hostname:port of web interface")
  41. flag.StringVar(&argInfo.RaftTLS.CAFile, "serverCAFile", "", "the path of the CAFile")
  42. flag.StringVar(&argInfo.RaftTLS.CertFile, "serverCert", "", "the cert file of the server")
  43. flag.StringVar(&argInfo.RaftTLS.KeyFile, "serverKey", "", "the key file of the server")
  44. flag.StringVar(&argInfo.EtcdTLS.CAFile, "clientCAFile", "", "the path of the client CAFile")
  45. flag.StringVar(&argInfo.EtcdTLS.CertFile, "clientCert", "", "the cert file of the client")
  46. flag.StringVar(&argInfo.EtcdTLS.KeyFile, "clientKey", "", "the key file of the client")
  47. flag.StringVar(&dirPath, "d", ".", "the directory to store log and snapshot")
  48. flag.BoolVar(&force, "f", false, "force new node configuration if existing is found (WARNING: data loss!)")
  49. flag.BoolVar(&snapshot, "snapshot", false, "open or close snapshot")
  50. flag.IntVar(&maxSize, "m", 1024, "the max size of result buffer")
  51. flag.IntVar(&retryTimes, "r", 3, "the max retry attempts when trying to join a cluster")
  52. flag.IntVar(&maxClusterSize, "maxsize", 9, "the max size of the cluster")
  53. flag.StringVar(&cpuprofile, "cpuprofile", "", "write cpu profile to file")
  54. }
  55. const (
  56. ElectionTimeout = 200 * time.Millisecond
  57. HeartbeatTimeout = 50 * time.Millisecond
  58. // Timeout for internal raft http connection
  59. // The original timeout for http is 45 seconds
  60. // which is too long for our usage.
  61. HTTPTimeout = 10 * time.Second
  62. RetryInterval = 10
  63. )
  64. //------------------------------------------------------------------------------
  65. //
  66. // Typedefs
  67. //
  68. //------------------------------------------------------------------------------
  69. type TLSInfo struct {
  70. CertFile string `json:"CertFile"`
  71. KeyFile string `json:"KeyFile"`
  72. CAFile string `json:"CAFile"`
  73. }
  74. type Info struct {
  75. Name string `json:"name"`
  76. RaftURL string `json:"raftURL"`
  77. EtcdURL string `json:"etcdURL"`
  78. WebURL string `json:"webURL"`
  79. RaftTLS TLSInfo `json:"raftTLS"`
  80. EtcdTLS TLSInfo `json:"etcdTLS"`
  81. }
  82. type TLSConfig struct {
  83. Scheme string
  84. Server tls.Config
  85. Client tls.Config
  86. }
  87. //------------------------------------------------------------------------------
  88. //
  89. // Variables
  90. //
  91. //------------------------------------------------------------------------------
  92. var etcdStore *store.Store
  93. var info *Info
  94. //------------------------------------------------------------------------------
  95. //
  96. // Functions
  97. //
  98. //------------------------------------------------------------------------------
  99. //--------------------------------------
  100. // Main
  101. //--------------------------------------
  102. func main() {
  103. flag.Parse()
  104. if cpuprofile != "" {
  105. runCPUProfile()
  106. }
  107. if veryVerbose {
  108. verbose = true
  109. raft.SetLogLevel(raft.Debug)
  110. }
  111. if machines != "" {
  112. cluster = strings.Split(machines, ",")
  113. } else if machinesFile != "" {
  114. b, err := ioutil.ReadFile(machinesFile)
  115. if err != nil {
  116. fatalf("Unable to read the given machines file: %s", err)
  117. }
  118. cluster = strings.Split(string(b), ",")
  119. }
  120. // Check TLS arguments
  121. raftTLSConfig, ok := tlsConfigFromInfo(argInfo.RaftTLS)
  122. if !ok {
  123. fatal("Please specify cert and key file or cert and key file and CAFile or none of the three")
  124. }
  125. etcdTLSConfig, ok := tlsConfigFromInfo(argInfo.EtcdTLS)
  126. if !ok {
  127. fatal("Please specify cert and key file or cert and key file and CAFile or none of the three")
  128. }
  129. argInfo.Name = strings.TrimSpace(argInfo.Name)
  130. if argInfo.Name == "" {
  131. fatal("ERROR: server name required. e.g. '-n=server_name'")
  132. }
  133. // Check host name arguments
  134. argInfo.RaftURL = sanitizeURL(argInfo.RaftURL, raftTLSConfig.Scheme)
  135. argInfo.EtcdURL = sanitizeURL(argInfo.EtcdURL, etcdTLSConfig.Scheme)
  136. argInfo.WebURL = sanitizeURL(argInfo.WebURL, "http")
  137. // Read server info from file or grab it from user.
  138. if err := os.MkdirAll(dirPath, 0744); err != nil {
  139. fatalf("Unable to create path: %s", err)
  140. }
  141. info = getInfo(dirPath)
  142. // Create etcd key-value store
  143. etcdStore = store.CreateStore(maxSize)
  144. snapConf = newSnapshotConf()
  145. startWebInterface()
  146. startRaft(raftTLSConfig)
  147. startEtcd(etcdTLSConfig)
  148. }
  149. // Start to listen and response etcd client command
  150. func startEtcd(tlsConf TLSConfig) {
  151. u, err := url.Parse(info.EtcdURL)
  152. if err != nil {
  153. fatalf("invalid url '%s': %s", info.EtcdURL, err)
  154. }
  155. infof("etcd server [%s:%s]", info.Name, u)
  156. server := http.Server{
  157. Handler: NewEtcdMuxer(),
  158. TLSConfig: &tlsConf.Server,
  159. Addr: u.Host,
  160. }
  161. if tlsConf.Scheme == "http" {
  162. fatal(server.ListenAndServe())
  163. } else {
  164. fatal(server.ListenAndServeTLS(info.EtcdTLS.CertFile, info.EtcdTLS.KeyFile))
  165. }
  166. }