-
Notifications
You must be signed in to change notification settings - Fork 162
/
vms.go
185 lines (142 loc) · 4.21 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package director
import (
"encoding/json"
"fmt"
"strings"
"time"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
)
const runningState = "running"
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"
Active *bool `json:"active"`
Bootstrap bool
IPs []string `json:"ips"`
Deployment string `json:"deployment_name"`
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"`
Ignore bool `json:"ignore"`
DiskIDs []string `json:"disk_cids"`
VMCreatedAtRaw string `json:"vm_created_at"`
VMCreatedAt time.Time `json:"-"`
CloudProperties interface{} `json:"cloud_properties"`
Processes []VMInfoProcess
Vitals VMInfoVitals
ResurrectionPaused bool `json:"resurrection_paused"`
Stemcell VmInfoStemcell
}
type VmInfoStemcell struct {
Name string
Version string
ApiVersion int `json:"api_version"`
}
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) InstanceState() string {
if i.ProcessState != runningState || len(i.Processes) > 0 {
return i.ProcessState
}
return ""
}
func (i VMInfo) IsRunning() bool {
if i.InstanceState() != runningState {
return false
}
for _, p := range i.Processes {
if !p.IsRunning() {
return false
}
}
return true
}
func (p VMInfoProcess) IsRunning() bool {
return p.State == runningState
}
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))
}
resp.Deployment = deploymentName
if len(resp.DiskIDs) == 0 && resp.DiskID != "" {
resp.DiskIDs = []string{resp.DiskID}
}
resp.VMCreatedAt, err = TimeParser{}.Parse(resp.VMCreatedAtRaw)
if err != nil {
return resps, bosherr.WrapErrorf(err, "Converting created_at '%s' to time", resp.VMCreatedAtRaw)
}
resps = append(resps, resp)
}
return resps, nil
}