forked from goodrain/rainbond
/
taskconfig.go
214 lines (197 loc) · 5.74 KB
/
taskconfig.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
// Copyright (C) 2014-2018 Goodrain Co., Ltd.
// RAINBOND, Application Management Platform
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. For any non-GPL usage of Rainbond,
// one or multiple Commercial Licenses authorized by Goodrain Co., Ltd.
// must be obtained first.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package config
import (
"context"
"fmt"
"regexp"
"strconv"
"strings"
"github.com/Sirupsen/logrus"
"github.com/goodrain/rainbond/cmd/node/option"
"github.com/goodrain/rainbond/node/core/store"
)
//GroupContext 组任务会话
type GroupContext struct {
ctx context.Context
groupID string
}
//NewGroupContext 创建组配置会话
func NewGroupContext(groupID string) *GroupContext {
return &GroupContext{
ctx: context.Background(),
groupID: groupID,
}
}
//Add 添加配置项
func (g *GroupContext) Add(k, v interface{}) {
g.ctx = context.WithValue(g.ctx, k, v)
store.DefalutClient.Put(fmt.Sprintf("%s/group/%s/%s", option.Config.ConfigStoragePath, g.groupID, k), v.(string))
}
//Get get
func (g *GroupContext) Get(k interface{}) interface{} {
if v := g.ctx.Value(k); v != nil {
return v
}
res, _ := store.DefalutClient.Get(fmt.Sprintf("%s/group/%s/%s", option.Config.ConfigStoragePath, g.groupID, k))
if res.Count > 0 {
return string(res.Kvs[0].Value)
}
return ""
}
//GetString get
func (g *GroupContext) GetString(k interface{}) string {
if v := g.ctx.Value(k); v != nil {
return v.(string)
}
res, _ := store.DefalutClient.Get(fmt.Sprintf("%s/group/%s/%s", option.Config.ConfigStoragePath, g.groupID, k))
if res.Count > 0 {
return string(res.Kvs[0].Value)
}
return ""
}
var reg = regexp.MustCompile(`(?U)\$\{.*\}`)
//GetConfigKey 获取配置key
func GetConfigKey(rk string) string {
if len(rk) < 4 {
return ""
}
left := strings.Index(rk, "{")
right := strings.Index(rk, "}")
return rk[left+1 : right]
}
//ResettingArray 根据实际配置解析数组字符串
func ResettingArray(groupCtx *GroupContext, source []string) ([]string, error) {
sourcecopy := make([]string, len(source))
// 使用copy
for i, s := range source {
sourcecopy[i] = s
}
for i, s := range sourcecopy {
resultKey := reg.FindAllString(s, -1)
for _, rk := range resultKey {
key := strings.ToUpper(GetConfigKey(rk))
// if len(key) < 1 {
// return nil, fmt.Errorf("%s Parameter configuration error.please make sure `${XXX}`", s)
// }
value := GetConfig(groupCtx, key)
sourcecopy[i] = strings.Replace(s, rk, value, -1)
}
}
return sourcecopy, nil
}
//GetConfig 获取配置信息
func GetConfig(groupCtx *GroupContext, key string) string {
if groupCtx != nil {
value := groupCtx.Get(key)
if value != nil {
switch value.(type) {
case string:
if value.(string) != "" {
return value.(string)
}
case int:
if value.(int) != 0 {
return strconv.Itoa(value.(int))
}
case []string:
if value.([]string) != nil {
result := strings.Join(value.([]string), ",")
if strings.HasSuffix(result, ",") {
return result
}
return result + ","
}
case []interface{}:
if value.([]interface{}) != nil && len(value.([]interface{})) > 0 {
result := ""
for _, v := range value.([]interface{}) {
switch v.(type) {
case string:
result += v.(string) + ","
case int:
result += strconv.Itoa(v.(int)) + ","
}
}
return result
}
}
}
}
if dataCenterConfig == nil {
return ""
}
cn := dataCenterConfig.GetConfig(key)
if cn != nil && cn.Value != nil {
if cn.ValueType == "string" || cn.ValueType == "" {
return cn.Value.(string)
}
if cn.ValueType == "array" {
switch cn.Value.(type) {
case []string:
return strings.Join(cn.Value.([]string), ",")
case []interface{}:
vas := cn.Value.([]interface{})
result := ""
for _, va := range vas {
switch va.(type) {
case string:
result += va.(string) + ","
case int:
result += strconv.Itoa(va.(int)) + ","
}
}
return result
}
}
if cn.ValueType == "int" {
return strconv.Itoa(cn.Value.(int))
}
}
logrus.Warnf("can not find config for key %s", key)
return ""
}
//ResettingString 根据实际配置解析字符串
func ResettingString(groupCtx *GroupContext, source string) (string, error) {
resultKey := reg.FindAllString(source, -1)
for _, rk := range resultKey {
key := strings.ToUpper(GetConfigKey(rk))
// if len(key) < 1 {
// return nil, fmt.Errorf("%s Parameter configuration error.please make sure `${XXX}`", s)
// }
value := GetConfig(groupCtx, key)
source = strings.Replace(source, rk, value, -1)
}
return source, nil
}
//ResettingMap 根据实际配置解析Map字符串
func ResettingMap(groupCtx *GroupContext, source map[string]string) (map[string]string, error) {
sourcecopy := make(map[string]string, len(source))
for k, v := range source {
sourcecopy[k] = v
}
for k, s := range sourcecopy {
resultKey := reg.FindAllString(s, -1)
for _, rk := range resultKey {
key := strings.ToUpper(GetConfigKey(rk))
// if len(key) < 1 {
// return nil, fmt.Errorf("%s Parameter configuration error.please make sure `${XXX}`", s)
// }
value := GetConfig(groupCtx, key)
sourcecopy[k] = strings.Replace(s, rk, value, -1)
}
}
return sourcecopy, nil
}