/
parse_topology.go
151 lines (134 loc) · 4.41 KB
/
parse_topology.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
// 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 (
"os"
"path"
"reflect"
"strings"
"github.com/joomcode/errorx"
"github.com/luyomo/OhMyTiUP/pkg/tui"
"github.com/luyomo/OhMyTiUP/pkg/utils"
"go.uber.org/zap"
"gopkg.in/yaml.v2"
)
var (
defaultDeployUser = "tidb"
errNSTopolohy = errorx.NewNamespace("topology")
// ErrTopologyReadFailed is ErrTopologyReadFailed
ErrTopologyReadFailed = errNSTopolohy.NewType("read_failed", utils.ErrTraitPreCheck)
// ErrTopologyParseFailed is ErrTopologyParseFailed
ErrTopologyParseFailed = errNSTopolohy.NewType("parse_failed", utils.ErrTraitPreCheck)
)
// ReadYamlFile read yaml content from file`
func ReadYamlFile(file string) ([]byte, error) {
suggestionProps := map[string]string{
"File": file,
}
yamlFile, err := os.ReadFile(file)
if err != nil {
return nil, ErrTopologyReadFailed.
Wrap(err, "Failed to read topology file %s", file).
WithProperty(tui.SuggestionFromTemplate(`
Please check whether your topology file {{ColorKeyword}}{{.File}}{{ColorReset}} exists and try again.
To generate a sample topology file:
{{ColorCommand}}{{OsArgs0}} template topology > topo.yaml{{ColorReset}}
`, suggestionProps))
}
return yamlFile, nil
}
// ParseTopologyYaml read yaml content from `file` and unmarshal it to `out`
func ParseTopologyYaml(file string, out Topology) error {
suggestionProps := map[string]string{
"File": file,
}
zap.L().Debug("Parse topology file", zap.String("file", file))
yamlFile, err := ReadYamlFile(file)
if err != nil {
return err
}
if err = yaml.UnmarshalStrict(yamlFile, out); err != nil {
return ErrTopologyParseFailed.
Wrap(err, "Failed to parse topology file %s", file).
WithProperty(tui.SuggestionFromTemplate(`
Please check the syntax of your topology file {{ColorKeyword}}{{.File}}{{ColorReset}} and try again.
`, suggestionProps))
}
zap.L().Debug("Parse topology file succeeded", zap.Any("topology", out))
return nil
}
// ExpandRelativeDir fill DeployDir, DataDir and LogDir to absolute path
func ExpandRelativeDir(topo Topology) {
expandRelativePath(deployUser(topo), topo)
}
func expandRelativePath(user string, topo interface{}) {
v := reflect.Indirect(reflect.ValueOf(topo).Elem())
switch v.Kind() {
case reflect.Slice:
for i := 0; i < v.Len(); i++ {
ref := reflect.New(v.Index(i).Type())
ref.Elem().Set(v.Index(i))
expandRelativePath(user, ref.Interface())
v.Index(i).Set(ref.Elem())
}
case reflect.Struct:
// We should deal with DeployDir first, because DataDir and LogDir depends on it
dirs := []string{"DeployDir", "DataDir", "LogDir"}
for _, dir := range dirs {
f := v.FieldByName(dir)
if !f.IsValid() || f.String() == "" {
continue
}
switch dir {
case "DeployDir":
f.SetString(Abs(user, f.String()))
case "DataDir":
// Some components supports multiple data dirs split by comma
ds := strings.Split(f.String(), ",")
ads := []string{}
for _, d := range ds {
if strings.HasPrefix(d, "/") {
ads = append(ads, d)
} else {
ads = append(ads, path.Join(v.FieldByName("DeployDir").String(), d))
}
}
f.SetString(strings.Join(ads, ","))
case "LogDir":
if !strings.HasPrefix(f.String(), "/") {
f.SetString(path.Join(v.FieldByName("DeployDir").String(), f.String()))
}
}
}
// Deal with all fields (expandRelativePath will do nothing on string filed)
for i := 0; i < v.NumField(); i++ {
// We don't deal with GlobalOptions because relative path in GlobalOptions.Data has special meaning
if v.Type().Field(i).Name == "GlobalOptions" {
continue
}
ref := reflect.New(v.Field(i).Type())
ref.Elem().Set(v.Field(i))
expandRelativePath(user, ref.Interface())
v.Field(i).Set(ref.Elem())
}
case reflect.Ptr:
expandRelativePath(user, v.Interface())
}
}
func deployUser(topo Topology) string {
base := topo.BaseTopo()
if base.GlobalOptions == nil || base.GlobalOptions.User == "" {
return defaultDeployUser
}
return base.GlobalOptions.User
}