diff --git a/command/agent/agent.go b/command/agent/agent.go index fea10ddf2..76b7771b6 100644 --- a/command/agent/agent.go +++ b/command/agent/agent.go @@ -71,7 +71,7 @@ func (a *Agent) serverConfig() (*nomad.Config, error) { conf.LogOutput = a.logOutput conf.DevMode = a.config.DevMode conf.Build = fmt.Sprintf("%s%s", a.config.Version, a.config.VersionPrerelease) - if a.config.Region != "" { + if i.config.Region != "" { conf.Region = a.config.Region } if a.config.Datacenter != "" { diff --git a/command/agent/config.go b/command/agent/config.go index 6137ea4f0..1904eee8a 100644 --- a/command/agent/config.go +++ b/command/agent/config.go @@ -46,6 +46,8 @@ type Config struct { // Addresses is used to override the network addresses we bind to. Addresses *Addresses `mapstructure:"addresses"` + Interfaces *Interfaces `mapstructure:"interfaces"` + // AdvertiseAddrs is used to control the addresses we advertise. AdvertiseAddrs *AdvertiseAddrs `mapstructure:"advertise"` @@ -255,6 +257,12 @@ type Addresses struct { Serf string `mapstructure:"serf"` } +type Interfaces struct { + HTTP string `mapstructure:"http"` + RPC string `mapstructure:"rpc"` + Serf string `mapstructure:"serf"` +} + // AdvertiseAddrs is used to control the addresses we advertise out for // different network services. Not all network services support an // advertise address. All are optional and default to BindAddr. @@ -366,6 +374,7 @@ func DefaultConfig() *Config { Serf: 4648, }, Addresses: &Addresses{}, + Interfaces: &Interfaces{}, AdvertiseAddrs: &AdvertiseAddrs{}, Atlas: &AtlasConfig{}, Client: &ClientConfig{ @@ -496,6 +505,14 @@ func (c *Config) Merge(b *Config) *Config { result.Addresses = result.Addresses.Merge(b.Addresses) } + // Apply the interfaces config + if result.Interfaces == nil && b.Interfaces != nil { + interfaces := *b.Interfaces + result.Interfaces = &interfaces + } else if b.Interfaces != nil { + result.Interfaces = result.Interfaces.Merge(b.Interfaces) + } + // Apply the advertise addrs config if result.AdvertiseAddrs == nil && b.AdvertiseAddrs != nil { advertise := *b.AdvertiseAddrs @@ -675,6 +692,22 @@ func (a *Addresses) Merge(b *Addresses) *Addresses { return &result } +// Merge is used to merge two interfaces configs together. +func (i *Interfaces) Merge(b *Interfaces) *Interfaces { + result := *i + + if b.HTTP != "" { + result.HTTP = b.HTTP + } + if b.RPC != "" { + result.RPC = b.RPC + } + if b.Serf != "" { + result.Serf = b.Serf + } + return &result +} + // Merge merges two advertise addrs configs together. func (a *AdvertiseAddrs) Merge(b *AdvertiseAddrs) *AdvertiseAddrs { result := *a diff --git a/command/agent/util.go b/command/agent/util.go index ceb333ed9..27df5f8f2 100644 --- a/command/agent/util.go +++ b/command/agent/util.go @@ -1,7 +1,9 @@ package agent import ( + "fmt" "math/rand" + "net" "time" ) @@ -9,3 +11,27 @@ import ( func randomStagger(intv time.Duration) time.Duration { return time.Duration(uint64(rand.Int63()) % uint64(intv)) } + +// IpOfDevice returns a routable ip addr of a device +func IpOfDevice(name string) (net.IP, error) { + intf, err := net.InterfaceByName(name) + if err != nil { + return nil, err + } + addrs, err := intf.Addrs() + if err != nil { + return nil, err + } + if len(addrs) == 0 { + return nil, fmt.Errorf("no ips were detected on the interface: %v", name) + } + for _, addr := range addrs { + switch v := (addr).(type) { + case *net.IPNet: + continue + case *net.IPAddr: + return v.IP, nil + } + } + return nil, fmt.Errorf("no ips were detected on the interface: %v", name) +}