etcd/etcd.go

597 lines
14 KiB
Go
Raw Normal View History

package main
import (
"bytes"
2013-06-21 02:59:23 +04:00
"crypto/tls"
"crypto/x509"
"encoding/json"
2013-06-20 08:03:28 +04:00
"encoding/pem"
"flag"
"fmt"
"github.com/coreos/etcd/store"
"github.com/coreos/etcd/web"
"github.com/coreos/go-raft"
"io/ioutil"
2013-06-21 02:59:23 +04:00
"log"
2013-07-11 20:43:14 +04:00
"net"
"net/http"
"os"
2013-06-21 02:59:23 +04:00
"strings"
"time"
)
//------------------------------------------------------------------------------
//
// Initialization
//
//------------------------------------------------------------------------------
var verbose bool
2013-06-29 01:17:16 +04:00
2013-07-11 06:02:58 +04:00
var machines string
2013-07-13 01:18:02 +04:00
var machinesFile string
2013-07-11 06:02:58 +04:00
var cluster []string
2013-06-29 01:17:16 +04:00
var hostname string
2013-06-29 01:17:16 +04:00
var clientPort int
var raftPort int
2013-06-18 22:13:24 +04:00
var webPort int
2013-06-29 01:17:16 +04:00
var serverCertFile string
var serverKeyFile string
var serverCAFile string
var clientCertFile string
var clientKeyFile string
var clientCAFile string
2013-06-29 01:17:16 +04:00
var dirPath string
var ignore bool
2013-07-01 03:30:41 +04:00
var maxSize int
2013-06-30 21:09:05 +04:00
2013-07-21 03:29:27 +04:00
var snapshot bool
func init() {
flag.BoolVar(&verbose, "v", false, "verbose logging")
2013-06-29 01:17:16 +04:00
2013-07-13 01:18:02 +04:00
flag.StringVar(&machines, "C", "", "the ip address and port of a existing machines in the cluster, sepearate by comma")
flag.StringVar(&machinesFile, "CF", "", "the file contains a list of existing machines in the cluster, seperate by comma")
2013-06-29 01:17:16 +04:00
flag.StringVar(&hostname, "h", "0.0.0.0", "the hostname of the local machine")
2013-07-01 22:16:30 +04:00
flag.IntVar(&clientPort, "c", 4001, "the port to communicate with clients")
flag.IntVar(&raftPort, "s", 7001, "the port to communicate with servers")
2013-06-18 22:13:24 +04:00
flag.IntVar(&webPort, "w", -1, "the port of web interface")
2013-06-29 01:17:16 +04:00
flag.StringVar(&serverCAFile, "serverCAFile", "", "the path of the CAFile")
flag.StringVar(&serverCertFile, "serverCert", "", "the cert file of the server")
flag.StringVar(&serverKeyFile, "serverKey", "", "the key file of the server")
2013-07-01 22:16:30 +04:00
flag.StringVar(&clientCAFile, "clientCAFile", "", "the path of the client CAFile")
flag.StringVar(&clientCertFile, "clientCert", "", "the cert file of the client")
flag.StringVar(&clientKeyFile, "clientKey", "", "the key file of the client")
2013-06-29 01:17:16 +04:00
2013-07-10 01:07:45 +04:00
flag.StringVar(&dirPath, "d", "/tmp/", "the directory to store log and snapshot")
flag.BoolVar(&ignore, "i", false, "ignore the old configuration, create a new node")
2013-07-01 03:30:41 +04:00
2013-07-21 03:29:27 +04:00
flag.BoolVar(&snapshot, "snapshot", false, "open or close snapshot")
2013-07-01 03:30:41 +04:00
flag.IntVar(&maxSize, "m", 1024, "the max size of result buffer")
}
2013-06-21 02:59:23 +04:00
// CONSTANTS
2013-06-21 02:59:23 +04:00
const (
HTTP = iota
HTTPS
HTTPSANDVERIFY
)
const (
SERVER = iota
CLIENT
)
2013-06-13 22:01:06 +04:00
const (
ELECTIONTIMTOUT = 200 * time.Millisecond
HEARTBEATTIMEOUT = 50 * time.Millisecond
2013-07-12 19:56:10 +04:00
2013-07-12 01:51:18 +04:00
// Timeout for internal raft http connection
2013-07-12 19:56:10 +04:00
// The original timeout for http is 45 seconds
2013-07-12 01:56:16 +04:00
// which is too long for our usage.
2013-07-14 06:13:07 +04:00
HTTPTIMEOUT = 10 * time.Second
2013-06-13 22:01:06 +04:00
)
//------------------------------------------------------------------------------
//
// Typedefs
//
//------------------------------------------------------------------------------
type Info struct {
Hostname string `json:"hostname"`
RaftPort int `json:"raftPort"`
ClientPort int `json:"clientPort"`
WebPort int `json:"webPort"`
2013-07-10 07:37:00 +04:00
ServerCertFile string `json:"serverCertFile"`
ServerKeyFile string `json:"serverKeyFile"`
ServerCAFile string `json:"serverCAFile"`
ClientCertFile string `json:"clientCertFile"`
ClientKeyFile string `json:"clientKeyFile"`
ClientCAFile string `json:"clientCAFile"`
}
//------------------------------------------------------------------------------
//
// Variables
//
//------------------------------------------------------------------------------
2013-07-10 01:55:45 +04:00
var raftServer *raft.Server
var raftTransporter transporter
2013-07-10 00:14:12 +04:00
var etcdStore *store.Store
2013-07-10 07:37:00 +04:00
var info *Info
2013-06-19 02:04:30 +04:00
//------------------------------------------------------------------------------
//
// Functions
//
//------------------------------------------------------------------------------
//--------------------------------------
// Main
//--------------------------------------
func main() {
flag.Parse()
2013-07-13 01:18:02 +04:00
if machines != "" {
cluster = strings.Split(machines, ",")
} else if machinesFile != "" {
b, err := ioutil.ReadFile(machinesFile)
if err != nil {
fatal("Unable to read the given machines file: %s", err)
}
cluster = strings.Split(string(b), ",")
}
2013-07-11 06:02:58 +04:00
// Setup commands.
registerCommands()
2013-06-21 02:59:23 +04:00
// Read server info from file or grab it from user.
if err := os.MkdirAll(dirPath, 0744); err != nil {
2013-07-13 01:18:02 +04:00
fatal("Unable to create path: %s", err)
}
2013-06-13 22:01:06 +04:00
2013-07-10 07:37:00 +04:00
info = getInfo(dirPath)
2013-06-13 22:01:06 +04:00
2013-07-16 03:04:41 +04:00
// security type
st := securityType(SERVER)
2013-06-20 08:03:28 +04:00
clientSt := securityType(CLIENT)
if st == -1 || clientSt == -1 {
fatal("Please specify cert and key file or cert and key file and CAFile or none of the three")
2013-06-20 08:03:28 +04:00
}
// Create etcd key-value store
2013-07-10 00:14:12 +04:00
etcdStore = store.CreateStore(maxSize)
2013-06-19 02:04:30 +04:00
2013-07-10 07:37:00 +04:00
startRaft(st)
if webPort != -1 {
// start web
etcdStore.SetMessager(&storeMsg)
go webHelper()
go web.Start(raftServer, webPort)
}
startClientTransport(info.ClientPort, clientSt)
}
// Start the raft server
func startRaft(securityType int) {
2013-07-10 07:52:41 +04:00
var err error
2013-07-10 07:37:00 +04:00
raftName := fmt.Sprintf("%s:%d", info.Hostname, info.RaftPort)
2013-07-10 07:37:00 +04:00
// Create transporter for raft
raftTransporter = createTransporter(securityType)
// Create raft server
2013-07-10 07:52:41 +04:00
raftServer, err = raft.NewServer(raftName, dirPath, raftTransporter, etcdStore, nil)
2013-06-19 02:04:30 +04:00
if err != nil {
2013-07-16 21:02:59 +04:00
fatal(fmt.Sprintln(err))
}
2013-06-12 20:46:53 +04:00
// LoadSnapshot
2013-07-21 03:29:27 +04:00
if snapshot {
err = raftServer.LoadSnapshot()
2013-07-21 03:29:27 +04:00
if err == nil {
debug("%s finished load snapshot", raftServer.Name())
} else {
debug(err.Error())
}
}
2013-07-10 01:58:59 +04:00
2013-07-10 01:55:45 +04:00
raftServer.Initialize()
raftServer.SetElectionTimeout(ELECTIONTIMTOUT)
raftServer.SetHeartbeatTimeout(HEARTBEATTIMEOUT)
2013-06-13 22:01:06 +04:00
2013-07-10 01:55:45 +04:00
if raftServer.IsLogEmpty() {
2013-06-13 22:01:06 +04:00
// start as a leader in a new cluster
2013-07-14 08:02:25 +04:00
if len(cluster) == 0 {
2013-07-10 01:55:45 +04:00
raftServer.StartLeader()
2013-07-07 09:32:08 +04:00
time.Sleep(time.Millisecond * 20)
2013-06-13 22:01:06 +04:00
2013-07-10 01:55:45 +04:00
// leader need to join self as a peer
2013-07-07 09:32:08 +04:00
for {
command := &JoinCommand{}
2013-07-10 01:55:45 +04:00
command.Name = raftServer.Name()
command.Hostname = hostname
command.RaftPort = raftPort
command.ClientPort = clientPort
2013-07-10 01:55:45 +04:00
_, err := raftServer.Do(command)
2013-07-07 09:32:08 +04:00
if err == nil {
break
}
}
2013-07-10 01:55:45 +04:00
debug("%s start as a leader", raftServer.Name())
2013-06-13 22:01:06 +04:00
2013-07-10 01:55:45 +04:00
// start as a follower in a existing cluster
} else {
2013-07-10 01:55:45 +04:00
raftServer.StartFollower()
2013-06-12 20:46:53 +04:00
2013-07-11 06:02:58 +04:00
for _, machine := range cluster {
2013-07-11 07:19:34 +04:00
err = joinCluster(raftServer, machine)
2013-07-11 06:02:58 +04:00
if err != nil {
debug("cannot join to cluster via machine %s %s", machine, err)
2013-07-11 06:02:58 +04:00
} else {
break
}
}
2013-06-20 08:03:28 +04:00
if err != nil {
2013-07-11 06:02:58 +04:00
fatal("cannot join to cluster via all given machines!")
2013-06-20 08:03:28 +04:00
}
2013-07-10 01:55:45 +04:00
debug("%s success join to the cluster", raftServer.Name())
}
2013-06-13 22:01:06 +04:00
2013-06-12 02:29:25 +04:00
} else {
2013-07-10 01:55:45 +04:00
// rejoin the previous cluster
raftServer.StartFollower()
debug("%s restart as a follower", raftServer.Name())
}
2013-06-13 22:01:06 +04:00
// open the snapshot
2013-07-21 03:29:27 +04:00
if snapshot {
go raftServer.Snapshot()
}
2013-07-10 07:37:00 +04:00
// start to response to raft requests
go startRaftTransport(info.RaftPort, securityType)
2013-06-20 08:03:28 +04:00
}
// Create transporter using by raft server
2013-07-10 04:31:24 +04:00
// Create http or https transporter based on
2013-07-16 03:04:41 +04:00
// whether the user give the server cert and key
2013-07-10 01:55:45 +04:00
func createTransporter(st int) transporter {
t := transporter{}
2013-06-20 08:03:28 +04:00
switch st {
case HTTP:
2013-07-12 19:44:40 +04:00
t.scheme = "http://"
2013-07-11 20:43:14 +04:00
tr := &http.Transport{
Dial: dialTimeout,
}
t.client = &http.Client{
Transport: tr,
}
2013-06-20 08:03:28 +04:00
case HTTPS:
fallthrough
case HTTPSANDVERIFY:
2013-07-12 19:44:40 +04:00
t.scheme = "https://"
2013-07-11 20:43:14 +04:00
tlsCert, err := tls.LoadX509KeyPair(serverCertFile, serverKeyFile)
2013-06-20 08:03:28 +04:00
if err != nil {
fatal(fmt.Sprintln(err))
2013-06-20 08:03:28 +04:00
}
tr := &http.Transport{
2013-06-21 02:59:23 +04:00
TLSClientConfig: &tls.Config{
Certificates: []tls.Certificate{tlsCert},
2013-06-20 08:03:28 +04:00
InsecureSkipVerify: true,
2013-06-21 02:59:23 +04:00
},
2013-07-11 20:43:14 +04:00
Dial: dialTimeout,
2013-06-21 02:59:23 +04:00
DisableCompression: true,
}
2013-06-20 08:03:28 +04:00
t.client = &http.Client{Transport: tr}
}
2013-07-16 21:32:00 +04:00
return t
2013-06-20 08:03:28 +04:00
}
2013-07-11 20:43:14 +04:00
// Dial with timeout
func dialTimeout(network, addr string) (net.Conn, error) {
return net.DialTimeout(network, addr, HTTPTIMEOUT)
}
// Start to listen and response raft command
func startRaftTransport(port int, st int) {
2013-06-20 08:03:28 +04:00
// internal commands
2013-06-21 02:59:23 +04:00
http.HandleFunc("/join", JoinHttpHandler)
http.HandleFunc("/vote", VoteHttpHandler)
http.HandleFunc("/log", GetLogHttpHandler)
http.HandleFunc("/log/append", AppendEntriesHttpHandler)
http.HandleFunc("/snapshot", SnapshotHttpHandler)
http.HandleFunc("/snapshotRecovery", SnapshotRecoveryHttpHandler)
http.HandleFunc("/client", ClientHttpHandler)
2013-06-29 01:17:16 +04:00
switch st {
case HTTP:
fmt.Printf("raft server [%s] listen on http port %v\n", hostname, port)
2013-06-29 01:17:16 +04:00
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
case HTTPS:
fmt.Printf("raft server [%s] listen on https port %v\n", hostname, port)
2013-07-10 01:55:45 +04:00
log.Fatal(http.ListenAndServeTLS(fmt.Sprintf(":%d", port), serverCertFile, serverKeyFile, nil))
2013-06-29 01:17:16 +04:00
case HTTPSANDVERIFY:
server := &http.Server{
TLSConfig: &tls.Config{
ClientAuth: tls.RequireAndVerifyClientCert,
ClientCAs: createCertPool(serverCAFile),
2013-06-29 01:17:16 +04:00
},
Addr: fmt.Sprintf(":%d", port),
}
fmt.Printf("raft server [%s] listen on https port %v\n", hostname, port)
err := server.ListenAndServeTLS(serverCertFile, serverKeyFile)
2013-06-29 01:17:16 +04:00
if err != nil {
log.Fatal(err)
}
}
}
// Start to listen and response client command
2013-06-29 01:17:16 +04:00
func startClientTransport(port int, st int) {
2013-06-21 02:59:23 +04:00
// external commands
2013-07-10 04:31:24 +04:00
http.HandleFunc("/"+version+"/keys/", Multiplexer)
http.HandleFunc("/"+version+"/watch/", WatchHttpHandler)
http.HandleFunc("/leader", LeaderHttpHandler)
2013-07-14 06:13:07 +04:00
http.HandleFunc("/machines", MachinesHttpHandler)
2013-06-21 02:59:23 +04:00
switch st {
2013-06-18 22:13:24 +04:00
2013-06-21 02:59:23 +04:00
case HTTP:
fmt.Printf("etcd [%s] listen on http port %v\n", hostname, clientPort)
2013-06-21 02:59:23 +04:00
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
2013-06-18 22:13:24 +04:00
2013-06-21 02:59:23 +04:00
case HTTPS:
fmt.Printf("etcd [%s] listen on https port %v\n", hostname, clientPort)
http.ListenAndServeTLS(fmt.Sprintf(":%d", port), clientCertFile, clientKeyFile, nil)
2013-06-20 08:03:28 +04:00
2013-06-21 02:59:23 +04:00
case HTTPSANDVERIFY:
2013-06-20 08:03:28 +04:00
server := &http.Server{
TLSConfig: &tls.Config{
ClientAuth: tls.RequireAndVerifyClientCert,
ClientCAs: createCertPool(clientCAFile),
2013-06-21 02:59:23 +04:00
},
Addr: fmt.Sprintf(":%d", port),
2013-06-20 08:03:28 +04:00
}
fmt.Printf("etcd [%s] listen on https port %v\n", hostname, clientPort)
err := server.ListenAndServeTLS(clientCertFile, clientKeyFile)
2013-06-20 08:03:28 +04:00
if err != nil {
2013-07-16 21:02:59 +04:00
fatal(fmt.Sprintln(err))
2013-06-20 08:03:28 +04:00
}
2013-06-21 02:59:23 +04:00
}
}
//--------------------------------------
// Config
//--------------------------------------
2013-07-10 07:37:00 +04:00
// Get the security type
func securityType(source int) int {
var keyFile, certFile, CAFile string
switch source {
case SERVER:
2013-07-10 07:37:00 +04:00
keyFile = info.ServerKeyFile
certFile = info.ServerCertFile
CAFile = info.ServerCAFile
case CLIENT:
2013-07-10 07:37:00 +04:00
keyFile = info.ClientKeyFile
certFile = info.ClientCertFile
CAFile = info.ClientCAFile
}
// If the user do not specify key file, cert file and
2013-07-10 04:31:24 +04:00
// CA file, the type will be HTTP
2013-06-21 02:59:23 +04:00
if keyFile == "" && certFile == "" && CAFile == "" {
2013-06-20 08:03:28 +04:00
return HTTP
}
if keyFile != "" && certFile != "" {
if CAFile != "" {
2013-07-10 04:31:24 +04:00
// If the user specify all the three file, the type
// will be HTTPS with client cert auth
2013-06-20 08:03:28 +04:00
return HTTPSANDVERIFY
}
// If the user specify key file and cert file but not
2013-07-10 04:31:24 +04:00
// CA file, the type will be HTTPS without client cert
// auth
2013-06-20 08:03:28 +04:00
return HTTPS
}
// bad specification
2013-06-20 08:03:28 +04:00
return -1
}
2013-07-10 07:37:00 +04:00
// Get the server info from previous conf file
// or from the user
func getInfo(path string) *Info {
info := &Info{}
// Read in the server info if available.
infoPath := fmt.Sprintf("%s/info", path)
2013-07-10 07:37:00 +04:00
// Delete the old configuration if exist
if ignore {
2013-07-11 07:00:05 +04:00
logPath := fmt.Sprintf("%s/log", path)
confPath := fmt.Sprintf("%s/conf", path)
snapshotPath := fmt.Sprintf("%s/snapshotPath", path)
os.Remove(infoPath)
os.Remove(logPath)
os.Remove(confPath)
os.RemoveAll(snapshotPath)
}
if file, err := os.Open(infoPath); err == nil {
if content, err := ioutil.ReadAll(file); err != nil {
fatal("Unable to read info: %v", err)
} else {
if err = json.Unmarshal(content, &info); err != nil {
fatal("Unable to parse info: %v", err)
}
}
file.Close()
2013-06-21 02:59:23 +04:00
} else {
2013-07-10 07:37:00 +04:00
// Otherwise ask user for info and write it to file.
2013-06-21 02:59:23 +04:00
if hostname == "" {
2013-06-13 22:01:06 +04:00
fatal("Please give the address of the local machine")
}
info.Hostname = hostname
info.Hostname = strings.TrimSpace(info.Hostname)
fmt.Println("address ", info.Hostname)
2013-06-21 02:59:23 +04:00
info.RaftPort = raftPort
2013-06-29 01:17:16 +04:00
info.ClientPort = clientPort
info.WebPort = webPort
2013-07-10 07:37:00 +04:00
info.ClientCAFile = clientCAFile
info.ClientCertFile = clientCertFile
info.ClientKeyFile = clientKeyFile
info.ServerCAFile = serverCAFile
info.ServerKeyFile = serverKeyFile
info.ServerCertFile = serverCertFile
// Write to file.
content, _ := json.Marshal(info)
content = []byte(string(content) + "\n")
if err := ioutil.WriteFile(infoPath, content, 0644); err != nil {
fatal("Unable to write info to file: %v", err)
}
}
2013-06-21 02:59:23 +04:00
return info
}
2013-07-10 07:37:00 +04:00
// Create client auth certpool
func createCertPool(CAFile string) *x509.CertPool {
pemByte, _ := ioutil.ReadFile(CAFile)
block, pemByte := pem.Decode(pemByte)
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
2013-07-16 21:02:59 +04:00
fatal(fmt.Sprintln(err))
}
certPool := x509.NewCertPool()
certPool.AddCert(cert)
return certPool
}
// Send join requests to the leader.
func joinCluster(s *raft.Server, serverName string) error {
var b bytes.Buffer
2013-06-21 02:59:23 +04:00
command := &JoinCommand{}
command.Name = s.Name()
command.Hostname = info.Hostname
command.RaftPort = info.RaftPort
command.ClientPort = info.ClientPort
json.NewEncoder(&b).Encode(command)
2013-06-20 08:03:28 +04:00
// t must be ok
2013-07-11 07:00:05 +04:00
t, ok := raftServer.Transporter().(transporter)
if !ok {
panic("wrong type")
}
debug("Send Join Request to %s", serverName)
2013-07-11 07:00:05 +04:00
2013-07-10 01:55:45 +04:00
resp, err := t.Post(fmt.Sprintf("%s/join", serverName), &b)
2013-06-20 08:03:28 +04:00
for {
if err != nil {
return fmt.Errorf("Unable to join: %v", err)
}
if resp != nil {
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
return nil
}
2013-07-11 07:00:05 +04:00
if resp.StatusCode == http.StatusTemporaryRedirect {
address := resp.Header.Get("Location")
debug("Leader is %s", address)
debug("Send Join Request to %s", address)
json.NewEncoder(&b).Encode(command)
2013-07-10 01:55:45 +04:00
resp, err = t.Post(fmt.Sprintf("%s/join", address), &b)
2013-07-11 07:14:32 +04:00
} else {
return fmt.Errorf("Unable to join")
}
}
2013-07-11 07:00:05 +04:00
}
return fmt.Errorf("Unable to join: %v", err)
}
2013-07-10 07:37:00 +04:00
// Register commands to raft server
func registerCommands() {
raft.RegisterCommand(&JoinCommand{})
raft.RegisterCommand(&SetCommand{})
raft.RegisterCommand(&GetCommand{})
raft.RegisterCommand(&DeleteCommand{})
raft.RegisterCommand(&WatchCommand{})
raft.RegisterCommand(&TestAndSetCommand{})
}