etcd.go 6.5 KB

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