/
config.go
336 lines (271 loc) · 8.26 KB
/
config.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// Copyright (c) 2018 DDN. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package agent
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"path"
"runtime"
"strings"
"github.com/hashicorp/hcl"
"github.com/hashicorp/hcl/hcl/ast"
"github.com/pkg/errors"
"github.com/intel-hpdd/lemur/cmd/lhsmd/config"
"github.com/intel-hpdd/logging/alert"
"github.com/intel-hpdd/logging/debug"
"github.com/intel-hpdd/go-lustre/fs/spec"
)
var (
optConfigPath string
)
type (
transportConfig struct {
Type string `hcl:"type"`
SocketDir string `hcl:"socket_dir"`
}
influxConfig struct {
URL string `hcl:"url"`
DB string `hcl:"db"`
User string `hcl:"user"`
Password string `hcl:"password"`
}
snapshotConfig struct {
Enabled bool `hcl:"enabled"`
}
clientMountOptions []string
// Config represents HSM Agent configuration
Config struct {
MountRoot string `hcl:"mount_root" json:"mount_root"`
ClientDevice *spec.ClientDevice `json:"client_device"`
ClientMountOptions clientMountOptions `hcl:"client_mount_options" json:"client_mount_options"`
Processes int `hcl:"handler_count" json:"handler_count"`
InfluxDB *influxConfig `hcl:"influxdb" json:"influxdb"`
EnabledPlugins []string `hcl:"enabled_plugins" json:"enabled_plugins"`
PluginDir string `hcl:"plugin_dir" json:"plugin_dir"`
Snapshots *snapshotConfig `hcl:"snapshots" json:"snapshots"`
Transport *transportConfig `hcl:"transport" json:"transport"`
}
)
func (cmo clientMountOptions) HasOption(o string) bool {
for _, option := range cmo {
if option == o {
return true
}
}
return false
}
func (cmo clientMountOptions) String() string {
return strings.Join(cmo, ",")
}
func (c *transportConfig) Merge(other *transportConfig) *transportConfig {
result := new(transportConfig)
result.Type = c.Type
if other.Type != "" {
result.Type = other.Type
}
result.SocketDir = c.SocketDir
if other.SocketDir != "" {
result.SocketDir = other.SocketDir
}
return result
}
func (c *transportConfig) ConnectionString() string {
return fmt.Sprintf("%s/lhsmd-%d", c.SocketDir, os.Getpid())
}
func (c *influxConfig) Merge(other *influxConfig) *influxConfig {
result := new(influxConfig)
result.URL = c.URL
if other.URL != "" {
result.URL = other.URL
}
result.DB = c.DB
if other.DB != "" {
result.DB = other.DB
}
result.User = c.User
if other.User != "" {
result.User = other.User
}
result.Password = c.Password
if other.Password != "" {
result.Password = other.Password
}
return result
}
func (c *snapshotConfig) Merge(other *snapshotConfig) *snapshotConfig {
result := new(snapshotConfig)
result.Enabled = other.Enabled
return result
}
func init() {
flag.StringVar(&optConfigPath, "config", config.DefaultConfigPath, "Path to agent config")
// The CLI argument takes precedence, if both are set.
if optConfigPath == config.DefaultConfigPath {
if cfgDir := os.Getenv(config.ConfigDirEnvVar); cfgDir != "" {
optConfigPath = path.Join(cfgDir, config.AgentConfigFile)
}
}
}
func (c *Config) String() string {
data, err := json.Marshal(c)
if err != nil {
alert.Abort(errors.Wrap(err, "marshal failed"))
}
var out bytes.Buffer
json.Indent(&out, data, "", "\t")
return out.String()
}
// Plugins returns a slice of *PluginConfig instances for enabled plugins
func (c *Config) Plugins() []*PluginConfig {
var plugins []*PluginConfig
// Ensure that this is set in our env so that plugins can use it to
// find their own configs
os.Setenv(config.ConfigDirEnvVar, path.Dir(optConfigPath))
connectAt := c.Transport.ConnectionString()
for _, name := range c.EnabledPlugins {
binPath := path.Join(c.PluginDir, name)
plugin := NewPlugin(name, binPath, connectAt, c.MountRoot)
plugins = append(plugins, plugin)
}
return plugins
}
// AgentMountpoint returns the calculated agent mountpoint under the
// agent mount root.
func (c *Config) AgentMountpoint() string {
return path.Join(c.MountRoot, "agent")
}
// Merge combines the supplied configuration's values with this one's
func (c *Config) Merge(other *Config) *Config {
result := new(Config)
result.MountRoot = c.MountRoot
if other.MountRoot != "" {
result.MountRoot = other.MountRoot
}
result.ClientDevice = c.ClientDevice
if other.ClientDevice != nil {
result.ClientDevice = other.ClientDevice
}
result.ClientMountOptions = c.ClientMountOptions
for _, otherOption := range other.ClientMountOptions {
if result.ClientMountOptions.HasOption(otherOption) {
continue
}
result.ClientMountOptions = append(result.ClientMountOptions, otherOption)
}
result.Processes = c.Processes
if other.Processes > result.Processes {
result.Processes = other.Processes
}
result.InfluxDB = c.InfluxDB
if other.InfluxDB != nil {
result.InfluxDB = result.InfluxDB.Merge(other.InfluxDB)
}
result.EnabledPlugins = c.EnabledPlugins
if len(other.EnabledPlugins) > 0 {
result.EnabledPlugins = other.EnabledPlugins
}
result.PluginDir = c.PluginDir
if other.PluginDir != "" {
result.PluginDir = other.PluginDir
}
result.Snapshots = c.Snapshots
if other.Snapshots != nil {
result.Snapshots = result.Snapshots.Merge(other.Snapshots)
}
result.Transport = c.Transport
if other.Transport != nil {
result.Transport = result.Transport.Merge(other.Transport)
}
return result
}
// DefaultConfig initializes a new Config struct with default values
func DefaultConfig() *Config {
cfg := NewConfig()
cfg.MountRoot = config.DefaultAgentMountRoot
cfg.ClientMountOptions = config.DefaultClientMountOptions
cfg.PluginDir = config.DefaultPluginDir
cfg.Processes = runtime.NumCPU()
cfg.Transport = &transportConfig{
Type: config.DefaultTransport,
SocketDir: config.DefaultTransportSocketDir,
}
return cfg
}
// NewConfig initializes a new Config struct with zero values
func NewConfig() *Config {
return &Config{
InfluxDB: &influxConfig{},
Snapshots: &snapshotConfig{},
Transport: &transportConfig{},
EnabledPlugins: []string{},
ClientMountOptions: clientMountOptions{},
}
}
// LoadConfig reads a config at the supplied path
func LoadConfig(configPath string) (*Config, error) {
data, err := ioutil.ReadFile(configPath)
if err != nil {
return nil, errors.Wrap(err, "read failed")
}
obj, err := hcl.Parse(string(data))
if err != nil {
return nil, errors.Wrap(err, "parse config failed")
}
defaults := DefaultConfig()
cfg := NewConfig()
if err = hcl.DecodeObject(cfg, obj); err != nil {
return nil, errors.Wrap(err, "decode config failed")
}
cfg = defaults.Merge(cfg)
list, ok := obj.Node.(*ast.ObjectList)
if !ok {
return nil, errors.Errorf("Malformed config file")
}
f := list.Filter("client_device")
if len(f.Items) == 0 {
return nil, errors.Errorf("No client_device specified")
}
if len(f.Items) > 1 {
return nil, errors.Errorf("Line %d: More than 1 client_device specified", f.Items[1].Assign.Line)
}
var devStr string
if err = hcl.DecodeObject(&devStr, f.Elem().Items[0].Val); err != nil {
return nil, errors.Wrap(err, "decode device failed")
}
cfg.ClientDevice, err = spec.ClientDeviceFromString(devStr)
if err != nil {
return nil, errors.Wrapf(err, "Line %d: Invalid client_device %q", f.Items[0].Assign.Line, devStr)
}
return cfg, nil
}
// ConfigInitMust returns a valid *Config or fails trying
func ConfigInitMust() *Config {
debug.Printf("loading config from %s", optConfigPath)
cfg, err := LoadConfig(optConfigPath)
if err != nil {
if !(optConfigPath == config.DefaultConfigPath && os.IsNotExist(err)) {
alert.Abort(errors.Wrap(err, "Failed to load config"))
}
}
if cfg.Transport == nil {
alert.Abort(errors.New("Invalid configuration: No transports configured"))
}
if _, err := os.Stat(cfg.PluginDir); os.IsNotExist(err) {
alert.Abort(errors.Errorf("Invalid configuration: plugin_dir %q does not exist", cfg.PluginDir))
}
if len(cfg.EnabledPlugins) == 0 {
alert.Abort(errors.New("Invalid configuration: No data mover plugins configured"))
}
for _, plugin := range cfg.EnabledPlugins {
pluginPath := path.Join(cfg.PluginDir, plugin)
if _, err := os.Stat(pluginPath); os.IsNotExist(err) {
alert.Abort(errors.Errorf("Invalid configuration: Plugin %q not found in %s", plugin, cfg.PluginDir))
}
}
return cfg
}