github.com/marineam/etcd@v0.1.2-0.20130821182615-9b7109b46686/etcd.go (about) 1 package main 2 3 import ( 4 "crypto/tls" 5 "flag" 6 "github.com/coreos/etcd/store" 7 "github.com/coreos/go-raft" 8 "io/ioutil" 9 "os" 10 "strings" 11 "time" 12 ) 13 14 //------------------------------------------------------------------------------ 15 // 16 // Initialization 17 // 18 //------------------------------------------------------------------------------ 19 20 var ( 21 verbose bool 22 veryVerbose bool 23 24 machines string 25 machinesFile string 26 27 cluster []string 28 29 argInfo Info 30 dirPath string 31 32 force bool 33 34 maxSize int 35 36 snapshot bool 37 38 retryTimes int 39 40 maxClusterSize int 41 42 cpuprofile string 43 ) 44 45 func init() { 46 flag.BoolVar(&verbose, "v", false, "verbose logging") 47 flag.BoolVar(&veryVerbose, "vv", false, "very verbose logging") 48 49 flag.StringVar(&machines, "C", "", "the ip address and port of a existing machines in the cluster, sepearate by comma") 50 flag.StringVar(&machinesFile, "CF", "", "the file contains a list of existing machines in the cluster, seperate by comma") 51 52 flag.StringVar(&argInfo.Name, "n", "default-name", "the node name (required)") 53 flag.StringVar(&argInfo.EtcdURL, "c", "127.0.0.1:4001", "the hostname:port for etcd client communication") 54 flag.StringVar(&argInfo.RaftURL, "s", "127.0.0.1:7001", "the hostname:port for raft server communication") 55 flag.StringVar(&argInfo.WebURL, "w", "", "the hostname:port of web interface") 56 57 flag.StringVar(&argInfo.RaftTLS.CAFile, "serverCAFile", "", "the path of the CAFile") 58 flag.StringVar(&argInfo.RaftTLS.CertFile, "serverCert", "", "the cert file of the server") 59 flag.StringVar(&argInfo.RaftTLS.KeyFile, "serverKey", "", "the key file of the server") 60 61 flag.StringVar(&argInfo.EtcdTLS.CAFile, "clientCAFile", "", "the path of the client CAFile") 62 flag.StringVar(&argInfo.EtcdTLS.CertFile, "clientCert", "", "the cert file of the client") 63 flag.StringVar(&argInfo.EtcdTLS.KeyFile, "clientKey", "", "the key file of the client") 64 65 flag.StringVar(&dirPath, "d", ".", "the directory to store log and snapshot") 66 67 flag.BoolVar(&force, "f", false, "force new node configuration if existing is found (WARNING: data loss!)") 68 69 flag.BoolVar(&snapshot, "snapshot", false, "open or close snapshot") 70 71 flag.IntVar(&maxSize, "m", 1024, "the max size of result buffer") 72 73 flag.IntVar(&retryTimes, "r", 3, "the max retry attempts when trying to join a cluster") 74 75 flag.IntVar(&maxClusterSize, "maxsize", 9, "the max size of the cluster") 76 77 flag.StringVar(&cpuprofile, "cpuprofile", "", "write cpu profile to file") 78 } 79 80 const ( 81 ElectionTimeout = 200 * time.Millisecond 82 HeartbeatTimeout = 50 * time.Millisecond 83 84 // Timeout for internal raft http connection 85 // The original timeout for http is 45 seconds 86 // which is too long for our usage. 87 HTTPTimeout = 10 * time.Second 88 RetryInterval = 10 89 ) 90 91 //------------------------------------------------------------------------------ 92 // 93 // Typedefs 94 // 95 //------------------------------------------------------------------------------ 96 97 type TLSInfo struct { 98 CertFile string `json:"CertFile"` 99 KeyFile string `json:"KeyFile"` 100 CAFile string `json:"CAFile"` 101 } 102 103 type Info struct { 104 Name string `json:"name"` 105 106 RaftURL string `json:"raftURL"` 107 EtcdURL string `json:"etcdURL"` 108 WebURL string `json:"webURL"` 109 110 RaftTLS TLSInfo `json:"raftTLS"` 111 EtcdTLS TLSInfo `json:"etcdTLS"` 112 } 113 114 type TLSConfig struct { 115 Scheme string 116 Server tls.Config 117 Client tls.Config 118 } 119 120 //------------------------------------------------------------------------------ 121 // 122 // Variables 123 // 124 //------------------------------------------------------------------------------ 125 126 var etcdStore *store.Store 127 128 //------------------------------------------------------------------------------ 129 // 130 // Functions 131 // 132 //------------------------------------------------------------------------------ 133 134 //-------------------------------------- 135 // Main 136 //-------------------------------------- 137 138 func main() { 139 flag.Parse() 140 141 if cpuprofile != "" { 142 runCPUProfile() 143 } 144 145 if veryVerbose { 146 verbose = true 147 raft.SetLogLevel(raft.Debug) 148 } 149 150 if machines != "" { 151 cluster = strings.Split(machines, ",") 152 } else if machinesFile != "" { 153 b, err := ioutil.ReadFile(machinesFile) 154 if err != nil { 155 fatalf("Unable to read the given machines file: %s", err) 156 } 157 cluster = strings.Split(string(b), ",") 158 } 159 160 // Check TLS arguments 161 raftTLSConfig, ok := tlsConfigFromInfo(argInfo.RaftTLS) 162 if !ok { 163 fatal("Please specify cert and key file or cert and key file and CAFile or none of the three") 164 } 165 166 etcdTLSConfig, ok := tlsConfigFromInfo(argInfo.EtcdTLS) 167 if !ok { 168 fatal("Please specify cert and key file or cert and key file and CAFile or none of the three") 169 } 170 171 argInfo.Name = strings.TrimSpace(argInfo.Name) 172 if argInfo.Name == "" { 173 fatal("ERROR: server name required. e.g. '-n=server_name'") 174 } 175 176 // Check host name arguments 177 argInfo.RaftURL = sanitizeURL(argInfo.RaftURL, raftTLSConfig.Scheme) 178 argInfo.EtcdURL = sanitizeURL(argInfo.EtcdURL, etcdTLSConfig.Scheme) 179 argInfo.WebURL = sanitizeURL(argInfo.WebURL, "http") 180 181 // Read server info from file or grab it from user. 182 if err := os.MkdirAll(dirPath, 0744); err != nil { 183 fatalf("Unable to create path: %s", err) 184 } 185 186 info := getInfo(dirPath) 187 188 // Create etcd key-value store 189 etcdStore = store.CreateStore(maxSize) 190 snapConf = newSnapshotConf() 191 192 // Create etcd and raft server 193 e = newEtcdServer(info.Name, info.EtcdURL, &etcdTLSConfig, &info.EtcdTLS) 194 r = newRaftServer(info.Name, info.RaftURL, &raftTLSConfig, &info.RaftTLS) 195 196 startWebInterface() 197 r.ListenAndServe() 198 e.ListenAndServe() 199 200 }