-
Notifications
You must be signed in to change notification settings - Fork 2
/
gen_conf.go
254 lines (223 loc) · 7.83 KB
/
gen_conf.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
// Copyright 2023 Huawei Cloud Computing Technologies Co., Ltd.
//
// 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,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package config
import (
"path/filepath"
"strconv"
"strings"
"github.com/openGemini/gemix/utils"
)
type HostFile struct {
Ip string
HasMeta bool
HasSql bool
HasStore bool
TsMeta MetaYaml
TsSql SqlYaml
TsStore StoreYaml
}
type MetaPorts struct {
Ip string
Peer int
Gossip int
}
func GenConfs(y Yaml, template Toml, path string) error {
hosts := make(map[string]HostFile)
var metaPorts []MetaPorts
// Build hosts
for _, meta := range y.TsMeta {
hostEntry, ok := hosts[meta.Host]
if !ok {
hostEntry = HostFile{Ip: meta.Host}
}
hostEntry.TsMeta = meta
hostEntry.HasMeta = true
hosts[meta.Host] = hostEntry
metaPorts = append(metaPorts, MetaPorts{Ip: meta.Host, Peer: meta.PeerPort, Gossip: meta.GossipPort})
}
for _, sql := range y.TsSql {
hostEntry, ok := hosts[sql.Host]
if !ok {
hostEntry = HostFile{Ip: sql.Host}
}
hostEntry.TsSql = sql
hostEntry.HasSql = true
hosts[sql.Host] = hostEntry
}
for _, store := range y.TsStore {
hostEntry, ok := hosts[store.Host]
if !ok {
hostEntry = HostFile{Ip: store.Host}
}
hostEntry.TsStore = store
hostEntry.HasStore = true
hosts[store.Host] = hostEntry
}
// build meta-join and gossip.member
var metaJoins []string
var gossipMembers []string
for i := 0; i < len(metaPorts); i++ {
if metaPorts[i].Peer == 0 {
metaJoins = append(metaJoins, strings.Replace(template.Common.MetaJoin[i], template.Common.MetaJoin[i][:15], metaPorts[i].Ip, -1))
} else {
metaJoins = append(metaJoins, metaPorts[i].Ip+":"+strconv.Itoa(metaPorts[i].Peer))
}
if metaPorts[i].Gossip == 0 {
gossipMembers = append(gossipMembers, strings.Replace(template.Gossip.Members[i], template.Gossip.Members[i][:15], metaPorts[i].Ip, -1))
} else {
gossipMembers = append(gossipMembers, metaPorts[i].Ip+":"+strconv.Itoa(metaPorts[i].Gossip))
}
}
// generate corresponding config files for every host.
for _, host := range hosts {
addr := host.Ip
// meta
if host.HasMeta {
newToml := template
// meta
if host.TsMeta.ClientPort != 0 {
newToml.Meta.HttpBindAddress = addr + ":" + strconv.Itoa(host.TsMeta.ClientPort)
} else {
newToml.Meta.HttpBindAddress = strings.Replace(newToml.Meta.HttpBindAddress, newToml.Meta.HttpBindAddress[:8], addr, -1)
}
if host.TsMeta.PeerPort != 0 {
newToml.Meta.RpcBindAddress = addr + ":" + strconv.Itoa(host.TsMeta.PeerPort)
} else {
newToml.Meta.RpcBindAddress = strings.Replace(newToml.Meta.RpcBindAddress, newToml.Meta.RpcBindAddress[:8], addr, -1)
}
if host.TsMeta.RaftPort != 0 {
newToml.Meta.BindAddress = addr + ":" + strconv.Itoa(host.TsMeta.RaftPort)
} else {
newToml.Meta.BindAddress = strings.Replace(newToml.Meta.BindAddress, newToml.Meta.BindAddress[:8], addr, -1)
}
if host.TsMeta.DataDir != "" {
newToml.Meta.Dir = host.TsMeta.DataDir
}
// common
newToml.Common.MetaJoin = metaJoins
newToml.Common.HaPolicy = host.TsMeta.Config.HaPolicy
// logging
if host.TsMeta.LogDir != "" {
newToml.Logging.Path = host.TsMeta.LogDir
}
// gossip
if host.TsMeta.GossipPort != 0 {
newToml.Gossip.MetaBindPort = host.TsMeta.GossipPort
}
newToml.Gossip.BindAddress = addr
newToml.Gossip.Members = gossipMembers
// monitor
newToml.Monitor = MonitorToml{Pushers: host.TsMeta.Config.Pushers, StoreEnabled: host.TsMeta.Config.StoreEnabled,
StoreDatabase: host.TsMeta.Config.StoreDatabase,
StoreInterval: host.TsMeta.Config.StoreInterval,
StorePath: host.TsMeta.Config.StorePath,
Compress: host.TsMeta.Config.Compress,
HttpEndpoint: host.TsMeta.Config.HttpEndpoint,
Username: host.TsMeta.Config.Username,
Password: host.TsMeta.Config.Password}
fileName := filepath.Join(path, host.Ip, utils.RemoteMetaConfName)
if err := GenNewToml(newToml, fileName); err != nil {
return err
}
}
// http
if host.HasSql {
newToml := template
// http
if host.TsSql.Port != 0 {
newToml.Http.BindAddress = addr + ":" + strconv.Itoa(host.TsSql.Port)
} else {
newToml.Http.BindAddress = strings.Replace(newToml.Http.BindAddress, newToml.Http.BindAddress[:8], addr, -1)
}
newToml.Http.AuthEnabled = host.TsSql.Config.AuthEnabled
newToml.Http.HttpsEnabled = host.TsSql.Config.HttpsEnabled
newToml.Http.HttpsCertificate = host.TsSql.Config.HttpsCertificate
newToml.Http.HttpsPrivateKey = host.TsSql.Config.HttpsPrivateKey
// common
newToml.Common.MetaJoin = metaJoins
newToml.Common.HaPolicy = host.TsSql.Config.HaPolicy
// logging
if host.TsSql.LogDir != "" {
newToml.Logging.Path = host.TsSql.LogDir
}
// gossip
newToml.Gossip.BindAddress = addr
newToml.Gossip.Members = gossipMembers
// monitor
newToml.Monitor = MonitorToml{Pushers: host.TsSql.Config.Pushers, StoreEnabled: host.TsSql.Config.StoreEnabled,
StoreDatabase: host.TsSql.Config.StoreDatabase,
StoreInterval: host.TsSql.Config.StoreInterval,
StorePath: host.TsSql.Config.StorePath,
Compress: host.TsSql.Config.Compress,
HttpEndpoint: host.TsSql.Config.HttpEndpoint,
Username: host.TsSql.Config.Username,
Password: host.TsSql.Config.Password}
fileName := filepath.Join(path, host.Ip, utils.RemoteSqlConfName)
if err := GenNewToml(newToml, fileName); err != nil {
return err
}
}
// data
if host.HasStore {
newToml := template
// data
if host.TsStore.IngestPort != 0 {
newToml.Data.StoreIngestAddr = addr + ":" + strconv.Itoa(host.TsStore.IngestPort)
} else {
newToml.Data.StoreIngestAddr = strings.Replace(newToml.Data.StoreIngestAddr, newToml.Data.StoreIngestAddr[:8], addr, -1)
}
if host.TsStore.SelectPort != 0 {
newToml.Data.StoreSelectAddr = addr + ":" + strconv.Itoa(host.TsStore.SelectPort)
} else {
newToml.Data.StoreSelectAddr = strings.Replace(newToml.Data.StoreSelectAddr, newToml.Data.StoreSelectAddr[:8], addr, -1)
}
if host.TsStore.DataDir != "" {
newToml.Data.StoreDataDir = host.TsStore.DataDir
newToml.Data.StoreWalDir = host.TsStore.DataDir
}
if host.TsStore.MetaDir != "" {
newToml.Data.StoreMetaDir = host.TsStore.MetaDir
}
// common
newToml.Common.MetaJoin = metaJoins
newToml.Common.HaPolicy = host.TsStore.Config.HaPolicy
// logging
if host.TsStore.LogDir != "" {
newToml.Logging.Path = host.TsStore.LogDir
}
// retention
newToml.Retention.CheckInterval = host.TsStore.Config.CheckInterval
// gossip
if host.TsStore.GossipPort != 0 {
newToml.Gossip.StoreBindPort = host.TsStore.GossipPort
}
newToml.Gossip.BindAddress = addr
newToml.Gossip.Members = gossipMembers
// monitor
newToml.Monitor = MonitorToml{Pushers: host.TsStore.Config.Pushers, StoreEnabled: host.TsStore.Config.StoreEnabled,
StoreDatabase: host.TsStore.Config.StoreDatabase,
StoreInterval: host.TsStore.Config.StoreInterval,
StorePath: host.TsStore.Config.StorePath,
Compress: host.TsStore.Config.Compress,
HttpEndpoint: host.TsStore.Config.HttpEndpoint,
Username: host.TsStore.Config.Username,
Password: host.TsStore.Config.Password}
fileName := filepath.Join(path, host.Ip, utils.RemoteStoreConfName)
if err := GenNewToml(newToml, fileName); err != nil {
return err
}
}
}
return nil
}