Allow -hostname to also override remote addr port

When starting a influxd in a docker container, the processess needs to know
the hosts address and port in order to create its NodeInfo correctly.  -hostname
previously only allowed us to change the hostname and the port would always be 8088
which may not be correctly if running multiple containers on the same host.
pull/3592/head
Jason Wilder 2015-08-07 10:27:45 -06:00
parent c90ffff855
commit 87376a1c35
1 changed files with 33 additions and 4 deletions

View File

@ -272,12 +272,12 @@ func (s *Server) Open() error {
// Start profiling, if set.
startProfile(s.CPUProfile, s.MemProfile)
// Resolve host to address.
_, port, err := net.SplitHostPort(s.BindAddress)
host, port, err := s.hostAddr()
if err != nil {
return fmt.Errorf("split bind address: %s", err)
return err
}
hostport := net.JoinHostPort(s.Hostname, port)
hostport := net.JoinHostPort(host, port)
addr, err := net.ResolveTCPAddr("tcp", hostport)
if err != nil {
return fmt.Errorf("resolve tcp: addr=%s, err=%s", hostport, err)
@ -446,6 +446,35 @@ func (s *Server) monitorErrorChan(ch <-chan error) {
}
}
// hostAddr returns the host and port that remote nodes will use to reach this
// node.
func (s *Server) hostAddr() (string, string, error) {
// Resolve host to address.
_, port, err := net.SplitHostPort(s.BindAddress)
if err != nil {
return "", "", fmt.Errorf("split bind address: %s", err)
}
host := s.Hostname
// See if we might have a port that will override the BindAddress port
if host != "" && host[len(host)-1] >= '0' && host[len(host)-1] <= '9' {
hostArg, portArg, err := net.SplitHostPort(s.Hostname)
if err != nil {
return "", "", err
}
if hostArg != "" {
host = hostArg
}
if portArg != "" {
port = portArg
}
}
return host, port, nil
}
// Service represents a service attached to the server.
type Service interface {
Open() error