Merge pull request #64 from Mistobaan/master

more cleanups
This commit is contained in:
Xiang Li 2013-08-06 17:55:38 -07:00
commit 4bf87b8cb2

58
etcd.go
View File

@ -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 := &JoinCommand{
command.Name = raftServer.Name() Name: raftServer.Name(),
command.Hostname = hostname Hostname: hostname,
command.RaftPort = raftPort RaftPort: raftPort,
command.ClientPort = clientPort 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 fmt.Println("address ", hostname)
info.Hostname = strings.TrimSpace(info.Hostname) info := &Info{
fmt.Println("address ", info.Hostname) Hostname: hostname,
info.RaftPort = raftPort RaftPort: raftPort,
info.ClientPort = clientPort ClientPort: clientPort,
info.WebPort = webPort WebPort: webPort,
info.ClientCAFile = clientCAFile ClientCAFile: clientCAFile,
info.ClientCertFile = clientCertFile ClientCertFile: clientCertFile,
info.ClientKeyFile = clientKeyFile ClientKeyFile: clientKeyFile,
info.ServerCAFile = serverCAFile ServerCAFile: serverCAFile,
info.ServerKeyFile = serverKeyFile ServerKeyFile: serverKeyFile,
info.ServerCertFile = serverCertFile ServerCertFile: serverCertFile,
}
// Write to file. // Write to file.
content, _ := json.Marshal(info) content, _ := json.Marshal(info)
@ -567,10 +571,9 @@ 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
func createCertPool(CAFile string) *x509.CertPool { func createCertPool(CAFile string) *x509.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 := &JoinCommand{
command.Name = s.Name() Name: s.Name(),
command.Hostname = info.Hostname Hostname: info.Hostname,
command.RaftPort = info.RaftPort RaftPort: info.RaftPort,
command.ClientPort = info.ClientPort ClientPort: info.ClientPort,
}
json.NewEncoder(&b).Encode(command) json.NewEncoder(&b).Encode(command)