forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.go
399 lines (337 loc) · 9.01 KB
/
settings.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
package settings
import (
"fmt"
"github.com/cloudfoundry/bosh-agent/platform/disk"
)
type DiskAssociations struct {
Associations []DiskAssociation `json:"disk_associations"`
}
type DiskAssociation struct {
Name string `json:"name"`
DiskCID string `json:"cid"`
}
const (
RootUsername = "root"
VCAPUsername = "vcap"
AdminGroup = "admin"
SudoersGroup = "bosh_sudoers"
SshersGroup = "bosh_sshers"
EphemeralUserPrefix = "bosh_"
)
type Settings struct {
AgentID string `json:"agent_id"`
Blobstore Blobstore `json:"blobstore"`
Disks Disks `json:"disks"`
Env Env `json:"env"`
Networks Networks `json:"networks"`
Ntp []string `json:"ntp"`
Mbus string `json:"mbus"`
VM VM `json:"vm"`
}
type UpdateSettings struct {
DiskAssociations []DiskAssociation `json:"disk_associations"`
TrustedCerts string `json:"trusted_certs"`
}
type Source interface {
PublicSSHKeyForUsername(string) (string, error)
Settings() (Settings, error)
}
type Blobstore struct {
Type string `json:"provider"`
Options map[string]interface{} `json:"options"`
}
type Disks struct {
// e.g "/dev/sda", "1"
System string `json:"system"`
// Older CPIs returned disk settings as string
// e.g "/dev/sdb", "2"
// Newer CPIs will populate it in a hash
// e.g {"path" => "/dev/sdc", "volume_id" => "3"}
// {"lun" => "0", "host_device_id" => "{host-device-id}"}
Ephemeral interface{} `json:"ephemeral"`
// Older CPIs returned disk settings as strings
// e.g {"disk-3845-43758-7243-38754" => "/dev/sdc"}
// {"disk-3845-43758-7243-38754" => "3"}
// Newer CPIs will populate it in a hash:
// e.g {"disk-3845-43758-7243-38754" => {"path" => "/dev/sdc"}}
// {"disk-3845-43758-7243-38754" => {"volume_id" => "3"}}
// {"disk-3845-43758-7243-38754" => {"lun" => "0", "host_device_id" => "{host-device-id}"}}
Persistent map[string]interface{} `json:"persistent"`
RawEphemeral []DiskSettings `json:"raw_ephemeral"`
}
type DiskSettings struct {
ID string
DeviceID string
VolumeID string
Lun string
HostDeviceID string
Path string
FileSystemType disk.FileSystemType
}
type VM struct {
Name string `json:"name"`
}
func (s Settings) PersistentDiskSettings(diskID string) (DiskSettings, bool) {
diskSettings := DiskSettings{}
for key, settings := range s.Disks.Persistent {
if key == diskID {
diskSettings.ID = diskID
if hashSettings, ok := settings.(map[string]interface{}); ok {
if path, ok := hashSettings["path"]; ok {
diskSettings.Path = path.(string)
}
if volumeID, ok := hashSettings["volume_id"]; ok {
diskSettings.VolumeID = volumeID.(string)
}
if deviceID, ok := hashSettings["id"]; ok {
diskSettings.DeviceID = deviceID.(string)
}
if lun, ok := hashSettings["lun"]; ok {
diskSettings.Lun = lun.(string)
}
if hostDeviceID, ok := hashSettings["host_device_id"]; ok {
diskSettings.HostDeviceID = hostDeviceID.(string)
}
} else {
// Old CPIs return disk path (string) or volume id (string) as disk settings
diskSettings.Path = settings.(string)
diskSettings.VolumeID = settings.(string)
}
diskSettings.FileSystemType = s.Env.PersistentDiskFS
return diskSettings, true
}
}
return diskSettings, false
}
func (s Settings) EphemeralDiskSettings() DiskSettings {
diskSettings := DiskSettings{}
if s.Disks.Ephemeral != nil {
if hashSettings, ok := s.Disks.Ephemeral.(map[string]interface{}); ok {
if path, ok := hashSettings["path"]; ok {
diskSettings.Path = path.(string)
}
if volumeID, ok := hashSettings["volume_id"]; ok {
diskSettings.VolumeID = volumeID.(string)
}
if deviceID, ok := hashSettings["id"]; ok {
diskSettings.DeviceID = deviceID.(string)
}
if lun, ok := hashSettings["lun"]; ok {
diskSettings.Lun = lun.(string)
}
if hostDeviceID, ok := hashSettings["host_device_id"]; ok {
diskSettings.HostDeviceID = hostDeviceID.(string)
}
} else {
// Old CPIs return disk path (string) or volume id (string) as disk settings
diskSettings.Path = s.Disks.Ephemeral.(string)
diskSettings.VolumeID = s.Disks.Ephemeral.(string)
}
}
return diskSettings
}
func (s Settings) RawEphemeralDiskSettings() (devices []DiskSettings) {
return s.Disks.RawEphemeral
}
type Env struct {
Bosh BoshEnv `json:"bosh"`
PersistentDiskFS disk.FileSystemType `json:"persistent_disk_fs"`
}
func (e Env) GetPassword() string {
return e.Bosh.Password
}
func (e Env) GetKeepRootPassword() bool {
return e.Bosh.KeepRootPassword
}
func (e Env) GetRemoveDevTools() bool {
return e.Bosh.RemoveDevTools
}
func (e Env) GetAuthorizedKeys() []string {
return e.Bosh.AuthorizedKeys
}
func (e Env) GetSwapSizeInBytes() *uint64 {
if e.Bosh.SwapSizeInMB == nil {
return nil
}
result := uint64(*e.Bosh.SwapSizeInMB * 1024 * 1024)
return &result
}
type BoshEnv struct {
Password string `json:"password"`
KeepRootPassword bool `json:"keep_root_password"`
RemoveDevTools bool `json:"remove_dev_tools"`
AuthorizedKeys []string `json:"authorized_keys"`
SwapSizeInMB *uint64 `json:"swap_size"`
}
type DNSRecords struct {
Version uint32 `json:"Version"`
Records [][2]string `json:"records"`
}
type NetworkType string
const (
NetworkTypeDynamic NetworkType = "dynamic"
NetworkTypeVIP NetworkType = "vip"
)
type Network struct {
Type NetworkType `json:"type"`
IP string `json:"ip"`
Netmask string `json:"netmask"`
Gateway string `json:"gateway"`
Resolved bool `json:"resolved"` // was resolved via DHCP
UseDHCP bool `json:"use_dhcp"`
Default []string `json:"default"`
DNS []string `json:"dns"`
Mac string `json:"mac"`
Preconfigured bool `json:"preconfigured"`
}
type Networks map[string]Network
func (n Network) IsDefaultFor(category string) bool {
return stringArrayContains(n.Default, category)
}
func (n Networks) NetworkForMac(mac string) (Network, bool) {
for i := range n {
if n[i].Mac == mac {
return n[i], true
}
}
return Network{}, false
}
func (n Networks) DefaultNetworkFor(category string) (Network, bool) {
if len(n) == 1 {
for _, net := range n {
return net, true
}
}
for _, net := range n {
if net.IsDefaultFor(category) {
return net, true
}
}
return Network{}, false
}
func stringArrayContains(stringArray []string, str string) bool {
for _, s := range stringArray {
if s == str {
return true
}
}
return false
}
func (n Networks) DefaultIP() (ip string, found bool) {
for _, networkSettings := range n {
if ip == "" {
ip = networkSettings.IP
}
if len(networkSettings.Default) > 0 {
ip = networkSettings.IP
}
}
if ip != "" {
found = true
}
return
}
func (n Networks) IPs() (ips []string) {
for _, net := range n {
if net.IP != "" {
ips = append(ips, net.IP)
}
}
return
}
func (n Networks) IsPreconfigured() bool {
for _, network := range n {
if network.IsVIP() {
// Skip VIP networks since we do not configure interfaces for them
continue
}
if !network.Preconfigured {
return false
}
}
return true
}
func (n Network) String() string {
return fmt.Sprintf(
"type: '%s', ip: '%s', netmask: '%s', gateway: '%s', mac: '%s', resolved: '%t', preconfigured: '%t', use_dhcp: '%t'",
n.Type, n.IP, n.Netmask, n.Gateway, n.Mac, n.Resolved, n.Preconfigured, n.UseDHCP,
)
}
func (n Network) IsDHCP() bool {
if n.IsVIP() {
return false
}
if n.isDynamic() {
return true
}
if n.UseDHCP {
return true
}
// If manual network does not have IP and Netmask it cannot be statically
// configured. We want to keep track how originally the network was resolved.
// Otherwise it will be considered as static on subsequent checks.
isStatic := (n.IP != "" && n.Netmask != "")
return n.Resolved || !isStatic
}
func (n Network) isDynamic() bool {
return n.Type == NetworkTypeDynamic
}
func (n Network) IsVIP() bool {
return n.Type == NetworkTypeVIP
}
//{
// "agent_id": "bm-xxxxxxxx",
// "blobstore": {
// "options": {
// "blobstore_path": "/var/vcap/micro_bosh/data/cache"
// },
// "provider": "local"
// },
// "disks": {
// "ephemeral": "/dev/sdb",
// "persistent": {
// "vol-xxxxxx": "/dev/sdf"
// },
// "system": "/dev/sda1"
// },
// "env": {
// "bosh": {
// "password": null
// },
// "persistent_disk_fs": "xfs"
// },
// "trusted_certs": "very\nlong\nmultiline\nstring"
// "mbus": "https://vcap:b00tstrap@0.0.0.0:6868",
// "networks": {
// "bosh": {
// "cloud_properties": {
// "subnet": "subnet-xxxxxx"
// },
// "default": [
// "dns",
// "gateway"
// ],
// "dns": [
// "xx.xx.xx.xx"
// ],
// "gateway": null,
// "ip": "xx.xx.xx.xx",
// "netmask": null,
// "type": "manual"
// },
// "vip": {
// "cloud_properties": {},
// "ip": "xx.xx.xx.xx",
// "type": "vip"
// }
// },
// "ntp": [
// "0.north-america.pool.ntp.org",
// "1.north-america.pool.ntp.org",
// "2.north-america.pool.ntp.org",
// "3.north-america.pool.ntp.org"
// ],
// "vm": {
// "name": "vm-xxxxxxxx"
// }
//}