forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vms.go
154 lines (118 loc) · 3.38 KB
/
vms.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package director
import (
"encoding/json"
"fmt"
"strings"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
)
type VMInfo struct {
AgentID string `json:"agent_id"`
JobName string `json:"job_name"`
ID string `json:"id"`
Index *int `json:"index"`
ProcessState string `json:"job_state"` // e.g. "running"
Bootstrap bool
IPs []string `json:"ips"`
DNS []string `json:"dns"`
AZ string `json:"az"`
State string `json:"state"`
VMID string `json:"vm_cid"`
VMType string `json:"vm_type"`
ResourcePool string `json:"resource_pool"`
DiskID string `json:"disk_cid"`
DiskIDs []string `json:"disk_cids"`
Processes []VMInfoProcess
Vitals VMInfoVitals
ResurrectionPaused bool `json:"resurrection_paused"`
}
type VMInfoProcess struct {
Name string
State string // e.g. "running"
CPU VMInfoVitalsCPU `json:"cpu"`
Mem VMInfoVitalsMemIntSize
Uptime VMInfoVitalsUptime
}
type VMInfoVitals struct {
CPU VMInfoVitalsCPU `json:"cpu"`
Mem VMInfoVitalsMemSize
Swap VMInfoVitalsMemSize
Uptime VMInfoVitalsUptime
Load []string
Disk map[string]VMInfoVitalsDiskSize
}
func (v VMInfoVitals) SystemDisk() VMInfoVitalsDiskSize { return v.Disk["system"] }
func (v VMInfoVitals) EphemeralDisk() VMInfoVitalsDiskSize { return v.Disk["ephemeral"] }
func (v VMInfoVitals) PersistentDisk() VMInfoVitalsDiskSize { return v.Disk["persistent"] }
type VMInfoVitalsCPU struct {
Total *float64 // used by VMInfoProcess
Sys string
User string
Wait string
}
type VMInfoVitalsDiskSize struct {
InodePercent string `json:"inode_percent"`
Percent string
}
type VMInfoVitalsMemSize struct {
KB string `json:"kb"`
Percent string
}
type VMInfoVitalsMemIntSize struct {
KB *uint64 `json:"kb"`
Percent *float64
}
type VMInfoVitalsUptime struct {
Seconds *uint64 `json:"secs"` // e.g. 48307
}
func (i VMInfo) IsRunning() bool {
if i.ProcessState != "running" {
return false
}
for _, p := range i.Processes {
if !p.IsRunning() {
return false
}
}
return true
}
func (p VMInfoProcess) IsRunning() bool {
return p.State == "running"
}
func (d DeploymentImpl) VMInfos() ([]VMInfo, error) {
infos, err := d.client.DeploymentVMInfos(d.name)
if err != nil {
return nil, err
}
return infos, nil
}
func (c Client) DeploymentVMInfos(deploymentName string) ([]VMInfo, error) {
return c.deploymentResourceInfos(deploymentName, "vms")
}
func (c Client) deploymentResourceInfos(deploymentName string, resourceType string) ([]VMInfo, error) {
if len(deploymentName) == 0 {
return nil, bosherr.Error("Expected non-empty deployment name")
}
path := fmt.Sprintf("/deployments/%s/%s?format=full", deploymentName, resourceType)
_, resultBytes, err := c.taskClientRequest.GetResult(path)
if err != nil {
return nil, bosherr.WrapErrorf(
err, "Listing deployment '%s' %s infos", deploymentName, resourceType)
}
var resps []VMInfo
for _, piece := range strings.Split(string(resultBytes), "\n") {
if len(piece) == 0 {
continue
}
var resp VMInfo
err := json.Unmarshal([]byte(piece), &resp)
if err != nil {
return nil, bosherr.WrapErrorf(
err, "Unmarshaling %s info response: '%s'", strings.TrimSuffix(resourceType, "s"), string(piece))
}
if len(resp.DiskIDs) == 0 && resp.DiskID != "" {
resp.DiskIDs = []string{resp.DiskID}
}
resps = append(resps, resp)
}
return resps, nil
}