forked from MediaMath/grim
/
config.go
248 lines (211 loc) · 6.1 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
package grim
// Copyright 2015 MediaMath <http://www.mediamath.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
import (
"fmt"
"os"
"path/filepath"
"time"
)
var (
defaultGrimQueueName = "grim-queue"
defaultConfigRoot = "/etc/grim"
defaultResultRoot = "/var/log/grim"
defaultWorkspaceRoot = "/var/tmp/grim"
defaultTimeout = 5 * time.Minute
configFileName = "config.json"
buildScriptName = "build.sh"
repoBuildScriptName = "grim_build.sh"
repoHiddenBuildScriptName = ".grim_build.sh"
defaultTemplateForStart = templateForStart()
defaultTemplateForError = templateForFailureandError("Error during")
defaultTemplateForSuccess = templateForSuccess()
defaultTemplateForFailure = templateForFailureandError("Failure during")
)
type configMap map[string]interface{}
type effectiveConfig struct {
grimQueueName string
resultRoot string
workspaceRoot string
awsRegion string
awsKey string
awsSecret string
gitHubToken string
pathToCloneIn string
snsTopicName string
hipChatRoom string
hipChatToken string
grimServerID string
origServerID string
truncateID string
pendingTemplate string
errorTemplate string
successTemplate string
failureTemplate string
timeout int
usernameWhitelist []string
}
//BuildTimeout is a "safe" way to get the timeout configured for builds. It will ensure non-zero build timeouts.
type BuildTimeout interface {
BuildTimeout() time.Duration
}
func (ec *effectiveConfig) BuildTimeout() time.Duration {
seconds := ec.timeout
if seconds > 0 {
return time.Second * time.Duration(seconds)
}
return defaultTimeout
}
func getEffectiveConfigRoot(configRootPtr *string) string {
if configRootPtr == nil || *configRootPtr == "" {
return defaultConfigRoot
}
return *configRootPtr
}
type repo struct {
owner, name string
}
func getAllConfiguredRepos(configRoot string) []repo {
var repos []repo
repoPattern := filepath.Join(configRoot, "*/*")
matches, err := filepath.Glob(repoPattern)
if err != nil {
return repos
}
for _, match := range matches {
fi, fiErr := os.Stat(match)
if fiErr != nil || !fi.Mode().IsDir() {
continue
}
rel, relErr := filepath.Rel(configRoot, match)
if relErr != nil {
continue
}
owner, name := filepath.Split(rel)
if owner != "" && name != "" {
repos = append(repos, repo{filepath.Clean(owner), filepath.Clean(name)})
}
}
return repos
}
func getEffectiveGlobalConfig(configRoot string) (ec *effectiveConfig, err error) {
gc, err := readGlobalConfig(configRoot)
if err == nil {
errs := gc.errors()
if len(errs) == 0 {
truncateID := ""
if gc.grimServerID() != gc.rawGrimServerID() {
truncateID = "GrimServerID"
if _, ok := gc["GrimServerID"]; !ok {
truncateID = "GrimQueueName"
}
}
ec = &effectiveConfig{
grimQueueName: gc.grimQueueName(),
resultRoot: gc.resultRoot(),
workspaceRoot: gc.workspaceRoot(),
awsRegion: gc.awsRegion(),
awsKey: gc.awsKey(),
awsSecret: gc.awsSecret(),
gitHubToken: gc.gitHubToken(),
snsTopicName: gc.snsTopicName(),
hipChatRoom: gc.hipChatRoom(),
hipChatToken: gc.hipChatToken(),
grimServerID: gc.grimServerID(),
origServerID: gc.rawGrimServerID(),
truncateID: truncateID,
pendingTemplate: gc.pendingTemplate(),
errorTemplate: gc.errorTemplate(),
successTemplate: gc.successTemplate(),
failureTemplate: gc.failureTemplate(),
timeout: 10,
usernameWhitelist: []string{},
}
} else {
err = errs[0]
}
}
return ec, err
}
func getEffectiveConfig(configRoot, owner, repo string) (ec *effectiveConfig, err error) {
lc, err := readLocalConfig(configRoot, owner, repo)
if err == nil {
errs := lc.errors()
if len(errs) == 0 {
ec = &effectiveConfig{
grimQueueName: lc.grimQueueName(),
resultRoot: lc.resultRoot(),
workspaceRoot: lc.workspaceRoot(),
awsRegion: lc.awsRegion(),
awsKey: lc.awsKey(),
awsSecret: lc.awsSecret(),
gitHubToken: lc.gitHubToken(),
pathToCloneIn: lc.pathToCloneIn(),
snsTopicName: lc.snsTopicName(),
hipChatRoom: lc.hipChatRoom(),
hipChatToken: lc.hipChatToken(),
grimServerID: lc.grimServerID(),
pendingTemplate: lc.pendingTemplate(),
errorTemplate: lc.errorTemplate(),
successTemplate: lc.successTemplate(),
failureTemplate: lc.failureTemplate(),
timeout: int(lc.timeout().Seconds()),
usernameWhitelist: lc.usernameWhitelist(),
}
} else {
err = errs[0]
}
}
return
}
func templateForStart() *string {
s := fmt.Sprintf("Starting build of {{.Owner}}/{{.Repo}} initiated by a {{.EventName}} to {{.Target}} by {{.UserName}}")
return &s
}
func templateForSuccess() *string {
s := fmt.Sprintf("Success after build of {{.Owner}}/{{.Repo}} initiated by a {{.EventName}} to {{.Target}} by {{.UserName}} ({{.Workspace}})")
return &s
}
func templateForFailureandError(preamble string) *string {
s := fmt.Sprintf("%s build of {{.Owner}}/{{.Repo}} initiated by a {{.EventName}} to {{.Target}} by {{.UserName}} ({{.LogDir}})", preamble)
return &s
}
func (ec *effectiveConfig) usernameCanBuild(username string) (allowed bool) {
wlLen := len(ec.usernameWhitelist)
if ec.usernameWhitelist == nil || wlLen == 0 {
allowed = true
} else {
for i := 0; i < wlLen; i++ {
if ec.usernameWhitelist[i] == username {
allowed = true
break
}
}
}
return
}
func readStringWithDefaults(m map[string]interface{}, key string, strs ...string) string {
val, _ := m[key]
str, _ := val.(string)
if str == "" {
for _, str = range strs {
if str != "" {
break
}
}
}
return str
}
func readIntWithDefaults(m map[string]interface{}, key string, ints ...int) int {
val, _ := m[key]
i, _ := val.(int)
if i == 0 {
for _, i = range ints {
if i != 0 {
break
}
}
}
return i
}