etcd.go 14 KB

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