forked from edgexfoundry/core-config-seed-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
313 lines (258 loc) · 8.11 KB
/
main.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
/*******************************************************************************
* Copyright 2017 Samsung Electronics All Rights Reserved.
*
* 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 main
import (
"bufio"
"errors"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/edgexfoundry/core-config-seed-go/internal/pkg"
"github.com/edgexfoundry/core-config-seed-go/internal/pkg/config"
"github.com/fatih/structs"
consulapi "github.com/hashicorp/consul/api"
"github.com/magiconair/properties"
"gopkg.in/yaml.v2"
)
var Version = "master"
const consulStatusPath = "/v1/agent/self"
// Hook the functions in the other packages for the tests.
var (
consulDefaultConfig = consulapi.DefaultConfig
consulNewClient = consulapi.NewClient
consulDeleteTree = (*consulapi.KV).DeleteTree
consulPut = (*consulapi.KV).Put
consulKeys = (*consulapi.KV).Keys
httpGet = http.Get
)
func main() {
var useConsul bool
var useProfile string
flag.BoolVar(&useConsul, "consul", false, "Indicates the service should use consul.")
flag.BoolVar(&useConsul, "c", false, "Indicates the service should use consul.")
flag.StringVar(&useProfile, "profile", "", "Specify a profile other than default.")
flag.StringVar(&useProfile, "p", "", "Specify a profile other than default.")
flag.Parse()
// Configuration data for the config-seed service.
coreConfig := &pkg.CoreConfig{}
err := config.LoadFromFile(useProfile, coreConfig)
if err != nil {
logBeforeTermination(err)
return
}
consulClient, err := getConsulClient(*coreConfig)
if err != nil {
fmt.Println(err.Error())
return
}
kv := consulClient.KV()
if coreConfig.IsReset {
removeStoredConfig(kv)
loadConfigFromPath(*coreConfig, kv)
} else if !isConfigInitialized(*coreConfig, kv) {
loadConfigFromPath(*coreConfig, kv)
}
printBanner("./res/banner.txt")
}
// Print a banner.
func printBanner(path string) {
b, err := ioutil.ReadFile(path)
if err != nil {
fmt.Print(err)
return
}
fmt.Println(string(b))
}
func logBeforeTermination(err error) {
fmt.Println(err.Error())
}
// Get handle of Consul client using the URL from configuration info.
// Before getting handle, it tries to receive a response from a Consul agent by simple health-check.
func getConsulClient(coreConfig pkg.CoreConfig) (*consulapi.Client, error) {
consulUrl := coreConfig.ConsulProtocol + "://" + coreConfig.ConsulHost + ":" + strconv.Itoa(coreConfig.ConsulPort)
// Check the connection to Consul
fails := 0
for fails < coreConfig.FailLimit {
resp, err := httpGet(consulUrl + consulStatusPath)
if err != nil {
fmt.Println(err.Error())
time.Sleep(time.Second * time.Duration(coreConfig.FailWaitTime))
fails++
continue
}
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
break
}
}
if fails >= coreConfig.FailLimit {
return nil, errors.New("Cannot get connection to Consul")
}
// Connect to the Consul Agent
configTemp := consulDefaultConfig()
configTemp.Address = consulUrl
return consulNewClient(configTemp)
}
// Remove all values in Consul K/V store, under the globalprefix which is presents in configuration file.
func removeStoredConfig(kv *consulapi.KV) {
_, err := consulDeleteTree(kv, pkg.CoreConfiguration.GlobalPrefix, nil)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println("All values under the globalPrefix(\"" + pkg.CoreConfiguration.GlobalPrefix + "\") is removed.")
}
// Check if Consul has been configured by trying to get any key that starts with a globalprefix.
func isConfigInitialized(coreConfig pkg.CoreConfig, kv *consulapi.KV) bool {
keys, _, err := consulKeys(kv, coreConfig.GlobalPrefix, "", nil)
if err != nil {
fmt.Println(err.Error())
return false
}
if len(keys) > 0 {
fmt.Printf("%s exists! The configuration data has been initialized.\n", coreConfig.GlobalPrefix)
return true
}
fmt.Printf("%s doesn't exist! Start importing configuration data.\n", coreConfig.GlobalPrefix)
return false
}
// Load a property file(.yaml or .properties) and parse it to a map.
func readPropertyFile(coreConfig pkg.CoreConfig, filePath string) (pkg.ConfigProperties, error) {
if isTomlExtension(coreConfig, filePath) {
// Read .toml
return readTomlFile(filePath)
} else if isYamlExtension(coreConfig, filePath) {
// Read .yaml/.yml file
return readYamlFile(filePath)
} else {
// Read .properties file
return readPropertiesFile(filePath)
}
}
// Load all config files and put the configuration info to Consul K/V store.
func loadConfigFromPath(coreConfig pkg.CoreConfig, kv *consulapi.KV) {
err := filepath.Walk(coreConfig.ConfigPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Skip directories & unacceptable property extension
if info.IsDir() || !isAcceptablePropertyExtensions(coreConfig, info.Name()) {
return nil
}
dir, file := filepath.Split(path)
configPath, err := filepath.Rel(".", coreConfig.ConfigPath)
if err != nil {
return err
}
dir = strings.TrimPrefix(dir, configPath+"/")
fmt.Println("found config file:", file, "in context", dir)
// Parse *.properties
props, err := readPropertyFile(coreConfig, path)
if err != nil {
return err
}
// Put config properties to Consul K/V store.
prefix := coreConfig.GlobalPrefix + "/" + dir
for k := range props {
p := &consulapi.KVPair{Key: prefix + k, Value: []byte(props[k])}
if _, err := consulPut(kv, p, nil); err != nil {
return err
}
}
return nil
})
if err != nil {
fmt.Println(err.Error())
return
}
}
func isAcceptablePropertyExtensions(coreConfig pkg.CoreConfig, file string) bool {
for _, v := range coreConfig.AcceptablePropertyExtensions {
if v == filepath.Ext(file) {
return true
}
}
return false
}
// Check whether a filename extension is yaml or not.
func isYamlExtension(coreConfig pkg.CoreConfig, file string) bool {
for _, v := range coreConfig.YamlExtensions {
if v == filepath.Ext(file) {
return true
}
}
return false
}
func isTomlExtension(coreConfig pkg.CoreConfig, file string) bool {
for _, v := range coreConfig.TomlExtensions {
if v == filepath.Ext(file) {
return true
}
}
return false
}
//This works for now because our TOML is simply key/value.
//Will not work once we go hierarchical
func readTomlFile(filePath string) (pkg.ConfigProperties, error) {
configProps := pkg.ConfigProperties{}
file, err := os.Open(filePath)
if err != nil {
return configProps, fmt.Errorf("could not load configuration file (%s): %v", filePath, err.Error())
}
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, "=") {
tokens := strings.Split(scanner.Text(), "=")
configProps[strings.Trim(tokens[0], " '")] = strings.Trim(tokens[1], " '")
}
}
return configProps, nil
}
// Parse a yaml file to a map.
func readYamlFile(filePath string) (pkg.ConfigProperties, error) {
configProps := pkg.ConfigProperties{}
contents, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, err
}
if err = yaml.Unmarshal(contents, configProps); err != nil {
return nil, err
}
m := structs.Map(configProps)
for key, value := range m {
m[key] = fmt.Sprint(value)
}
return configProps, nil
}
// Parse a properties file to a map.
func readPropertiesFile(filePath string) (pkg.ConfigProperties, error) {
configProps := pkg.ConfigProperties{}
props, err := properties.LoadFile(filePath, properties.UTF8)
if err != nil {
return nil, err
}
configProps = props.Map()
return configProps, nil
}