-
Notifications
You must be signed in to change notification settings - Fork 4
/
cluster.go
95 lines (81 loc) · 2.15 KB
/
cluster.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package mesos
import (
"fmt"
"regexp"
)
func DiscoverCluster(client MesosClient) (*Cluster, error) {
uri := "master/state.json"
url := client.buildDiscoveryURL(uri)
cluster := &Cluster{}
if _, _, err := client.doApiRequest(url, cluster); err != nil {
return cluster, ErrClusterDiscoveryError
}
client.setMasterURL(cluster.GetLeader())
if len(cluster.Frameworks) == 0 {
url := fmt.Sprintf("%s/%s", client.masterURL(), uri)
client.doApiRequest(url, cluster)
}
return cluster, nil
}
type Cluster struct {
LeaderPID string `json:"leader"`
Version string `json:"version"`
Frameworks []*Framework `json:"frameworks,omitempty"`
Slaves []*Slave `json:"slaves"`
}
func (c *Cluster) GetLeader() string {
// String to extract IP from: [email protected]:5050
re, _ := regexp.Compile(`[\d]+\.[\d]+\.[\d]+\.[\d]+`)
return re.FindString(c.LeaderPID)
}
func (c *Cluster) LoadSlaveStates(client MesosClient) error {
var erred bool
for i := range c.Slaves {
if err := c.Slaves[i].LoadState(client); err != nil {
erred = true
}
}
if erred {
return ErrSlaveStateLoadError
}
return nil
}
func (c *Cluster) LoadSlaveStats(client MesosClient) error {
var erred bool
for i := range c.Slaves {
if err := c.Slaves[i].LoadStats(client); err != nil {
erred = true
}
}
if erred {
return ErrSlaveStateLoadError
}
return nil
}
func (c *Cluster) GetFramework(framework string) map[string]*Framework {
fs := make(map[string]*Framework)
for _, f := range c.Frameworks {
if f.Name == framework {
fs[f.Id] = f
}
}
return fs
}
func (c *Cluster) GetSlaveById(slaveId string) *Slave {
var slave *Slave
for _, s := range c.Slaves {
if s.Id == slaveId {
slave = s
}
}
return slave
}
func (c *Cluster) GetSlaveByHostName(hostName string) *Slave {
var slave *Slave
for _, s := range c.Slaves {
if s.HostName == hostName {
slave = s
}
}
return slave
}