-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathwinbot.go
More file actions
427 lines (373 loc) · 12.3 KB
/
winbot.go
File metadata and controls
427 lines (373 loc) · 12.3 KB
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
// Copyright 2017 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package main
import (
"bufio"
"bytes"
"fmt"
"os"
"os/exec"
"path"
"strings"
"sync"
"time"
"github.com/keybase/slackbot"
"github.com/keybase/slackbot/cli"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)
type winbot struct {
testAuto chan struct{}
stopAuto chan struct{}
}
const numLogLines = 10
// Keep track of the current build process, protected with a mutex,
// to support cancellation
var buildProcessMutex sync.Mutex
var buildProcess *os.Process
func (d *winbot) Run(bot *slackbot.Bot, channel string, args []string) (string, error) {
app := kingpin.New("winbot", "Job command parser for winbot")
app.Terminate(nil)
stringBuffer := new(bytes.Buffer)
app.Writer(stringBuffer)
buildWindows := app.Command("build", "Start a windows build")
buildWindowsTest := buildWindows.Flag("test", "Test build, skips admin/test channel").Bool()
buildWindowsCientCommit := buildWindows.Flag("client-commit", "Build a specific client commit").String()
buildWindowsKbfsCommit := buildWindows.Flag("kbfs-commit", "Build a specific kbfs commit").String()
buildWindowsUpdaterCommit := buildWindows.Flag("updater-commit", "Build a specific updater commit").String()
buildWindowsSmoke := buildWindows.Flag("smoke", "Build a smoke pair").Bool()
buildWindowsDevCert := buildWindows.Flag("dev-cert", "Build using devel code signing cert").Bool()
buildWindowsAuto := buildWindows.Flag("automated", "Specify build was triggered automatically").Hidden().Bool()
cancel := app.Command("cancel", "Cancel current")
dumplogCmd := app.Command("dumplog", "Show the last log file")
gitDiffCmd := app.Command("gdiff", "Show the git diff")
gitDiffRepo := gitDiffCmd.Arg("repo", "Repo path relative to $GOPATH/src").Required().String()
gitCleanCmd := app.Command("gclean", "Clean the repo")
gitCleanRepo := gitCleanCmd.Arg("repo", "Repo path relative to $GOPATH/src").Required().String()
logFileName := path.Join(os.TempDir(), "keybase.build.windows.log")
testAutoBuild := app.Command("testauto", "Simulate an automated daily build").Hidden()
startAutoTimer := app.Command("startAutoTimer", "Start the auto build timer")
startAutoTimerInterval := startAutoTimer.Flag("interval", "Number of hours between auto builds, 0 to stop").Default("24").Int()
startAutoTimerStartHour := startAutoTimer.Flag("startHour", "Number of hours after midnight to build, local time").Default("7").Int()
startAutoTimerDelay := startAutoTimer.Flag("delay", "Number of hours to wait before starting auto timer").Default("0").Int()
restartCmd := app.Command("restart", "Quit and let calling script invoke bot again")
cmd, usage, cmdErr := cli.Parse(app, args, stringBuffer)
if usage != "" || cmdErr != nil {
return usage, cmdErr
}
// do these regardless of dry run status
if cmd == testAutoBuild.FullCommand() {
d.testAuto <- struct{}{}
return "Sent test signal", nil
}
if cmd == startAutoTimer.FullCommand() {
if d.stopAuto != nil {
d.stopAuto <- struct{}{}
}
if *startAutoTimerInterval > 0 {
go d.winAutoBuild(bot, channel, *startAutoTimerInterval, *startAutoTimerDelay, *startAutoTimerStartHour)
}
return "", nil
}
if bot.Config().DryRun() {
return fmt.Sprintf("I would have run: `%#v`", cmd), nil
}
switch cmd {
case cancel.FullCommand():
buildProcessMutex.Lock()
defer buildProcessMutex.Unlock()
if buildProcess == nil {
return "No build running", nil
}
if err := buildProcess.Kill(); err != nil {
return "failed to cancel build", err
}
case buildWindows.FullCommand():
smokeTest := *buildWindowsSmoke
skipTestChannel := *buildWindowsTest
devCert := 0
if *buildWindowsDevCert {
devCert = 1
}
var autoBuild string
if bot.Config().DryRun() {
return "I would have done a build", nil
}
if bot.Config().Paused() {
return "I'm paused so I can't do that, but I would have done a build", nil
}
if *buildWindowsAuto {
autoBuild = "Automatic Build: "
}
// Test channel tells the scripts this is an admin build
updateChannel := "Test"
if skipTestChannel {
if smokeTest {
return "Test and Smoke are exclusive options", nil
}
updateChannel = "None"
} else if smokeTest {
updateChannel = "Smoke"
}
msg := fmt.Sprintf(autoBuild+"I'm starting the job `windows build`. To cancel run `!%s cancel`. ", bot.Name())
msg = fmt.Sprintf(msg+"updateChannel is %s, smokeTest is %v, devCert is %v, logFileName %s",
updateChannel, smokeTest, devCert, logFileName)
bot.SendMessage(msg, channel)
os.Remove(logFileName)
logf, err := os.OpenFile(logFileName, os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
return "Unable to open logfile", err
}
gitCmd := exec.Command(
"git.exe",
"checkout",
"master",
)
gitCmd.Dir = os.ExpandEnv("$GOPATH/src/github.com/keybase/client")
stdoutStderr, err := gitCmd.CombinedOutput()
_, _ = logf.Write(stdoutStderr)
if err != nil {
_, _ = logf.WriteString(gitCmd.Dir)
logf.Close()
return string(stdoutStderr), err
}
gitCmd = exec.Command(
"git.exe",
"pull",
)
gitCmd.Dir = os.ExpandEnv("$GOPATH/src/github.com/keybase/client")
stdoutStderr, err = gitCmd.CombinedOutput()
_, _ = logf.Write(stdoutStderr)
if err != nil {
_, _ = logf.WriteString(gitCmd.Dir)
logf.Close()
return string(stdoutStderr), err
}
if buildWindowsCientCommit != nil && *buildWindowsCientCommit != "" && *buildWindowsCientCommit != "master" {
msg := fmt.Sprintf(autoBuild+"I'm trying to use commit %s", *buildWindowsCientCommit)
bot.SendMessage(msg, channel)
gitCmd = exec.Command(
"git.exe",
"checkout",
*buildWindowsCientCommit,
)
gitCmd.Dir = os.ExpandEnv("$GOPATH/src/github.com/keybase/client")
stdoutStderr, err = gitCmd.CombinedOutput()
_, _ = logf.Write(stdoutStderr)
if err != nil {
_, _ = logf.WriteString(fmt.Sprintf("error doing git pull in %s\n", gitCmd.Dir))
logf.Close()
return string(stdoutStderr), err
}
// Test if we're on a branch. If so, do git pull once more.
gitCmd = exec.Command(
"git.exe",
"rev-parse",
"--abbrev-ref",
"HEAD",
)
gitCmd.Dir = os.ExpandEnv("$GOPATH/src/github.com/keybase/client")
stdoutStderr, err = gitCmd.CombinedOutput()
if err != nil {
_, _ = logf.WriteString(fmt.Sprintf("error going git rev-parse dir: %s\n", gitCmd.Dir))
logf.Close()
return string(stdoutStderr), err
}
commit := strings.TrimSpace(string(stdoutStderr[:]))
if commit != "HEAD" {
gitCmd = exec.Command(
"git.exe",
"pull",
)
gitCmd.Dir = os.ExpandEnv("$GOPATH/src/github.com/keybase/client")
stdoutStderr, err = gitCmd.CombinedOutput()
_, _ = logf.Write(stdoutStderr)
if err != nil {
_, _ = logf.WriteString(fmt.Sprintf("error doing git pull on %s in %s\n", commit, gitCmd.Dir))
logf.Close()
return string(stdoutStderr), err
}
}
}
gitCmd = exec.Command(
"git.exe",
"rev-parse",
"HEAD",
)
gitCmd.Dir = os.ExpandEnv("$GOPATH/src/github.com/keybase/client")
stdoutStderr, err = gitCmd.CombinedOutput()
if err != nil {
_, _ = logf.WriteString(fmt.Sprintf("error getting current commit for logs: %s", gitCmd.Dir))
logf.Close()
return string(stdoutStderr), err
}
_, _ = logf.WriteString(fmt.Sprintf("HEAD is currently at %s\n", string(stdoutStderr)))
cmd := exec.Command(
"cmd", "/c",
path.Join(os.Getenv("GOPATH"), "src/github.com/keybase/client/packaging/windows/dorelease.cmd"),
">>",
logFileName,
"2>&1")
cmd.Env = append(os.Environ(),
"ClientRevision="+*buildWindowsCientCommit,
"KbfsRevision="+*buildWindowsKbfsCommit,
"UpdaterRevision="+*buildWindowsUpdaterCommit,
"UpdateChannel="+updateChannel,
fmt.Sprintf("DevCert=%d", devCert),
"SlackBot=1",
)
_, _ = logf.WriteString(fmt.Sprintf("cmd: %+v\n", cmd))
logf.Close()
go func() {
err := cmd.Start()
if err != nil {
bot.SendMessage(fmt.Sprintf("unable to start: %s", err), channel)
}
buildProcessMutex.Lock()
buildProcess = cmd.Process
buildProcessMutex.Unlock()
err = cmd.Wait()
bucketName := os.Getenv("BUCKET_NAME")
if bucketName == "" {
bucketName = "prerelease.keybase.io"
}
sendLogCmd := exec.Command(
path.Join(os.Getenv("GOPATH"), "src/github.com/keybase/release/release.exe"),
"save-log",
"--maxsize=5000000",
"--bucket-name="+bucketName,
"--path="+logFileName,
)
resultMsg := autoBuild + "Finished the job `windows build`"
if err != nil {
resultMsg = autoBuild + "Error in job `windows build`"
var lines [numLogLines]string
// Send a log snippet too
index := 0
lineCount := 0
f, err := os.Open(logFileName)
if err != nil {
bot.SendMessage(autoBuild+"Error reading "+logFileName+": "+err.Error(), channel)
}
scanner := bufio.NewScanner(f)
for scanner.Scan() {
lines[lineCount%numLogLines] = scanner.Text()
lineCount += 1
}
if err := scanner.Err(); err != nil {
bot.SendMessage(autoBuild+"Error scanning "+logFileName+": "+err.Error(), channel)
}
if lineCount > numLogLines {
index = lineCount % numLogLines
lineCount = numLogLines
}
snippet := "```\n"
for i := 0; i < lineCount; i++ {
snippet += lines[(i+index)%numLogLines] + "\n"
}
snippet += "```"
bot.SendMessage(snippet, channel)
}
urlBytes, err2 := sendLogCmd.Output()
if err2 != nil {
msg := fmt.Sprintf("%s, log upload error %s", resultMsg, err2.Error())
bot.SendMessage(msg, channel)
} else {
msg := fmt.Sprintf("%s, view log at %s", resultMsg, string(urlBytes[:]))
bot.SendMessage(msg, channel)
}
}()
return "", nil
case dumplogCmd.FullCommand():
logContents, err := os.ReadFile(logFileName)
if err != nil {
return "Error reading " + logFileName, err
}
index := 0
if len(logContents) > 1000 {
index = len(logContents) - 1000
}
bot.SendMessage(string(logContents[index:]), channel)
case gitDiffCmd.FullCommand():
rawRepoText := *gitDiffRepo
repoParsed := strings.Split(strings.Trim(rawRepoText, "`<>"), "|")[1]
gitDiffCmd := exec.Command(
"git.exe",
"diff",
)
gitDiffCmd.Dir = os.ExpandEnv(path.Join("$GOPATH/src", repoParsed))
if exists, err := Exists(path.Join(gitDiffCmd.Dir, ".git")); !exists {
return "Not a git repo", err
}
stdoutStderr, err := gitDiffCmd.CombinedOutput()
if err != nil {
return "Error", err
}
bot.SendMessage(string(stdoutStderr), channel)
case gitCleanCmd.FullCommand():
rawRepoText := *gitCleanRepo
repoParsed := strings.Split(strings.Trim(rawRepoText, "`<>"), "|")[1]
gitCleanCmd := exec.Command(
"git.exe",
"clean",
"-f",
)
gitCleanCmd.Dir = os.ExpandEnv(path.Join("$GOPATH/src", repoParsed))
if exists, err := Exists(path.Join(gitCleanCmd.Dir, ".git")); !exists {
return "Not a git repo", err
}
stdoutStderr, err := gitCleanCmd.CombinedOutput()
if err != nil {
return "Error", err
}
bot.SendMessage(string(stdoutStderr), channel)
case restartCmd.FullCommand():
os.Exit(0)
}
return cmd, nil
}
func (d *winbot) Help(bot *slackbot.Bot) string {
out, err := d.Run(bot, "", nil)
if err != nil {
return fmt.Sprintf("Error getting help: %s", err)
}
return out
}
func Exists(name string) (bool, error) {
_, err := os.Stat(name)
if os.IsNotExist(err) {
return false, nil
}
return err != nil, err
}
func (d *winbot) winAutoBuild(bot *slackbot.Bot, channel string, interval int, delay int, startHour int) {
d.testAuto = make(chan struct{})
d.stopAuto = make(chan struct{})
for {
hour := time.Now().Hour() + delay
if delay > 0 {
delay = 0
} else {
hour = ((interval - hour) + startHour)
}
next := time.Now().Add(time.Hour * time.Duration(hour))
for next.Weekday() == time.Saturday || next.Weekday() == time.Sunday {
hour += interval
next = time.Now().Add(time.Hour * time.Duration(hour))
}
msg := fmt.Sprintf("Next automatic build at %s", next.Format(time.RFC822))
bot.SendMessage(msg, channel)
args := []string{"build", "--automated"}
select {
case <-d.testAuto:
case <-time.After(time.Duration(hour) * time.Hour):
args = append(args, "--smoke")
case <-d.stopAuto:
return
}
message, err := d.Run(bot, channel, args)
if err != nil {
msg := fmt.Sprintf("AutoBuild ERROR -- %s: %s", message, err.Error())
bot.SendMessage(msg, channel)
}
}
}