2016-03-30 00:39:19 +00:00
|
|
|
// Package agent provides a logical endpoint for Consul agents in the
|
|
|
|
// network. agent data originates from Serf gossip and is primarily used to
|
|
|
|
// communicate Consul server information. Gossiped information that ends up
|
|
|
|
// in Server contains the necessary metadata required for servers.Manager to
|
|
|
|
// select which server an RPC request should be routed to.
|
|
|
|
package agent
|
2016-02-20 01:26:45 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2017-03-21 23:36:44 +00:00
|
|
|
"regexp"
|
2016-02-20 01:26:45 +00:00
|
|
|
"strconv"
|
|
|
|
|
2017-03-21 23:36:44 +00:00
|
|
|
"github.com/hashicorp/go-version"
|
2016-02-20 01:26:45 +00:00
|
|
|
"github.com/hashicorp/serf/serf"
|
|
|
|
)
|
|
|
|
|
2016-03-27 00:58:12 +00:00
|
|
|
// Key is used in maps and for equality tests. A key is based on endpoints.
|
|
|
|
type Key struct {
|
2016-03-28 19:53:19 +00:00
|
|
|
name string
|
2016-03-27 00:58:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Equal compares two Key objects
|
|
|
|
func (k *Key) Equal(x *Key) bool {
|
2016-03-28 19:53:19 +00:00
|
|
|
return k.name == x.name
|
2016-03-27 00:58:12 +00:00
|
|
|
}
|
|
|
|
|
2016-03-30 00:39:19 +00:00
|
|
|
// Server is used to return details of a consul server
|
|
|
|
type Server struct {
|
2017-02-22 20:53:32 +00:00
|
|
|
Name string
|
|
|
|
ID string
|
|
|
|
Datacenter string
|
|
|
|
Port int
|
2017-03-15 19:26:54 +00:00
|
|
|
WanJoinPort int
|
2017-02-22 20:53:32 +00:00
|
|
|
Bootstrap bool
|
|
|
|
Expect int
|
2017-03-21 23:36:44 +00:00
|
|
|
Build version.Version
|
2017-02-22 20:53:32 +00:00
|
|
|
Version int
|
|
|
|
RaftVersion int
|
2017-03-21 23:36:44 +00:00
|
|
|
NonVoter bool
|
2017-02-22 20:53:32 +00:00
|
|
|
Addr net.Addr
|
2017-03-18 01:42:28 +00:00
|
|
|
Status serf.MemberStatus
|
2017-05-10 21:25:48 +00:00
|
|
|
|
|
|
|
// If true, use TLS when connecting to this server
|
|
|
|
UseTLS bool
|
2016-02-20 01:26:45 +00:00
|
|
|
}
|
|
|
|
|
2016-03-27 00:58:12 +00:00
|
|
|
// Key returns the corresponding Key
|
2016-03-30 00:39:19 +00:00
|
|
|
func (s *Server) Key() *Key {
|
2016-03-27 00:58:12 +00:00
|
|
|
return &Key{
|
2016-03-28 19:53:19 +00:00
|
|
|
name: s.Name,
|
2016-03-27 00:58:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-30 00:39:19 +00:00
|
|
|
// String returns a string representation of Server
|
|
|
|
func (s *Server) String() string {
|
2016-03-27 08:32:04 +00:00
|
|
|
var addrStr, networkStr string
|
2016-03-27 02:30:04 +00:00
|
|
|
if s.Addr != nil {
|
2016-03-27 08:32:04 +00:00
|
|
|
addrStr = s.Addr.String()
|
|
|
|
networkStr = s.Addr.Network()
|
2016-03-27 02:30:04 +00:00
|
|
|
}
|
|
|
|
|
2016-03-28 18:37:25 +00:00
|
|
|
return fmt.Sprintf("%s (Addr: %s/%s) (DC: %s)", s.Name, networkStr, addrStr, s.Datacenter)
|
2016-02-20 01:26:45 +00:00
|
|
|
}
|
|
|
|
|
2017-03-21 23:36:44 +00:00
|
|
|
var versionFormat = regexp.MustCompile(`\d+\.\d+\.\d+`)
|
|
|
|
|
2016-03-30 00:39:19 +00:00
|
|
|
// IsConsulServer returns true if a serf member is a consul server
|
|
|
|
// agent. Returns a bool and a pointer to the Server.
|
|
|
|
func IsConsulServer(m serf.Member) (bool, *Server) {
|
2016-02-20 01:26:45 +00:00
|
|
|
if m.Tags["role"] != "consul" {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
datacenter := m.Tags["dc"]
|
|
|
|
_, bootstrap := m.Tags["bootstrap"]
|
|
|
|
|
2017-05-10 21:25:48 +00:00
|
|
|
_, useTLS := m.Tags["use_tls"]
|
|
|
|
|
2016-02-20 01:26:45 +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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
port_str := m.Tags["port"]
|
|
|
|
port, err := strconv.Atoi(port_str)
|
|
|
|
if err != nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2017-03-21 23:36:44 +00:00
|
|
|
build_version, err := version.NewVersion(versionFormat.FindString(m.Tags["build"]))
|
|
|
|
if err != nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2017-03-15 19:26:54 +00:00
|
|
|
wan_join_port := 0
|
|
|
|
wan_join_port_str, ok := m.Tags["wan_join_port"]
|
|
|
|
if ok {
|
|
|
|
wan_join_port, err = strconv.Atoi(wan_join_port_str)
|
|
|
|
if err != nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-20 01:26:45 +00:00
|
|
|
vsn_str := m.Tags["vsn"]
|
|
|
|
vsn, err := strconv.Atoi(vsn_str)
|
|
|
|
if err != nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2017-03-14 17:43:43 +00:00
|
|
|
raft_vsn := 0
|
|
|
|
raft_vsn_str, ok := m.Tags["raft_vsn"]
|
|
|
|
if ok {
|
|
|
|
raft_vsn, err = strconv.Atoi(raft_vsn_str)
|
|
|
|
if err != nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
2017-02-22 20:53:32 +00:00
|
|
|
}
|
|
|
|
|
2017-03-21 23:36:44 +00:00
|
|
|
_, nonVoter := m.Tags["nonvoter"]
|
|
|
|
|
2016-02-20 01:26:45 +00:00
|
|
|
addr := &net.TCPAddr{IP: m.Addr, Port: port}
|
|
|
|
|
2016-03-30 00:39:19 +00:00
|
|
|
parts := &Server{
|
2017-02-24 04:32:13 +00:00
|
|
|
Name: m.Name,
|
|
|
|
ID: m.Tags["id"],
|
|
|
|
Datacenter: datacenter,
|
|
|
|
Port: port,
|
2017-03-15 19:26:54 +00:00
|
|
|
WanJoinPort: wan_join_port,
|
2017-02-24 04:32:13 +00:00
|
|
|
Bootstrap: bootstrap,
|
|
|
|
Expect: expect,
|
|
|
|
Addr: addr,
|
2017-03-21 23:36:44 +00:00
|
|
|
Build: *build_version,
|
2017-02-24 04:32:13 +00:00
|
|
|
Version: vsn,
|
2017-02-22 20:53:32 +00:00
|
|
|
RaftVersion: raft_vsn,
|
2017-03-18 01:42:28 +00:00
|
|
|
Status: m.Status,
|
2017-03-21 23:36:44 +00:00
|
|
|
NonVoter: nonVoter,
|
2017-05-10 21:25:48 +00:00
|
|
|
UseTLS: useTLS,
|
2016-02-20 01:26:45 +00:00
|
|
|
}
|
|
|
|
return true, parts
|
|
|
|
}
|