open-nomad/nomad/util.go

211 lines
4.5 KiB
Go
Raw Normal View History

2015-06-01 15:49:10 +00:00
package nomad
import (
2015-06-04 10:33:12 +00:00
"fmt"
2015-06-05 22:14:08 +00:00
"math/rand"
2015-06-03 11:35:48 +00:00
"net"
2015-06-01 15:49:10 +00:00
"os"
"path/filepath"
2015-06-03 10:26:50 +00:00
"runtime"
"strconv"
2015-06-03 11:35:48 +00:00
2017-09-07 23:56:15 +00:00
version "github.com/hashicorp/go-version"
2015-06-03 11:35:48 +00:00
"github.com/hashicorp/serf/serf"
2015-06-01 15:49:10 +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)
}
2015-06-03 10:26:50 +00:00
2015-08-20 22:29:30 +00:00
// RuntimeStats is used to return various runtime information
func RuntimeStats() map[string]string {
2015-06-03 10:26:50 +00:00
return map[string]string{
"kernel.name": runtime.GOOS,
"arch": runtime.GOARCH,
"version": runtime.Version(),
"max_procs": strconv.FormatInt(int64(runtime.GOMAXPROCS(0)), 10),
"goroutines": strconv.FormatInt(int64(runtime.NumGoroutine()), 10),
"cpu_count": strconv.FormatInt(int64(runtime.NumCPU()), 10),
2015-06-03 10:26:50 +00:00
}
}
2015-06-03 11:35:48 +00:00
// serverParts is used to return the parts of a server role
type serverParts struct {
Name string
ID string
Region string
Datacenter string
Port int
Bootstrap bool
Expect int
MajorVersion int
MinorVersion int
2017-09-07 23:56:15 +00:00
Build version.Version
RaftVersion int
Addr net.Addr
RPCAddr net.Addr
2017-09-07 23:56:15 +00:00
Status serf.MemberStatus
2015-06-03 11:35:48 +00:00
}
2015-06-04 10:33:12 +00:00
func (s *serverParts) String() string {
return fmt.Sprintf("%s (Addr: %s) (DC: %s)",
s.Name, s.Addr, s.Datacenter)
}
2015-06-03 11:35:48 +00:00
// Returns if a member is a Nomad server. Returns a boolean,
// and a struct with the various important components
func isNomadServer(m serf.Member) (bool, *serverParts) {
if m.Tags["role"] != "nomad" {
return false, nil
}
2017-11-27 22:46:37 +00:00
id := "unknown"
if v, ok := m.Tags["id"]; ok {
id = v
}
2015-06-03 11:35:48 +00:00
region := m.Tags["region"]
datacenter := m.Tags["dc"]
_, bootstrap := m.Tags["bootstrap"]
expect := 0
2018-01-16 21:35:32 +00:00
expectStr, ok := m.Tags["expect"]
2015-06-03 11:35:48 +00:00
var err error
if ok {
2018-01-16 21:35:32 +00:00
expect, err = strconv.Atoi(expectStr)
2015-06-03 11:35:48 +00:00
if err != nil {
return false, nil
}
}
// If the server is missing the rpc_addr tag, default to the serf advertise addr
2018-01-16 21:35:32 +00:00
rpcIP := net.ParseIP(m.Tags["rpc_addr"])
if rpcIP == nil {
rpcIP = m.Addr
}
2018-01-16 21:35:32 +00:00
portStr := m.Tags["port"]
port, err := strconv.Atoi(portStr)
2015-06-03 11:35:48 +00:00
if err != nil {
return false, nil
}
2018-01-16 21:35:32 +00:00
buildVersion, err := version.NewVersion(m.Tags["build"])
2017-09-07 23:56:15 +00:00
if err != nil {
return false, nil
}
// The "vsn" tag was Version, which is now the MajorVersion number.
majorVersionStr := m.Tags["vsn"]
majorVersion, err := strconv.Atoi(majorVersionStr)
2015-06-03 11:35:48 +00:00
if err != nil {
return false, nil
}
// To keep some semblance of convention, "mvn" is now the "Minor
// Version Number."
minorVersionStr := m.Tags["mvn"]
minorVersion, err := strconv.Atoi(minorVersionStr)
if err != nil {
minorVersion = 0
}
2018-01-16 21:35:32 +00:00
raftVsn := 0
raftVsnString, ok := m.Tags["raft_vsn"]
if ok {
2018-01-16 21:35:32 +00:00
raftVsn, err = strconv.Atoi(raftVsnString)
if err != nil {
return false, nil
}
}
2015-06-03 11:35:48 +00:00
addr := &net.TCPAddr{IP: m.Addr, Port: port}
2018-01-16 21:35:32 +00:00
rpcAddr := &net.TCPAddr{IP: rpcIP, Port: port}
2015-06-03 11:35:48 +00:00
parts := &serverParts{
Name: m.Name,
ID: id,
Region: region,
Datacenter: datacenter,
Port: port,
Bootstrap: bootstrap,
Expect: expect,
Addr: addr,
RPCAddr: rpcAddr,
MajorVersion: majorVersion,
MinorVersion: minorVersion,
2018-01-16 21:35:32 +00:00
Build: *buildVersion,
RaftVersion: raftVsn,
2017-09-07 23:56:15 +00:00
Status: m.Status,
2015-06-03 11:35:48 +00:00
}
return true, parts
}
2015-06-05 22:14:08 +00:00
2017-09-07 23:56:15 +00:00
// ServersMeetMinimumVersion returns whether the given alive servers are at least on the
// given Nomad version
func ServersMeetMinimumVersion(members []serf.Member, minVersion *version.Version) bool {
for _, member := range members {
if valid, parts := isNomadServer(member); valid && parts.Status == serf.StatusAlive {
// Check if the versions match - version.LessThan will return true for
// 0.8.0-rc1 < 0.8.0, so we want to ignore the metadata
versionsMatch := slicesMatch(minVersion.Segments(), parts.Build.Segments())
if parts.Build.LessThan(minVersion) && !versionsMatch {
2017-09-07 23:56:15 +00:00
return false
}
}
}
return true
}
func slicesMatch(a, b []int) bool {
if a == nil && b == nil {
return true
}
if a == nil || b == nil {
return false
}
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
2015-07-24 00:30:07 +00:00
// shuffleStrings randomly shuffles the list of strings
func shuffleStrings(list []string) {
for i := range list {
j := rand.Intn(i + 1)
list[i], list[j] = list[j], list[i]
}
}
2015-08-05 00:13:40 +00:00
// maxUint64 returns the maximum value
2017-10-13 21:36:02 +00:00
func maxUint64(inputs ...uint64) uint64 {
l := len(inputs)
if l == 0 {
return 0
} else if l == 1 {
return inputs[0]
2015-08-05 00:13:40 +00:00
}
2017-10-13 21:36:02 +00:00
max := inputs[0]
for i := 1; i < l; i++ {
cur := inputs[i]
if cur > max {
max = cur
}
}
return max
2015-08-05 00:13:40 +00:00
}