/
config.go
159 lines (129 loc) · 3.94 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
// 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 harness
import (
"bytes"
"encoding/json"
"io/ioutil"
"os"
"os/user"
"path"
"github.com/intel-hpdd/logging/alert"
"github.com/intel-hpdd/logging/debug"
"github.com/hashicorp/hcl"
"github.com/pkg/errors"
)
const (
// UATConfigFile is the name of the harness configuration file
UATConfigFile = ".lhsmd-test"
// UATConfigEnvVar is the name of the optional environment variable that
// may be set to specify config location
UATConfigEnvVar = "LHSMD_UAT_CONFIG_FILE"
)
// Config holds configuration for the test harness
type Config struct {
HsmDriver string `hcl:"hsm_driver" json:"hsm_driver"`
LustrePath string `hcl:"lustre_path" json:"lustre_path"`
CleanupOnFailure bool `hcl:"cleanup_on_failure" json:"cleanup_on_failure"`
EnableAgentDebug bool `hcl:"enable_agent_debug" json:"enable_agent_debug"`
AWSAccessKeyID string `hcl:"aws_access_key_id" json:"aws_access_key_id"`
AWSSecretAccessKey string `hcl:"aws_secret_access_key" json:"aws_secret_access_key"`
S3Region string `hcl:"s3_region" json:"s3_region"`
S3Endpoint string `hcl:"s3_endpoint" json:"s3_endpoint"`
S3Bucket string `hcl:"s3_bucket" json:"s3_bucket"`
S3Prefix string `hcl:"s3_prefix" json:"s3_prefix"`
}
// Merge combines this config's values with the other config's values
func (c *Config) Merge(other *Config) *Config {
result := new(Config)
result.HsmDriver = c.HsmDriver
if other.HsmDriver != "" {
result.HsmDriver = other.HsmDriver
}
result.LustrePath = c.LustrePath
if other.LustrePath != "" {
result.LustrePath = other.LustrePath
}
result.CleanupOnFailure = other.CleanupOnFailure
result.EnableAgentDebug = other.EnableAgentDebug
result.S3Region = c.S3Region
if other.S3Region != "" {
result.S3Region = other.S3Region
}
result.S3Endpoint = c.S3Endpoint
if other.S3Endpoint != "" {
result.S3Endpoint = other.S3Endpoint
}
result.S3Bucket = c.S3Bucket
if other.S3Bucket != "" {
result.S3Bucket = other.S3Bucket
}
result.S3Prefix = c.S3Prefix
if other.S3Prefix != "" {
result.S3Prefix = other.S3Prefix
}
result.AWSAccessKeyID = c.AWSAccessKeyID
if other.AWSAccessKeyID != "" {
result.AWSAccessKeyID = other.AWSAccessKeyID
}
result.AWSSecretAccessKey = c.AWSSecretAccessKey
if other.AWSSecretAccessKey != "" {
result.AWSSecretAccessKey = other.AWSSecretAccessKey
}
return result
}
func (c *Config) String() string {
data, err := json.Marshal(c)
if err != nil {
alert.Abort(errors.Wrap(err, "couldn't marshal test config to json"))
}
var out bytes.Buffer
json.Indent(&out, data, "", "\t")
return out.String()
}
// NewConfig initializes a new Config instance with default values
func NewConfig() *Config {
return &Config{
S3Region: "us-east-1",
}
}
// LoadConfig attempts to load a config from one of the default locations
func LoadConfig() (*Config, error) {
cfg := NewConfig()
user, err := user.Current()
if err != nil {
return nil, errors.Wrap(err, "Unable to get current user")
}
cwd, err := os.Getwd()
if err != nil {
return nil, errors.Wrap(err, "Unable to get current directory")
}
// list of locations to try, in decreasing precedence
locations := []string{
os.Getenv(UATConfigEnvVar),
path.Join(cwd, UATConfigFile),
path.Join(user.HomeDir, UATConfigFile),
}
for _, location := range locations {
debug.Printf("trying to load config from %s", location)
if loaded, err := loadConfigFile(location); err == nil {
cfg = cfg.Merge(loaded)
break
}
}
debug.Printf("Harness config: %s", cfg)
return cfg, nil
}
func loadConfigFile(cfgPath string) (*Config, error) {
cfg := NewConfig()
data, err := ioutil.ReadFile(cfgPath)
if err != nil {
return nil, err
}
if err := hcl.Decode(cfg, string(data)); err != nil {
alert.Warnf("config file error %s:%s", cfgPath, err)
return nil, err
}
return cfg, nil
}