open-consul/command/agent/agent.go

247 lines
5.9 KiB
Go
Raw Normal View History

2013-12-20 01:14:46 +00:00
package agent
2013-12-20 23:33:13 +00:00
import (
"fmt"
"github.com/hashicorp/consul/consul"
2013-12-30 22:42:41 +00:00
"github.com/hashicorp/serf/serf"
2013-12-21 00:39:32 +00:00
"io"
"log"
"os"
"sync"
2013-12-20 23:33:13 +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
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")
}
2013-12-20 01:14:46 +00:00
agent := &Agent{
2013-12-21 00:39:32 +00:00
config: config,
logger: log.New(logOutput, "", log.LstdFlags),
logOutput: logOutput,
shutdownCh: make(chan struct{}),
2013-12-20 01:14:46 +00:00
}
2013-12-20 23:33:13 +00:00
// Setup either the client or the server
var err error
if config.Server {
err = agent.setupServer()
} else {
err = agent.setupClient()
}
if err != nil {
return nil, err
}
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
}
2013-12-21 00:39:32 +00:00
if a.config.EncryptKey != "" {
key, _ := a.config.EncryptBytes()
base.SerfLANConfig.MemberlistConfig.SecretKey = key
base.SerfWANConfig.MemberlistConfig.SecretKey = key
}
2013-12-20 23:33:13 +00:00
if a.config.NodeName != "" {
base.NodeName = a.config.NodeName
}
if a.config.SerfBindAddr != "" {
base.SerfLANConfig.MemberlistConfig.BindAddr = a.config.SerfBindAddr
base.SerfWANConfig.MemberlistConfig.BindAddr = a.config.SerfBindAddr
}
if a.config.SerfLanPort != 0 {
2013-12-27 22:50:02 +00:00
base.SerfLANConfig.MemberlistConfig.BindPort = a.config.SerfLanPort
2013-12-20 23:33:13 +00:00
}
if a.config.SerfWanPort != 0 {
2013-12-27 22:50:02 +00:00
base.SerfWANConfig.MemberlistConfig.BindPort = a.config.SerfWanPort
2013-12-20 23:33:13 +00:00
}
2013-12-21 00:39:32 +00:00
if a.config.ServerAddr != "" {
base.RPCAddr = a.config.ServerAddr
2013-12-20 23:33:13 +00:00
}
if a.config.Bootstrap {
base.Bootstrap = true
}
2013-12-20 23:33:13 +00:00
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 {
server, err := consul.NewServer(a.consulConfig())
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 {
client, err := consul.NewClient(a.consulConfig())
if err != nil {
return fmt.Errorf("Failed to start Consul client: %v", err)
}
a.client = client
return nil
}
// 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)
}
2013-12-20 01:14:46 +00:00
// Leave prepares the agent for a graceful shutdown
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
}
// Shutdown is used to hard stop the agent. Should be preceeded
// by a call to Leave to do it gracefully.
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
}
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
a.logger.Println("[INFO] agent: shutdown complete")
a.shutdown = true
close(a.shutdownCh)
return err
}
// ShutdownCh returns a channel that can be selected to wait
// for the agent to perform a shutdown.
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
}
// Used to retrieve the LAN members
func (a *Agent) LANMembers() []serf.Member {
if a.server != nil {
return a.server.LANMembers()
} else {
return a.client.LANMembers()
}
}
// Used to retrieve the WAN members
func (a *Agent) WANMembers() []serf.Member {
if a.server != nil {
return a.server.WANMembers()
} else {
return nil
}
}