Xiang Li 12 years ago
parent
commit
cb33641f5f
4 changed files with 79 additions and 73 deletions
  1. 29 31
      config.go
  2. 12 35
      etcd.go
  3. 5 7
      raft_server.go
  4. 33 0
      util.go

+ 29 - 31
config.go

@@ -14,39 +14,14 @@ import (
 // Config
 // Config
 //--------------------------------------
 //--------------------------------------
 
 
-func parseInfo(path string) *Info {
-	file, err := os.Open(path)
-
-	if err != nil {
-		return nil
-	}
-	defer file.Close()
-
-	info := &Info{}
-
-	content, err := ioutil.ReadAll(file)
-	if err != nil {
-		fatalf("Unable to read info: %v", err)
-		return nil
-	}
-
-	if err = json.Unmarshal(content, &info); err != nil {
-		fatalf("Unable to parse info: %v", err)
-		return nil
-	}
-
-	return info
-}
-
 // Get the server info from previous conf file
 // Get the server info from previous conf file
 // or from the user
 // or from the user
 func getInfo(path string) *Info {
 func getInfo(path string) *Info {
 
 
-	// Read in the server info if available.
 	infoPath := filepath.Join(path, "info")
 	infoPath := filepath.Join(path, "info")
 
 
-	// Delete the old configuration if exist
 	if force {
 	if force {
+		// Delete the old configuration if exist
 		logPath := filepath.Join(path, "log")
 		logPath := filepath.Join(path, "log")
 		confPath := filepath.Join(path, "conf")
 		confPath := filepath.Join(path, "conf")
 		snapshotPath := filepath.Join(path, "snapshot")
 		snapshotPath := filepath.Join(path, "snapshot")
@@ -54,15 +29,13 @@ func getInfo(path string) *Info {
 		os.Remove(logPath)
 		os.Remove(logPath)
 		os.Remove(confPath)
 		os.Remove(confPath)
 		os.RemoveAll(snapshotPath)
 		os.RemoveAll(snapshotPath)
-	}
-
-	info := parseInfo(infoPath)
-	if info != nil {
+	} else if info := readInfo(infoPath); info != nil {
 		infof("Found node configuration in '%s'. Ignoring flags", infoPath)
 		infof("Found node configuration in '%s'. Ignoring flags", infoPath)
 		return info
 		return info
 	}
 	}
 
 
-	info = &argInfo
+	// Read info from command line
+	info := &argInfo
 
 
 	// Write to file.
 	// Write to file.
 	content, _ := json.MarshalIndent(info, "", " ")
 	content, _ := json.MarshalIndent(info, "", " ")
@@ -76,6 +49,31 @@ func getInfo(path string) *Info {
 	return info
 	return info
 }
 }
 
 
+// readInfo reads from info file and decode to Info struct
+func readInfo(path string) *Info {
+	file, err := os.Open(path)
+
+	if err != nil {
+		return nil
+	}
+	defer file.Close()
+
+	info := &Info{}
+
+	content, err := ioutil.ReadAll(file)
+	if err != nil {
+		fatalf("Unable to read info: %v", err)
+		return nil
+	}
+
+	if err = json.Unmarshal(content, &info); err != nil {
+		fatalf("Unable to parse info: %v", err)
+		return nil
+	}
+
+	return info
+}
+
 func tlsConfigFromInfo(info TLSInfo) (t TLSConfig, ok bool) {
 func tlsConfigFromInfo(info TLSInfo) (t TLSConfig, ok bool) {
 	var keyFile, certFile, CAFile string
 	var keyFile, certFile, CAFile string
 	var tlsCert tls.Certificate
 	var tlsCert tls.Certificate

+ 12 - 35
etcd.go

@@ -4,13 +4,11 @@ import (
 	"crypto/tls"
 	"crypto/tls"
 	"flag"
 	"flag"
 	"github.com/coreos/etcd/store"
 	"github.com/coreos/etcd/store"
-	"github.com/coreos/etcd/web"
+	"github.com/coreos/go-raft"
 	"io/ioutil"
 	"io/ioutil"
 	"net/http"
 	"net/http"
 	"net/url"
 	"net/url"
 	"os"
 	"os"
-	"os/signal"
-	"runtime/pprof"
 	"strings"
 	"strings"
 	"time"
 	"time"
 )
 )
@@ -142,27 +140,12 @@ func main() {
 	flag.Parse()
 	flag.Parse()
 
 
 	if cpuprofile != "" {
 	if cpuprofile != "" {
-		f, err := os.Create(cpuprofile)
-		if err != nil {
-			fatal(err)
-		}
-		pprof.StartCPUProfile(f)
-		defer pprof.StopCPUProfile()
-
-		c := make(chan os.Signal, 1)
-		signal.Notify(c, os.Interrupt)
-		go func() {
-			for sig := range c {
-				infof("captured %v, stopping profiler and exiting..", sig)
-				pprof.StopCPUProfile()
-				os.Exit(1)
-			}
-		}()
-
+		runCPUProfile()
 	}
 	}
 
 
 	if veryVerbose {
 	if veryVerbose {
 		verbose = true
 		verbose = true
+		raft.SetLogLevel(raft.Debug)
 	}
 	}
 
 
 	if machines != "" {
 	if machines != "" {
@@ -175,6 +158,7 @@ func main() {
 		cluster = strings.Split(string(b), ",")
 		cluster = strings.Split(string(b), ",")
 	}
 	}
 
 
+	// Check TLS arguments
 	raftTLSConfig, ok := tlsConfigFromInfo(argInfo.RaftTLS)
 	raftTLSConfig, ok := tlsConfigFromInfo(argInfo.RaftTLS)
 	if !ok {
 	if !ok {
 		fatal("Please specify cert and key file or cert and key file and CAFile or none of the three")
 		fatal("Please specify cert and key file or cert and key file and CAFile or none of the three")
@@ -190,13 +174,11 @@ func main() {
 		fatal("ERROR: server name required. e.g. '-n=server_name'")
 		fatal("ERROR: server name required. e.g. '-n=server_name'")
 	}
 	}
 
 
+	// Check host name arguments
 	argInfo.RaftURL = sanitizeURL(argInfo.RaftURL, raftTLSConfig.Scheme)
 	argInfo.RaftURL = sanitizeURL(argInfo.RaftURL, raftTLSConfig.Scheme)
 	argInfo.EtcdURL = sanitizeURL(argInfo.EtcdURL, etcdTLSConfig.Scheme)
 	argInfo.EtcdURL = sanitizeURL(argInfo.EtcdURL, etcdTLSConfig.Scheme)
 	argInfo.WebURL = sanitizeURL(argInfo.WebURL, "http")
 	argInfo.WebURL = sanitizeURL(argInfo.WebURL, "http")
 
 
-	// Setup commands.
-	registerCommands()
-
 	// Read server info from file or grab it from user.
 	// Read server info from file or grab it from user.
 	if err := os.MkdirAll(dirPath, 0744); err != nil {
 	if err := os.MkdirAll(dirPath, 0744); err != nil {
 		fatalf("Unable to create path: %s", err)
 		fatalf("Unable to create path: %s", err)
@@ -208,21 +190,16 @@ func main() {
 	etcdStore = store.CreateStore(maxSize)
 	etcdStore = store.CreateStore(maxSize)
 	snapConf = newSnapshotConf()
 	snapConf = newSnapshotConf()
 
 
-	startRaft(raftTLSConfig)
+	startWebInterface()
 
 
-	if argInfo.WebURL != "" {
-		// start web
-		argInfo.WebURL = sanitizeURL(argInfo.WebURL, "http")
-		go webHelper()
-		go web.Start(raftServer, argInfo.WebURL)
-	}
+	startRaft(raftTLSConfig)
 
 
-	startEtcdTransport(*info, etcdTLSConfig.Scheme, etcdTLSConfig.Server)
+	startEtcd(etcdTLSConfig)
 
 
 }
 }
 
 
-// Start to listen and response client command
-func startEtcdTransport(info Info, scheme string, tlsConf tls.Config) {
+// Start to listen and response etcd client command
+func startEtcd(tlsConf TLSConfig) {
 	u, err := url.Parse(info.EtcdURL)
 	u, err := url.Parse(info.EtcdURL)
 	if err != nil {
 	if err != nil {
 		fatalf("invalid url '%s': %s", info.EtcdURL, err)
 		fatalf("invalid url '%s': %s", info.EtcdURL, err)
@@ -231,11 +208,11 @@ func startEtcdTransport(info Info, scheme string, tlsConf tls.Config) {
 
 
 	server := http.Server{
 	server := http.Server{
 		Handler:   NewEtcdMuxer(),
 		Handler:   NewEtcdMuxer(),
-		TLSConfig: &tlsConf,
+		TLSConfig: &tlsConf.Server,
 		Addr:      u.Host,
 		Addr:      u.Host,
 	}
 	}
 
 
-	if scheme == "http" {
+	if tlsConf.Scheme == "http" {
 		fatal(server.ListenAndServe())
 		fatal(server.ListenAndServe())
 	} else {
 	} else {
 		fatal(server.ListenAndServeTLS(info.EtcdTLS.CertFile, info.EtcdTLS.KeyFile))
 		fatal(server.ListenAndServeTLS(info.EtcdTLS.CertFile, info.EtcdTLS.KeyFile))

+ 5 - 7
raft_server.go

@@ -12,23 +12,21 @@ import (
 	"github.com/coreos/go-raft"
 	"github.com/coreos/go-raft"
 )
 )
 
 
-var raftTransporter transporter
 var raftServer *raft.Server
 var raftServer *raft.Server
 
 
 // Start the raft server
 // Start the raft server
 func startRaft(tlsConfig TLSConfig) {
 func startRaft(tlsConfig TLSConfig) {
-	if veryVerbose {
-		raft.SetLogLevel(raft.Debug)
-	}
-
-	var err error
 
 
 	raftName := info.Name
 	raftName := info.Name
 
 
+	// Setup commands.
+	registerCommands()
+
 	// Create transporter for raft
 	// Create transporter for raft
-	raftTransporter = newTransporter(tlsConfig.Scheme, tlsConfig.Client)
+	raftTransporter := newTransporter(tlsConfig.Scheme, tlsConfig.Client)
 
 
 	// Create raft server
 	// Create raft server
+	var err error
 	raftServer, err = raft.NewServer(raftName, dirPath, raftTransporter, etcdStore, nil)
 	raftServer, err = raft.NewServer(raftName, dirPath, raftTransporter, etcdStore, nil)
 
 
 	if err != nil {
 	if err != nil {

+ 33 - 0
util.go

@@ -10,6 +10,8 @@ import (
 	"net/http"
 	"net/http"
 	"net/url"
 	"net/url"
 	"os"
 	"os"
+	"os/signal"
+	"runtime/pprof"
 	"strconv"
 	"strconv"
 	"time"
 	"time"
 )
 )
@@ -48,6 +50,15 @@ func webHelper() {
 	}
 	}
 }
 }
 
 
+// startWebInterface starts web interface if webURL is not empty
+func startWebInterface() {
+	if argInfo.WebURL != "" {
+		// start web
+		go webHelper()
+		go web.Start(raftServer, argInfo.WebURL)
+	}
+}
+
 //--------------------------------------
 //--------------------------------------
 // HTTP Utilities
 // HTTP Utilities
 //--------------------------------------
 //--------------------------------------
@@ -144,3 +155,25 @@ func fatal(v ...interface{}) {
 	logger.Println("FATAL " + fmt.Sprint(v...))
 	logger.Println("FATAL " + fmt.Sprint(v...))
 	os.Exit(1)
 	os.Exit(1)
 }
 }
+
+//--------------------------------------
+// CPU profile
+//--------------------------------------
+func runCPUProfile() {
+
+	f, err := os.Create(cpuprofile)
+	if err != nil {
+		fatal(err)
+	}
+	pprof.StartCPUProfile(f)
+
+	c := make(chan os.Signal, 1)
+	signal.Notify(c, os.Interrupt)
+	go func() {
+		for sig := range c {
+			infof("captured %v, stopping profiler and exiting..", sig)
+			pprof.StopCPUProfile()
+			os.Exit(1)
+		}
+	}()
+}