etcd.go 7.1 KB

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