2013-12-12 19:07:14 +00:00
|
|
|
package consul
|
|
|
|
|
2013-12-19 22:18:55 +00:00
|
|
|
import (
|
2014-05-08 23:31:03 +00:00
|
|
|
crand "crypto/rand"
|
2014-01-08 00:58:16 +00:00
|
|
|
"encoding/binary"
|
2013-12-31 23:44:17 +00:00
|
|
|
"fmt"
|
2015-05-15 00:59:11 +00:00
|
|
|
"math/rand"
|
2013-12-31 23:44:17 +00:00
|
|
|
"net"
|
2013-12-19 22:18:55 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2014-04-29 17:55:42 +00:00
|
|
|
"runtime"
|
2013-12-19 22:37:54 +00:00
|
|
|
"strconv"
|
2014-07-17 06:37:25 +00:00
|
|
|
"strings"
|
2015-05-15 00:59:11 +00:00
|
|
|
"time"
|
2014-06-16 21:36:12 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/serf/serf"
|
2013-12-19 22:18:55 +00:00
|
|
|
)
|
|
|
|
|
2013-12-31 23:44:17 +00:00
|
|
|
/*
|
|
|
|
* Contains an entry for each private block:
|
|
|
|
* 10.0.0.0/8
|
2015-06-19 19:20:30 +00:00
|
|
|
* 100.64.0.0/10
|
2013-12-31 23:44:17 +00:00
|
|
|
* 172.16.0.0/12
|
|
|
|
* 192.168/16
|
|
|
|
*/
|
|
|
|
var privateBlocks []*net.IPNet
|
|
|
|
|
2014-01-20 23:39:07 +00:00
|
|
|
// serverparts is used to return the parts of a server role
|
|
|
|
type serverParts struct {
|
2014-05-27 22:07:31 +00:00
|
|
|
Name string
|
2014-01-20 23:39:07 +00:00
|
|
|
Datacenter string
|
|
|
|
Port int
|
2014-01-30 21:13:29 +00:00
|
|
|
Bootstrap bool
|
2014-06-16 21:36:12 +00:00
|
|
|
Expect int
|
2014-05-27 22:07:31 +00:00
|
|
|
Version int
|
2014-05-27 21:45:36 +00:00
|
|
|
Addr net.Addr
|
2014-01-20 23:56:29 +00:00
|
|
|
}
|
|
|
|
|
2014-05-27 22:07:31 +00:00
|
|
|
func (s *serverParts) String() string {
|
|
|
|
return fmt.Sprintf("%s (Addr: %s) (DC: %s)", s.Name, s.Addr, s.Datacenter)
|
|
|
|
}
|
|
|
|
|
2013-12-31 23:44:17 +00:00
|
|
|
func init() {
|
|
|
|
// Add each private block
|
2015-06-19 19:20:30 +00:00
|
|
|
privateBlocks = make([]*net.IPNet, 4)
|
|
|
|
|
2013-12-31 23:44:17 +00:00
|
|
|
_, block, err := net.ParseCIDR("10.0.0.0/8")
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("Bad cidr. Got %v", err))
|
|
|
|
}
|
|
|
|
privateBlocks[0] = block
|
|
|
|
|
|
|
|
_, block, err = net.ParseCIDR("172.16.0.0/12")
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("Bad cidr. Got %v", err))
|
|
|
|
}
|
|
|
|
privateBlocks[1] = block
|
|
|
|
|
|
|
|
_, block, err = net.ParseCIDR("192.168.0.0/16")
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("Bad cidr. Got %v", err))
|
|
|
|
}
|
|
|
|
privateBlocks[2] = block
|
2015-06-19 19:20:30 +00:00
|
|
|
|
|
|
|
_, block, err = net.ParseCIDR("100.64.0.0/10")
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("Bad cidr. Got %v", err))
|
|
|
|
}
|
|
|
|
privateBlocks[3] = block
|
2013-12-31 23:44:17 +00:00
|
|
|
}
|
|
|
|
|
2013-12-12 19:07:14 +00:00
|
|
|
// strContains checks if a list contains a string
|
|
|
|
func strContains(l []string, s string) bool {
|
|
|
|
for _, v := range l {
|
|
|
|
if v == s {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2013-12-19 22:18:55 +00:00
|
|
|
|
2014-07-17 06:37:25 +00:00
|
|
|
func ToLowerList(l []string) []string {
|
|
|
|
var out []string
|
|
|
|
for _, value := range l {
|
|
|
|
out = append(out, strings.ToLower(value))
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2013-12-19 22:18:55 +00:00
|
|
|
// ensurePath is used to make sure a path exists
|
|
|
|
func ensurePath(path string, dir bool) error {
|
|
|
|
if !dir {
|
|
|
|
path = filepath.Dir(path)
|
|
|
|
}
|
|
|
|
return os.MkdirAll(path, 0755)
|
|
|
|
}
|
2013-12-19 22:37:54 +00:00
|
|
|
|
|
|
|
// Returns if a member is a consul server. Returns a bool,
|
2015-02-19 22:45:47 +00:00
|
|
|
// the datacenter, and the rpc port
|
2014-01-20 23:39:07 +00:00
|
|
|
func isConsulServer(m serf.Member) (bool, *serverParts) {
|
2014-01-30 21:13:29 +00:00
|
|
|
if m.Tags["role"] != "consul" {
|
2014-01-20 23:39:07 +00:00
|
|
|
return false, nil
|
2013-12-19 22:37:54 +00:00
|
|
|
}
|
|
|
|
|
2014-01-30 21:13:29 +00:00
|
|
|
datacenter := m.Tags["dc"]
|
|
|
|
_, bootstrap := m.Tags["bootstrap"]
|
2014-05-27 22:07:31 +00:00
|
|
|
|
2014-06-18 22:47:05 +00:00
|
|
|
expect := 0
|
|
|
|
expect_str, ok := m.Tags["expect"]
|
|
|
|
var err error
|
|
|
|
if ok {
|
|
|
|
expect, err = strconv.Atoi(expect_str)
|
|
|
|
if err != nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
2014-06-16 21:36:12 +00:00
|
|
|
}
|
|
|
|
|
2014-05-27 22:07:31 +00:00
|
|
|
port_str := m.Tags["port"]
|
2013-12-19 22:37:54 +00:00
|
|
|
port, err := strconv.Atoi(port_str)
|
|
|
|
if err != nil {
|
2014-01-20 23:39:07 +00:00
|
|
|
return false, nil
|
2013-12-19 22:37:54 +00:00
|
|
|
}
|
2014-05-27 22:07:31 +00:00
|
|
|
|
|
|
|
vsn_str := m.Tags["vsn"]
|
|
|
|
vsn, err := strconv.Atoi(vsn_str)
|
|
|
|
if err != nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2014-05-27 21:45:36 +00:00
|
|
|
addr := &net.TCPAddr{IP: m.Addr, Port: port}
|
2014-05-27 22:07:31 +00:00
|
|
|
|
|
|
|
parts := &serverParts{
|
|
|
|
Name: m.Name,
|
|
|
|
Datacenter: datacenter,
|
|
|
|
Port: port,
|
|
|
|
Bootstrap: bootstrap,
|
2014-06-16 21:36:12 +00:00
|
|
|
Expect: expect,
|
2014-05-27 22:07:31 +00:00
|
|
|
Addr: addr,
|
|
|
|
Version: vsn,
|
|
|
|
}
|
|
|
|
return true, parts
|
2013-12-19 22:37:54 +00:00
|
|
|
}
|
2013-12-31 23:44:17 +00:00
|
|
|
|
2014-01-09 23:45:14 +00:00
|
|
|
// Returns if a member is a consul node. Returns a boo,
|
2015-02-19 22:45:47 +00:00
|
|
|
// and the datacenter.
|
2014-01-09 23:45:14 +00:00
|
|
|
func isConsulNode(m serf.Member) (bool, string) {
|
2014-01-30 21:13:29 +00:00
|
|
|
if m.Tags["role"] != "node" {
|
2014-01-09 23:45:14 +00:00
|
|
|
return false, ""
|
|
|
|
}
|
2014-01-30 21:13:29 +00:00
|
|
|
return true, m.Tags["dc"]
|
2014-01-09 23:45:14 +00:00
|
|
|
}
|
|
|
|
|
2013-12-31 23:44:17 +00:00
|
|
|
// Returns if the given IP is in a private block
|
|
|
|
func isPrivateIP(ip_str string) bool {
|
|
|
|
ip := net.ParseIP(ip_str)
|
|
|
|
for _, priv := range privateBlocks {
|
|
|
|
if priv.Contains(ip) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPrivateIP is used to return the first private IP address
|
|
|
|
// associated with an interface on the machine
|
2014-05-15 18:27:30 +00:00
|
|
|
func GetPrivateIP() (net.IP, error) {
|
2013-12-31 23:44:17 +00:00
|
|
|
addresses, err := net.InterfaceAddrs()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to get interface addresses: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find private IPv4 address
|
2014-05-15 18:27:30 +00:00
|
|
|
for _, rawAddr := range addresses {
|
|
|
|
var ip net.IP
|
|
|
|
switch addr := rawAddr.(type) {
|
|
|
|
case *net.IPAddr:
|
|
|
|
ip = addr.IP
|
|
|
|
case *net.IPNet:
|
|
|
|
ip = addr.IP
|
|
|
|
default:
|
2013-12-31 23:44:17 +00:00
|
|
|
continue
|
|
|
|
}
|
2014-05-15 18:27:30 +00:00
|
|
|
|
|
|
|
if ip.To4() == nil {
|
2013-12-31 23:44:17 +00:00
|
|
|
continue
|
|
|
|
}
|
2014-05-15 18:27:30 +00:00
|
|
|
if !isPrivateIP(ip.String()) {
|
2013-12-31 23:44:17 +00:00
|
|
|
continue
|
|
|
|
}
|
2014-05-15 18:27:30 +00:00
|
|
|
|
2013-12-31 23:44:17 +00:00
|
|
|
return ip, nil
|
|
|
|
}
|
2014-05-15 18:27:30 +00:00
|
|
|
|
2013-12-31 23:44:17 +00:00
|
|
|
return nil, fmt.Errorf("No private IP address found")
|
|
|
|
}
|
2014-01-08 00:58:16 +00:00
|
|
|
|
|
|
|
// Converts bytes to an integer
|
|
|
|
func bytesToUint64(b []byte) uint64 {
|
|
|
|
return binary.BigEndian.Uint64(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Converts a uint to a byte slice
|
|
|
|
func uint64ToBytes(u uint64) []byte {
|
|
|
|
buf := make([]byte, 8)
|
|
|
|
binary.BigEndian.PutUint64(buf, u)
|
|
|
|
return buf
|
|
|
|
}
|
2014-04-29 17:55:42 +00:00
|
|
|
|
|
|
|
// runtimeStats is used to return various runtime information
|
|
|
|
func runtimeStats() map[string]string {
|
|
|
|
return map[string]string{
|
|
|
|
"os": runtime.GOOS,
|
|
|
|
"arch": runtime.GOARCH,
|
|
|
|
"version": runtime.Version(),
|
|
|
|
"max_procs": strconv.FormatInt(int64(runtime.GOMAXPROCS(0)), 10),
|
|
|
|
"goroutines": strconv.FormatInt(int64(runtime.NumGoroutine()), 10),
|
|
|
|
"cpu_count": strconv.FormatInt(int64(runtime.NumCPU()), 10),
|
|
|
|
}
|
|
|
|
}
|
2014-05-08 23:31:03 +00:00
|
|
|
|
|
|
|
// generateUUID is used to generate a random UUID
|
|
|
|
func generateUUID() string {
|
|
|
|
buf := make([]byte, 16)
|
|
|
|
if _, err := crand.Read(buf); err != nil {
|
|
|
|
panic(fmt.Errorf("failed to read random bytes: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%08x-%04x-%04x-%04x-%12x",
|
|
|
|
buf[0:4],
|
|
|
|
buf[4:6],
|
|
|
|
buf[6:8],
|
|
|
|
buf[8:10],
|
|
|
|
buf[10:16])
|
|
|
|
}
|
2015-05-15 00:59:11 +00:00
|
|
|
|
|
|
|
// 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))
|
|
|
|
}
|