etcd.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. package main
  2. import (
  3. "bytes"
  4. "crypto/tls"
  5. "crypto/x509"
  6. "encoding/json"
  7. "encoding/pem"
  8. "flag"
  9. "fmt"
  10. "github.com/coreos/etcd/store"
  11. "github.com/coreos/etcd/web"
  12. "github.com/coreos/go-raft"
  13. "io/ioutil"
  14. "log"
  15. "net/http"
  16. "os"
  17. "strings"
  18. "time"
  19. )
  20. //------------------------------------------------------------------------------
  21. //
  22. // Initialization
  23. //
  24. //------------------------------------------------------------------------------
  25. var verbose bool
  26. var cluster string
  27. var address string
  28. var clientPort int
  29. var serverPort int
  30. var webPort int
  31. var serverCertFile string
  32. var serverKeyFile string
  33. var serverCAFile string
  34. var clientCertFile string
  35. var clientKeyFile string
  36. var clientCAFile string
  37. var dirPath string
  38. var ignore bool
  39. var maxSize int
  40. func init() {
  41. flag.BoolVar(&verbose, "v", false, "verbose logging")
  42. flag.StringVar(&cluster, "C", "", "the ip address and port of a existing cluster")
  43. flag.StringVar(&address, "a", "0.0.0.0", "the ip address of the local machine")
  44. flag.IntVar(&clientPort, "c", 4001, "the port to communicate with clients")
  45. flag.IntVar(&serverPort, "s", 7001, "the port to communicate with servers")
  46. flag.IntVar(&webPort, "w", -1, "the port of web interface")
  47. flag.StringVar(&serverCAFile, "serverCAFile", "", "the path of the CAFile")
  48. flag.StringVar(&serverCertFile, "serverCert", "", "the cert file of the server")
  49. flag.StringVar(&serverKeyFile, "serverKey", "", "the key file of the server")
  50. flag.StringVar(&clientCAFile, "clientCAFile", "", "the path of the client CAFile")
  51. flag.StringVar(&clientCertFile, "clientCert", "", "the cert file of the client")
  52. flag.StringVar(&clientKeyFile, "clientKey", "", "the key file of the client")
  53. flag.StringVar(&dirPath, "d", "/tmp/", "the directory to store log and snapshot")
  54. flag.BoolVar(&ignore, "i", false, "ignore the old configuration, create a new node")
  55. flag.IntVar(&maxSize, "m", 1024, "the max size of result buffer")
  56. }
  57. // CONSTANTS
  58. const (
  59. HTTP = iota
  60. HTTPS
  61. HTTPSANDVERIFY
  62. )
  63. const (
  64. SERVER = iota
  65. CLIENT
  66. )
  67. const (
  68. ELECTIONTIMTOUT = 200 * time.Millisecond
  69. HEARTBEATTIMEOUT = 50 * time.Millisecond
  70. )
  71. //------------------------------------------------------------------------------
  72. //
  73. // Typedefs
  74. //
  75. //------------------------------------------------------------------------------
  76. type Info struct {
  77. Address string `json:"address"`
  78. ServerPort int `json:"serverPort"`
  79. ClientPort int `json:"clientPort"`
  80. WebPort int `json:"webPort"`
  81. ServerCertFile string `json:"serverCertFile"`
  82. ServerKeyFile string `json:"serverKeyFile"`
  83. ServerCAFile string `json:"serverCAFile"`
  84. ClientCertFile string `json:"clientCertFile"`
  85. ClientKeyFile string `json:"clientKeyFile"`
  86. ClientCAFile string `json:"clientCAFile"`
  87. }
  88. //------------------------------------------------------------------------------
  89. //
  90. // Variables
  91. //
  92. //------------------------------------------------------------------------------
  93. var raftServer *raft.Server
  94. var raftTransporter transporter
  95. var etcdStore *store.Store
  96. var info *Info
  97. //------------------------------------------------------------------------------
  98. //
  99. // Functions
  100. //
  101. //------------------------------------------------------------------------------
  102. //--------------------------------------
  103. // Main
  104. //--------------------------------------
  105. func main() {
  106. flag.Parse()
  107. // Setup commands.
  108. registerCommands()
  109. // Read server info from file or grab it from user.
  110. if err := os.MkdirAll(dirPath, 0744); err != nil {
  111. fatal("Unable to create path: %v", err)
  112. }
  113. info = getInfo(dirPath)
  114. // secrity type
  115. st := securityType(SERVER)
  116. clientSt := securityType(CLIENT)
  117. if st == -1 || clientSt == -1 {
  118. fatal("Please specify cert and key file or cert and key file and CAFile or none of the three")
  119. }
  120. // Create etcd key-value store
  121. etcdStore = store.CreateStore(maxSize)
  122. startRaft(st)
  123. if webPort != -1 {
  124. // start web
  125. etcdStore.SetMessager(&storeMsg)
  126. go webHelper()
  127. go web.Start(raftServer, webPort)
  128. }
  129. startClientTransport(info.ClientPort, clientSt)
  130. }
  131. // Start the raft server
  132. func startRaft(securityType int) {
  133. var err error
  134. raftName := fmt.Sprintf("%s:%d", info.Address, info.ServerPort)
  135. // Create transporter for raft
  136. raftTransporter = createTransporter(securityType)
  137. // Create raft server
  138. raftServer, err = raft.NewServer(raftName, dirPath, raftTransporter, etcdStore, nil)
  139. if err != nil {
  140. fmt.Println(err)
  141. os.Exit(1)
  142. }
  143. // LoadSnapshot
  144. // err = raftServer.LoadSnapshot()
  145. // if err == nil {
  146. // debug("%s finished load snapshot", raftServer.Name())
  147. // } else {
  148. // debug(err)
  149. // }
  150. raftServer.Initialize()
  151. raftServer.SetElectionTimeout(ELECTIONTIMTOUT)
  152. raftServer.SetHeartbeatTimeout(HEARTBEATTIMEOUT)
  153. if raftServer.IsLogEmpty() {
  154. // start as a leader in a new cluster
  155. if cluster == "" {
  156. raftServer.StartLeader()
  157. time.Sleep(time.Millisecond * 20)
  158. // leader need to join self as a peer
  159. for {
  160. command := &JoinCommand{}
  161. command.Name = raftServer.Name()
  162. _, err := raftServer.Do(command)
  163. if err == nil {
  164. break
  165. }
  166. }
  167. debug("%s start as a leader", raftServer.Name())
  168. // start as a follower in a existing cluster
  169. } else {
  170. raftServer.StartFollower()
  171. err := joinCluster(raftServer, cluster)
  172. if err != nil {
  173. fatal(fmt.Sprintln(err))
  174. }
  175. debug("%s success join to the cluster", raftServer.Name())
  176. }
  177. } else {
  178. // rejoin the previous cluster
  179. raftServer.StartFollower()
  180. debug("%s restart as a follower", raftServer.Name())
  181. }
  182. // open the snapshot
  183. // go server.Snapshot()
  184. // start to response to raft requests
  185. go startRaftTransport(info.ServerPort, securityType)
  186. }
  187. // Create transporter using by raft server
  188. // Create http or https transporter based on
  189. // wether the user give the server cert and key
  190. func createTransporter(st int) transporter {
  191. t := transporter{}
  192. switch st {
  193. case HTTP:
  194. t.client = nil
  195. return t
  196. case HTTPS:
  197. fallthrough
  198. case HTTPSANDVERIFY:
  199. tlsCert, err := tls.LoadX509KeyPair(serverCertFile, serverKeyFile)
  200. if err != nil {
  201. fatal(fmt.Sprintln(err))
  202. }
  203. tr := &http.Transport{
  204. TLSClientConfig: &tls.Config{
  205. Certificates: []tls.Certificate{tlsCert},
  206. InsecureSkipVerify: true,
  207. },
  208. DisableCompression: true,
  209. }
  210. t.client = &http.Client{Transport: tr}
  211. return t
  212. }
  213. // for complier
  214. return transporter{}
  215. }
  216. // Start to listen and response raft command
  217. func startRaftTransport(port int, st int) {
  218. // internal commands
  219. http.HandleFunc("/join", JoinHttpHandler)
  220. http.HandleFunc("/vote", VoteHttpHandler)
  221. http.HandleFunc("/log", GetLogHttpHandler)
  222. http.HandleFunc("/log/append", AppendEntriesHttpHandler)
  223. http.HandleFunc("/snapshot", SnapshotHttpHandler)
  224. http.HandleFunc("/client", ClientHttpHandler)
  225. switch st {
  226. case HTTP:
  227. fmt.Printf("raft server [%s] listen on http port %v\n", address, port)
  228. log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
  229. case HTTPS:
  230. fmt.Printf("raft server [%s] listen on https port %v\n", address, port)
  231. log.Fatal(http.ListenAndServeTLS(fmt.Sprintf(":%d", port), serverCertFile, serverKeyFile, nil))
  232. case HTTPSANDVERIFY:
  233. server := &http.Server{
  234. TLSConfig: &tls.Config{
  235. ClientAuth: tls.RequireAndVerifyClientCert,
  236. ClientCAs: createCertPool(serverCAFile),
  237. },
  238. Addr: fmt.Sprintf(":%d", port),
  239. }
  240. fmt.Printf("raft server [%s] listen on https port %v\n", address, port)
  241. err := server.ListenAndServeTLS(serverCertFile, serverKeyFile)
  242. if err != nil {
  243. log.Fatal(err)
  244. }
  245. }
  246. }
  247. // Start to listen and response client command
  248. func startClientTransport(port int, st int) {
  249. // external commands
  250. http.HandleFunc("/"+version+"/keys/", Multiplexer)
  251. http.HandleFunc("/"+version+"/watch/", WatchHttpHandler)
  252. http.HandleFunc("/"+version+"/list/", ListHttpHandler)
  253. http.HandleFunc("/"+version+"/testAndSet/", TestAndSetHttpHandler)
  254. http.HandleFunc("/leader", LeaderHttpHandler)
  255. switch st {
  256. case HTTP:
  257. fmt.Printf("etcd [%s] listen on http port %v\n", address, clientPort)
  258. log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
  259. case HTTPS:
  260. fmt.Printf("etcd [%s] listen on https port %v\n", address, clientPort)
  261. http.ListenAndServeTLS(fmt.Sprintf(":%d", port), clientCertFile, clientKeyFile, nil)
  262. case HTTPSANDVERIFY:
  263. server := &http.Server{
  264. TLSConfig: &tls.Config{
  265. ClientAuth: tls.RequireAndVerifyClientCert,
  266. ClientCAs: createCertPool(clientCAFile),
  267. },
  268. Addr: fmt.Sprintf(":%d", port),
  269. }
  270. fmt.Printf("etcd [%s] listen on https port %v\n", address, clientPort)
  271. err := server.ListenAndServeTLS(clientCertFile, clientKeyFile)
  272. if err != nil {
  273. log.Fatal(err)
  274. os.Exit(1)
  275. }
  276. }
  277. }
  278. //--------------------------------------
  279. // Config
  280. //--------------------------------------
  281. // Get the security type
  282. func securityType(source int) int {
  283. var keyFile, certFile, CAFile string
  284. switch source {
  285. case SERVER:
  286. keyFile = info.ServerKeyFile
  287. certFile = info.ServerCertFile
  288. CAFile = info.ServerCAFile
  289. case CLIENT:
  290. keyFile = info.ClientKeyFile
  291. certFile = info.ClientCertFile
  292. CAFile = info.ClientCAFile
  293. }
  294. // If the user do not specify key file, cert file and
  295. // CA file, the type will be HTTP
  296. if keyFile == "" && certFile == "" && CAFile == "" {
  297. return HTTP
  298. }
  299. if keyFile != "" && certFile != "" {
  300. if CAFile != "" {
  301. // If the user specify all the three file, the type
  302. // will be HTTPS with client cert auth
  303. return HTTPSANDVERIFY
  304. }
  305. // If the user specify key file and cert file but not
  306. // CA file, the type will be HTTPS without client cert
  307. // auth
  308. return HTTPS
  309. }
  310. // bad specification
  311. return -1
  312. }
  313. // Get the server info from previous conf file
  314. // or from the user
  315. func getInfo(path string) *Info {
  316. info := &Info{}
  317. // Read in the server info if available.
  318. infoPath := fmt.Sprintf("%s/info", path)
  319. // Delete the old configuration if exist
  320. if ignore {
  321. logPath := fmt.Sprintf("%s/log", path)
  322. snapshotPath := fmt.Sprintf("%s/snapshotPath", path)
  323. os.Remove(infoPath)
  324. os.Remove(logPath)
  325. os.RemoveAll(snapshotPath)
  326. }
  327. if file, err := os.Open(infoPath); err == nil {
  328. if content, err := ioutil.ReadAll(file); err != nil {
  329. fatal("Unable to read info: %v", err)
  330. } else {
  331. if err = json.Unmarshal(content, &info); err != nil {
  332. fatal("Unable to parse info: %v", err)
  333. }
  334. }
  335. file.Close()
  336. } else {
  337. // Otherwise ask user for info and write it to file.
  338. if address == "" {
  339. fatal("Please give the address of the local machine")
  340. }
  341. info.Address = address
  342. info.Address = strings.TrimSpace(info.Address)
  343. fmt.Println("address ", info.Address)
  344. info.ServerPort = serverPort
  345. info.ClientPort = clientPort
  346. info.WebPort = webPort
  347. info.ClientCAFile = clientCAFile
  348. info.ClientCertFile = clientCertFile
  349. info.ClientKeyFile = clientKeyFile
  350. info.ServerCAFile = serverCAFile
  351. info.ServerKeyFile = serverKeyFile
  352. info.ServerCertFile = serverCertFile
  353. // Write to file.
  354. content, _ := json.Marshal(info)
  355. content = []byte(string(content) + "\n")
  356. if err := ioutil.WriteFile(infoPath, content, 0644); err != nil {
  357. fatal("Unable to write info to file: %v", err)
  358. }
  359. }
  360. return info
  361. }
  362. // Create client auth certpool
  363. func createCertPool(CAFile string) *x509.CertPool {
  364. pemByte, _ := ioutil.ReadFile(CAFile)
  365. block, pemByte := pem.Decode(pemByte)
  366. cert, err := x509.ParseCertificate(block.Bytes)
  367. if err != nil {
  368. fmt.Println(err)
  369. os.Exit(1)
  370. }
  371. certPool := x509.NewCertPool()
  372. certPool.AddCert(cert)
  373. return certPool
  374. }
  375. // Send join requests to the leader.
  376. func joinCluster(s *raft.Server, serverName string) error {
  377. var b bytes.Buffer
  378. command := &JoinCommand{}
  379. command.Name = s.Name()
  380. json.NewEncoder(&b).Encode(command)
  381. // t must be ok
  382. t, _ := raftServer.Transporter().(transporter)
  383. debug("Send Join Request to %s", serverName)
  384. resp, err := t.Post(fmt.Sprintf("%s/join", serverName), &b)
  385. for {
  386. if resp != nil {
  387. defer resp.Body.Close()
  388. if resp.StatusCode == http.StatusOK {
  389. return nil
  390. }
  391. if resp.StatusCode == http.StatusServiceUnavailable {
  392. address, err := ioutil.ReadAll(resp.Body)
  393. if err != nil {
  394. warn("Cannot Read Leader info: %v", err)
  395. }
  396. debug("Leader is %s", address)
  397. debug("Send Join Request to %s", address)
  398. json.NewEncoder(&b).Encode(command)
  399. resp, err = t.Post(fmt.Sprintf("%s/join", address), &b)
  400. }
  401. }
  402. }
  403. return fmt.Errorf("Unable to join: %v", err)
  404. }
  405. // Register commands to raft server
  406. func registerCommands() {
  407. raft.RegisterCommand(&JoinCommand{})
  408. raft.RegisterCommand(&SetCommand{})
  409. raft.RegisterCommand(&GetCommand{})
  410. raft.RegisterCommand(&DeleteCommand{})
  411. raft.RegisterCommand(&WatchCommand{})
  412. raft.RegisterCommand(&ListCommand{})
  413. raft.RegisterCommand(&TestAndSetCommand{})
  414. }