-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
467 lines (385 loc) · 13.3 KB
/
main.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
package main
import (
"encoding/json"
"fmt"
slackbot "github.com/wolfbeacon/go-slackbot"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/codebuild"
"github.com/aws/aws-sdk-go/service/elasticbeanstalk"
"github.com/nlopes/slack"
"golang.org/x/net/context"
"github.com/robfig/cron"
"os"
"github.com/satori/go.uuid"
"strings"
)
// Configuration struct, this will be read from json config file
type Configuration struct {
SlackKey string `json:"slack-key"`
AWSRegion string `json:"aws-region"`
AnnounceChannel string `json:"announce-channel"`
}
// User struct, will be read from users.json file
type User struct {
Email string `json:"email"`
Permissions []string `json:"permissions"`
}
type BuildStatuses map[string]string
type EnvStatuses map[string]string
func (u *User) Can(permission string) bool {
for _, p := range u.Permissions {
if permission == p {
return true
}
}
return false
}
// Will return User with no permimssion if not found
func FindUser(email string) User {
for _, u := range users {
if u.Email == email {
return u
}
}
return User{email, make([]string, 0)}
}
var api *slack.Client
var users []User
var config Configuration
var buildStatuses map[string]string
var envStatuses map[string]string
var bot *slackbot.Bot
var codeBuildBuilds []*codebuild.Build
var elasticBeanstalkEnviroments []*elasticbeanstalk.EnvironmentDescription
var firstBuildStatusLoad bool
var firstEnviromentStatusLoad bool
func main() {
codeBuildBuilds = make([]*codebuild.Build, 0)
elasticBeanstalkEnviroments = make([]*elasticbeanstalk.EnvironmentDescription, 0)
firstBuildStatusLoad = true
firstEnviromentStatusLoad = true
// Read configurations
file, _ := os.Open("./config/settings.json")
decoder := json.NewDecoder(file)
config = Configuration{}
err := decoder.Decode(&config)
if err != nil {
fmt.Println("Error reading config file:", err)
}
fmt.Println("Slack Key:", config.SlackKey)
file.Close()
// Read user configurations
file, _ = os.Open("./config/users.json")
decoder = json.NewDecoder(file)
users = make([]User, 0)
err = decoder.Decode(&users)
if err != nil {
fmt.Println("Error reading users config file:", err)
}
fmt.Println(len(users), "user(s) read.")
file.Close()
api = slack.New(config.SlackKey)
bot = slackbot.New(config.SlackKey)
toMe := bot.Messages(slackbot.Mention, slackbot.DirectMention, slackbot.DirectMessage).Subrouter()
buildStatuses = make(map[string]string)
envStatuses = make(map[string]string)
// Configure cron jobs
c := cron.New()
c.AddFunc("*/10 * * * * *", CheckBuildStatusCronJob)
c.AddFunc("*/10 * * * * *", CheckEnviromentsCronJob)
c.Start()
ReloadCodeBuildBuilds()
ReloadElasticBeanstalkEnviroments()
toMe.Hear("(?i)(hi|hello).*").MessageHandler(HelloHandler)
toMe.Hear("^help$").MessageHandler(HelpHandler)
toMe.Hear("list builds").MessageHandler(ListCodeBuildBuildsHandler)
toMe.Hear("run build .*").MessageHandler(StartCodeBuildBuildHandler)
toMe.Hear("list envs").MessageHandler(ListElasticBeanstalkEnviromentsHandler)
toMe.Hear("rebuild env .*").MessageHandler(RebuildElasticBeanstalkEnviromentHandler)
toMe.Hear("list projects").MessageHandler(ListCodeBuildProjectsHandler)
bot.Run()
}
func HelloHandler(ctx context.Context, bot *slackbot.Bot, evt *slack.MessageEvent) {
bot.Reply(evt, "Hello! If you need help, type `help`.", slackbot.WithTyping)
}
func HelpHandler(ctx context.Context, bot *slackbot.Bot, evt *slack.MessageEvent) {
msg := `*Here are all the commands you can use:*
help - Display help message
list {playbooks | tasks} - List playbook or tasks currently running
run {playbook} <playbook-name> - Run playbook
me - Show detailed status about yourself
`
bot.Reply(evt, msg, slackbot.WithTyping)
}
// list projects handler
func ListCodeBuildProjectsHandler(ctx context.Context, bot *slackbot.Bot, evt *slack.MessageEvent){
sess, err := session.NewSession(&aws.Config{
Region: aws.String(config.AWSRegion)},
)
svc := codebuild.New(sess)
projects, err := svc.ListProjects(&codebuild.ListProjectsInput{SortOrder: aws.String("ASCENDING")})
if err != nil {
bot.Reply(evt, "Failed to list build projects.", slackbot.WithTyping)
}
var result string
for _, project := range projects.Projects {
result += *project + "\n"
}
attachment := slack.Attachment{
Color: "#1565C0",
Fallback: result,
Text: result,
Footer: "Data from AWS CodeBuild",
}
attachments := make([]slack.Attachment, 0)
attachments = append(attachments, attachment)
bot.ReplyWithAttachments(evt, attachments, slackbot.WithTyping)
}
// list builds handler, will only list last 5 builds
func ListCodeBuildBuildsHandler(ctx context.Context, bot *slackbot.Bot, evt *slack.MessageEvent) {
attachments := make([]slack.Attachment, 0)
for _, build := range codeBuildBuilds[(len(codeBuildBuilds)-5):] {
attachments = append(attachments, ConstructCodeBuildBuildAttachment(build))
}
bot.ReplyWithAttachments(evt, attachments, slackbot.WithTyping)
}
func StartCodeBuildBuildHandler(ctx context.Context, bot *slackbot.Bot, evt *slack.MessageEvent) {
// Get user information
u, _ := api.GetUserInfo(evt.User)
user := FindUser(u.Profile.Email)
// If user have permission to start a build
if user.Can("start-build") {
evt.Text = slackbot.StripDirectMention(evt.Text)
args := strings.Split(evt.Text, " ")
if len(args) > 2 {
sess, err := session.NewSession(&aws.Config{
Region: aws.String(config.AWSRegion)},
)
svc := codebuild.New(sess)
_, err = svc.StartBuild(&codebuild.StartBuildInput{ProjectName: aws.String(args[2])})
if err != nil {
bot.Reply(evt, "Failed to start build ..." + err.Error() + "<@" + evt.User + ">", slackbot.WithTyping)
} else {
bot.Reply(evt, "Project build started ... <@" + evt.User + ">" , slackbot.WithTyping)
}
} else {
bot.Reply(evt, "You must specify a project. <@" + evt.User + ">", slackbot.WithTyping)
}
} else {
bot.Reply(evt, "You do not have permission to start a new build. <@" + evt.User + ">", slackbot.WithTyping)
}
}
func ListElasticBeanstalkEnviromentsHandler(ctx context.Context, bot *slackbot.Bot, evt *slack.MessageEvent) {
attachments := make([]slack.Attachment, 0)
for _, env := range elasticBeanstalkEnviroments {
attachments = append(attachments, ConstructElasticBeanstalkEnviromentAttachment(env, false))
}
bot.ReplyWithAttachments(evt, attachments, slackbot.WithTyping)
}
func RebuildElasticBeanstalkEnviromentHandler(ctx context.Context, bot *slackbot.Bot, evt *slack.MessageEvent) {
// Get user information
u, _ := api.GetUserInfo(evt.User)
user := FindUser(u.Profile.Email)
// If user have permission to start a build
if user.Can("rebuild-env") {
evt.Text = slackbot.StripDirectMention(evt.Text)
args := strings.Split(evt.Text, " ")
if len(args) > 3 {
sess, err := session.NewSession(&aws.Config{
Region: aws.String(config.AWSRegion)},
)
v := uuid.NewV4().String()
svc := elasticbeanstalk.New(sess)
input := &elasticbeanstalk.CreateApplicationVersionInput{
ApplicationName: aws.String(args[2]),
AutoCreateApplication: aws.Bool(true),
Process: aws.Bool(true),
SourceBundle: &elasticbeanstalk.S3Location{
S3Bucket: aws.String("wolfbeacon-dockerrun-aws"),
S3Key: aws.String(args[2] + "/Dockerrun.aws.json"),
},
VersionLabel: aws.String(v),
}
_, err = svc.CreateApplicationVersion(input)
if err != nil {
bot.Reply(evt, "Failed to initialize a rebuild", slackbot.WithTyping)
} else {
input := &elasticbeanstalk.UpdateEnvironmentInput{
EnvironmentName: aws.String(args[3]),
VersionLabel: aws.String(v),
}
_, err = svc.UpdateEnvironment(input)
if err != nil {
bot.Reply(evt, "Failed to initialize a rebuild " + err.Error() + "<@" + evt.User + ">", slackbot.WithTyping)
} else {
bot.Reply(evt, "Now rebuilding enviroment... this can take a few minutes to complete. <@" + evt.User + ">", slackbot.WithTyping)
}
}
} else {
bot.Reply(evt, "You must specify a enviroment name. <@" + evt.User + ">", slackbot.WithTyping)
}
} else {
bot.Reply(evt, "You do not have permission to rebuild an enviroment. <@" + evt.User + ">", slackbot.WithTyping)
}
}
func PostToChannelWithAttachments(text string, attachments []slack.Attachment){
params := slack.PostMessageParameters{AsUser: true}
params.Attachments = attachments
bot.Client.PostMessage(config.AnnounceChannel, text, params)
}
func ReloadCodeBuildBuilds() bool {
sess, err := session.NewSession(&aws.Config{
Region: aws.String(config.AWSRegion)},
)
svc := codebuild.New(sess)
names, err := svc.ListBuilds(&codebuild.ListBuildsInput{SortOrder: aws.String("ASCENDING")})
if err != nil {
return false
}
builds, err := svc.BatchGetBuilds(&codebuild.BatchGetBuildsInput{Ids: names.Ids})
if err != nil {
return false
}
codeBuildBuilds = builds.Builds
return true
}
func ReloadElasticBeanstalkEnviroments() bool {
sess, err := session.NewSession(&aws.Config{
Region: aws.String(config.AWSRegion)},
)
ebSvc := elasticbeanstalk.New(sess)
environments, err := ebSvc.DescribeEnvironments(&elasticbeanstalk.DescribeEnvironmentsInput{})
if err != nil {
return false
}
elasticBeanstalkEnviroments = environments.Environments
return true
}
func ConstructCodeBuildBuildAttachment(build *codebuild.Build) slack.Attachment {
var c string
var statusText string
switch aws.StringValue(build.BuildStatus) {
case "SUCCEEDED":
c = "#7CD197"
statusText = "Succeeded ✓"
case "FAILED":
c = "#C62828"
statusText = "Failed ×"
case "FAULT":
c = "#C62828"
statusText = "Fault ×"
case "TIMED_OUT":
c = "#C62828"
statusText = "Timed Out ×"
default:
c = "#F9A825"
statusText = "In Progress"
}
result := slack.Attachment{
Color: c,
Fallback: aws.StringValue(build.ProjectName),
Text: aws.StringValue(build.ProjectName) + " | " + statusText,
Footer: "Started: " + build.StartTime.String() + " | ID: " + aws.StringValue(build.Id),
}
return result
}
func ConstructElasticBeanstalkEnviromentAttachment(env *elasticbeanstalk.EnvironmentDescription, showDetail bool) slack.Attachment {
var c string
var statusText string
switch aws.StringValue(env.Health) {
case "Green":
c = "#7CD197"
statusText = "Functional ✓"
case "Yellow":
c = "#F9A825"
statusText = "Warning ×"
case "Red":
c = "#C62828"
statusText = "Fault ×"
default:
c = "#9E9E9E"
statusText = "Unknown"
}
if aws.StringValue(env.Status) == "Updating" {
c = "#0288D1"
statusText += " Updaing ..."
}
if aws.StringValue(env.Status) == "Launching" {
c = "#0288D1"
statusText += " Launching ..."
}
result := slack.Attachment{
Color: c,
Fallback: aws.StringValue(env.EnvironmentName),
Text: aws.StringValue(env.ApplicationName) + " " + aws.StringValue(env.EnvironmentName) + " | " + statusText,
}
if showDetail {
result.Text += "\n" + "Enviroment Status: " + aws.StringValue(env.Status) + "\n"
result.Text += "Version: " + aws.StringValue(env.VersionLabel)
result.Footer = "Last update: " + env.DateUpdated.String()
}
return result
}
// Cron job to check all CodeBuild jobs.
func CheckBuildStatusCronJob() {
// Take a copy of old builds
oldBuilds := codeBuildBuilds
// Reload jobs
if !ReloadCodeBuildBuilds() {
fmt.Println("Failed to reload codebuild build jobs")
}
oldBuildFound := false
for _, newBuild := range codeBuildBuilds {
for _, oldBuild := range oldBuilds {
// We are comparing two same builds
if aws.StringValue(oldBuild.Id) == aws.StringValue(newBuild.Id) {
oldBuildFound = true
// If build status has changed
if aws.StringValue(oldBuild.BuildStatus) != aws.StringValue(newBuild.BuildStatus) {
attachments := make([]slack.Attachment, 0)
attachments = append(attachments, ConstructCodeBuildBuildAttachment(newBuild))
PostToChannelWithAttachments("Build status updated", attachments)
}
}
}
if !oldBuildFound {
// We have detected a new build job
attachments := make([]slack.Attachment, 0)
attachments = append(attachments, ConstructCodeBuildBuildAttachment(newBuild))
PostToChannelWithAttachments("New build job started ...", attachments)
}
oldBuildFound = false
}
}
// Cron job to check elasticbeanstalk enviroments.
func CheckEnviromentsCronJob(){
// Take a copy of old builds
oldEnviroments := elasticBeanstalkEnviroments
// We first reload the env
if !ReloadElasticBeanstalkEnviroments() {
fmt.Println("Failed to load enviroments")
}
oldEnviromentFound := false
for _, newEnv := range elasticBeanstalkEnviroments {
for _, oldEnv := range oldEnviroments {
// We are comparing two same envs
if aws.StringValue(oldEnv.EnvironmentName) == aws.StringValue(newEnv.EnvironmentName) {
oldEnviromentFound = true
// If env status has changed
if aws.StringValue(oldEnv.Status) != aws.StringValue(newEnv.Status) {
attachments := make([]slack.Attachment, 0)
attachments = append(attachments, ConstructElasticBeanstalkEnviromentAttachment(newEnv, true))
PostToChannelWithAttachments("Enviroment status updated", attachments)
}
}
}
if !oldEnviromentFound {
// We have detected a new env
attachments := make([]slack.Attachment, 0)
attachments = append(attachments, ConstructElasticBeanstalkEnviromentAttachment(newEnv, true))
PostToChannelWithAttachments("New enviroment created ...", attachments)
}
oldEnviromentFound = false
}
}