forked from opsgenie/oec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configuration.go
204 lines (166 loc) · 6.08 KB
/
configuration.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
package conf
import (
"github.com/opsgenie/oec/git"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"os"
"path/filepath"
"strings"
"time"
)
const (
LocalSourceType = "local"
GitSourceType = "git"
DefaultBaseUrl = "https://api.opsgenie.com"
)
var readConfigurationFromGitFunc = readConfigurationFromGit
var readConfigurationFromLocalFunc = readConfigurationFromLocal
var defaultConfFilepath = filepath.Join("~", "oec", "config.json")
type Configuration struct {
ActionSpecifications `yaml:",inline"`
AppName string `json:"appName" yaml:"appName"`
ApiKey string `json:"apiKey" yaml:"apiKey"`
BaseUrl string `json:"baseUrl" yaml:"baseUrl"`
PollerConf PollerConf `json:"pollerConf" yaml:"pollerConf"`
PoolConf PoolConf `json:"poolConf" yaml:"poolConf"`
LogLevel string `json:"logLevel" yaml:"logLevel"`
LogrusLevel logrus.Level
}
type ActionSpecifications struct {
ActionMappings ActionMappings `json:"actionMappings" yaml:"actionMappings"`
GlobalFlags Flags `json:"globalFlags" yaml:"globalFlags"`
GlobalArgs []string `json:"globalArgs" yaml:"globalArgs"`
GlobalEnv []string `json:"globalEnv" yaml:"globalEnv"`
}
type ActionName string
type ActionMappings map[ActionName]MappedAction
func (m ActionMappings) GitActions() []git.GitOptions {
opts := make([]git.GitOptions, 0)
for _, action := range m {
if (action.GitOptions != git.GitOptions{}) {
opts = append(opts, action.GitOptions)
}
}
return opts
}
type MappedAction struct {
SourceType string `json:"sourceType" yaml:"sourceType"`
GitOptions git.GitOptions `json:"gitOptions" yaml:"gitOptions"`
Filepath string `json:"filepath" yaml:"filepath"`
Flags Flags `json:"flags" yaml:"flags"`
Args []string `json:"args" yaml:"args"`
Env []string `json:"env" yaml:"env"`
Stdout string `json:"stdout" yaml:"stdout"`
Stderr string `json:"stderr" yaml:"stderr"`
}
type Flags map[string]string
func (f Flags) Args() []string {
args := make([]string, 0)
for flagName, flagValue := range f {
args = append(args, "-"+flagName)
args = append(args, flagValue)
}
return args
}
type PollerConf struct {
PollingWaitIntervalInMillis time.Duration `json:"pollingWaitIntervalInMillis" yaml:"pollingWaitIntervalInMillis"`
VisibilityTimeoutInSeconds int64 `json:"visibilityTimeoutInSeconds" yaml:"visibilityTimeoutInSeconds"`
MaxNumberOfMessages int64 `json:"maxNumberOfMessages" yaml:"maxNumberOfMessages"`
}
type PoolConf struct {
MaxNumberOfWorker int32 `json:"maxNumberOfWorker" yaml:"maxNumberOfWorker"`
MinNumberOfWorker int32 `json:"minNumberOfWorker" yaml:"minNumberOfWorker"`
QueueSize int32 `json:"queueSize" yaml:"queueSize"`
KeepAliveTimeInMillis time.Duration `json:"keepAliveTimeInMillis" yaml:"keepAliveTimeInMillis"`
MonitoringPeriodInMillis time.Duration `json:"monitoringPeriodInMillis" yaml:"monitoringPeriodInMillis"`
}
func ReadConfFile() (*Configuration, error) {
confSourceType := os.Getenv("OEC_CONF_SOURCE_TYPE")
conf, err := readConfFileFromSource(strings.ToLower(confSourceType))
if err != nil {
return nil, err
}
err = validateConfiguration(conf)
if err != nil {
return nil, err
}
addHomeDirPrefixToActionMappings(conf.ActionMappings)
chmodLocalActions(conf.ActionMappings, 0700)
conf.addDefaultFlags()
return conf, nil
}
func (c *Configuration) addDefaultFlags() {
c.GlobalArgs = append(
[]string{
"-apiKey", c.ApiKey,
"-opsgenieUrl", c.BaseUrl,
"-logLevel", strings.ToUpper(c.LogLevel),
},
c.GlobalArgs...,
)
}
func validateConfiguration(conf *Configuration) error {
if conf == nil || conf == (&Configuration{}) {
return errors.New("The configuration is empty.")
}
if conf.ApiKey == "" {
return errors.New("ApiKey is not found in the configuration file.")
}
if conf.BaseUrl == "" {
conf.BaseUrl = DefaultBaseUrl
logrus.Infof("BaseUrl is not found in the configuration file, default url[%s] is set.", DefaultBaseUrl)
}
if len(conf.ActionMappings) == 0 {
return errors.New("Action mappings configuration is not found in the configuration file.")
} else {
for actionName, action := range conf.ActionMappings {
if action.SourceType != LocalSourceType &&
action.SourceType != GitSourceType {
return errors.Errorf("Action source type of action[%s] should be either local or git.", actionName)
} else {
if action.Filepath == "" {
return errors.Errorf("Filepath of action[%s] is empty.", actionName)
}
if action.SourceType == GitSourceType &&
action.GitOptions == (git.GitOptions{}) {
return errors.Errorf("Git options of action[%s] is empty.", actionName)
}
}
}
}
level, err := logrus.ParseLevel(conf.LogLevel)
if err != nil {
conf.LogrusLevel = logrus.InfoLevel
} else {
conf.LogrusLevel = level
}
return nil
}
func readConfFileFromSource(confSourceType string) (*Configuration, error) {
switch confSourceType {
case GitSourceType:
url := os.Getenv("OEC_CONF_GIT_URL")
privateKeyFilepath := os.Getenv("OEC_CONF_GIT_PRIVATE_KEY_FILEPATH")
passphrase := os.Getenv("OEC_CONF_GIT_PASSPHRASE")
confFilepath := os.Getenv("OEC_CONF_GIT_FILEPATH")
if privateKeyFilepath != "" {
privateKeyFilepath = addHomeDirPrefix(privateKeyFilepath)
}
if confFilepath == "" {
return nil, errors.New("Git configuration filepath could not be empty.")
}
return readConfigurationFromGitFunc(url, privateKeyFilepath, passphrase, confFilepath)
case LocalSourceType:
confFilepath := os.Getenv("OEC_CONF_LOCAL_FILEPATH")
if len(confFilepath) <= 0 {
confFilepath = addHomeDirPrefix(defaultConfFilepath)
} else {
confFilepath = addHomeDirPrefix(confFilepath)
}
return readConfigurationFromLocalFunc(confFilepath)
case "":
return nil, errors.Errorf("OEC_CONF_SOURCE_TYPE should be set as \"local\" or \"git\".")
default:
return nil, errors.Errorf("Unknown configuration source type[%s], valid types are \"local\" and \"git\".", confSourceType)
}
}