forked from MediaMath/grim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grim.go
257 lines (204 loc) · 8.29 KB
/
grim.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
//Package grim is the "GitHub Responder In MediaMath". We liked the acronym and awkwardly filled in the details to fit it. In short, it is a task runner that is triggered by GitHub push/pull request hooks that is intended as a much simpler and easy-to-use build server than the more modular alternatives (eg. Jenkins).
//grim provides the library functions to support this use case.
//grimd is a daemon process that uses the grim library.
package grim
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"path/filepath"
)
// 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.
// Instance models the state of a configured Grim instance.
type Instance struct {
configRoot *string
queue *sqsQueue
}
// SetConfigRoot sets the base path of the configuration directory and clears any previously read config values from memory.
func (i *Instance) SetConfigRoot(path string) {
i.configRoot = &path
i.queue = nil
}
// PrepareGrimQueue creates or reuses the Amazon SQS queue named in the config.
func (i *Instance) PrepareGrimQueue(logger *log.Logger) error {
configRoot := getEffectiveConfigRoot(i.configRoot)
config, err := getEffectiveGlobalConfig(configRoot)
if err != nil {
return fatalGrimErrorf("error while reading config: %v", err)
}
if config.origServerID != config.grimServerID {
logger.Printf(buildTruncatedMessage(config.truncateID))
}
queue, err := prepareSQSQueue(config.awsKey, config.awsSecret, config.awsRegion, config.grimQueueName)
if err != nil {
return fatalGrimErrorf("error preparing queue: %v", err)
}
i.queue = queue
return nil
}
// PrepareRepos discovers all repos that are configured then sets up SNS and GitHub.
// It is an error to call this without calling PrepareGrimQueue first.
func (i *Instance) PrepareRepos() error {
if err := i.checkGrimQueue(); err != nil {
return err
}
configRoot := getEffectiveConfigRoot(i.configRoot)
config, err := getEffectiveGlobalConfig(configRoot)
if err != nil {
return fatalGrimErrorf("error while reading config: %v", err)
}
repos := getAllConfiguredRepos(configRoot)
var topicARNs []string
for _, repo := range repos {
localConfig, err := getEffectiveConfig(configRoot, repo.owner, repo.name)
if err != nil {
return fatalGrimErrorf("Error with config for %s/%s. %v", repo.owner, repo.name, err)
}
snsTopicARN, err := prepareSNSTopic(config.awsKey, config.awsSecret, config.awsRegion, localConfig.snsTopicName)
if err != nil {
return fatalGrimErrorf("error creating SNS Topic %s for %s/%s topic: %v", localConfig.snsTopicName, repo.owner, repo.name, err)
}
err = prepareSubscription(config.awsKey, config.awsSecret, config.awsRegion, snsTopicARN, i.queue.ARN)
if err != nil {
return fatalGrimErrorf("error subscribing Grim queue %q to SNS topic %q: %v", i.queue.ARN, snsTopicARN, err)
}
err = prepareAmazonSNSService(localConfig.gitHubToken, repo.owner, repo.name, snsTopicARN, config.awsKey, config.awsSecret, config.awsRegion)
if err != nil {
return fatalGrimErrorf("error creating configuring GitHub AmazonSNS service: %v", err)
}
topicARNs = append(topicARNs, snsTopicARN)
}
err = setPolicy(config.awsKey, config.awsSecret, config.awsRegion, i.queue.ARN, i.queue.URL, topicARNs)
if err != nil {
return fatalGrimErrorf("error setting policy for Grim queue %q with topics %v: %v", i.queue.ARN, topicARNs, err)
}
return nil
}
// BuildNextInGrimQueue creates or reuses an SQS queue as a source of work.
func (i *Instance) BuildNextInGrimQueue(logger *log.Logger) error {
if err := i.checkGrimQueue(); err != nil {
return err
}
configRoot := getEffectiveConfigRoot(i.configRoot)
globalConfig, err := getEffectiveGlobalConfig(configRoot)
if err != nil {
return grimErrorf("error while reading config: %v", err)
}
message, err := getNextMessage(globalConfig.awsKey, globalConfig.awsSecret, globalConfig.awsRegion, i.queue.URL)
if err != nil {
return grimErrorf("error retrieving message from Grim queue %q: %v", i.queue.URL, err)
}
if message != "" {
hook, err := extractHookEvent(message)
if err != nil {
return grimErrorf("error extracting hook from message: %v", err)
}
if skipReason := shouldSkip(hook); skipReason != nil {
logger.Printf("hook skipped %v: %s\n", skipReason, hook.Describe())
return nil
}
logger.Printf("hook built: %s\n", hook.Describe())
if hook.EventName == "pull_request" {
sha, err := pollForMergeCommitSha(globalConfig.gitHubToken, hook.Owner, hook.Repo, hook.PrNumber)
if err != nil {
return grimErrorf("error getting merge commit sha: %v", err)
} else if sha == "" {
return grimErrorf("error getting merge commit sha: field empty")
}
hook.Ref = sha
}
localConfig, err := getEffectiveConfig(configRoot, hook.Owner, hook.Repo)
if err != nil {
return grimErrorf("error while reading config: %v", err)
}
if localConfig.usernameCanBuild(hook.UserName) {
return buildForHook(configRoot, localConfig, *hook, logger)
}
return grimErrorf("username %q is not permitted to build", hook.UserName)
}
return nil
}
// BuildRef builds a git ref immediately.
func (i *Instance) BuildRef(owner, repo, ref string, logger *log.Logger) error {
configRoot := getEffectiveConfigRoot(i.configRoot)
config, err := getEffectiveConfig(configRoot, owner, repo)
if err != nil {
return fatalGrimErrorf("error while reading config: %v", err)
}
return buildForHook(configRoot, config, hookEvent{
Owner: owner,
Repo: repo,
Ref: ref,
}, logger)
}
func buildOnHook(configRoot string, resultPath string, config *effectiveConfig, hook hookEvent, basename string) (*executeResult, string, error) {
return build(config.gitHubToken, configRoot, config.workspaceRoot, resultPath, config.pathToCloneIn, hook.Owner, hook.Repo, hook.Ref, hook.env(), basename, config.BuildTimeout())
}
func buildForHook(configRoot string, config *effectiveConfig, hook hookEvent, logger *log.Logger) error {
return onHookBuild(configRoot, config, hook, logger, buildOnHook)
}
type hookAction func(string, string, *effectiveConfig, hookEvent, string) (*executeResult, string, error)
func writeHookEvent(resultPath string, hook hookEvent) error {
hookFile := filepath.Join(resultPath, "hook.json")
hookBytes, marshalErr := json.Marshal(&hook)
if marshalErr != nil {
return marshalErr
}
ioutil.WriteFile(hookFile, hookBytes, 0644)
return nil
}
func onHookBuild(configRoot string, config *effectiveConfig, hook hookEvent, logger *log.Logger, action hookAction) error {
basename := getTimeStamp()
resultPath, err := makeTree(config.resultRoot, hook.Owner, hook.Repo, basename)
if err != nil {
return fatalGrimErrorf("error creating result path: %v", err)
}
// TODO: do something with this err
writeHookEvent(resultPath, hook)
notify(config, hook, "", resultPath, GrimPending, logger)
result, ws, err := action(configRoot, resultPath, config, hook, basename)
if err != nil {
notify(config, hook, ws, resultPath, GrimError, logger)
return fatalGrimErrorf("error during %v: %v", hook.Describe(), err)
}
gn := GrimFailure
if result.ExitCode == 0 {
gn = GrimSuccess
}
return notify(config, hook, ws, resultPath, gn, logger)
}
func (i *Instance) checkGrimQueue() error {
if i.queue == nil {
return fatalGrimErrorf("the Grim queue must be prepared first")
}
return nil
}
const truncatedMessage = `%q shouldn't be over 15 characters and has been truncated
Please update your config.json file to have a shorter %q.
Or to use the server defaults, remove the entry %q`
func buildTruncatedMessage(truncateID string) string {
return fmt.Sprintf(truncatedMessage, truncateID, truncateID, truncateID)
}
func shouldSkip(hook *hookEvent) *string {
var message *string
switch {
case hook.Deleted:
message = getStringPtr("because it was on a deleted branch")
case hook.EventName == "push":
case hook.EventName == "pull_request":
switch {
case hook.Action == "opened":
case hook.Action == "reopened":
case hook.Action == "synchronize":
default:
message = getStringPtr(fmt.Sprintf("because the action: %q was not 'opened', 'reopened', or 'synchronize'", hook.Action))
}
default:
message = getStringPtr(fmt.Sprintf("because the eventName: %q was not 'push' or 'pull_request'", hook.EventName))
}
return message
}
func getStringPtr(s string) *string { return &s }