package agent

import "github.com/hashicorp/serf/command/agent"

Index

Constants

const (
	MinIPCVersion = 1
	MaxIPCVersion = 1
)
const DefaultBindPort int = 7946

This is the default port that we use for Serf communication

Variables

var DefaultConfig = &Config{
	BindAddr:     "0.0.0.0",
	LogLevel:     "INFO",
	RPCAddr:      "127.0.0.1:7373",
	Protocol:     serf.ProtocolVersionMax,
	ReplayOnJoin: false,
	Profile:      "lan",
	LeaveOnInt:   true,
}

DefaultConfig contains the defaults for configurations.

Functions

func LevelFilter

func LevelFilter() *logutils.LevelFilter

LevelFilter returns a LevelFilter that is configured with the log levels that we use.

func NewLogWriter

func NewLogWriter(buf int) *logWriter

NewLogWriter creates a logWriter with the given buffer capacity

func ValidateLevelFilter

func ValidateLevelFilter(minLevel logutils.LogLevel, filter *logutils.LevelFilter) bool

ValidateLevelFilter verifies that the log levels within the filter are valid.

Types

type Agent

type Agent struct {
	// contains filtered or unexported fields
}

Agent starts and manages a Serf instance, adding some niceties on top of Serf such as storing logs that you can later retrieve, and invoking EventHandlers when events occur.

func Create

func Create(conf *serf.Config, logOutput io.Writer) (*Agent, error)

Start creates a new agent, potentially returning an error

func (*Agent) DeregisterEventHandler

func (a *Agent) DeregisterEventHandler(eh EventHandler)

DeregisterEventHandler removes an EventHandler and prevents more invocations

func (*Agent) ForceLeave

func (a *Agent) ForceLeave(node string) error

ForceLeave is used to eject a failed node from the cluster

func (*Agent) Join

func (a *Agent) Join(addrs []string, replay bool) (n int, err error)

Join asks the Serf instance to join. See the Serf.Join function.

func (*Agent) Leave

func (a *Agent) Leave() error

Leave prepares for a graceful shutdown of the agent and its processes

func (*Agent) RegisterEventHandler

func (a *Agent) RegisterEventHandler(eh EventHandler)

RegisterEventHandler adds an event handler to recieve event notifications

func (*Agent) Serf

func (a *Agent) Serf() *serf.Serf

Returns the Serf agent of the running Agent.

func (*Agent) SerfConfig

func (a *Agent) SerfConfig() *serf.Config

Returns the Serf config of the running Agent.

func (*Agent) Shutdown

func (a *Agent) Shutdown() error

Shutdown closes this agent and all of its processes. Should be preceeded by a Leave for a graceful shutdown.

func (*Agent) ShutdownCh

func (a *Agent) ShutdownCh() <-chan struct{}

ShutdownCh returns a channel that can be selected to wait for the agent to perform a shutdown.

func (*Agent) Start

func (a *Agent) Start() error

Start is used to initiate the event listeners. It is seperate from create so that there isn't a race condition between creating the agent and registering handlers

func (*Agent) UserEvent

func (a *Agent) UserEvent(name string, payload []byte, coalesce bool) error

UserEvent sends a UserEvent on Serf, see Serf.UserEvent.

type AgentIPC

type AgentIPC struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewAgentIPC

func NewAgentIPC(agent *Agent, listener net.Listener,
	logOutput io.Writer, logWriter *logWriter) *AgentIPC

NewAgentIPC is used to create a new Agent IPC handler

func (*AgentIPC) Shutdown

func (i *AgentIPC) Shutdown()

Shutdown is used to shutdown the IPC layer

type AppendSliceValue

type AppendSliceValue []string

AppendSliceValue implements the flag.Value interface and allows multiple calls to the same variable to append a list.

func (*AppendSliceValue) Set

func (s *AppendSliceValue) Set(value string) error

func (*AppendSliceValue) String

func (s *AppendSliceValue) String() string

type Command

type Command struct {
	Ui         cli.Ui
	ShutdownCh <-chan struct{}
	// contains filtered or unexported fields
}

Command is a Command implementation that runs a Serf agent. The command will not end unless a shutdown message is sent on the ShutdownCh. If two messages are sent on the ShutdownCh it will forcibly exit.

func (*Command) Help

func (c *Command) Help() string

func (*Command) Run

func (c *Command) Run(args []string) int

func (*Command) Synopsis

func (c *Command) Synopsis() string

type Config

type Config struct {
	// All the configurations in this section are identical to their
	// Serf counterparts. See the documentation for Serf.Config for
	// more info.
	NodeName string `mapstructure:"node_name"`
	Role     string `mapstructure:"role"`

	// BindAddr is the address that the Serf agent's communication ports
	// will bind to. Serf will use this address to bind to for both TCP
	// and UDP connections. If no port is present in the address, the default
	// port will be used.
	BindAddr string `mapstructure:"bind"`

	// EncryptKey is the secret key to use for encrypting communication
	// traffic for Serf. The secret key must be exactly 16-bytes, base64
	// encoded. The easiest way to do this on Unix machines is this command:
	// "head -c16 /dev/urandom | base64". If this is not specified, the
	// traffic will not be encrypted.
	EncryptKey string `mapstructure:"encrypt_key"`

	// LogLevel is the level of the logs to output.
	// This can be updated during a reload.
	LogLevel string `mapstructure:"log_level"`

	// RPCAddr is the address and port to listen on for the agent's RPC
	// interface.
	RPCAddr string `mapstructure:"rpc_addr"`

	// Protocol is the Serf protocol version to use.
	Protocol int `mapstructure:"protocol"`

	// ReplayOnJoin tells Serf to replay past user events
	// when joining based on a `StartJoin`.
	ReplayOnJoin bool `mapstructure:"replay_on_join"`

	// StartJoin is a list of addresses to attempt to join when the
	// agent starts. If Serf is unable to communicate with any of these
	// addresses, then the agent will error and exit.
	StartJoin []string `mapstructure:"start_join"`

	// EventHandlers is a list of event handlers that will be invoked.
	// These can be updated during a reload.
	EventHandlers []string `mapstructure:"event_handlers"`

	// Profile is used to select a timing profile for Serf. The supported choices
	// are "wan", "lan", and "local". The default is "lan"
	Profile string `mapstructure:"profile"`

	// SnapshotPath is used to allow Serf to snapshot important transactional
	// state to make a more graceful recovery possible. This enables auto
	// re-joining a cluster on failure and avoids old message replay.
	SnapshotPath string `mapstructure:"snapshot_path"`

	// LeaveOnTerm controls if Serf does a graceful leave when receiving
	// the TERM signal. Defaults false. This can be changed on reload.
	LeaveOnTerm bool `mapstructure:"leave_on_terminate"`

	// LeaveOnInt controls if Serf does a graceful leave when receiving
	// the INT signal. Defaults true. This can be changed on reload.
	LeaveOnInt bool `mapstructure:"leave_on_interrupt"`
}

Config is the configuration that can be set for an Agent. Some of these configurations are exposed as command-line flags to `serf agent`, whereas many of the more advanced configurations can only be set by creating a configuration file.

func DecodeConfig

func DecodeConfig(r io.Reader) (*Config, error)

DecodeConfig reads the configuration from the given reader in JSON format and decodes it into a proper Config structure.

func MergeConfig

func MergeConfig(a, b *Config) *Config

MergeConfig merges two configurations together to make a single new configuration.

func ReadConfigPaths

func ReadConfigPaths(paths []string) (*Config, error)

ReadConfigPaths reads the paths in the given order to load configurations. The paths can be to files or directories. If the path is a directory, we read one directory deep and read any files ending in ".json" as configuration files.

func (*Config) BindAddrParts

func (c *Config) BindAddrParts() (string, int, error)

BindAddrParts returns the parts of the BindAddr that should be used to configure Serf.

func (*Config) EncryptBytes

func (c *Config) EncryptBytes() ([]byte, error)

EncryptBytes returns the encryption key configured.

func (*Config) EventScripts

func (c *Config) EventScripts() []EventScript

EventScripts returns the list of EventScripts associated with this configuration and specified by the "event_handlers" configuration.

type EventFilter

type EventFilter struct {
	Event     string
	UserEvent string
}

EventFilter is used to filter which events are processed

func ParseEventFilter

func ParseEventFilter(v string) []EventFilter

ParseEventFilter a string with the event type filters and parses it into a series of EventFilters if it can.

func (*EventFilter) Invoke

func (s *EventFilter) Invoke(e serf.Event) bool

Invoke tests whether or not this event script should be invoked for the given Serf event.

func (*EventFilter) Valid

func (s *EventFilter) Valid() bool

Valid checks if this is a valid agent event script.

type EventHandler

type EventHandler interface {
	HandleEvent(serf.Event)
}

EventHandler is a handler that does things when events happen.

type EventScript

type EventScript struct {
	EventFilter
	Script string
}

EventScript is a single event script that will be executed in the case of an event, and is configured from the command-line or from a configuration file.

func ParseEventScript

func ParseEventScript(v string) []EventScript

ParseEventScript takes a string in the format of "type=script" and parses it into an EventScript struct, if it can.

func (*EventScript) String

func (s *EventScript) String() string

type GatedWriter

type GatedWriter struct {
	Writer io.Writer
	// contains filtered or unexported fields
}

GatedWriter is an io.Writer implementation that buffers all of its data into an internal buffer until it is told to let data through.

func (*GatedWriter) Flush

func (w *GatedWriter) Flush()

Flush tells the GatedWriter to flush any buffered data and to stop buffering.

func (*GatedWriter) Write

func (w *GatedWriter) Write(p []byte) (n int, err error)

type IPCClient

type IPCClient struct {
	// contains filtered or unexported fields
}

func (*IPCClient) Send

func (c *IPCClient) Send(header *responseHeader, obj interface{}) error

send is used to send an object using the MsgPack encoding. send is serialized to prevent write overlaps, while properly buffering.

func (*IPCClient) String

func (c *IPCClient) String() string

type LogHandler

type LogHandler interface {
	HandleLog(string)
}

LogHandler interface is used for clients that want to subscribe to logs, for example to stream them over an IPC mechanism

type Member

type Member struct {
	Name        string
	Addr        net.IP
	Port        uint16
	Role        string
	Status      string
	ProtocolMin uint8
	ProtocolMax uint8
	ProtocolCur uint8
	DelegateMin uint8
	DelegateMax uint8
	DelegateCur uint8
}

type MockEventHandler

type MockEventHandler struct {
	Events []serf.Event
	sync.Mutex
}

MockEventHandler is an EventHandler implementation that can be used for tests.

func (*MockEventHandler) HandleEvent

func (h *MockEventHandler) HandleEvent(e serf.Event)

type RPCClient

type RPCClient struct {
	// contains filtered or unexported fields
}

RPCClient is the RPC client to make requests to the agent RPC.

func NewRPCClient

func NewRPCClient(addr string) (*RPCClient, error)

NewRPCClient is used to create a new RPC client given the address. This will properly dial, handshake, and start listening

func (*RPCClient) Close

func (c *RPCClient) Close() error

Close is used to free any resources associated with the client

func (*RPCClient) ForceLeave

func (c *RPCClient) ForceLeave(node string) error

ForceLeave is used to ask the agent to issue a leave command for a given node

func (*RPCClient) Join

func (c *RPCClient) Join(addrs []string, replay bool) (int, error)

Join is used to instruct the agent to attempt a join

func (*RPCClient) Leave

func (c *RPCClient) Leave() error

Leave is used to trigger a graceful leave and shutdown

func (*RPCClient) Members

func (c *RPCClient) Members() ([]Member, error)

Members is used to fetch a list of known members

func (*RPCClient) Monitor

func (c *RPCClient) Monitor(level logutils.LogLevel, ch chan<- string) (StreamHandle, error)

Monitor is used to subscribe to the logs of the agent

func (*RPCClient) Stop

func (c *RPCClient) Stop(handle StreamHandle) error

Stop is used to unsubscribe from logs or event streams

func (*RPCClient) Stream

func (c *RPCClient) Stream(filter string, ch chan<- map[string]interface{}) (StreamHandle, error)

Stream is used to subscribe to events

func (*RPCClient) UserEvent

func (c *RPCClient) UserEvent(name string, payload []byte, coalesce bool) error

UserEvent is used to trigger sending an event

type ScriptEventHandler

type ScriptEventHandler struct {
	Self    serf.Member
	Scripts []EventScript
	Logger  *log.Logger
	// contains filtered or unexported fields
}

ScriptEventHandler invokes scripts for the events that it receives.

func (*ScriptEventHandler) HandleEvent

func (h *ScriptEventHandler) HandleEvent(e serf.Event)

func (*ScriptEventHandler) UpdateScripts

func (h *ScriptEventHandler) UpdateScripts(scripts []EventScript)

UpdateScripts is used to safely update the scripts we invoke in a thread safe manner

type StreamHandle

type StreamHandle uint64

StreamHandle is an opaque handle passed to stop to stop streaming

Source Files

agent.go command.go config.go event_handler.go event_handler_mock.go flag_slice_value.go gated_writer.go invoke.go ipc.go ipc_event_stream.go ipc_log_stream.go log_levels.go log_writer.go rpc_client.go

Version
v0.3.0
Published
Dec 5, 2013
Platform
windows/amd64
Imports
26 packages
Last checked
3 weeks ago

Tools for package owners.