cd837b0b18
command/agent/* -> agent/* command/consul/* -> agent/consul/* command/agent/command{,_test}.go -> command/agent{,_test}.go command/base/command.go -> command/base.go command/base/* -> command/* commands.go -> command/commands.go The script which did the refactor is: ( cd $GOPATH/src/github.com/hashicorp/consul git mv command/agent/command.go command/agent.go git mv command/agent/command_test.go command/agent_test.go git mv command/agent/flag_slice_value{,_test}.go command/ git mv command/agent . git mv command/base/command.go command/base.go git mv command/base/config_util{,_test}.go command/ git mv commands.go command/ git mv consul agent rmdir command/base/ gsed -i -e 's|package agent|package command|' command/agent{,_test}.go gsed -i -e 's|package agent|package command|' command/flag_slice_value{,_test}.go gsed -i -e 's|package base|package command|' command/base.go command/config_util{,_test}.go gsed -i -e 's|package main|package command|' command/commands.go gsed -i -e 's|base.Command|BaseCommand|' command/commands.go gsed -i -e 's|agent.Command|AgentCommand|' command/commands.go gsed -i -e 's|\tCommand:|\tBaseCommand:|' command/commands.go gsed -i -e 's|base\.||' command/commands.go gsed -i -e 's|command\.||' command/commands.go gsed -i -e 's|command|c|' main.go gsed -i -e 's|range Commands|range command.Commands|' main.go gsed -i -e 's|Commands: Commands|Commands: command.Commands|' main.go gsed -i -e 's|base\.BoolValue|BoolValue|' command/operator_autopilot_set.go gsed -i -e 's|base\.DurationValue|DurationValue|' command/operator_autopilot_set.go gsed -i -e 's|base\.StringValue|StringValue|' command/operator_autopilot_set.go gsed -i -e 's|base\.UintValue|UintValue|' command/operator_autopilot_set.go gsed -i -e 's|\bCommand\b|BaseCommand|' command/base.go gsed -i -e 's|BaseCommand Options|Command Options|' command/base.go gsed -i -e 's|base.Command|BaseCommand|' command/*.go gsed -i -e 's|c\.Command|c.BaseCommand|g' command/*.go gsed -i -e 's|\tCommand:|\tBaseCommand:|' command/*_test.go gsed -i -e 's|base\.||' command/*_test.go gsed -i -e 's|\bCommand\b|AgentCommand|' command/agent{,_test}.go gsed -i -e 's|cmd.AgentCommand|cmd.BaseCommand|' command/agent.go gsed -i -e 's|cli.AgentCommand = new(Command)|cli.Command = new(AgentCommand)|' command/agent_test.go gsed -i -e 's|exec.AgentCommand|exec.Command|' command/agent_test.go gsed -i -e 's|exec.BaseCommand|exec.Command|' command/agent_test.go gsed -i -e 's|NewTestAgent|agent.NewTestAgent|' command/agent_test.go gsed -i -e 's|= TestConfig|= agent.TestConfig|' command/agent_test.go gsed -i -e 's|: RetryJoin|: agent.RetryJoin|' command/agent_test.go gsed -i -e 's|\.\./\.\./|../|' command/config_util_test.go gsed -i -e 's|\bverifyUniqueListeners|VerifyUniqueListeners|' agent/config{,_test}.go command/agent.go gsed -i -e 's|\bserfLANKeyring\b|SerfLANKeyring|g' agent/{agent,keyring,testagent}.go command/agent.go gsed -i -e 's|\bserfWANKeyring\b|SerfWANKeyring|g' agent/{agent,keyring,testagent}.go command/agent.go gsed -i -e 's|\bNewAgent\b|agent.New|g' command/agent{,_test}.go gsed -i -e 's|\bNewAgent|New|' agent/{acl_test,agent,testagent}.go gsed -i -e 's|\bAgent\b|agent.&|g' command/agent{,_test}.go gsed -i -e 's|\bBool\b|agent.&|g' command/agent{,_test}.go gsed -i -e 's|\bConfig\b|agent.&|g' command/agent{,_test}.go gsed -i -e 's|\bDefaultConfig\b|agent.&|g' command/agent{,_test}.go gsed -i -e 's|\bDevConfig\b|agent.&|g' command/agent{,_test}.go gsed -i -e 's|\bMergeConfig\b|agent.&|g' command/agent{,_test}.go gsed -i -e 's|\bReadConfigPaths\b|agent.&|g' command/agent{,_test}.go gsed -i -e 's|\bParseMetaPair\b|agent.&|g' command/agent{,_test}.go gsed -i -e 's|\bSerfLANKeyring\b|agent.&|g' command/agent{,_test}.go gsed -i -e 's|\bSerfWANKeyring\b|agent.&|g' command/agent{,_test}.go gsed -i -e 's|circonus\.agent|circonus|g' command/agent{,_test}.go gsed -i -e 's|logger\.agent|logger|g' command/agent{,_test}.go gsed -i -e 's|metrics\.agent|metrics|g' command/agent{,_test}.go gsed -i -e 's|// agent.Agent|// agent|' command/agent{,_test}.go gsed -i -e 's|a\.agent\.Config|a.Config|' command/agent{,_test}.go gsed -i -e 's|agent\.AppendSliceValue|AppendSliceValue|' command/{configtest,validate}.go gsed -i -e 's|consul/consul|agent/consul|' GNUmakefile gsed -i -e 's|\.\./test|../../test|' agent/consul/server_test.go # fix imports f=$(grep -rl 'github.com/hashicorp/consul/command/agent' * | grep '\.go') gsed -i -e 's|github.com/hashicorp/consul/command/agent|github.com/hashicorp/consul/agent|' $f goimports -w $f f=$(grep -rl 'github.com/hashicorp/consul/consul' * | grep '\.go') gsed -i -e 's|github.com/hashicorp/consul/consul|github.com/hashicorp/consul/agent/consul|' $f goimports -w $f goimports -w command/*.go main.go )
314 lines
6.9 KiB
Go
314 lines
6.9 KiB
Go
package consul
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"fmt"
|
|
"net"
|
|
"runtime"
|
|
"strconv"
|
|
|
|
"github.com/hashicorp/consul/agent/consul/agent"
|
|
"github.com/hashicorp/go-version"
|
|
"github.com/hashicorp/serf/serf"
|
|
)
|
|
|
|
/*
|
|
* Contains an entry for each private block:
|
|
* 10.0.0.0/8
|
|
* 100.64.0.0/10
|
|
* 127.0.0.0/8
|
|
* 169.254.0.0/16
|
|
* 172.16.0.0/12
|
|
* 192.168.0.0/16
|
|
*/
|
|
var privateBlocks []*net.IPNet
|
|
|
|
func init() {
|
|
// Add each private block
|
|
privateBlocks = make([]*net.IPNet, 6)
|
|
|
|
_, 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("100.64.0.0/10")
|
|
if err != nil {
|
|
panic(fmt.Sprintf("Bad cidr. Got %v", err))
|
|
}
|
|
privateBlocks[1] = block
|
|
|
|
_, block, err = net.ParseCIDR("127.0.0.0/8")
|
|
if err != nil {
|
|
panic(fmt.Sprintf("Bad cidr. Got %v", err))
|
|
}
|
|
privateBlocks[2] = block
|
|
|
|
_, block, err = net.ParseCIDR("169.254.0.0/16")
|
|
if err != nil {
|
|
panic(fmt.Sprintf("Bad cidr. Got %v", err))
|
|
}
|
|
privateBlocks[3] = block
|
|
|
|
_, block, err = net.ParseCIDR("172.16.0.0/12")
|
|
if err != nil {
|
|
panic(fmt.Sprintf("Bad cidr. Got %v", err))
|
|
}
|
|
privateBlocks[4] = block
|
|
|
|
_, block, err = net.ParseCIDR("192.168.0.0/16")
|
|
if err != nil {
|
|
panic(fmt.Sprintf("Bad cidr. Got %v", err))
|
|
}
|
|
privateBlocks[5] = block
|
|
}
|
|
|
|
// CanServersUnderstandProtocol checks to see if all the servers in the given
|
|
// list understand the given protocol version. If there are no servers in the
|
|
// list then this will return false.
|
|
func CanServersUnderstandProtocol(members []serf.Member, version uint8) (bool, error) {
|
|
numServers, numWhoGrok := 0, 0
|
|
for _, m := range members {
|
|
if m.Tags["role"] != "consul" {
|
|
continue
|
|
}
|
|
numServers++
|
|
|
|
vsn_min, err := strconv.Atoi(m.Tags["vsn_min"])
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
vsn_max, err := strconv.Atoi(m.Tags["vsn_max"])
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
v := int(version)
|
|
if (v >= vsn_min) && (v <= vsn_max) {
|
|
numWhoGrok++
|
|
}
|
|
}
|
|
return (numServers > 0) && (numWhoGrok == numServers), nil
|
|
}
|
|
|
|
// ServerMinRaftProtocol returns the lowest supported Raft protocol among alive servers
|
|
func ServerMinRaftProtocol(members []serf.Member) (int, error) {
|
|
minVersion := -1
|
|
for _, m := range members {
|
|
if m.Tags["role"] != "consul" || m.Status != serf.StatusAlive {
|
|
continue
|
|
}
|
|
|
|
vsn, ok := m.Tags["raft_vsn"]
|
|
if !ok {
|
|
vsn = "1"
|
|
}
|
|
raftVsn, err := strconv.Atoi(vsn)
|
|
if err != nil {
|
|
return -1, err
|
|
}
|
|
|
|
if minVersion == -1 || raftVsn < minVersion {
|
|
minVersion = raftVsn
|
|
}
|
|
}
|
|
|
|
if minVersion == -1 {
|
|
return minVersion, fmt.Errorf("No servers found")
|
|
}
|
|
|
|
return minVersion, nil
|
|
}
|
|
|
|
// Returns if a member is a consul node. Returns a bool,
|
|
// and the datacenter.
|
|
func isConsulNode(m serf.Member) (bool, string) {
|
|
if m.Tags["role"] != "node" {
|
|
return false, ""
|
|
}
|
|
return true, m.Tags["dc"]
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// Returns addresses from interfaces that is up
|
|
func activeInterfaceAddresses() ([]net.Addr, error) {
|
|
var upAddrs []net.Addr
|
|
var loAddrs []net.Addr
|
|
|
|
interfaces, err := net.Interfaces()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("Failed to get interfaces: %v", err)
|
|
}
|
|
|
|
for _, iface := range interfaces {
|
|
// Require interface to be up
|
|
if iface.Flags&net.FlagUp == 0 {
|
|
continue
|
|
}
|
|
|
|
addresses, err := iface.Addrs()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("Failed to get interface addresses: %v", err)
|
|
}
|
|
|
|
if iface.Flags&net.FlagLoopback != 0 {
|
|
loAddrs = append(loAddrs, addresses...)
|
|
continue
|
|
}
|
|
|
|
upAddrs = append(upAddrs, addresses...)
|
|
}
|
|
|
|
if len(upAddrs) == 0 {
|
|
return loAddrs, nil
|
|
}
|
|
|
|
return upAddrs, nil
|
|
}
|
|
|
|
// GetPrivateIP is used to return the first private IP address
|
|
// associated with an interface on the machine
|
|
func GetPrivateIP() (net.IP, error) {
|
|
addresses, err := activeInterfaceAddresses()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("Failed to get interface addresses: %v", err)
|
|
}
|
|
|
|
return getPrivateIP(addresses)
|
|
}
|
|
|
|
func getPrivateIP(addresses []net.Addr) (net.IP, error) {
|
|
var candidates []net.IP
|
|
|
|
// Find private IPv4 address
|
|
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:
|
|
continue
|
|
}
|
|
|
|
if ip.To4() == nil {
|
|
continue
|
|
}
|
|
if !isPrivateIP(ip.String()) {
|
|
continue
|
|
}
|
|
candidates = append(candidates, ip)
|
|
}
|
|
numIps := len(candidates)
|
|
switch numIps {
|
|
case 0:
|
|
return nil, fmt.Errorf("No private IP address found")
|
|
case 1:
|
|
return candidates[0], nil
|
|
default:
|
|
return nil, fmt.Errorf("Multiple private IPs found. Please configure one.")
|
|
}
|
|
|
|
}
|
|
|
|
// GetPublicIPv6 is used to return the first public IP address
|
|
// associated with an interface on the machine
|
|
func GetPublicIPv6() (net.IP, error) {
|
|
addresses, err := net.InterfaceAddrs()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("Failed to get interface addresses: %v", err)
|
|
}
|
|
|
|
return getPublicIPv6(addresses)
|
|
}
|
|
|
|
func isUniqueLocalAddress(ip net.IP) bool {
|
|
return len(ip) == net.IPv6len && ip[0] == 0xfc && ip[1] == 0x00
|
|
}
|
|
|
|
func getPublicIPv6(addresses []net.Addr) (net.IP, error) {
|
|
var candidates []net.IP
|
|
|
|
// Find public IPv6 address
|
|
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:
|
|
continue
|
|
}
|
|
|
|
if ip.To4() != nil {
|
|
continue
|
|
}
|
|
|
|
if ip.IsLinkLocalUnicast() || isUniqueLocalAddress(ip) || ip.IsLoopback() {
|
|
continue
|
|
}
|
|
candidates = append(candidates, ip)
|
|
}
|
|
numIps := len(candidates)
|
|
switch numIps {
|
|
case 0:
|
|
return nil, fmt.Errorf("No public IPv6 address found")
|
|
case 1:
|
|
return candidates[0], nil
|
|
default:
|
|
return nil, fmt.Errorf("Multiple public IPv6 addresses found. Please configure one.")
|
|
}
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// 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),
|
|
}
|
|
}
|
|
|
|
// ServersMeetMinimumVersion returns whether the given alive servers are at least on the
|
|
// given Consul version
|
|
func ServersMeetMinimumVersion(members []serf.Member, minVersion *version.Version) bool {
|
|
for _, member := range members {
|
|
if valid, parts := agent.IsConsulServer(member); valid && parts.Status == serf.StatusAlive {
|
|
if parts.Build.LessThan(minVersion) {
|
|
return false
|
|
}
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|