diff --git a/etcd.go b/etcd.go index 39743d5ab..0bc3ef4ed 100644 --- a/etcd.go +++ b/etcd.go @@ -50,8 +50,10 @@ func init() { flag.StringVar(&machinesFile, "CF", "", "the file contains a list of existing machines in the cluster, seperate by comma") flag.StringVar(&argInfo.Name, "n", "default-name", "the node name (required)") - flag.StringVar(&argInfo.EtcdURL, "c", "127.0.0.1:4001", "the hostname:port for etcd client communication") - flag.StringVar(&argInfo.RaftURL, "s", "127.0.0.1:7001", "the hostname:port for raft server communication") + flag.StringVar(&argInfo.EtcdURL, "c", "127.0.0.1:4001", "the advertised public hostname:port for etcd client communication") + flag.StringVar(&argInfo.RaftURL, "s", "127.0.0.1:7001", "the advertised public hostname:port for raft server communication") + flag.StringVar(&argInfo.EtcdListenHost, "cl", "127.0.0.1", "the listening hostname for etcd client communication") + flag.StringVar(&argInfo.RaftListenHost, "sl", "127.0.0.1", "the listening hostname for raft server communication") flag.StringVar(&argInfo.WebURL, "w", "", "the hostname:port of web interface") flag.StringVar(&argInfo.RaftTLS.CAFile, "serverCAFile", "", "the path of the CAFile") @@ -107,6 +109,9 @@ type Info struct { EtcdURL string `json:"etcdURL"` WebURL string `json:"webURL"` + RaftListenHost string `json:"raftListenHost"` + EtcdListenHost string `json:"etcdListenHost"` + RaftTLS TLSInfo `json:"raftTLS"` EtcdTLS TLSInfo `json:"etcdTLS"` } @@ -178,6 +183,9 @@ func main() { argInfo.EtcdURL = sanitizeURL(argInfo.EtcdURL, etcdTLSConfig.Scheme) argInfo.WebURL = sanitizeURL(argInfo.WebURL, "http") + argInfo.RaftListenHost = sanitizeListenHost(argInfo.RaftListenHost, argInfo.RaftURL) + argInfo.EtcdListenHost = sanitizeListenHost(argInfo.EtcdListenHost, argInfo.EtcdURL) + // Read server info from file or grab it from user. if err := os.MkdirAll(dirPath, 0744); err != nil { fatalf("Unable to create path: %s", err) @@ -190,8 +198,8 @@ func main() { snapConf = newSnapshotConf() // Create etcd and raft server - e = newEtcdServer(info.Name, info.EtcdURL, &etcdTLSConfig, &info.EtcdTLS) - r = newRaftServer(info.Name, info.RaftURL, &raftTLSConfig, &info.RaftTLS) + e = newEtcdServer(info.Name, info.EtcdURL, info.EtcdListenHost, &etcdTLSConfig, &info.EtcdTLS) + r = newRaftServer(info.Name, info.RaftURL, info.RaftListenHost, &raftTLSConfig, &info.RaftTLS) startWebInterface() r.ListenAndServe() diff --git a/etcd_server.go b/etcd_server.go index 51ff1e9e8..2cef01558 100644 --- a/etcd_server.go +++ b/etcd_server.go @@ -2,7 +2,6 @@ package main import ( "net/http" - "net/url" ) type etcdServer struct { @@ -15,18 +14,12 @@ type etcdServer struct { var e *etcdServer -func newEtcdServer(name string, urlStr string, tlsConf *TLSConfig, tlsInfo *TLSInfo) *etcdServer { - u, err := url.Parse(urlStr) - - if err != nil { - fatalf("invalid url '%s': %s", e.url, err) - } - +func newEtcdServer(name string, urlStr string, listenHost string, tlsConf *TLSConfig, tlsInfo *TLSInfo) *etcdServer { return &etcdServer{ Server: http.Server{ Handler: NewEtcdMuxer(), TLSConfig: &tlsConf.Server, - Addr: u.Host, + Addr: listenHost, }, name: name, url: urlStr, diff --git a/raft_server.go b/raft_server.go index a90c65f93..b3b5d3277 100644 --- a/raft_server.go +++ b/raft_server.go @@ -20,13 +20,14 @@ type raftServer struct { joinIndex uint64 name string url string + listenHost string tlsConf *TLSConfig tlsInfo *TLSInfo } var r *raftServer -func newRaftServer(name string, url string, tlsConf *TLSConfig, tlsInfo *TLSInfo) *raftServer { +func newRaftServer(name string, url string, listenHost string, tlsConf *TLSConfig, tlsInfo *TLSInfo) *raftServer { // Create transporter for raft raftTransporter := newTransporter(tlsConf.Scheme, tlsConf.Client) @@ -41,6 +42,7 @@ func newRaftServer(name string, url string, tlsConf *TLSConfig, tlsInfo *TLSInfo version: raftVersion, name: name, url: url, + listenHost: listenHost, tlsConf: tlsConf, tlsInfo: tlsInfo, } @@ -134,15 +136,14 @@ func startAsFollower() { // Start to listen and response raft command func (r *raftServer) startTransport(scheme string, tlsConf tls.Config) { - u, _ := url.Parse(r.url) - infof("raft server [%s:%s]", r.name, u) + infof("raft server [%s:%s]", r.name, r.listenHost) raftMux := http.NewServeMux() server := &http.Server{ Handler: raftMux, TLSConfig: &tlsConf, - Addr: u.Host, + Addr: r.listenHost, } // internal commands diff --git a/util.go b/util.go index 5f86cbaa9..8ddaa93cc 100644 --- a/util.go +++ b/util.go @@ -106,6 +106,22 @@ func sanitizeURL(host string, defaultScheme string) string { return p.String() } +// sanitizeListenHost cleans up the ListenHost parameter and appends a port +// if necessary based on the advertised port. +func sanitizeListenHost(listen string, advertised string) string { + aurl, err := url.Parse(advertised) + if err != nil { + fatal(err) + } + + _, aport, err := net.SplitHostPort(aurl.Host) + if err != nil { + fatal(err) + } + + return net.JoinHostPort(listen, aport) +} + func check(err error) { if err != nil { fatal(err)