forked from hashicorp/serf
/
config.go
67 lines (55 loc) · 1.97 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package agent
import (
"fmt"
"net"
"strings"
)
// This is the default port that we use for Serf communication
const DefaultBindPort int = 7946
// 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.
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_addr"`
// RPCAddr is the address and port to listen on for the agent's RPC
// interface.
RPCAddr string `mapstructure:"rpc_addr"`
// EventHandlers is a list of event handlers that will be invoked.
EventHandlers []string `mapstructure:"event_handlers"`
}
// BindAddrParts returns the parts of the BindAddr that should be
// used to configure Serf.
func (c *Config) BindAddrParts() (string, int, error) {
checkAddr := c.BindAddr
if !strings.Contains(checkAddr, ":") {
checkAddr += fmt.Sprintf(":%d", DefaultBindPort)
}
addr, err := net.ResolveTCPAddr("tcp", checkAddr)
if err != nil {
return "", 0, err
}
return addr.IP.String(), addr.Port, nil
}
// EventScripts returns the list of EventScripts associated with this
// configuration and specified by the "event_handlers" configuration.
func (c *Config) EventScripts() ([]EventScript, error) {
result := make([]EventScript, 0, len(c.EventHandlers))
for _, v := range c.EventHandlers {
part, err := ParseEventScript(v)
if err != nil {
return nil, err
}
result = append(result, part...)
}
return result, nil
}