github.com/hhrutter/nomad@v0.6.0-rc2.0.20170723054333-80c4b03f0705/nomad/util.go (about) 1 package nomad 2 3 import ( 4 "fmt" 5 "math/rand" 6 "net" 7 "os" 8 "path/filepath" 9 "runtime" 10 "strconv" 11 12 "github.com/hashicorp/serf/serf" 13 ) 14 15 // ensurePath is used to make sure a path exists 16 func ensurePath(path string, dir bool) error { 17 if !dir { 18 path = filepath.Dir(path) 19 } 20 return os.MkdirAll(path, 0755) 21 } 22 23 // RuntimeStats is used to return various runtime information 24 func RuntimeStats() map[string]string { 25 return map[string]string{ 26 "kernel.name": runtime.GOOS, 27 "arch": runtime.GOARCH, 28 "version": runtime.Version(), 29 "max_procs": strconv.FormatInt(int64(runtime.GOMAXPROCS(0)), 10), 30 "goroutines": strconv.FormatInt(int64(runtime.NumGoroutine()), 10), 31 "cpu_count": strconv.FormatInt(int64(runtime.NumCPU()), 10), 32 } 33 } 34 35 // serverParts is used to return the parts of a server role 36 type serverParts struct { 37 Name string 38 Region string 39 Datacenter string 40 Port int 41 Bootstrap bool 42 Expect int 43 MajorVersion int 44 MinorVersion int 45 Addr net.Addr 46 } 47 48 func (s *serverParts) String() string { 49 return fmt.Sprintf("%s (Addr: %s) (DC: %s)", 50 s.Name, s.Addr, s.Datacenter) 51 } 52 53 // Returns if a member is a Nomad server. Returns a boolean, 54 // and a struct with the various important components 55 func isNomadServer(m serf.Member) (bool, *serverParts) { 56 if m.Tags["role"] != "nomad" { 57 return false, nil 58 } 59 60 region := m.Tags["region"] 61 datacenter := m.Tags["dc"] 62 _, bootstrap := m.Tags["bootstrap"] 63 64 expect := 0 65 expect_str, ok := m.Tags["expect"] 66 var err error 67 if ok { 68 expect, err = strconv.Atoi(expect_str) 69 if err != nil { 70 return false, nil 71 } 72 } 73 74 port_str := m.Tags["port"] 75 port, err := strconv.Atoi(port_str) 76 if err != nil { 77 return false, nil 78 } 79 80 // The "vsn" tag was Version, which is now the MajorVersion number. 81 majorVersionStr := m.Tags["vsn"] 82 majorVersion, err := strconv.Atoi(majorVersionStr) 83 if err != nil { 84 return false, nil 85 } 86 87 // To keep some semblance of convention, "mvn" is now the "Minor 88 // Version Number." 89 minorVersionStr := m.Tags["mvn"] 90 minorVersion, err := strconv.Atoi(minorVersionStr) 91 if err != nil { 92 minorVersion = 0 93 } 94 95 addr := &net.TCPAddr{IP: m.Addr, Port: port} 96 parts := &serverParts{ 97 Name: m.Name, 98 Region: region, 99 Datacenter: datacenter, 100 Port: port, 101 Bootstrap: bootstrap, 102 Expect: expect, 103 Addr: addr, 104 MajorVersion: majorVersion, 105 MinorVersion: minorVersion, 106 } 107 return true, parts 108 } 109 110 // shuffleStrings randomly shuffles the list of strings 111 func shuffleStrings(list []string) { 112 for i := range list { 113 j := rand.Intn(i + 1) 114 list[i], list[j] = list[j], list[i] 115 } 116 } 117 118 // maxUint64 returns the maximum value 119 func maxUint64(a, b uint64) uint64 { 120 if a >= b { 121 return a 122 } 123 return b 124 }