open-consul/consul/util.go

47 lines
887 B
Go
Raw Normal View History

2013-12-12 19:07:14 +00:00
package consul
2013-12-19 22:18:55 +00:00
import (
2013-12-19 22:37:54 +00:00
"github.com/hashicorp/serf/serf"
2013-12-19 22:18:55 +00:00
"os"
"path/filepath"
2013-12-19 22:37:54 +00:00
"strconv"
"strings"
2013-12-19 22:18:55 +00:00
)
2013-12-12 19:07:14 +00:00
// strContains checks if a list contains a string
func strContains(l []string, s string) bool {
for _, v := range l {
if v == s {
return true
}
}
return false
}
2013-12-19 22:18:55 +00:00
// ensurePath is used to make sure a path exists
func ensurePath(path string, dir bool) error {
if !dir {
path = filepath.Dir(path)
}
return os.MkdirAll(path, 0755)
}
2013-12-19 22:37:54 +00:00
// Returns if a member is a consul server. Returns a bool,
// the data center, and the rpc port
func isConsulServer(m serf.Member) (bool, string, int) {
role := m.Role
if !strings.HasPrefix(role, "consul:") {
return false, "", 0
}
parts := strings.SplitN(role, ":", 3)
datacenter := parts[1]
port_str := parts[2]
port, err := strconv.Atoi(port_str)
if err != nil {
return false, "", 0
}
return true, datacenter, port
}