-
Notifications
You must be signed in to change notification settings - Fork 312
/
server_config.go
352 lines (313 loc) · 9.48 KB
/
server_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
// Copyright 2020 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package spec
import (
"bytes"
"context"
"errors"
"fmt"
"os"
"path"
"reflect"
"strings"
"github.com/BurntSushi/toml"
perrs "github.com/pingcap/errors"
"github.com/pingcap/tiup/pkg/cluster/clusterutil"
"github.com/pingcap/tiup/pkg/cluster/ctxt"
"github.com/pingcap/tiup/pkg/meta"
"github.com/pingcap/tiup/pkg/utils"
"go.uber.org/zap"
"gopkg.in/yaml.v2"
)
const (
// AnsibleImportedConfigPath is the sub path where all imported configs are stored
AnsibleImportedConfigPath = "ansible-imported-configs"
// TempConfigPath is the sub path where generated temporary configs are stored
TempConfigPath = "config-cache"
// migrateLockName is the directory name of migrating lock
migrateLockName = "tiup-migrate.lck"
)
// ErrorCheckConfig represent error occurred in config check stage
var ErrorCheckConfig = errors.New("check config failed")
// strKeyMap tries to convert `map[interface{}]interface{}` to `map[string]interface{}`
func strKeyMap(val interface{}) interface{} {
m, ok := val.(map[interface{}]interface{})
if ok {
ret := map[string]interface{}{}
for k, v := range m {
kk, ok := k.(string)
if !ok {
return val
}
ret[kk] = strKeyMap(v)
}
return ret
}
rv := reflect.ValueOf(val)
if rv.Kind() == reflect.Slice {
var ret []interface{}
for i := 0; i < rv.Len(); i++ {
ret = append(ret, strKeyMap(rv.Index(i).Interface()))
}
return ret
}
return val
}
func foldKey(key string, val interface{}) (string, interface{}) {
parts := strings.SplitN(key, ".", 2)
if len(parts) == 1 {
return key, strKeyMap(val)
}
subKey, subVal := foldKey(parts[1], val)
return parts[0], map[string]interface{}{
subKey: strKeyMap(subVal),
}
}
func patch(origin map[string]interface{}, key string, val interface{}) {
origVal, found := origin[key]
if !found {
origin[key] = strKeyMap(val)
return
}
origMap, lhsOk := origVal.(map[string]interface{})
valMap, rhsOk := val.(map[string]interface{})
if lhsOk && rhsOk {
for k, v := range valMap {
patch(origMap, k, v)
}
} else {
// overwrite
origin[key] = strKeyMap(val)
}
}
// FoldMap convert single layer map to multi-layer
func FoldMap(ms map[string]interface{}) map[string]interface{} {
// we flatten map first to deal with the case like:
// a.b:
// c.d: xxx
ms = FlattenMap(ms)
result := map[string]interface{}{}
for k, v := range ms {
key, val := foldKey(k, v)
patch(result, key, val)
}
return result
}
// FlattenMap convert mutil-layer map to single layer
func FlattenMap(ms map[string]interface{}) map[string]interface{} {
result := map[string]interface{}{}
for k, v := range ms {
var sub map[string]interface{}
if m, ok := v.(map[string]interface{}); ok {
sub = FlattenMap(m)
} else if m, ok := v.(map[interface{}]interface{}); ok {
fixM := map[string]interface{}{}
for k, v := range m {
if sk, ok := k.(string); ok {
fixM[sk] = v
}
}
sub = FlattenMap(fixM)
} else {
result[k] = v
continue
}
for sk, sv := range sub {
result[k+"."+sk] = sv
}
}
return result
}
// MergeConfig merge two or more config into one and unflat them
// config1:
// a.b.a: 1
// a.b.b: 2
// config2:
// a.b.a: 3
// a.b.c: 4
// config3:
// b.c = 5
// After MergeConfig(config1, config2, config3):
// a:
// b:
// a: 3
// b: 2
// c: 4
// b:
// c: 5
func MergeConfig(orig map[string]interface{}, overwrites ...map[string]interface{}) map[string]interface{} {
lhs := FoldMap(orig)
for _, overwrite := range overwrites {
rhs := FoldMap(overwrite)
for k, v := range rhs {
patch(lhs, k, v)
}
}
return lhs
}
// GetValueFromPath try to find the value by path recursively
func GetValueFromPath(m map[string]interface{}, p string) interface{} {
ss := strings.Split(p, ".")
searchMap := make(map[interface{}]interface{})
m = FoldMap(m)
for k, v := range m {
searchMap[k] = v
}
return searchValue(searchMap, ss)
}
func searchValue(m map[interface{}]interface{}, ss []string) interface{} {
l := len(ss)
switch l {
case 0:
return m
case 1:
return m[ss[0]]
}
key := ss[0]
if pm, ok := m[key].(map[interface{}]interface{}); ok {
return searchValue(pm, ss[1:])
} else if pm, ok := m[key].(map[string]interface{}); ok {
searchMap := make(map[interface{}]interface{})
for k, v := range pm {
searchMap[k] = v
}
return searchValue(searchMap, ss[1:])
}
return nil
}
// Merge2Toml merge the config of global.
func Merge2Toml(comp string, global, overwrite map[string]interface{}) ([]byte, error) {
return merge2Toml(comp, global, overwrite)
}
func merge2Toml(comp string, global, overwrite map[string]interface{}) ([]byte, error) {
lhs := MergeConfig(global, overwrite)
buf := bytes.NewBufferString(fmt.Sprintf(`# WARNING: This file is auto-generated. Do not edit! All your modification will be overwritten!
# You can use 'tiup cluster edit-config' and 'tiup cluster reload' to update the configuration
# All configuration items you want to change can be added to:
# server_configs:
# %s:
# aa.b1.c3: value
# aa.b2.c4: value
`, comp))
enc := toml.NewEncoder(buf)
enc.Indent = ""
err := enc.Encode(lhs)
if err != nil {
return nil, perrs.Trace(err)
}
return buf.Bytes(), nil
}
func encodeRemoteCfg2Yaml(remote Remote) ([]byte, error) {
if len(remote.RemoteRead) == 0 && len(remote.RemoteWrite) == 0 {
return []byte{}, nil
}
buf := bytes.NewBufferString("")
enc := yaml.NewEncoder(buf)
err := enc.Encode(remote)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func mergeImported(importConfig []byte, specConfigs ...map[string]interface{}) (map[string]interface{}, error) {
var configData map[string]interface{}
if err := toml.Unmarshal(importConfig, &configData); err != nil {
return nil, perrs.Trace(err)
}
// overwrite topology specifieced configs upon the imported configs
lhs := MergeConfig(configData, specConfigs...)
return lhs, nil
}
// BindVersion map the cluster version to the third components binding version.
type BindVersion func(comp string, version string) (bindVersion string)
func checkConfig(ctx context.Context, e ctxt.Executor, componentName, clusterVersion, nodeOS, arch, config string, paths meta.DirPaths, bindVersion BindVersion) error {
var cmd string
configPath := path.Join(paths.Deploy, "conf", config)
switch componentName {
case ComponentPrometheus:
cmd = fmt.Sprintf("%s/bin/prometheus/promtool check config %s", paths.Deploy, configPath)
case ComponentAlertmanager:
cmd = fmt.Sprintf("%s/bin/alertmanager/amtool check-config %s", paths.Deploy, configPath)
default:
repo, err := clusterutil.NewRepository(nodeOS, arch)
if err != nil {
return perrs.Annotate(ErrorCheckConfig, err.Error())
}
clsVer := utils.Version(clusterVersion)
ver := clusterVersion
if clsVer.IsNightly() {
ver = utils.NightlyVersionAlias
}
// FIXME: workaround for nightly versions, need refactor
if bindVersion != nil {
ver = bindVersion(componentName, ver)
}
entry, err := repo.ComponentBinEntry(componentName, ver)
if err != nil {
return perrs.Annotate(ErrorCheckConfig, err.Error())
}
binPath := path.Join(paths.Deploy, "bin", entry)
// Skip old versions
if !hasConfigCheckFlag(ctx, e, binPath) {
return nil
}
extra := ""
if componentName == ComponentTiKV {
// Pass in an empty pd address and the correct data dir
extra = fmt.Sprintf(`--pd "" --data-dir "%s"`, paths.Data[0])
}
cmd = fmt.Sprintf("%s --config-check --config=%s %s", binPath, configPath, extra)
}
_, _, err := e.Execute(ctx, cmd, false)
if err != nil {
return perrs.Annotate(ErrorCheckConfig, err.Error())
}
return nil
}
func hasConfigCheckFlag(ctx context.Context, e ctxt.Executor, binPath string) bool {
stdout, stderr, _ := e.Execute(ctx, fmt.Sprintf("%s --help", binPath), false)
return strings.Contains(string(stdout), "config-check") || strings.Contains(string(stderr), "config-check")
}
// HandleImportPathMigration tries to rename old configs file directory for imported clusters to the new name
func HandleImportPathMigration(clsName string) error {
dirPath := ClusterPath(clsName)
targetPath := path.Join(dirPath, AnsibleImportedConfigPath)
_, err := os.Stat(targetPath)
if os.IsNotExist(err) {
zap.L().Warn("renaming config dir", zap.String("orig", dirPath), zap.String("new", targetPath))
if lckErr := utils.Retry(func() error {
_, lckErr := os.Stat(path.Join(dirPath, migrateLockName))
if os.IsNotExist(lckErr) {
return nil
}
return perrs.Errorf("config dir already lock by another task, %s", lckErr)
}); lckErr != nil {
return lckErr
}
if lckErr := os.Mkdir(path.Join(dirPath, migrateLockName), 0755); lckErr != nil {
return perrs.Errorf("can not lock config dir, %s", lckErr)
}
defer func() {
rmErr := os.Remove(path.Join(dirPath, migrateLockName))
if rmErr != nil {
zap.L().Error("error unlocking config dir", zap.Error(rmErr))
}
}()
// ignore if the old config path does not exist
if _, err := os.Stat(path.Join(dirPath, "config")); os.IsNotExist(err) {
return nil
}
return os.Rename(path.Join(dirPath, "config"), targetPath)
}
return nil
}