forked from cloudfoundry-attic/bosh-init
/
settings.go
272 lines (229 loc) · 5.46 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
package settings
import (
"fmt"
)
const (
RootUsername = "root"
VCAPUsername = "vcap"
AdminGroup = "admin"
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 Source interface {
PublicSSHKeyForUsername(string) (string, error)
Settings() (Settings, error)
}
const (
BlobstoreTypeDummy = "dummy"
BlobstoreTypeLocal = "local"
)
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"`
// e.g "/dev/sdb", "2"
Ephemeral string `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"}}
Persistent map[string]interface{} `json:"persistent"`
}
type DiskSettings struct {
ID string
VolumeID string
Path string
}
type VM struct {
Name string `json:"name"`
}
func (s Settings) PersistentDiskSettings(diskID string) (DiskSettings, bool) {
diskSettings := DiskSettings{}
for id, settings := range s.Disks.Persistent {
if id == diskID {
diskSettings.ID = diskID
if hashSettings, ok := settings.(map[string]interface{}); ok {
diskSettings.Path = hashSettings["path"].(string)
diskSettings.VolumeID = hashSettings["volume_id"].(string)
} else {
// Old CPIs return disk path (string) or volume id (string) as disk settings
diskSettings.Path = settings.(string)
diskSettings.VolumeID = settings.(string)
}
return diskSettings, true
}
}
return diskSettings, false
}
func (s Settings) EphemeralDiskSettings() DiskSettings {
return DiskSettings{
VolumeID: s.Disks.Ephemeral,
Path: s.Disks.Ephemeral,
}
}
type Env struct {
Bosh BoshEnv `json:"bosh"`
}
func (e Env) GetPassword() string {
return e.Bosh.Password
}
type BoshEnv struct {
Password string `json:"password"`
}
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
Default []string `json:"default"`
DNS []string `json:"dns"`
Mac string `json:"mac"`
}
type Networks map[string]Network
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 stringArrayContains(net.Default, 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 Network) String() string {
return fmt.Sprintf("type: '%s', ip: '%s', netmask: '%s', gateway: '%s', mac: '%s', resolved: '%t'", n.Type, n.IP, n.Netmask, n.Gateway, n.Mac, n.Resolved)
}
func (n Network) IsDHCP() bool {
if n.IsVIP() {
return false
}
if n.isDynamic() {
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
// }
// },
// "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"
// }
//}