etcd.go 6.6 KB

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