Browse Source

Merge pull request #64 from Mistobaan/master

more cleanups
Xiang Li 12 years ago
parent
commit
4bf87b8cb2
1 changed files with 31 additions and 27 deletions
  1. 31 27
      etcd.go

+ 31 - 27
etcd.go

@@ -72,7 +72,7 @@ func init() {
 	flag.StringVar(&hostname, "h", "0.0.0.0", "the hostname of the local machine")
 	flag.StringVar(&hostname, "h", "0.0.0.0", "the hostname of the local machine")
 	flag.IntVar(&clientPort, "c", 4001, "the port to communicate with clients")
 	flag.IntVar(&clientPort, "c", 4001, "the port to communicate with clients")
 	flag.IntVar(&raftPort, "s", 7001, "the port to communicate with servers")
 	flag.IntVar(&raftPort, "s", 7001, "the port to communicate with servers")
-	flag.IntVar(&webPort, "w", -1, "the port of web interface")
+	flag.IntVar(&webPort, "w", -1, "the port of web interface (-1 means do not start web interface)")
 
 
 	flag.StringVar(&serverCAFile, "serverCAFile", "", "the path of the CAFile")
 	flag.StringVar(&serverCAFile, "serverCAFile", "", "the path of the CAFile")
 	flag.StringVar(&serverCertFile, "serverCert", "", "the cert file of the server")
 	flag.StringVar(&serverCertFile, "serverCert", "", "the cert file of the server")
@@ -279,11 +279,12 @@ func startRaft(securityType int) {
 
 
 			// leader need to join self as a peer
 			// leader need to join self as a peer
 			for {
 			for {
-				command := &JoinCommand{}
-				command.Name = raftServer.Name()
-				command.Hostname = hostname
-				command.RaftPort = raftPort
-				command.ClientPort = clientPort
+				command := &JoinCommand{
+					Name:       raftServer.Name(),
+					Hostname:   hostname,
+					RaftPort:   raftPort,
+					ClientPort: clientPort,
+				}
 				_, err := raftServer.Do(command)
 				_, err := raftServer.Do(command)
 				if err == nil {
 				if err == nil {
 					break
 					break
@@ -510,7 +511,6 @@ func securityType(source int) int {
 // 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 {
-	info := &Info{}
 
 
 	// Read in the server info if available.
 	// Read in the server info if available.
 	infoPath := fmt.Sprintf("%s/info", path)
 	infoPath := fmt.Sprintf("%s/info", path)
@@ -529,6 +529,7 @@ func getInfo(path string) *Info {
 	}
 	}
 
 
 	if file, err := os.Open(infoPath); err == nil {
 	if file, err := os.Open(infoPath); err == nil {
+		info := &Info{}
 		if content, err := ioutil.ReadAll(file); err != nil {
 		if content, err := ioutil.ReadAll(file); err != nil {
 			fatalf("Unable to read info: %v", err)
 			fatalf("Unable to read info: %v", err)
 		} else {
 		} else {
@@ -537,29 +538,32 @@ func getInfo(path string) *Info {
 			}
 			}
 		}
 		}
 		file.Close()
 		file.Close()
-
+		return info
 	} else {
 	} else {
 		// Otherwise ask user for info and write it to file.
 		// Otherwise ask user for info and write it to file.
 
 
+		hostname = strings.TrimSpace(hostname)
+
 		if hostname == "" {
 		if hostname == "" {
 			fatal("Please give the address of the local machine")
 			fatal("Please give the address of the local machine")
 		}
 		}
 
 
-		info.Hostname = hostname
-		info.Hostname = strings.TrimSpace(info.Hostname)
-		fmt.Println("address ", info.Hostname)
+		fmt.Println("address ", hostname)
+		info := &Info{
+			Hostname: hostname,
 
 
-		info.RaftPort = raftPort
-		info.ClientPort = clientPort
-		info.WebPort = webPort
+			RaftPort: raftPort,
+			ClientPort: clientPort,
+			WebPort:    webPort,
 
 
-		info.ClientCAFile = clientCAFile
-		info.ClientCertFile = clientCertFile
-		info.ClientKeyFile = clientKeyFile
+			ClientCAFile:   clientCAFile,
+			ClientCertFile: clientCertFile,
+			ClientKeyFile:  clientKeyFile,
 
 
-		info.ServerCAFile = serverCAFile
-		info.ServerKeyFile = serverKeyFile
-		info.ServerCertFile = serverCertFile
+			ServerCAFile:   serverCAFile,
+			ServerKeyFile:  serverKeyFile,
+			ServerCertFile: serverCertFile,
+		}
 
 
 		// Write to file.
 		// Write to file.
 		content, _ := json.Marshal(info)
 		content, _ := json.Marshal(info)
@@ -567,9 +571,8 @@ func getInfo(path string) *Info {
 		if err := ioutil.WriteFile(infoPath, content, 0644); err != nil {
 		if err := ioutil.WriteFile(infoPath, content, 0644); err != nil {
 			fatalf("Unable to write info to file: %v", err)
 			fatalf("Unable to write info to file: %v", err)
 		}
 		}
+		return info
 	}
 	}
-
-	return info
 }
 }
 
 
 // Create client auth certpool
 // Create client auth certpool
@@ -595,11 +598,12 @@ func createCertPool(CAFile string) *x509.CertPool {
 func joinCluster(s *raft.Server, serverName string) error {
 func joinCluster(s *raft.Server, serverName string) error {
 	var b bytes.Buffer
 	var b bytes.Buffer
 
 
-	command := &JoinCommand{}
-	command.Name = s.Name()
-	command.Hostname = info.Hostname
-	command.RaftPort = info.RaftPort
-	command.ClientPort = info.ClientPort
+	command := &JoinCommand{
+		Name:       s.Name(),
+		Hostname:   info.Hostname,
+		RaftPort:   info.RaftPort,
+		ClientPort: info.ClientPort,
+	}
 
 
 	json.NewEncoder(&b).Encode(command)
 	json.NewEncoder(&b).Encode(command)