etcd.go 6.9 KB

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