etcd.go 13 KB

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