open-nomad/command/agent/util.go

38 lines
803 B
Go
Raw Normal View History

2015-08-16 21:34:38 +00:00
package agent
import (
2016-03-18 20:50:29 +00:00
"fmt"
2015-08-16 21:34:38 +00:00
"math/rand"
2016-03-18 20:50:29 +00:00
"net"
2015-08-16 21:34:38 +00:00
"time"
)
// Returns a random stagger interval between 0 and the duration
func randomStagger(intv time.Duration) time.Duration {
return time.Duration(uint64(rand.Int63()) % uint64(intv))
}
2016-03-18 20:50:29 +00:00
// 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)
}