/
host.go
412 lines (378 loc) · 12.3 KB
/
host.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
package rest
import (
"encoding/json"
"errors"
"fmt"
"net/url"
"path"
)
// Host describes a host record from the rest api
type Host struct {
Appliance struct {
Broker bool `json:"broker"`
ClusterID string `json:"clusterId"`
Cma string `json:"cma"`
CPUGovernor string `json:"cpuGovernor"`
CRS bool `json:"crs"`
DbName string `json:"dbName"`
DoNotParticipate bool `json:"doNotParticipate"`
Firmware struct {
Active string `json:"active"`
Software string `json:"software"`
PendingSwap bool `json:"pendingSwap"`
} `json:"firmware"`
Hostname string `json:"hostname"`
Loglevel string `json:"loglevel"`
MaxCloneDensity int `json:"maxCloneDensity"`
Ntp string `json:"ntp"`
Timezone string `json:"timezone"`
Role string `json:"role"`
} `json:"appliance"`
Capabilities struct {
StorageTypes []string `json:"storageTypes"`
} `json:"capabilities"`
Certificate struct {
Expiration interface{} `json:"expiration"`
Issuer string `json:"issuer"`
Status string `json:"status"`
} `json:"certificate"`
Hardware struct {
BIOS struct {
ReleaseDate string `json:"releaseDate"`
Vender string `json:"vender"`
Version string `json:"version"`
} `json:"BIOS"`
Chasis struct {
AssetTag string `json:"assetTag"`
Manufacturer string `json:"manufacturer"`
Version string `json:"version"`
} `json:"Chasis"`
HyperThreadingEnabled bool `json:"HyperThreadingEnabled"`
PhysicalCPUs int `json:"PhysicalCPUs"`
PhysicalCoresPerCPU int `json:"PhysicalCoresPerCPU"`
Processor []struct {
Cores int `json:"cores"`
Family string `json:"family"`
Manufacturer string `json:"manufacturer"`
Threads int `json:"threads"`
Version string `json:"version"`
} `json:"Processor"`
System struct {
Manufacturer string `json:"manufacturer"`
ProductName string `json:"productName"`
} `json:"System"`
TotalPhysicalMemory int `json:"TotalPhysicalMemory"`
CPUFeatures interface{}
Memory []struct {
Size int `json:"size"`
Type string `json:"type"`
} `json:"memory"`
VideoCards []struct {
Bus int `json:"bus"`
DeviceClass int `json:"deviceClass"`
DeviceID int `json:"deviceId"`
Domain int `json:"domain"`
Func int `json:"func"`
IommuGroup int `json:"iommu_group"`
Mode string `json:"mode"`
Path string `json:"path"`
Slot int `json:"slot"`
VendorID int `json:"vendorId"`
} `json:"videoCards"`
} `json:"hardware"`
Hostid string `json:"hostid"`
Hostname string `json:"hostname"`
IP string `json:"ip"`
Networking map[string]interface{} `json:"networking"`
RdbID string `json:"rdbId"`
Software interface{} `json:"software"`
State string `json:"state"`
Storage struct {
Blockdevices []interface{} `json:"blockdevices"`
Disk map[string]interface{} `json:"disk"`
RAM struct {
RamdiskPercent int `json:"ramdiskPercent"`
SwapSize int `json:"swapSize"`
Swappiness int `json:"swappiness"`
Zram struct {
Compression string `json:"compression"`
Dedup string `json:"dedup"`
Filesystems struct {
Users struct {
Mountpoint string `json:"mountpoint"`
} `json:"users"`
} `json:"filesystems"`
Mountpoint string `json:"mountpoint"`
} `json:"zram"`
} `json:"ram"`
} `json:"storage"`
Tags []string `json:"tags"`
}
func (host Host) String() string {
json, _ := json.MarshalIndent(host, "", " ")
return string(json)
}
// ListHosts returns an array of all host with an optional filter string
func (client *Client) ListHosts(query string) ([]Host, error) {
var hosts []Host
path := "hosts"
if query != "" {
path += "?" + query
}
body, err := client.request("GET", path, nil)
if err != nil {
return hosts, err
}
err = json.Unmarshal(body, &hosts)
return hosts, err
}
// GetHost requests a single guest by hostid
func (client *Client) GetHost(hostid string) (Host, error) {
var host Host
if hostid == "" {
return host, errors.New("hostid cannot be empty")
}
body, err := client.request("GET", "host/"+hostid, nil)
if err != nil {
return host, err
}
err = json.Unmarshal(body, &host)
return host, err
}
// GetHostByName requests a host by hostname
func (client *Client) GetHostByName(name string) (*Host, error) {
var hosts, err = client.ListHosts("hostname=" + url.QueryEscape(name))
if err != nil {
return nil, err
}
for _, host := range hosts {
if host.Hostname == name {
return &host, nil
}
}
return nil, errors.New("Host not found")
}
// GetHostByIP requests a host by hostname
func (client *Client) GetHostByIP(ip string) (*Host, error) {
var hosts, err = client.ListHosts("ip=" + ip)
if err != nil {
return nil, err
}
for _, host := range hosts {
if host.IP == ip {
return &host, nil
}
}
return nil, errors.New("Host not found")
}
// UpdateAppliance updates settings from Host.appliance
func (host *Host) UpdateAppliance(client *Client) (string, error) {
var result string
data := map[string]interface{}{"appliance": host.Appliance}
jsonValue, err := json.Marshal(data)
if err != nil {
return "", err
}
body, err := client.request("PUT", "host/"+host.Hostid, jsonValue)
if err == nil {
result = string(body)
}
return result, err
}
// Delete removes a host from the database
func (host *Host) Delete(client *Client) error {
if host.Hostid == "" {
return errors.New("id cannot be empty")
}
_, err := client.request("DELETE", "host/"+host.Hostid, nil)
if err != nil {
return err
}
return err
}
// RestartServices calls restart hive services
func (host *Host) RestartServices(client *Client) error {
_, err := client.request("POST", "host/"+host.Hostid+"/services/hive-services/restart", nil)
return err
}
// Reboot calls reboot on a host
func (host *Host) Reboot(client *Client) error {
_, err := client.request("POST", "host/"+host.Hostid+"/system/reboot", nil)
return err
}
// Shutdown calls shutdown on a host
func (host *Host) Shutdown(client *Client) error {
_, err := client.request("POST", "host/"+host.Hostid+"/system/shutdown", nil)
return err
}
// Version is a structure containing version information returned by HostVersion
type Version struct {
Major uint `json:"major"`
Minor uint `json:"minor"`
Patch uint `json:"patch"`
Version string `json:"version"`
}
// SetState can be used to set a host's state to available or maintenance
func (host *Host) SetState(client *Client, state string) (*Task, error) {
return client.getTaskFromResponse(client.request("POST", "host/"+host.Hostid+"/state?state="+state, nil))
}
// GetState gets the current state of the host
func (host *Host) GetState(client *Client) (string, error) {
body, err := client.request("GET", "host/"+host.Hostid+"/state", nil)
if err != nil {
return "", err
}
var state string
err = json.Unmarshal(body, &state)
return state, err
}
// UnjoinCluster removes a host from the cluster
func (host *Host) UnjoinCluster(client *Client) (*Task, error) {
return client.getTaskFromResponse(client.request("POST", "host/"+host.Hostid+"/cluster/unjoin", nil))
}
// HostPackageInfo contains information about software and firmware packages
type HostPackageInfo struct {
Packages []string `json:"packages"`
Current string `json:"current"`
}
// ListSoftware returns the current software version and available packages
func (host *Host) ListSoftware(client *Client) (HostPackageInfo, error) {
var info HostPackageInfo
body, err := client.request("GET", "host/"+host.Hostid+"/firmware/software/packages", nil)
if err != nil {
return info, err
}
err = json.Unmarshal(body, &info)
return info, err
}
// DeleteSoftware deletes a software package from a host
func (host *Host) DeleteSoftware(client *Client, pkg string) error {
_, err := client.request("DELETE", "host/"+host.Hostid+"/firmware/software/"+pkg, nil)
return err
}
// UploadSoftware uploads a firmware pkg file to the host
func (host *Host) UploadSoftware(client *Client, filename string) error {
if client.CheckHostVersion("8.2.6") != nil {
_, err := client.postMultipart(fmt.Sprintf("host/%s/firmware/software/upload", host.Hostid), "data", filename, nil)
return err
}
sp := StoragePool{ID: "softwarePackage"}
return sp.Upload(client, filename, path.Base(filename))
}
// RestartNetworking calls restarts networking on the host
func (host *Host) RestartNetworking(client *Client) error {
_, err := client.request("POST", "host/"+host.Hostid+"/networking/networking/restart", nil)
return err
}
// ChangeGatewayMode enable or disable gateway mode on the host
func (host *Host) ChangeGatewayMode(client *Client, enable bool) error {
jsonValue, _ := json.Marshal(map[string]interface{}{"enable": enable})
_, err := client.request("POST", "host/"+host.Hostid+"/changeGatewayMode", jsonValue)
return err
}
// EnableCRS enables crs on the host
func (host *Host) EnableCRS(client *Client) error {
_, err := client.request("POST", "host/"+host.Hostid+"/enableCRS", nil)
return err
}
// DisableCRS disables crs on the host
func (host *Host) DisableCRS(client *Client) error {
_, err := client.request("POST", "host/"+host.Hostid+"/disableCRS", nil)
return err
}
type HostNetwork struct {
Name string `json:"-"`
DHCP bool `json:"dhcp"`
DNS string `json:"dns"`
Gw string `json:"gw"`
Interface string `json:"interface"`
IP string `json:"ip"`
Mask string `json:"mask"`
Search string `json:"search"`
VLAN int64 `json:"vlan"`
}
func (net HostNetwork) MarshalJSON() ([]byte, error) {
switch net.Name {
case "production":
return json.Marshal(net)
case "storage":
return json.Marshal(map[string]interface{}{
"interface": net.Interface,
"ip": net.IP,
"mask": net.Mask,
"vlan": net.VLAN,
})
default:
return json.Marshal(map[string]interface{}{
"interface": net.Interface,
"vlan": net.VLAN,
})
}
}
func (net *HostNetwork) UnmarshalJSON(b []byte) error {
return json.Unmarshal(b, &(net))
}
// GetNetwork retrieves settings for a host network
func (host Host) GetNetwork(client *Client, name string) (HostNetwork, error) {
net := HostNetwork{Name: name}
body, err := client.request("GET", "host/"+host.Hostid+"/networking/"+name, nil)
if err != nil {
return net, err
}
err = json.Unmarshal(body, &net)
return net, err
}
// SetNetwork adds or edits network settings for the host
func (host Host) SetNetwork(client *Client, net HostNetwork) error {
jsonValue, err := json.Marshal(net)
if err != nil {
return err
}
_, err = client.request("POST", "host/"+host.Hostid+"/networking/"+net.Name, jsonValue)
return err
}
type HostNetworkInterface struct {
Name string `json:"name"`
Address string `json:"address"`
Speed int64 `json:"speed"`
Carrier int64 `json:"carrier"`
Duplex string `json:"duplex"`
MTU int64 `json:"mtu"`
LinkModes []struct {
Speed string `json:"speed"`
Duplex string `json:"duplex"`
} `json:"linkModes"`
Autoneg bool `json:"autoneg"`
Link bool `json:"link"`
PortTypes string `json:"portTypes"`
Driver string `json:"driver"`
}
// ListNics returns information about the network interfaces on a host
func (host Host) ListNics(client *Client) ([]HostNetworkInterface, error) {
interfaces := []HostNetworkInterface{}
body, err := client.request("GET", "host/"+host.Hostid+"/networking/interfaces", nil)
if err != nil {
return interfaces, err
}
err = json.Unmarshal(body, &interfaces)
return interfaces, err
}
type HostNetworkInterfaceSettings struct {
Speed int64 `json:"speed"`
Duplex string `json:"duplex"`
MTU int64 `json:"mtu,omitempty"`
}
// UpdateNetworkInterface hardcodes settings for a nic
func (host Host) UpdateNicSettings(client *Client, nic string, settings HostNetworkInterfaceSettings) error {
jsonValue, err := json.Marshal(settings)
if err != nil {
return err
}
_, err = client.request("POST", "host/"+host.Hostid+"/networking/interface/"+nic, jsonValue)
return err
}
// UpdateNetworkInterface resets the settings for a nic
func (host Host) ResetNicSettings(client *Client, nic string) error {
_, err := client.request("DELETE", "host/"+host.Hostid+"/networking/interface/"+nic, nil)
return err
}