etcd.go 13 KB

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