-
Notifications
You must be signed in to change notification settings - Fork 17
/
clusterwide_config.go
358 lines (272 loc) · 9.15 KB
/
clusterwide_config.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
package repair
import (
"fmt"
"os"
"path/filepath"
"sort"
"github.com/mitchellh/mapstructure"
"github.com/tarantool/cartridge-cli/cli/common"
"gopkg.in/yaml.v2"
)
const (
configDirName = "config"
configFileName = "config.yml"
topologyConfFilename = "topology.yml"
keyTopology = "topology"
keyInstances = "servers"
keyReplicasets = "replicasets"
keyInstanceAdvertiseURI = "uri"
keyInstanceDisabled = "disabled"
keyInstanceReplicasetUUID = "replicaset_uuid"
keyReplicasetLeaders = "master"
keyReplicasetAlias = "alias"
keyReplicasetRoles = "roles"
expelledState = "expelled"
unnamedReplicasetAlias = "unnamed"
)
type RawConfType map[interface{}]interface{}
type InstanceConfType struct {
AdvertiseURI string `mapstructure:"uri"`
ReplicasetUUID string `mapstructure:"replicaset_uuid"`
IsExpelled bool
IsDisabled bool `mapstructure:"disabled"`
Raw RawConfType
}
type ReplicasetConfType struct {
Alias string `mapstructure:"alias"`
Leaders []string `mapstructure:"master"`
RolesMap map[string]bool `mapstructure:"roles"`
Instances []string
Raw RawConfType
LeadersIsString bool
}
type TopologyConfType struct {
raw RawConfType
topologyRaw RawConfType
Instances map[string]*InstanceConfType
instancesRaw RawConfType
Replicasets map[string]*ReplicasetConfType
replicasetsRaw RawConfType
}
type TopologyConfFileInfo struct {
Path string
IsOneFile bool
}
// TOPOLOGY
func getTopologyConfPath(workDir string) (string, error) {
var topologyConfPath string
// check config directory <work-dir>/config/
confDirPath := filepath.Join(workDir, configDirName)
if _, err := os.Stat(confDirPath); err == nil {
// find <work-dir>/config/topology.yml
topologyConfPath = filepath.Join(confDirPath, topologyConfFilename)
if _, err := os.Stat(topologyConfPath); err == nil {
return topologyConfPath, nil
} else if !os.IsNotExist(err) {
return "", fmt.Errorf("Failed to use topology config file: %s", err)
}
} else if !os.IsNotExist(err) {
return "", fmt.Errorf("Failed to use clusterwide config directory: %s", err)
}
// try old format: <work-dir>/config.yml
topologyConfPath = filepath.Join(workDir, configFileName)
if _, err := os.Stat(topologyConfPath); err == nil {
return topologyConfPath, nil
} else if !os.IsNotExist(err) {
return "", fmt.Errorf("Failed to use clusterwide config file: %s", err)
}
return "", nil
}
func getTopologyConf(topologyConfPath string) (*TopologyConfType, error) {
var err error
var topologyConf TopologyConfType
if _, err := os.Stat(topologyConfPath); err != nil {
return nil, fmt.Errorf("Failed to use topology config path: %s", err)
}
confContent, err := common.GetFileContentBytes(topologyConfPath)
if err != nil {
return nil, fmt.Errorf("Failed to read config: %s", err)
}
if err := yaml.Unmarshal(confContent, &topologyConf.raw); err != nil {
return nil, fmt.Errorf("Failed to parse config: %s", err)
}
if err := topologyConf.setTopologyConfRaw(); err != nil {
return nil, fmt.Errorf("Failed to parse topology config: %s", err)
}
if err := topologyConf.setInstancesConf(); err != nil {
return nil, fmt.Errorf("Failed to parse instances config: %s", err)
}
if err := topologyConf.setReplicasetsConf(); err != nil {
return nil, fmt.Errorf("Failed to get replicasets config: %s", err)
}
return &topologyConf, nil
}
func (topologyConf *TopologyConfType) setTopologyConfRaw() error {
topologyConfRaw, found := topologyConf.raw[keyTopology]
if !found {
// multifile config
topologyConf.topologyRaw = topologyConf.raw
return nil
}
var ok bool
if topologyConf.topologyRaw, ok = topologyConfRaw.(RawConfType); !ok {
return fmt.Errorf("Clusterwide config %q value isn't a map", keyTopology)
}
return nil
}
func (topologyConf *TopologyConfType) setInstancesConf() error {
instancesConfRaw, found := topologyConf.topologyRaw[keyInstances]
if !found {
return fmt.Errorf("Topology config doesn't contain %q key", keyInstances)
}
instancesConfRawMap, ok := instancesConfRaw.(RawConfType)
if !ok {
return fmt.Errorf("%q value isn't a map", keyInstances)
}
topologyConf.instancesRaw = instancesConfRawMap
topologyConf.Instances = make(map[string]*InstanceConfType)
for instanceUUIDRaw, instanceConfRaw := range instancesConfRawMap {
instanceUUID, ok := instanceUUIDRaw.(string)
if !ok {
return fmt.Errorf("Instance UUID isn't a string")
}
var instanceConf InstanceConfType
switch conf := instanceConfRaw.(type) {
case string:
if conf != expelledState {
return fmt.Errorf("Instance %s is in the unknown state %s", instanceUUID, conf)
}
instanceConf.IsExpelled = true
case RawConfType:
if err := mapstructure.Decode(conf, &instanceConf); err != nil {
return fmt.Errorf("failed to get instance from conf: %s", err)
}
instanceConf.Raw = conf
default:
return fmt.Errorf("Instance %s config isn't a map or a string", instanceUUID)
}
topologyConf.Instances[instanceUUID] = &instanceConf
}
return nil
}
func (topologyConf *TopologyConfType) setReplicasetsConf() error {
replicasetsConfRaw, found := topologyConf.topologyRaw[keyReplicasets]
if !found {
return fmt.Errorf("Topology config doesn't contain %q key", keyReplicasets)
}
replicasetsConfRawMap, ok := replicasetsConfRaw.(RawConfType)
if !ok {
return fmt.Errorf("%q value isn't a map", keyReplicasets)
}
topologyConf.replicasetsRaw = replicasetsConfRawMap
topologyConf.Replicasets = make(map[string]*ReplicasetConfType)
for replicasetUUIDRaw, replicasetConfRaw := range replicasetsConfRawMap {
replicasetUUID, ok := replicasetUUIDRaw.(string)
if !ok {
return fmt.Errorf("Replicaset UUID isn't a string")
}
var replicasetConf ReplicasetConfType
switch conf := replicasetConfRaw.(type) {
case RawConfType:
replicasetConf.Raw = conf
if err := mapstructure.WeakDecode(conf, &replicasetConf); err != nil {
return fmt.Errorf("failed to get instance from conf: %s", err)
}
if replicasetConf.Alias == unnamedReplicasetAlias {
replicasetConf.Alias = ""
}
// leaders
leadersRaw, ok := conf[keyReplicasetLeaders]
if ok {
switch leadersRaw.(type) {
case string:
replicasetConf.LeadersIsString = true
}
}
// instances
replicasetConf.Instances = make([]string, 0)
for instanceUUID, instanceConf := range topologyConf.Instances {
if instanceConf.ReplicasetUUID == replicasetUUID {
replicasetConf.Instances = append(replicasetConf.Instances, instanceUUID)
}
}
sort.Sort(sort.StringSlice(replicasetConf.Instances))
default:
return fmt.Errorf("Replicaset %s config isn't a map", replicasetUUID)
}
topologyConf.Replicasets[replicasetUUID] = &replicasetConf
}
return nil
}
func (topologyConf *TopologyConfType) MarshalContent() ([]byte, error) {
content, err := yaml.Marshal(topologyConf.raw)
if err != nil {
return nil, fmt.Errorf("Failed to YAML encode: %s", err)
}
return content, nil
}
// INSTANCES
func (topologyConf *TopologyConfType) GetOrderedInstaceUUIDs() []string {
instanceUUIDs := make([]string, len(topologyConf.Instances))
i := 0
for instanceUUID := range topologyConf.Instances {
instanceUUIDs[i] = instanceUUID
i++
}
sort.Sort(sort.StringSlice(instanceUUIDs))
return instanceUUIDs
}
func (topologyConf *TopologyConfType) SetInstanceURI(instanceUUID, newURI string) error {
instanceConf, ok := topologyConf.Instances[instanceUUID]
if !ok {
return fmt.Errorf("Instance %s isn't found in cluster", instanceUUID)
}
if instanceConf.IsExpelled {
return fmt.Errorf("Instance %s is expelled", instanceUUID)
}
instanceConf.AdvertiseURI = newURI
instanceConf.Raw[keyInstanceAdvertiseURI] = newURI
return nil
}
func (topologyConf *TopologyConfType) RemoveInstance(instanceUUID string) error {
if _, ok := topologyConf.Instances[instanceUUID]; !ok {
return fmt.Errorf("Instance %s isn't found in cluster", instanceUUID)
}
delete(topologyConf.Instances, instanceUUID)
delete(topologyConf.instancesRaw, instanceUUID)
return nil
}
// REPLICASETS
func (topologyConf *TopologyConfType) GetOrderedReplicasetUUIDs() []string {
replicasetUUIDs := make([]string, len(topologyConf.Replicasets))
i := 0
for replicasetUUID := range topologyConf.Replicasets {
replicasetUUIDs[i] = replicasetUUID
i++
}
sort.Sort(sort.StringSlice(replicasetUUIDs))
return replicasetUUIDs
}
func (topologyConf *TopologyConfType) RemoveReplicaset(replicasetUUID string) error {
if _, ok := topologyConf.Replicasets[replicasetUUID]; !ok {
return fmt.Errorf("Replicaset %s isn't found in cluster", replicasetUUID)
}
delete(topologyConf.Replicasets, replicasetUUID)
delete(topologyConf.replicasetsRaw, replicasetUUID)
return nil
}
func (replicasetConf *ReplicasetConfType) SetInstances(newInstances []string) {
replicasetConf.Instances = newInstances
}
func (replicasetConf *ReplicasetConfType) SetLeaders(newLeaders []string) {
replicasetConf.Leaders = newLeaders
if replicasetConf.LeadersIsString {
if len(newLeaders) > 0 {
replicasetConf.Raw[keyReplicasetLeaders] = newLeaders[0]
} else {
delete(replicasetConf.Raw, keyReplicasetLeaders)
}
} else {
replicasetConf.Raw[keyReplicasetLeaders] = newLeaders
}
}