-
Notifications
You must be signed in to change notification settings - Fork 29
/
conf.go
301 lines (272 loc) · 8.91 KB
/
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
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
// Copyright (c) 2015 Rackspace
//
// 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 conf
import (
"bytes"
"errors"
"fmt"
"os"
"os/user"
"path/filepath"
"sort"
"strconv"
"strings"
"github.com/troubling/hummingbird/common"
"github.com/vaughan0/go-ini"
)
// Config represents an ini file.
type Config struct{ ini.File }
type Section struct {
ini.Section
c Config
section string
}
// Get fetches a value from the Config, looking in the DEFAULT section if not found in the specific section. Also ignores "set " key prefixes, like paste.
func (f Config) Get(section string, key string) (string, bool) {
if value, ok := f.File.Get(section, key); ok {
return value, true
} else if value, ok := f.File.Get("DEFAULT", key); ok {
return value, true
} else if value, ok := f.File.Get(section, "set "+key); ok {
return value, true
} else if value, ok := f.File.Get("DEFAULT", "set "+key); ok {
return value, true
}
return "", false
}
// GetDefault returns a value from the config, or returns the default setting if the entry doesn't exist.
func (f Config) GetDefault(section string, key string, dfl string) string {
if value, ok := f.Get(section, key); ok {
return value
}
return dfl
}
// GetBool loads a true/false value from the config, with support for things like "yes", "true", "1", "t", etc.
func (f Config) GetBool(section string, key string, dfl bool) bool {
if value, ok := f.Get(section, key); ok {
return common.LooksTrue(value)
}
return dfl
}
// GetInt loads an entry from the config, parsed as an integer value.
func (f Config) GetInt(section string, key string, dfl int64) int64 {
if value, ok := f.Get(section, key); ok {
if val, err := strconv.ParseInt(value, 10, 64); err == nil {
return val
}
panic(fmt.Sprintf("Error parsing integer %s/%s from config.", section, key))
}
return dfl
}
// GetFloat loads an entry from the config, parsed as a floating point value.
func (f Config) GetFloat(section string, key string, dfl float64) float64 {
if value, ok := f.Get(section, key); ok {
if val, err := strconv.ParseFloat(value, 64); err == nil {
return val
}
panic(fmt.Sprintf("Error parsing float %s/%s from config.", section, key))
}
return dfl
}
// GetLimit loads an entry from the config in the format of %d/%d.
func (f Config) GetLimit(section string, key string, dfla int64, dflb int64) (int64, int64) {
if value, ok := f.Get(section, key); ok {
fmt.Sscanf(value, "%d/%d", &dfla, &dflb)
}
return dfla, dflb
}
// HasSection determines whether or not the section exists in the ini file.
func (f Config) HasSection(section string) bool {
return f.File[section] != nil
}
// GetSection returns a Section struct.
func (f Config) GetSection(section string) Section {
return Section{f.File[section], f, section}
}
func (s Section) Get(key string) (string, bool) {
return s.c.Get(s.section, key)
}
func (s Section) GetDefault(key string, dfl string) string {
return s.c.GetDefault(s.section, key, dfl)
}
func (s Section) GetBool(key string, dfl bool) bool {
return s.c.GetBool(s.section, key, dfl)
}
func (s Section) GetInt(key string, dfl int64) int64 {
return s.c.GetInt(s.section, key, dfl)
}
func (s Section) GetFloat(key string, dfl float64) float64 {
return s.c.GetFloat(s.section, key, dfl)
}
func (s Section) GetLimit(key string, dfla int64, dflb int64) (int64, int64) {
return s.c.GetLimit(s.section, key, dfla, dflb)
}
// LoadConfig loads an ini from a path. The path should be a *.conf file or a *.conf.d directory.
func LoadConfig(path string) (Config, error) {
file := Config{make(ini.File)}
if fi, err := os.Stat(path); err != nil {
return file, err
} else if fi.IsDir() {
files, err := filepath.Glob(filepath.Join(path, "*.conf"))
if err != nil {
return file, err
}
sort.Strings(files)
for _, subfile := range files {
sf, err := LoadConfig(subfile)
if err != nil {
return file, err
}
for sec, val := range sf.File {
file.File[sec] = val
}
}
return file, nil
}
return file, file.LoadFile(path)
}
// LoadConfigs finds and loads any configs that exist for the given path. Multiple configs are supported for things like SAIO setups.
func LoadConfigs(path string) ([]Config, error) {
configPaths := []string{}
configs := []Config{}
if fi, err := os.Stat(path); err == nil && fi.IsDir() && !strings.HasSuffix(path, ".conf.d") {
if multiConfigs, err := filepath.Glob(filepath.Join(path, "*.conf")); err == nil {
configPaths = append(configPaths, multiConfigs...)
}
if multiConfigs, err := filepath.Glob(filepath.Join(path, "*.conf.d")); err == nil {
configPaths = append(configPaths, multiConfigs...)
}
} else {
configPaths = append(configPaths, path)
}
for _, p := range configPaths {
if config, err := LoadConfig(p); err == nil {
configs = append(configs, config)
}
}
if len(configs) == 0 {
return nil, errors.New("Unable to find any configs")
}
return configs, nil
}
// StringConfig returns an Config from a string, for use in tests.
func StringConfig(data string) (Config, error) {
file := Config{make(ini.File)}
return file, file.Load(bytes.NewBufferString(data))
}
// UidFromConf returns the uid and gid for the user set in the first config found.
func UidFromConf(path string) (uint32, uint32, error) {
configs, err := LoadConfigs(path)
if err != nil {
return 0, 0, err
}
for _, config := range configs {
var usr *user.User
var err error
username := config.GetDefault("DEFAULT", "user", "")
if username != "" {
usr, err = user.Lookup(username)
} else {
usr, err = user.Current()
}
if err != nil {
return 0, 0, err
}
uid, err := strconv.ParseUint(usr.Uid, 10, 32)
if err != nil {
return 0, 0, err
}
gid, err := strconv.ParseUint(usr.Gid, 10, 32)
if err != nil {
return 0, 0, err
}
return uint32(uid), uint32(gid), nil
}
return 0, 0, fmt.Errorf("Unable to find config")
}
var configLocations = []string{"/etc/hummingbird/hummingbird.conf", "/etc/swift/swift.conf"}
// GetHashPrefixAndSuffix retrieves the hash path prefix and suffix from
// the correct configs based on the environments setup. The suffix cannot
// be nil
type getHashPrefixAndSuffixFunc func() (pfx string, sfx string, err error)
var GetHashPrefixAndSuffix getHashPrefixAndSuffixFunc = normalGetHashPrefixAndSuffix
func normalGetHashPrefixAndSuffix() (prefix string, suffix string, err error) {
for _, loc := range configLocations {
if conf, e := LoadConfig(loc); e == nil {
var ok bool
prefix, _ = conf.Get("swift-hash", "swift_hash_path_prefix")
if suffix, ok = conf.Get("swift-hash", "swift_hash_path_suffix"); !ok {
return prefix, suffix, errors.New("Hash path suffix not defined")
}
return prefix, suffix, nil
}
}
return "", "", fmt.Errorf("No conf found; looked for %s", configLocations)
}
func ReadResellerOptions(conf Section, defaults map[string][]string) ([]string, map[string]map[string][]string) {
resellerPrefixOpt := conf.GetDefault("reseller_prefix", "AUTH")
s := []string{}
for _, val := range strings.Split(resellerPrefixOpt, ",") {
v := strings.TrimSpace(val)
if v != "" {
s = append(s, strings.TrimSpace(v))
}
}
resellerPrefix := []string{}
for _, prefix := range s {
if prefix == "''" {
prefix = ""
}
if prefix != "" && !strings.HasSuffix(prefix, "_") {
prefix = prefix + "_"
}
if !common.StringInSlice(prefix, resellerPrefix) {
resellerPrefix = append(resellerPrefix, prefix)
}
}
if len(resellerPrefix) == 0 {
resellerPrefix = append(resellerPrefix, "")
}
associatedOptions := make(map[string]map[string][]string)
for _, prefix := range resellerPrefix {
associatedOptions[prefix] = make(map[string][]string)
for k, v := range defaults {
associatedOptions[prefix][k] = v
}
for k, v := range ReadPrefixedOptions(conf, "", defaults) {
associatedOptions[prefix][k] = v
}
prefix_name := "''"
if prefix != "" {
prefix_name = prefix
}
for k, v := range ReadPrefixedOptions(conf, prefix_name, defaults) {
associatedOptions[prefix][k] = v
}
}
return resellerPrefix, associatedOptions
}
func ReadPrefixedOptions(conf Section, prefixName string, defaults map[string][]string) map[string][]string {
params := make(map[string][]string)
for optionName := range defaults {
if value, ok := conf.Get(fmt.Sprintf("%s%s", prefixName, optionName)); ok {
params[optionName] = []string{}
for _, role := range strings.Split(strings.ToLower(value), ",") {
params[optionName] = append(params[optionName], strings.TrimSpace(role))
}
}
}
return params
}