2013-12-20 01:14:46 +00:00
|
|
|
package agent
|
|
|
|
|
2013-12-20 23:33:13 +00:00
|
|
|
import (
|
2014-11-24 08:36:03 +00:00
|
|
|
"encoding/json"
|
2013-12-20 23:33:13 +00:00
|
|
|
"fmt"
|
2013-12-21 00:39:32 +00:00
|
|
|
"io"
|
|
|
|
"log"
|
2014-01-01 00:45:13 +00:00
|
|
|
"net"
|
2013-12-21 00:39:32 +00:00
|
|
|
"os"
|
2014-09-06 00:22:33 +00:00
|
|
|
"path/filepath"
|
2014-02-24 00:42:39 +00:00
|
|
|
"strconv"
|
2013-12-21 00:39:32 +00:00
|
|
|
"sync"
|
2014-06-16 21:36:12 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/consul"
|
|
|
|
"github.com/hashicorp/consul/consul/structs"
|
|
|
|
"github.com/hashicorp/serf/serf"
|
2013-12-20 23:33:13 +00:00
|
|
|
)
|
|
|
|
|
2014-11-24 08:36:03 +00:00
|
|
|
const (
|
|
|
|
// Path to save agent service definitions
|
|
|
|
servicesDir = "services"
|
|
|
|
|
|
|
|
// Path to save local agent checks
|
|
|
|
checksDir = "checks"
|
2015-01-16 20:39:15 +00:00
|
|
|
|
|
|
|
// errSocketFileExists is the human-friendly error message displayed when
|
|
|
|
// trying to bind a socket to an existing file.
|
|
|
|
errSocketFileExists = "A file exists at the requested socket path %q. " +
|
|
|
|
"If Consul was not shut down properly, the socket file may " +
|
|
|
|
"be left behind. If the path looks correct, remove the file " +
|
|
|
|
"and try again."
|
2015-01-15 08:16:34 +00:00
|
|
|
|
|
|
|
// The ID of the faux health check for maintenance mode
|
|
|
|
maintCheckID = "_maintenance_"
|
2014-11-24 08:36:03 +00:00
|
|
|
)
|
|
|
|
|
2013-12-20 01:14:46 +00:00
|
|
|
/*
|
|
|
|
The agent is the long running process that is run on every machine.
|
|
|
|
It exposes an RPC interface that is used by the CLI to control the
|
|
|
|
agent. The agent runs the query interfaces like HTTP, DNS, and RPC.
|
|
|
|
However, it can run in either a client, or server mode. In server
|
|
|
|
mode, it runs a full Consul server. In client-only mode, it only forwards
|
|
|
|
requests to other Consul servers.
|
|
|
|
*/
|
|
|
|
type Agent struct {
|
|
|
|
config *Config
|
2013-12-20 23:33:13 +00:00
|
|
|
|
2013-12-21 00:39:32 +00:00
|
|
|
// Used for writing our logs
|
|
|
|
logger *log.Logger
|
|
|
|
|
|
|
|
// Output sink for logs
|
|
|
|
logOutput io.Writer
|
|
|
|
|
2013-12-20 23:33:13 +00:00
|
|
|
// We have one of a client or a server, depending
|
|
|
|
// on our configuration
|
|
|
|
server *consul.Server
|
|
|
|
client *consul.Client
|
2013-12-21 00:39:32 +00:00
|
|
|
|
2014-01-16 01:14:50 +00:00
|
|
|
// state stores a local representation of the node,
|
|
|
|
// services and checks. Used for anti-entropy.
|
|
|
|
state localState
|
2014-01-21 20:05:56 +00:00
|
|
|
|
|
|
|
// checkMonitors maps the check ID to an associated monitor
|
|
|
|
checkMonitors map[string]*CheckMonitor
|
2015-01-09 22:43:24 +00:00
|
|
|
|
|
|
|
// checkHTTPs maps the check ID to an associated HTTP check
|
|
|
|
checkHTTPs map[string]*CheckHTTP
|
|
|
|
|
|
|
|
// checkTTLs maps the check ID to an associated check TTL
|
|
|
|
checkTTLs map[string]*CheckTTL
|
|
|
|
|
|
|
|
// checkLock protects updates to the check* maps
|
|
|
|
checkLock sync.Mutex
|
2014-01-21 20:05:56 +00:00
|
|
|
|
2014-08-27 23:49:12 +00:00
|
|
|
// eventCh is used to receive user events
|
|
|
|
eventCh chan serf.UserEvent
|
|
|
|
|
2014-08-28 00:01:10 +00:00
|
|
|
// eventBuf stores the most recent events in a ring buffer
|
|
|
|
// using eventIndex as the next index to insert into. This
|
|
|
|
// is guarded by eventLock. When an insert happens, the
|
|
|
|
// eventNotify group is notified.
|
2014-08-28 17:56:30 +00:00
|
|
|
eventBuf []*UserEvent
|
2014-08-28 00:01:10 +00:00
|
|
|
eventIndex int
|
|
|
|
eventLock sync.RWMutex
|
|
|
|
eventNotify consul.NotifyGroup
|
|
|
|
|
2014-01-21 20:05:56 +00:00
|
|
|
shutdown bool
|
|
|
|
shutdownCh chan struct{}
|
|
|
|
shutdownLock sync.Mutex
|
2013-12-20 01:14:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create is used to create a new Agent. Returns
|
|
|
|
// the agent or potentially an error.
|
2013-12-21 00:39:32 +00:00
|
|
|
func Create(config *Config, logOutput io.Writer) (*Agent, error) {
|
|
|
|
// Ensure we have a log sink
|
|
|
|
if logOutput == nil {
|
|
|
|
logOutput = os.Stderr
|
|
|
|
}
|
|
|
|
|
2013-12-24 00:20:51 +00:00
|
|
|
// Validate the config
|
|
|
|
if config.Datacenter == "" {
|
|
|
|
return nil, fmt.Errorf("Must configure a Datacenter")
|
|
|
|
}
|
|
|
|
if config.DataDir == "" {
|
|
|
|
return nil, fmt.Errorf("Must configure a DataDir")
|
|
|
|
}
|
|
|
|
|
2014-01-01 00:45:13 +00:00
|
|
|
// Try to get an advertise address
|
|
|
|
if config.AdvertiseAddr != "" {
|
|
|
|
if ip := net.ParseIP(config.AdvertiseAddr); ip == nil {
|
|
|
|
return nil, fmt.Errorf("Failed to parse advertise address: %v", config.AdvertiseAddr)
|
|
|
|
}
|
2014-04-11 22:46:55 +00:00
|
|
|
} else if config.BindAddr != "0.0.0.0" && config.BindAddr != "" {
|
|
|
|
config.AdvertiseAddr = config.BindAddr
|
2014-01-01 00:45:13 +00:00
|
|
|
} else {
|
|
|
|
ip, err := consul.GetPrivateIP()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to get advertise address: %v", err)
|
|
|
|
}
|
2014-05-15 18:27:30 +00:00
|
|
|
config.AdvertiseAddr = ip.String()
|
2014-01-01 00:45:13 +00:00
|
|
|
}
|
|
|
|
|
2013-12-20 01:14:46 +00:00
|
|
|
agent := &Agent{
|
2014-01-21 20:05:56 +00:00
|
|
|
config: config,
|
|
|
|
logger: log.New(logOutput, "", log.LstdFlags),
|
|
|
|
logOutput: logOutput,
|
|
|
|
checkMonitors: make(map[string]*CheckMonitor),
|
|
|
|
checkTTLs: make(map[string]*CheckTTL),
|
2015-01-09 22:43:24 +00:00
|
|
|
checkHTTPs: make(map[string]*CheckHTTP),
|
2014-08-27 23:49:12 +00:00
|
|
|
eventCh: make(chan serf.UserEvent, 1024),
|
2014-08-28 17:56:30 +00:00
|
|
|
eventBuf: make([]*UserEvent, 256),
|
2014-01-21 20:05:56 +00:00
|
|
|
shutdownCh: make(chan struct{}),
|
2013-12-20 01:14:46 +00:00
|
|
|
}
|
2013-12-20 23:33:13 +00:00
|
|
|
|
2014-02-07 20:11:34 +00:00
|
|
|
// Initialize the local state
|
|
|
|
agent.state.Init(config, agent.logger)
|
|
|
|
|
2013-12-20 23:33:13 +00:00
|
|
|
// Setup either the client or the server
|
|
|
|
var err error
|
|
|
|
if config.Server {
|
|
|
|
err = agent.setupServer()
|
2014-02-07 20:11:34 +00:00
|
|
|
agent.state.SetIface(agent.server)
|
2014-10-14 22:05:41 +00:00
|
|
|
|
|
|
|
// Automatically register the "consul" service on server nodes
|
|
|
|
consulService := structs.NodeService{
|
2014-10-14 22:42:49 +00:00
|
|
|
Service: consul.ConsulServiceName,
|
|
|
|
ID: consul.ConsulServiceID,
|
2014-10-14 22:05:41 +00:00
|
|
|
Port: agent.config.Ports.Server,
|
2014-10-17 21:29:12 +00:00
|
|
|
Tags: []string{},
|
2014-10-14 22:05:41 +00:00
|
|
|
}
|
|
|
|
agent.state.AddService(&consulService)
|
2013-12-20 23:33:13 +00:00
|
|
|
} else {
|
|
|
|
err = agent.setupClient()
|
2014-02-07 20:11:34 +00:00
|
|
|
agent.state.SetIface(agent.client)
|
2013-12-20 23:33:13 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-11-26 07:58:02 +00:00
|
|
|
// Load checks/services
|
2015-01-08 02:05:46 +00:00
|
|
|
if err := agent.loadServices(config); err != nil {
|
2014-11-24 08:36:03 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-01-08 02:05:46 +00:00
|
|
|
if err := agent.loadChecks(config); err != nil {
|
2014-11-24 08:36:03 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-08-27 23:49:12 +00:00
|
|
|
// Start handling events
|
|
|
|
go agent.handleEvents()
|
|
|
|
|
2014-05-06 19:43:33 +00:00
|
|
|
// Write out the PID file if necessary
|
2014-05-06 16:57:53 +00:00
|
|
|
err = agent.storePid()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-05-06 03:29:50 +00:00
|
|
|
|
2013-12-20 01:14:46 +00:00
|
|
|
return agent, nil
|
|
|
|
}
|
|
|
|
|
2013-12-20 23:33:13 +00:00
|
|
|
// consulConfig is used to return a consul configuration
|
|
|
|
func (a *Agent) consulConfig() *consul.Config {
|
|
|
|
// Start with the provided config or default config
|
|
|
|
var base *consul.Config
|
|
|
|
if a.config.ConsulConfig != nil {
|
|
|
|
base = a.config.ConsulConfig
|
|
|
|
} else {
|
|
|
|
base = consul.DefaultConfig()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Override with our config
|
|
|
|
if a.config.Datacenter != "" {
|
|
|
|
base.Datacenter = a.config.Datacenter
|
|
|
|
}
|
|
|
|
if a.config.DataDir != "" {
|
|
|
|
base.DataDir = a.config.DataDir
|
|
|
|
}
|
|
|
|
if a.config.NodeName != "" {
|
|
|
|
base.NodeName = a.config.NodeName
|
|
|
|
}
|
2014-04-11 22:22:35 +00:00
|
|
|
if a.config.BindAddr != "" {
|
|
|
|
base.SerfLANConfig.MemberlistConfig.BindAddr = a.config.BindAddr
|
|
|
|
base.SerfWANConfig.MemberlistConfig.BindAddr = a.config.BindAddr
|
2013-12-20 23:33:13 +00:00
|
|
|
}
|
2014-04-11 22:22:35 +00:00
|
|
|
if a.config.Ports.SerfLan != 0 {
|
|
|
|
base.SerfLANConfig.MemberlistConfig.BindPort = a.config.Ports.SerfLan
|
|
|
|
base.SerfLANConfig.MemberlistConfig.AdvertisePort = a.config.Ports.SerfLan
|
2013-12-20 23:33:13 +00:00
|
|
|
}
|
2014-04-11 22:22:35 +00:00
|
|
|
if a.config.Ports.SerfWan != 0 {
|
|
|
|
base.SerfWANConfig.MemberlistConfig.BindPort = a.config.Ports.SerfWan
|
|
|
|
base.SerfWANConfig.MemberlistConfig.AdvertisePort = a.config.Ports.SerfWan
|
2013-12-20 23:33:13 +00:00
|
|
|
}
|
2014-04-11 22:22:35 +00:00
|
|
|
if a.config.BindAddr != "" {
|
2014-04-14 19:37:49 +00:00
|
|
|
bindAddr := &net.TCPAddr{
|
|
|
|
IP: net.ParseIP(a.config.BindAddr),
|
|
|
|
Port: a.config.Ports.Server,
|
|
|
|
}
|
|
|
|
base.RPCAddr = bindAddr
|
2014-01-01 00:45:13 +00:00
|
|
|
}
|
|
|
|
if a.config.AdvertiseAddr != "" {
|
|
|
|
base.SerfLANConfig.MemberlistConfig.AdvertiseAddr = a.config.AdvertiseAddr
|
|
|
|
base.SerfWANConfig.MemberlistConfig.AdvertiseAddr = a.config.AdvertiseAddr
|
|
|
|
base.RPCAdvertise = &net.TCPAddr{
|
|
|
|
IP: net.ParseIP(a.config.AdvertiseAddr),
|
2014-04-14 19:37:49 +00:00
|
|
|
Port: a.config.Ports.Server,
|
2014-01-01 00:45:13 +00:00
|
|
|
}
|
2013-12-20 23:33:13 +00:00
|
|
|
}
|
2013-12-25 00:48:07 +00:00
|
|
|
if a.config.Bootstrap {
|
|
|
|
base.Bootstrap = true
|
|
|
|
}
|
2014-06-18 17:32:19 +00:00
|
|
|
if a.config.RejoinAfterLeave {
|
|
|
|
base.RejoinAfterLeave = true
|
|
|
|
}
|
2014-06-20 00:08:48 +00:00
|
|
|
if a.config.BootstrapExpect != 0 {
|
|
|
|
base.BootstrapExpect = a.config.BootstrapExpect
|
2014-06-16 21:36:12 +00:00
|
|
|
}
|
2014-03-09 22:57:03 +00:00
|
|
|
if a.config.Protocol > 0 {
|
|
|
|
base.ProtocolVersion = uint8(a.config.Protocol)
|
|
|
|
}
|
2014-08-05 22:20:35 +00:00
|
|
|
if a.config.ACLToken != "" {
|
|
|
|
base.ACLToken = a.config.ACLToken
|
|
|
|
}
|
2014-08-05 22:36:08 +00:00
|
|
|
if a.config.ACLMasterToken != "" {
|
|
|
|
base.ACLMasterToken = a.config.ACLMasterToken
|
|
|
|
}
|
2014-08-05 22:20:35 +00:00
|
|
|
if a.config.ACLDatacenter != "" {
|
|
|
|
base.ACLDatacenter = a.config.ACLDatacenter
|
|
|
|
}
|
|
|
|
if a.config.ACLTTLRaw != "" {
|
|
|
|
base.ACLTTL = a.config.ACLTTL
|
|
|
|
}
|
|
|
|
if a.config.ACLDefaultPolicy != "" {
|
|
|
|
base.ACLDefaultPolicy = a.config.ACLDefaultPolicy
|
|
|
|
}
|
|
|
|
if a.config.ACLDownPolicy != "" {
|
|
|
|
base.ACLDownPolicy = a.config.ACLDownPolicy
|
|
|
|
}
|
2013-12-20 23:33:13 +00:00
|
|
|
|
2014-06-06 22:36:40 +00:00
|
|
|
// Format the build string
|
|
|
|
revision := a.config.Revision
|
|
|
|
if len(revision) > 8 {
|
|
|
|
revision = revision[:8]
|
|
|
|
}
|
|
|
|
base.Build = fmt.Sprintf("%s%s:%s",
|
|
|
|
a.config.Version, a.config.VersionPrerelease, revision)
|
|
|
|
|
2014-04-04 23:52:39 +00:00
|
|
|
// Copy the TLS configuration
|
|
|
|
base.VerifyIncoming = a.config.VerifyIncoming
|
|
|
|
base.VerifyOutgoing = a.config.VerifyOutgoing
|
|
|
|
base.CAFile = a.config.CAFile
|
|
|
|
base.CertFile = a.config.CertFile
|
|
|
|
base.KeyFile = a.config.KeyFile
|
2014-06-13 18:27:44 +00:00
|
|
|
base.ServerName = a.config.ServerName
|
2014-04-04 23:52:39 +00:00
|
|
|
|
2014-02-07 20:11:34 +00:00
|
|
|
// Setup the ServerUp callback
|
|
|
|
base.ServerUp = a.state.ConsulServerUp
|
|
|
|
|
2014-08-27 23:49:12 +00:00
|
|
|
// Setup the user event callback
|
|
|
|
base.UserEventHandler = func(e serf.UserEvent) {
|
|
|
|
select {
|
|
|
|
case a.eventCh <- e:
|
|
|
|
case <-a.shutdownCh:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-21 00:39:32 +00:00
|
|
|
// Setup the loggers
|
|
|
|
base.LogOutput = a.logOutput
|
2013-12-20 23:33:13 +00:00
|
|
|
return base
|
|
|
|
}
|
|
|
|
|
|
|
|
// setupServer is used to initialize the Consul server
|
|
|
|
func (a *Agent) setupServer() error {
|
2014-09-12 02:52:16 +00:00
|
|
|
config := a.consulConfig()
|
|
|
|
|
2014-10-10 18:13:30 +00:00
|
|
|
if err := a.setupKeyrings(config); err != nil {
|
|
|
|
return fmt.Errorf("Failed to configure keyring: %v", err)
|
2014-09-12 02:52:16 +00:00
|
|
|
}
|
|
|
|
|
2014-09-12 05:46:57 +00:00
|
|
|
server, err := consul.NewServer(config)
|
2013-12-20 23:33:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to start Consul server: %v", err)
|
|
|
|
}
|
|
|
|
a.server = server
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// setupClient is used to initialize the Consul client
|
|
|
|
func (a *Agent) setupClient() error {
|
2014-09-12 05:46:57 +00:00
|
|
|
config := a.consulConfig()
|
|
|
|
|
2014-10-10 18:13:30 +00:00
|
|
|
if err := a.setupKeyrings(config); err != nil {
|
|
|
|
return fmt.Errorf("Failed to configure keyring: %v", err)
|
2014-09-12 05:46:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
client, err := consul.NewClient(config)
|
2013-12-20 23:33:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to start Consul client: %v", err)
|
|
|
|
}
|
|
|
|
a.client = client
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-10-10 18:13:30 +00:00
|
|
|
// setupKeyrings is used to initialize and load keyrings during agent startup
|
|
|
|
func (a *Agent) setupKeyrings(config *consul.Config) error {
|
|
|
|
fileLAN := filepath.Join(a.config.DataDir, serfLANKeyring)
|
|
|
|
fileWAN := filepath.Join(a.config.DataDir, serfWANKeyring)
|
|
|
|
|
|
|
|
if a.config.EncryptKey == "" {
|
|
|
|
goto LOAD
|
|
|
|
}
|
|
|
|
if _, err := os.Stat(fileLAN); err != nil {
|
|
|
|
if err := initKeyring(fileLAN, a.config.EncryptKey); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if a.config.Server {
|
|
|
|
if _, err := os.Stat(fileWAN); err != nil {
|
|
|
|
if err := initKeyring(fileWAN, a.config.EncryptKey); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LOAD:
|
|
|
|
if _, err := os.Stat(fileLAN); err == nil {
|
|
|
|
config.SerfLANConfig.KeyringFile = fileLAN
|
|
|
|
}
|
|
|
|
if err := loadKeyringFile(config.SerfLANConfig); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if a.config.Server {
|
|
|
|
if _, err := os.Stat(fileWAN); err == nil {
|
|
|
|
config.SerfWANConfig.KeyringFile = fileWAN
|
|
|
|
}
|
|
|
|
if err := loadKeyringFile(config.SerfWANConfig); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Success!
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-12-20 23:33:13 +00:00
|
|
|
// RPC is used to make an RPC call to the Consul servers
|
|
|
|
// This allows the agent to implement the Consul.Interface
|
|
|
|
func (a *Agent) RPC(method string, args interface{}, reply interface{}) error {
|
|
|
|
if a.server != nil {
|
|
|
|
return a.server.RPC(method, args, reply)
|
|
|
|
}
|
|
|
|
return a.client.RPC(method, args, reply)
|
|
|
|
}
|
|
|
|
|
2014-04-18 05:46:31 +00:00
|
|
|
// Leave is used to prepare the agent for a graceful shutdown
|
2013-12-20 01:14:46 +00:00
|
|
|
func (a *Agent) Leave() error {
|
2013-12-20 23:33:13 +00:00
|
|
|
if a.server != nil {
|
|
|
|
return a.server.Leave()
|
|
|
|
} else {
|
|
|
|
return a.client.Leave()
|
|
|
|
}
|
2013-12-20 01:14:46 +00:00
|
|
|
}
|
|
|
|
|
2014-04-18 05:46:31 +00:00
|
|
|
// Shutdown is used to hard stop the agent. Should be
|
2014-12-04 23:25:06 +00:00
|
|
|
// preceded by a call to Leave to do it gracefully.
|
2013-12-20 01:14:46 +00:00
|
|
|
func (a *Agent) Shutdown() error {
|
2013-12-21 00:39:32 +00:00
|
|
|
a.shutdownLock.Lock()
|
|
|
|
defer a.shutdownLock.Unlock()
|
|
|
|
|
|
|
|
if a.shutdown {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-01-21 20:05:56 +00:00
|
|
|
// Stop all the checks
|
|
|
|
a.checkLock.Lock()
|
|
|
|
defer a.checkLock.Unlock()
|
|
|
|
for _, chk := range a.checkMonitors {
|
|
|
|
chk.Stop()
|
|
|
|
}
|
|
|
|
for _, chk := range a.checkTTLs {
|
|
|
|
chk.Stop()
|
|
|
|
}
|
|
|
|
|
2015-01-09 22:43:24 +00:00
|
|
|
for _, chk := range a.checkHTTPs {
|
|
|
|
chk.Stop()
|
|
|
|
}
|
|
|
|
|
2013-12-21 00:39:32 +00:00
|
|
|
a.logger.Println("[INFO] agent: requesting shutdown")
|
|
|
|
var err error
|
2013-12-20 23:33:13 +00:00
|
|
|
if a.server != nil {
|
2013-12-21 00:39:32 +00:00
|
|
|
err = a.server.Shutdown()
|
2013-12-20 23:33:13 +00:00
|
|
|
} else {
|
2013-12-21 00:39:32 +00:00
|
|
|
err = a.client.Shutdown()
|
2013-12-20 23:33:13 +00:00
|
|
|
}
|
2013-12-21 00:39:32 +00:00
|
|
|
|
2014-05-06 16:57:53 +00:00
|
|
|
pidErr := a.deletePid()
|
|
|
|
if pidErr != nil {
|
|
|
|
a.logger.Println("[WARN] agent: could not delete pid file ", pidErr)
|
|
|
|
}
|
2014-05-06 03:29:50 +00:00
|
|
|
|
2013-12-21 00:39:32 +00:00
|
|
|
a.logger.Println("[INFO] agent: shutdown complete")
|
|
|
|
a.shutdown = true
|
|
|
|
close(a.shutdownCh)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-04-18 05:46:31 +00:00
|
|
|
// ShutdownCh is used to return a channel that can be
|
|
|
|
// selected to wait for the agent to perform a shutdown.
|
2013-12-21 00:39:32 +00:00
|
|
|
func (a *Agent) ShutdownCh() <-chan struct{} {
|
|
|
|
return a.shutdownCh
|
2013-12-20 01:14:46 +00:00
|
|
|
}
|
2013-12-30 22:42:41 +00:00
|
|
|
|
|
|
|
// JoinLAN is used to have the agent join a LAN cluster
|
|
|
|
func (a *Agent) JoinLAN(addrs []string) (n int, err error) {
|
|
|
|
a.logger.Printf("[INFO] agent: (LAN) joining: %v", addrs)
|
|
|
|
if a.server != nil {
|
|
|
|
n, err = a.server.JoinLAN(addrs)
|
|
|
|
} else {
|
|
|
|
n, err = a.client.JoinLAN(addrs)
|
|
|
|
}
|
|
|
|
a.logger.Printf("[INFO] agent: (LAN) joined: %d Err: %v", n, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// JoinWAN is used to have the agent join a WAN cluster
|
|
|
|
func (a *Agent) JoinWAN(addrs []string) (n int, err error) {
|
|
|
|
a.logger.Printf("[INFO] agent: (WAN) joining: %v", addrs)
|
|
|
|
if a.server != nil {
|
|
|
|
n, err = a.server.JoinWAN(addrs)
|
|
|
|
} else {
|
|
|
|
err = fmt.Errorf("Must be a server to join WAN cluster")
|
|
|
|
}
|
|
|
|
a.logger.Printf("[INFO] agent: (WAN) joined: %d Err: %v", n, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// ForceLeave is used to remove a failed node from the cluster
|
|
|
|
func (a *Agent) ForceLeave(node string) (err error) {
|
|
|
|
a.logger.Printf("[INFO] Force leaving node: %v", node)
|
|
|
|
if a.server != nil {
|
|
|
|
err = a.server.RemoveFailedNode(node)
|
|
|
|
} else {
|
|
|
|
err = a.client.RemoveFailedNode(node)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
a.logger.Printf("[WARN] Failed to remove node: %v", err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-05-25 23:59:48 +00:00
|
|
|
// LocalMember is used to return the local node
|
|
|
|
func (a *Agent) LocalMember() serf.Member {
|
2014-05-29 18:21:56 +00:00
|
|
|
if a.server != nil {
|
|
|
|
return a.server.LocalMember()
|
|
|
|
} else {
|
|
|
|
return a.client.LocalMember()
|
|
|
|
}
|
2014-05-25 23:59:48 +00:00
|
|
|
}
|
|
|
|
|
2014-04-18 05:46:31 +00:00
|
|
|
// LANMembers is used to retrieve the LAN members
|
2013-12-30 22:42:41 +00:00
|
|
|
func (a *Agent) LANMembers() []serf.Member {
|
|
|
|
if a.server != nil {
|
|
|
|
return a.server.LANMembers()
|
|
|
|
} else {
|
|
|
|
return a.client.LANMembers()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-18 05:46:31 +00:00
|
|
|
// WANMembers is used to retrieve the WAN members
|
2013-12-30 22:42:41 +00:00
|
|
|
func (a *Agent) WANMembers() []serf.Member {
|
|
|
|
if a.server != nil {
|
|
|
|
return a.server.WANMembers()
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2014-01-21 19:52:25 +00:00
|
|
|
|
|
|
|
// StartSync is called once Services and Checks are registered.
|
|
|
|
// This is called to prevent a race between clients and the anti-entropy routines
|
|
|
|
func (a *Agent) StartSync() {
|
|
|
|
// Start the anti entropy routine
|
|
|
|
go a.state.antiEntropy(a.shutdownCh)
|
|
|
|
}
|
2014-01-30 21:39:02 +00:00
|
|
|
|
2014-04-18 05:46:31 +00:00
|
|
|
// PauseSync is used to pause anti-entropy while bulk changes are make
|
2014-02-07 20:19:56 +00:00
|
|
|
func (a *Agent) PauseSync() {
|
|
|
|
a.state.Pause()
|
|
|
|
}
|
|
|
|
|
2014-04-18 05:46:31 +00:00
|
|
|
// ResumeSync is used to unpause anti-entropy after bulk changes are make
|
2014-02-07 20:19:56 +00:00
|
|
|
func (a *Agent) ResumeSync() {
|
|
|
|
a.state.Resume()
|
|
|
|
}
|
|
|
|
|
2014-11-24 08:36:03 +00:00
|
|
|
// persistService saves a service definition to a JSON file in the data dir
|
|
|
|
func (a *Agent) persistService(service *structs.NodeService) error {
|
2015-01-08 03:11:21 +00:00
|
|
|
svcPath := filepath.Join(a.config.DataDir, servicesDir, stringHash(service.ID))
|
2014-11-24 08:36:03 +00:00
|
|
|
if _, err := os.Stat(svcPath); os.IsNotExist(err) {
|
|
|
|
encoded, err := json.Marshal(service)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err := os.MkdirAll(filepath.Dir(svcPath), 0700); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fh, err := os.OpenFile(svcPath, os.O_CREATE|os.O_WRONLY, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer fh.Close()
|
|
|
|
if _, err := fh.Write(encoded); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// purgeService removes a persisted service definition file from the data dir
|
|
|
|
func (a *Agent) purgeService(serviceID string) error {
|
2015-01-08 03:11:21 +00:00
|
|
|
svcPath := filepath.Join(a.config.DataDir, servicesDir, stringHash(serviceID))
|
2014-11-24 08:36:03 +00:00
|
|
|
if _, err := os.Stat(svcPath); err == nil {
|
|
|
|
return os.Remove(svcPath)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// persistCheck saves a check definition to the local agent's state directory
|
2014-11-29 20:25:01 +00:00
|
|
|
func (a *Agent) persistCheck(check *structs.HealthCheck, chkType *CheckType) error {
|
2015-01-08 03:11:21 +00:00
|
|
|
checkPath := filepath.Join(a.config.DataDir, checksDir, stringHash(check.CheckID))
|
2014-11-29 20:25:01 +00:00
|
|
|
if _, err := os.Stat(checkPath); !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the persisted check
|
|
|
|
p := persistedCheck{check, chkType}
|
|
|
|
|
|
|
|
encoded, err := json.Marshal(p)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err := os.MkdirAll(filepath.Dir(checkPath), 0700); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fh, err := os.OpenFile(checkPath, os.O_CREATE|os.O_WRONLY, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer fh.Close()
|
|
|
|
if _, err := fh.Write(encoded); err != nil {
|
|
|
|
return err
|
2014-11-24 08:36:03 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// purgeCheck removes a persisted check definition file from the data dir
|
|
|
|
func (a *Agent) purgeCheck(checkID string) error {
|
2015-01-08 03:11:21 +00:00
|
|
|
checkPath := filepath.Join(a.config.DataDir, checksDir, stringHash(checkID))
|
2014-11-24 08:36:03 +00:00
|
|
|
if _, err := os.Stat(checkPath); err == nil {
|
|
|
|
return os.Remove(checkPath)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-01-30 21:39:02 +00:00
|
|
|
// AddService is used to add a service entry.
|
|
|
|
// This entry is persistent and the agent will make a best effort to
|
|
|
|
// ensure it is registered
|
2014-11-25 03:24:32 +00:00
|
|
|
func (a *Agent) AddService(service *structs.NodeService, chkType *CheckType, persist bool) error {
|
2014-01-30 21:39:02 +00:00
|
|
|
if service.Service == "" {
|
|
|
|
return fmt.Errorf("Service name missing")
|
|
|
|
}
|
|
|
|
if service.ID == "" && service.Service != "" {
|
|
|
|
service.ID = service.Service
|
|
|
|
}
|
|
|
|
if chkType != nil && !chkType.Valid() {
|
|
|
|
return fmt.Errorf("Check type is not valid")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the service
|
|
|
|
a.state.AddService(service)
|
|
|
|
|
2014-11-24 08:36:03 +00:00
|
|
|
// Persist the service to a file
|
2014-11-25 03:24:32 +00:00
|
|
|
if persist {
|
|
|
|
if err := a.persistService(service); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-11-24 09:58:39 +00:00
|
|
|
}
|
2014-11-24 08:36:03 +00:00
|
|
|
|
2014-01-30 21:39:02 +00:00
|
|
|
// Create an associated health check
|
|
|
|
if chkType != nil {
|
|
|
|
check := &structs.HealthCheck{
|
|
|
|
Node: a.config.NodeName,
|
|
|
|
CheckID: fmt.Sprintf("service:%s", service.ID),
|
|
|
|
Name: fmt.Sprintf("Service '%s' check", service.Service),
|
2014-10-15 17:14:46 +00:00
|
|
|
Status: structs.HealthCritical,
|
2014-11-07 02:24:04 +00:00
|
|
|
Notes: chkType.Notes,
|
2014-01-30 21:39:02 +00:00
|
|
|
ServiceID: service.ID,
|
|
|
|
ServiceName: service.Service,
|
|
|
|
}
|
2014-11-25 03:24:32 +00:00
|
|
|
if err := a.AddCheck(check, chkType, persist); err != nil {
|
2014-01-30 21:39:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveService is used to remove a service entry.
|
|
|
|
// The agent will make a best effort to ensure it is deregistered
|
2014-11-26 07:58:02 +00:00
|
|
|
func (a *Agent) RemoveService(serviceID string, persist bool) error {
|
2014-10-14 22:05:41 +00:00
|
|
|
// Protect "consul" service from deletion by a user
|
2014-10-14 22:42:49 +00:00
|
|
|
if a.server != nil && serviceID == consul.ConsulServiceID {
|
2014-10-14 22:05:41 +00:00
|
|
|
return fmt.Errorf(
|
2014-10-15 21:56:15 +00:00
|
|
|
"Deregistering the %s service is not allowed",
|
|
|
|
consul.ConsulServiceID)
|
2014-10-14 22:05:41 +00:00
|
|
|
}
|
|
|
|
|
2014-01-30 21:39:02 +00:00
|
|
|
// Remove service immeidately
|
|
|
|
a.state.RemoveService(serviceID)
|
|
|
|
|
2014-11-24 08:36:03 +00:00
|
|
|
// Remove the service from the data dir
|
2014-11-26 07:58:02 +00:00
|
|
|
if persist {
|
|
|
|
if err := a.purgeService(serviceID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-11-24 08:36:03 +00:00
|
|
|
}
|
|
|
|
|
2014-01-30 21:39:02 +00:00
|
|
|
// Deregister any associated health checks
|
|
|
|
checkID := fmt.Sprintf("service:%s", serviceID)
|
2015-01-08 06:26:40 +00:00
|
|
|
if err := a.RemoveCheck(checkID, persist); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] agent: removed service %q", serviceID)
|
|
|
|
return nil
|
2014-01-30 21:39:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddCheck is used to add a health check to the agent.
|
|
|
|
// This entry is persistent and the agent will make a best effort to
|
|
|
|
// ensure it is registered. The Check may include a CheckType which
|
|
|
|
// is used to automatically update the check status
|
2014-11-25 03:24:32 +00:00
|
|
|
func (a *Agent) AddCheck(check *structs.HealthCheck, chkType *CheckType, persist bool) error {
|
2014-01-30 21:39:02 +00:00
|
|
|
if check.CheckID == "" {
|
|
|
|
return fmt.Errorf("CheckID missing")
|
|
|
|
}
|
|
|
|
if chkType != nil && !chkType.Valid() {
|
|
|
|
return fmt.Errorf("Check type is not valid")
|
|
|
|
}
|
|
|
|
|
|
|
|
a.checkLock.Lock()
|
|
|
|
defer a.checkLock.Unlock()
|
|
|
|
|
|
|
|
// Check if already registered
|
|
|
|
if chkType != nil {
|
|
|
|
if chkType.IsTTL() {
|
2014-06-17 23:48:19 +00:00
|
|
|
if existing, ok := a.checkTTLs[check.CheckID]; ok {
|
|
|
|
existing.Stop()
|
2014-01-30 21:39:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ttl := &CheckTTL{
|
|
|
|
Notify: &a.state,
|
|
|
|
CheckID: check.CheckID,
|
|
|
|
TTL: chkType.TTL,
|
|
|
|
Logger: a.logger,
|
|
|
|
}
|
|
|
|
ttl.Start()
|
|
|
|
a.checkTTLs[check.CheckID] = ttl
|
|
|
|
|
2015-01-09 22:43:24 +00:00
|
|
|
} else if chkType.IsHTTP() {
|
|
|
|
if existing, ok := a.checkHTTPs[check.CheckID]; ok {
|
|
|
|
existing.Stop()
|
|
|
|
}
|
|
|
|
if chkType.Interval < MinInterval {
|
|
|
|
a.logger.Println(fmt.Sprintf("[WARN] agent: check '%s' has interval below minimum of %v",
|
|
|
|
check.CheckID, MinInterval))
|
|
|
|
chkType.Interval = MinInterval
|
|
|
|
}
|
|
|
|
|
|
|
|
http := &CheckHTTP{
|
|
|
|
Notify: &a.state,
|
|
|
|
CheckID: check.CheckID,
|
|
|
|
HTTP: chkType.HTTP,
|
|
|
|
Interval: chkType.Interval,
|
|
|
|
Logger: a.logger,
|
|
|
|
}
|
|
|
|
http.Start()
|
|
|
|
a.checkHTTPs[check.CheckID] = http
|
|
|
|
|
2014-01-30 21:39:02 +00:00
|
|
|
} else {
|
2014-06-17 23:48:19 +00:00
|
|
|
if existing, ok := a.checkMonitors[check.CheckID]; ok {
|
|
|
|
existing.Stop()
|
2014-01-30 21:39:02 +00:00
|
|
|
}
|
2014-04-21 21:42:42 +00:00
|
|
|
if chkType.Interval < MinInterval {
|
|
|
|
a.logger.Println(fmt.Sprintf("[WARN] agent: check '%s' has interval below minimum of %v",
|
|
|
|
check.CheckID, MinInterval))
|
|
|
|
chkType.Interval = MinInterval
|
|
|
|
}
|
2014-01-30 21:39:02 +00:00
|
|
|
|
|
|
|
monitor := &CheckMonitor{
|
|
|
|
Notify: &a.state,
|
|
|
|
CheckID: check.CheckID,
|
|
|
|
Script: chkType.Script,
|
|
|
|
Interval: chkType.Interval,
|
|
|
|
Logger: a.logger,
|
|
|
|
}
|
|
|
|
monitor.Start()
|
|
|
|
a.checkMonitors[check.CheckID] = monitor
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add to the local state for anti-entropy
|
|
|
|
a.state.AddCheck(check)
|
2014-11-24 08:36:03 +00:00
|
|
|
|
|
|
|
// Persist the check
|
2014-11-25 03:24:32 +00:00
|
|
|
if persist {
|
2014-11-29 20:25:01 +00:00
|
|
|
return a.persistCheck(check, chkType)
|
2014-11-25 03:24:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2014-01-30 21:39:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveCheck is used to remove a health check.
|
|
|
|
// The agent will make a best effort to ensure it is deregistered
|
2014-11-26 07:58:02 +00:00
|
|
|
func (a *Agent) RemoveCheck(checkID string, persist bool) error {
|
2014-01-30 21:39:02 +00:00
|
|
|
// Add to the local state for anti-entropy
|
|
|
|
a.state.RemoveCheck(checkID)
|
|
|
|
|
|
|
|
a.checkLock.Lock()
|
|
|
|
defer a.checkLock.Unlock()
|
|
|
|
|
|
|
|
// Stop any monitors
|
|
|
|
if check, ok := a.checkMonitors[checkID]; ok {
|
|
|
|
check.Stop()
|
|
|
|
delete(a.checkMonitors, checkID)
|
|
|
|
}
|
2015-01-12 22:34:39 +00:00
|
|
|
if check, ok := a.checkHTTPs[checkID]; ok {
|
|
|
|
check.Stop()
|
|
|
|
delete(a.checkHTTPs, checkID)
|
|
|
|
}
|
2014-01-30 21:39:02 +00:00
|
|
|
if check, ok := a.checkTTLs[checkID]; ok {
|
|
|
|
check.Stop()
|
|
|
|
delete(a.checkTTLs, checkID)
|
|
|
|
}
|
2014-11-26 07:58:02 +00:00
|
|
|
if persist {
|
|
|
|
return a.purgeCheck(checkID)
|
|
|
|
}
|
2015-01-08 06:26:40 +00:00
|
|
|
log.Printf("[DEBUG] agent: removed check %q", checkID)
|
2014-11-26 07:58:02 +00:00
|
|
|
return nil
|
2014-01-30 21:39:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateCheck is used to update the status of a check.
|
|
|
|
// This can only be used with checks of the TTL type.
|
|
|
|
func (a *Agent) UpdateCheck(checkID, status, output string) error {
|
|
|
|
a.checkLock.Lock()
|
|
|
|
defer a.checkLock.Unlock()
|
|
|
|
|
|
|
|
check, ok := a.checkTTLs[checkID]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("CheckID does not have associated TTL")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the status through CheckTTL to reset the TTL
|
|
|
|
check.SetStatus(status, output)
|
|
|
|
return nil
|
|
|
|
}
|
2014-02-24 00:42:39 +00:00
|
|
|
|
|
|
|
// Stats is used to get various debugging state from the sub-systems
|
|
|
|
func (a *Agent) Stats() map[string]map[string]string {
|
|
|
|
toString := func(v uint64) string {
|
|
|
|
return strconv.FormatUint(v, 10)
|
|
|
|
}
|
|
|
|
var stats map[string]map[string]string
|
|
|
|
if a.server != nil {
|
|
|
|
stats = a.server.Stats()
|
|
|
|
} else {
|
|
|
|
stats = a.client.Stats()
|
|
|
|
}
|
|
|
|
stats["agent"] = map[string]string{
|
|
|
|
"check_monitors": toString(uint64(len(a.checkMonitors))),
|
|
|
|
"check_ttls": toString(uint64(len(a.checkTTLs))),
|
|
|
|
"checks": toString(uint64(len(a.state.checks))),
|
|
|
|
"services": toString(uint64(len(a.state.services))),
|
|
|
|
}
|
2014-06-06 21:40:22 +00:00
|
|
|
|
|
|
|
revision := a.config.Revision
|
|
|
|
if len(revision) > 8 {
|
|
|
|
revision = revision[:8]
|
|
|
|
}
|
|
|
|
stats["build"] = map[string]string{
|
|
|
|
"revision": revision,
|
|
|
|
"version": a.config.Version,
|
|
|
|
"prerelease": a.config.VersionPrerelease,
|
|
|
|
}
|
2014-02-24 00:42:39 +00:00
|
|
|
return stats
|
|
|
|
}
|
2014-05-06 03:29:50 +00:00
|
|
|
|
2014-05-06 19:43:33 +00:00
|
|
|
// storePid is used to write out our PID to a file if necessary
|
2014-05-06 16:57:53 +00:00
|
|
|
func (a *Agent) storePid() error {
|
2014-05-06 19:43:33 +00:00
|
|
|
// Quit fast if no pidfile
|
2014-05-06 03:29:50 +00:00
|
|
|
pidPath := a.config.PidFile
|
2014-05-06 19:43:33 +00:00
|
|
|
if pidPath == "" {
|
|
|
|
return nil
|
|
|
|
}
|
2014-05-06 03:29:50 +00:00
|
|
|
|
2014-05-06 19:43:33 +00:00
|
|
|
// Open the PID file
|
|
|
|
pidFile, err := os.OpenFile(pidPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Could not open pid file: %v", err)
|
2014-05-06 03:29:50 +00:00
|
|
|
}
|
2014-05-06 19:43:33 +00:00
|
|
|
defer pidFile.Close()
|
2014-05-06 16:57:53 +00:00
|
|
|
|
2014-05-06 19:43:33 +00:00
|
|
|
// Write out the PID
|
|
|
|
pid := os.Getpid()
|
|
|
|
_, err = pidFile.WriteString(fmt.Sprintf("%d", pid))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Could not write to pid file: %s", err)
|
|
|
|
}
|
2014-05-06 16:57:53 +00:00
|
|
|
return nil
|
2014-05-06 03:29:50 +00:00
|
|
|
}
|
|
|
|
|
2014-05-06 19:43:33 +00:00
|
|
|
// deletePid is used to delete our PID on exit
|
2014-05-06 16:57:53 +00:00
|
|
|
func (a *Agent) deletePid() error {
|
2014-05-06 19:43:33 +00:00
|
|
|
// Quit fast if no pidfile
|
2014-05-06 03:29:50 +00:00
|
|
|
pidPath := a.config.PidFile
|
2014-05-06 19:43:33 +00:00
|
|
|
if pidPath == "" {
|
|
|
|
return nil
|
|
|
|
}
|
2014-05-06 03:29:50 +00:00
|
|
|
|
2014-05-06 19:43:33 +00:00
|
|
|
stat, err := os.Stat(pidPath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Could not remove pid file: %s", err)
|
|
|
|
}
|
2014-05-06 03:29:50 +00:00
|
|
|
|
2014-05-06 19:43:33 +00:00
|
|
|
if stat.IsDir() {
|
|
|
|
return fmt.Errorf("Specified pid file path is directory")
|
2014-05-06 03:29:50 +00:00
|
|
|
}
|
2014-05-06 16:57:53 +00:00
|
|
|
|
2014-05-06 19:43:33 +00:00
|
|
|
err = os.Remove(pidPath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Could not remove pid file: %s", err)
|
|
|
|
}
|
2014-05-06 16:57:53 +00:00
|
|
|
return nil
|
2014-05-06 03:29:50 +00:00
|
|
|
}
|
2014-11-26 07:58:02 +00:00
|
|
|
|
2015-01-08 02:05:46 +00:00
|
|
|
// loadServices will load service definitions from configuration and persisted
|
|
|
|
// definitions on disk, and load them into the local agent.
|
|
|
|
func (a *Agent) loadServices(conf *Config) error {
|
2014-11-26 07:58:02 +00:00
|
|
|
// Register the services from config
|
|
|
|
for _, service := range conf.Services {
|
|
|
|
ns := service.NodeService()
|
|
|
|
chkType := service.CheckType()
|
|
|
|
if err := a.AddService(ns, chkType, false); err != nil {
|
|
|
|
return fmt.Errorf("Failed to register service '%s': %v", service.ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load any persisted services
|
2015-01-08 05:24:47 +00:00
|
|
|
svcDir := filepath.Join(a.config.DataDir, servicesDir)
|
|
|
|
if _, err := os.Stat(svcDir); os.IsNotExist(err) {
|
|
|
|
return nil
|
2014-11-26 07:58:02 +00:00
|
|
|
}
|
|
|
|
|
2015-01-08 05:24:47 +00:00
|
|
|
err := filepath.Walk(svcDir, func(path string, fi os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if fi.Name() == servicesDir {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
filePath := filepath.Join(svcDir, fi.Name())
|
|
|
|
fh, err := os.Open(filePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
content := make([]byte, fi.Size())
|
|
|
|
if _, err := fh.Read(content); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var svc *structs.NodeService
|
|
|
|
if err := json.Unmarshal(content, &svc); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := a.state.services[svc.ID]; ok {
|
|
|
|
// Purge previously persisted service. This allows config to be
|
|
|
|
// preferred over services persisted from the API.
|
2015-01-08 06:26:40 +00:00
|
|
|
a.logger.Printf("[DEBUG] agent: service %q exists, not restoring from %q",
|
2015-01-08 05:24:47 +00:00
|
|
|
svc.ID, filePath)
|
|
|
|
return a.purgeService(svc.ID)
|
|
|
|
} else {
|
2015-01-08 06:26:40 +00:00
|
|
|
a.logger.Printf("[DEBUG] agent: restored service definition %q from %q",
|
2015-01-08 05:24:47 +00:00
|
|
|
svc.ID, filePath)
|
|
|
|
return a.AddService(svc, nil, false)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return err
|
2014-11-26 07:58:02 +00:00
|
|
|
}
|
|
|
|
|
2015-01-08 02:05:46 +00:00
|
|
|
// unloadServices will deregister all services other than the 'consul' service
|
|
|
|
// known to the local agent.
|
|
|
|
func (a *Agent) unloadServices() error {
|
|
|
|
for _, service := range a.state.Services() {
|
|
|
|
if service.ID == consul.ConsulServiceID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := a.RemoveService(service.ID, false); err != nil {
|
|
|
|
return fmt.Errorf("Failed deregistering service '%s': %v", service.ID, err)
|
2014-11-26 07:58:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-08 02:05:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// loadChecks loads check definitions and/or persisted check definitions from
|
|
|
|
// disk and re-registers them with the local agent.
|
|
|
|
func (a *Agent) loadChecks(conf *Config) error {
|
2014-11-26 07:58:02 +00:00
|
|
|
// Register the checks from config
|
|
|
|
for _, check := range conf.Checks {
|
|
|
|
health := check.HealthCheck(conf.NodeName)
|
|
|
|
chkType := &check.CheckType
|
|
|
|
if err := a.AddCheck(health, chkType, false); err != nil {
|
|
|
|
return fmt.Errorf("Failed to register check '%s': %v %v", check.Name, err, check)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load any persisted checks
|
2015-01-08 05:24:47 +00:00
|
|
|
checkDir := filepath.Join(a.config.DataDir, checksDir)
|
|
|
|
if _, err := os.Stat(checkDir); os.IsNotExist(err) {
|
|
|
|
return nil
|
2014-11-26 07:58:02 +00:00
|
|
|
}
|
|
|
|
|
2015-01-08 05:24:47 +00:00
|
|
|
err := filepath.Walk(checkDir, func(path string, fi os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if fi.Name() == checksDir {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
filePath := filepath.Join(checkDir, fi.Name())
|
|
|
|
fh, err := os.Open(filePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
content := make([]byte, fi.Size())
|
|
|
|
if _, err := fh.Read(content); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var p persistedCheck
|
|
|
|
if err := json.Unmarshal(content, &p); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := a.state.checks[p.Check.CheckID]; ok {
|
|
|
|
// Purge previously persisted check. This allows config to be
|
|
|
|
// preferred over persisted checks from the API.
|
2015-01-08 06:26:40 +00:00
|
|
|
a.logger.Printf("[DEBUG] agent: check %q exists, not restoring from %q",
|
2015-01-08 05:24:47 +00:00
|
|
|
p.Check.CheckID, filePath)
|
|
|
|
return a.purgeCheck(p.Check.CheckID)
|
|
|
|
} else {
|
|
|
|
// Default check to critical to avoid placing potentially unhealthy
|
|
|
|
// services into the active pool
|
|
|
|
p.Check.Status = structs.HealthCritical
|
|
|
|
|
2015-01-08 06:26:40 +00:00
|
|
|
a.logger.Printf("[DEBUG] agent: restored health check %q from %q",
|
2015-01-08 05:24:47 +00:00
|
|
|
p.Check.CheckID, filePath)
|
|
|
|
return a.AddCheck(p.Check, p.ChkType, false)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return err
|
2014-11-26 07:58:02 +00:00
|
|
|
}
|
2015-01-08 02:05:46 +00:00
|
|
|
|
|
|
|
// unloadChecks will deregister all checks known to the local agent.
|
|
|
|
func (a *Agent) unloadChecks() error {
|
|
|
|
for _, check := range a.state.Checks() {
|
|
|
|
if err := a.RemoveCheck(check.CheckID, false); err != nil {
|
|
|
|
return fmt.Errorf("Failed deregistering check '%s': %s", check.CheckID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2015-01-15 08:16:34 +00:00
|
|
|
|
2015-01-15 08:25:36 +00:00
|
|
|
// EnableServiceMaintenance will register a false health check against the given
|
|
|
|
// service ID with critical status. This will exclude the service from queries.
|
2015-01-15 08:16:34 +00:00
|
|
|
func (a *Agent) EnableServiceMaintenance(serviceID string) error {
|
|
|
|
var service *structs.NodeService
|
|
|
|
for _, svc := range a.state.Services() {
|
|
|
|
if svc.ID == serviceID {
|
|
|
|
service = svc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the service exists
|
|
|
|
if service == nil {
|
|
|
|
return fmt.Errorf("No service registered with ID %q", serviceID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure maintenance mode is not already enabled
|
|
|
|
for _, check := range a.state.Checks() {
|
|
|
|
if check.CheckID == maintCheckID {
|
|
|
|
return fmt.Errorf("Maintenance mode already enabled for service %q", serviceID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create and register the critical health check
|
|
|
|
check := &structs.HealthCheck{
|
|
|
|
Node: a.config.NodeName,
|
|
|
|
CheckID: maintCheckID,
|
|
|
|
Name: "Service Maintenance Mode",
|
|
|
|
Notes: "Maintenance mode is enabled for this service",
|
|
|
|
ServiceID: service.ID,
|
|
|
|
ServiceName: service.Service,
|
|
|
|
Status: structs.HealthCritical,
|
|
|
|
}
|
2015-01-15 18:25:22 +00:00
|
|
|
a.AddCheck(check, nil, true)
|
2015-01-15 08:16:34 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-15 08:25:36 +00:00
|
|
|
// DisableServiceMaintenance will deregister the fake maintenance mode check
|
|
|
|
// if the service has been marked as in maintenance.
|
2015-01-15 08:16:34 +00:00
|
|
|
func (a *Agent) DisableServiceMaintenance(serviceID string) error {
|
|
|
|
var service *structs.NodeService
|
|
|
|
for _, svc := range a.state.Services() {
|
|
|
|
if svc.ID == serviceID {
|
|
|
|
service = svc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the service exists
|
|
|
|
if service == nil {
|
|
|
|
return fmt.Errorf("No service registered with ID %q", serviceID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure maintenance mode is enabled
|
|
|
|
for _, check := range a.state.Checks() {
|
|
|
|
if check.CheckID == maintCheckID {
|
|
|
|
goto DEREGISTER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fmt.Errorf("Maintenance mode not enabled for service %q", serviceID)
|
|
|
|
|
|
|
|
DEREGISTER:
|
|
|
|
// Deregister the maintenance check
|
2015-01-15 18:25:22 +00:00
|
|
|
a.RemoveCheck(maintCheckID, true)
|
2015-01-15 08:16:34 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|