forked from viliproject/vili
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slackbot.go
237 lines (221 loc) · 5.38 KB
/
slackbot.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
package vili
import (
"fmt"
"regexp"
"strings"
"time"
"unicode"
"github.com/airware/vili/api"
"github.com/airware/vili/docker"
"github.com/airware/vili/environments"
"github.com/airware/vili/log"
"github.com/airware/vili/redis"
"github.com/airware/vili/slack"
"github.com/airware/vili/util"
)
var publishedRegexp = regexp.MustCompile(
`(?:[Pp]ublished) ([\w, ]+) with tag ([\w-]+) from branch ([\w/-]+)`,
)
// runDeployBot runs the deploy bot that listens to messages in the slack channel
func runDeployBot() {
mentionsRegexp, err := slack.MentionsRegexp()
if err != nil {
log.Error(err)
return
}
mentions := make(chan *slack.Message)
publishes := make(chan *slack.Message)
go slack.ListenForMessages(map[*regexp.Regexp]chan<- *slack.Message{
mentionsRegexp: mentions,
publishedRegexp: publishes,
})
for {
select {
case mention, ok := <-mentions:
if !ok {
mentions = nil
} else {
locked, err := redis.GetClient().SetNX(
fmt.Sprintf("deploybotlock:%s", mention.Timestamp),
true,
1*time.Hour,
).Result()
if err != nil {
log.Error(err)
continue
}
if !locked {
continue
}
err = handleCommand(strings.Fields(mention.Matches[0]), mention.Username)
if err != nil {
log.Error(err)
}
}
case publish, ok := <-publishes:
if !ok {
publishes = nil
} else {
locked, err := redis.GetClient().SetNX(
fmt.Sprintf("deploybotlock:%s", publish.Timestamp),
true,
1*time.Hour,
).Result()
if err != nil {
log.Error(err)
continue
}
if !locked {
continue
}
images := strings.FieldsFunc(publish.Matches[0], func(c rune) bool {
return !unicode.IsLetter(c) && !unicode.IsDigit(c)
})
tag := publish.Matches[1]
branch := publish.Matches[2]
err = handlePublish(images, tag, branch, publish.Username)
if err != nil {
log.Error(err)
}
}
}
if mentions == nil && publishes == nil {
break
}
}
}
func handleCommand(command []string, username string) error {
if len(command) == 0 {
log.Debug("Skipping empty command")
return nil
}
switch command[0] {
case "deploy":
if len(command) < 4 || len(command) > 5 {
log.Debugf("Skipping invalid command %s", command)
return nil
}
app := command[1]
branch := command[2]
tag := command[3]
var env string
if len(command) == 5 {
env = command[4]
if _, err := environments.Get(env); err != nil {
log.Debugf("Invalid environment %s", env)
return nil
}
} else {
for _, e := range environments.Environments() {
if e.Branch == branch && util.NewStringSet(e.Apps).Contains(app) {
env = e.Name
break
}
}
if env == "" {
log.Debugf("No environment found for branch %s with app %s", branch, app)
return nil
}
}
deployApp(env, app, tag, branch, username)
case "run":
if len(command) < 4 || len(command) > 5 {
log.Debugf("Skipping invalid command %s", command)
return nil
}
job := command[1]
branch := command[2]
tag := command[3]
var env string
if len(command) == 5 {
env = command[4]
if _, err := environments.Get(env); err != nil {
log.Debugf("Invalid environment %s", env)
return nil
}
} else {
for _, e := range environments.Environments() {
if e.Branch == branch && util.NewStringSet(e.Jobs).Contains(job) {
env = e.Name
break
}
}
if env == "" {
log.Debugf("No environment found for branch %s with job %s", branch, job)
return nil
}
}
runJob(env, job, tag, branch, username)
default:
// TODO print usage?
log.Debugf("Ignoring unknown command", command[0])
}
return nil
}
func handlePublish(images []string, tag, branch, username string) error {
if len(images) == 0 {
log.Debug("Skipping empty publish")
return nil
}
for _, image := range images {
var isApp, isJob bool
var env string
for _, e := range environments.Environments() {
if e.Branch == branch {
if util.NewStringSet(e.Apps).Contains(image) {
isApp = true
env = e.Name
break
} else if util.NewStringSet(e.Jobs).Contains(image) {
isJob = true
env = e.Name
break
}
}
}
if isApp {
deployApp(env, image, tag, branch, username)
} else if isJob {
runJob(env, image, tag, branch, username)
} else {
log.Debugf("No app or job found for branch %s with name %s", branch, image)
}
}
return nil
}
func deployApp(env, app, tag, branch, username string) {
log.Debugf("Deploying app %s, tag %s to env %s, requested by %s", app, tag, env, username)
deployment := api.Deployment{
Branch: branch,
Tag: tag,
}
err := deployment.Init(env, app, username, true)
if err != nil {
switch e := err.(type) {
case api.DeploymentInitError:
slack.PostLogMessage(e.Error(), "error")
case *docker.NotFoundError:
slack.PostLogMessage(fmt.Sprintf("App *%s* with tag *%s* not found", app, tag), "error")
default:
log.Error(e)
}
}
}
func runJob(env, job, tag, branch, username string) {
log.Debugf("Running job %s, tag %s in env %s, requested by %s", job, tag, env, username)
run := api.Run{
Branch: branch,
Tag: tag,
}
err := run.Init(env, job, username, true)
if err != nil {
switch e := err.(type) {
case api.RunInitError:
slack.PostLogMessage(e.Error(), "error")
case *docker.NotFoundError:
slack.PostLogMessage(fmt.Sprintf("Job *%s* with tag *%s* not found", job, tag), "error")
default:
log.Error(e)
}
}
}