2014-02-25 00:53:28 +00:00
|
|
|
package queue
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2014-06-04 21:44:05 +00:00
|
|
|
"github.com/drone/drone/server/channel"
|
|
|
|
"github.com/drone/drone/shared/build/git"
|
|
|
|
r "github.com/drone/drone/shared/build/repo"
|
2014-06-12 00:42:49 +00:00
|
|
|
"github.com/drone/drone/shared/build/script"
|
2014-02-25 00:53:28 +00:00
|
|
|
"io"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
2014-06-04 21:25:38 +00:00
|
|
|
|
2014-06-04 21:44:05 +00:00
|
|
|
"github.com/drone/drone/server/resource/commit"
|
|
|
|
"github.com/drone/drone/server/resource/repo"
|
2014-02-25 00:53:28 +00:00
|
|
|
)
|
|
|
|
|
2014-02-25 01:51:25 +00:00
|
|
|
type worker struct {
|
2014-06-04 21:25:38 +00:00
|
|
|
commits commit.CommitManager
|
|
|
|
|
2014-02-26 01:14:14 +00:00
|
|
|
runner BuildRunner
|
2014-02-25 01:51:25 +00:00
|
|
|
}
|
2014-02-25 00:53:28 +00:00
|
|
|
|
|
|
|
// work is a function that will infinitely
|
|
|
|
// run in the background waiting for tasks that
|
|
|
|
// it can pull off the queue and execute.
|
|
|
|
func (w *worker) work(queue <-chan *BuildTask) {
|
|
|
|
var task *BuildTask
|
|
|
|
for {
|
|
|
|
// get work item (pointer) from the queue
|
|
|
|
task = <-queue
|
|
|
|
if task == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// execute the task
|
|
|
|
w.execute(task)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// execute will execute the build task and persist
|
|
|
|
// the results to the datastore.
|
|
|
|
func (w *worker) execute(task *BuildTask) error {
|
|
|
|
// we need to be sure that we can recover
|
|
|
|
// from any sort panic that could occur
|
|
|
|
// to avoid brining down the entire application
|
|
|
|
defer func() {
|
|
|
|
if e := recover(); e != nil {
|
2014-06-04 21:25:38 +00:00
|
|
|
task.Commit.Finished = time.Now().Unix()
|
2014-06-07 20:46:05 +00:00
|
|
|
task.Commit.Duration = task.Commit.Finished - task.Commit.Started
|
|
|
|
task.Commit.Status = commit.StatusError
|
2014-06-04 21:25:38 +00:00
|
|
|
w.commits.Update(task.Commit)
|
2014-02-25 00:53:28 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2014-06-12 00:42:49 +00:00
|
|
|
// parse the build script
|
|
|
|
params, err := task.Repo.ParamMap()
|
|
|
|
task.Script, err = script.ParseBuild(task.Commit.Config, params)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-02-25 00:53:28 +00:00
|
|
|
// update commit and build status
|
2014-06-07 20:46:05 +00:00
|
|
|
task.Commit.Status = commit.StatusStarted
|
2014-06-04 21:25:38 +00:00
|
|
|
task.Commit.Started = time.Now().Unix()
|
2014-02-25 00:53:28 +00:00
|
|
|
|
|
|
|
// persist the commit to the database
|
2014-06-04 21:25:38 +00:00
|
|
|
if err := w.commits.Update(task.Commit); err != nil {
|
2014-02-25 00:53:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// get settings
|
2014-06-04 21:25:38 +00:00
|
|
|
//settings, _ := database.GetSettings()
|
2014-02-25 00:53:28 +00:00
|
|
|
|
|
|
|
// notification context
|
2014-06-04 21:25:38 +00:00
|
|
|
//context := ¬ify.Context{
|
|
|
|
// Repo: task.Repo,
|
|
|
|
// Commit: task.Commit,
|
|
|
|
// Host: settings.URL().String(),
|
|
|
|
//}
|
2014-05-23 03:16:01 +00:00
|
|
|
|
2014-02-25 00:53:28 +00:00
|
|
|
// send all "started" notifications
|
2014-06-04 21:25:38 +00:00
|
|
|
//if task.Script.Notifications != nil {
|
|
|
|
// task.Script.Notifications.Send(context)
|
|
|
|
//}
|
2014-02-25 00:53:28 +00:00
|
|
|
|
|
|
|
// Send "started" notification to Github
|
2014-06-04 21:25:38 +00:00
|
|
|
//if err := updateGitHubStatus(task.Repo, task.Commit); err != nil {
|
|
|
|
// log.Printf("error updating github status: %s\n", err.Error())
|
|
|
|
//}
|
2014-02-25 00:53:28 +00:00
|
|
|
|
|
|
|
// make sure a channel exists for the repository,
|
|
|
|
// the commit, and the commit output (TODO)
|
2014-06-07 20:46:05 +00:00
|
|
|
reposlug := fmt.Sprintf("%s/%s/%s", task.Repo.Host, task.Repo.Owner, task.Repo.Name)
|
2014-06-09 06:59:23 +00:00
|
|
|
//commitslug := fmt.Sprintf("%s/%s/%s/%s/%s", task.Repo.Host, task.Repo.Owner, task.Repo.Name, task.Commit.Branch, task.Commit.Sha)
|
|
|
|
consoleslug := fmt.Sprintf("%s/%s/%s/%s/%s", task.Repo.Host, task.Repo.Owner, task.Repo.Name, task.Commit.Branch, task.Commit.Sha)
|
2014-02-25 00:53:28 +00:00
|
|
|
channel.Create(reposlug)
|
2014-06-09 06:59:23 +00:00
|
|
|
//channel.Create(commitslug)
|
2014-02-25 00:53:28 +00:00
|
|
|
channel.CreateStream(consoleslug)
|
|
|
|
|
|
|
|
// notify the channels that the commit and build started
|
|
|
|
channel.SendJSON(reposlug, task.Commit)
|
2014-06-09 06:59:23 +00:00
|
|
|
//channel.SendJSON(commitslug, task.Commit)
|
2014-02-25 00:53:28 +00:00
|
|
|
|
|
|
|
var buf = &bufferWrapper{channel: consoleslug}
|
|
|
|
|
|
|
|
// append private parameters to the environment
|
2014-02-25 23:48:43 +00:00
|
|
|
// variable section of the .drone.yml file, iff
|
|
|
|
// this is not a pull request (for security purposes)
|
2014-06-04 21:25:38 +00:00
|
|
|
//if task.Repo.Params != nil && len(task.Commit.PullRequest) == 0 {
|
|
|
|
// for k, v := range task.Repo.Params {
|
|
|
|
// task.Script.Env = append(task.Script.Env, k+"="+v)
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
|
|
|
|
//defer func() {
|
|
|
|
// // update the status of the commit using the
|
|
|
|
// // GitHub status API.
|
|
|
|
// if err := updateGitHubStatus(task.Repo, task.Commit); err != nil {
|
|
|
|
// log.Printf("error updating github status: %s\n", err.Error())
|
|
|
|
// }
|
|
|
|
//}()
|
2014-02-25 00:53:28 +00:00
|
|
|
|
|
|
|
// execute the build
|
2014-06-04 21:25:38 +00:00
|
|
|
passed, buildErr := w.runBuild(task, buf)
|
2014-02-25 00:53:28 +00:00
|
|
|
|
2014-06-04 21:25:38 +00:00
|
|
|
task.Commit.Finished = time.Now().Unix()
|
2014-06-07 20:46:05 +00:00
|
|
|
task.Commit.Duration = task.Commit.Finished - task.Commit.Started
|
|
|
|
task.Commit.Status = commit.StatusSuccess
|
2014-06-04 21:25:38 +00:00
|
|
|
|
|
|
|
// capture build output
|
|
|
|
stdout := buf.buf.String()
|
2014-02-25 00:53:28 +00:00
|
|
|
|
|
|
|
// if exit code != 0 set to failure
|
|
|
|
if passed {
|
2014-06-07 20:46:05 +00:00
|
|
|
task.Commit.Status = commit.StatusFailure
|
2014-06-04 21:25:38 +00:00
|
|
|
if buildErr != nil && len(stdout) == 0 {
|
2014-02-25 00:53:28 +00:00
|
|
|
// TODO: If you wanted to have very friendly error messages, you could do that here
|
2014-06-04 21:25:38 +00:00
|
|
|
stdout = fmt.Sprintf("%s\n", buildErr.Error())
|
2014-02-25 00:53:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-04 21:25:38 +00:00
|
|
|
// persist the build output
|
2014-06-07 20:46:05 +00:00
|
|
|
if err := w.commits.UpdateOutput(task.Commit, []byte(stdout)); err != nil {
|
2014-06-04 21:25:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-02-25 00:53:28 +00:00
|
|
|
// persist the commit to the database
|
2014-06-04 21:25:38 +00:00
|
|
|
if err := w.commits.Update(task.Commit); err != nil {
|
2014-02-25 00:53:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// notify the channels that the commit and build finished
|
|
|
|
channel.SendJSON(reposlug, task.Commit)
|
2014-06-09 06:59:23 +00:00
|
|
|
//channel.SendJSON(commitslug, task.Commit)
|
2014-02-25 00:53:28 +00:00
|
|
|
channel.Close(consoleslug)
|
|
|
|
|
|
|
|
// send all "finished" notifications
|
2014-06-04 21:25:38 +00:00
|
|
|
//if task.Script.Notifications != nil {
|
|
|
|
// task.Script.Notifications.Send(context)
|
|
|
|
//}
|
2014-02-25 00:53:28 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-06-04 21:25:38 +00:00
|
|
|
func (w *worker) runBuild(task *BuildTask, buf io.Writer) (bool, error) {
|
2014-02-25 01:32:40 +00:00
|
|
|
repo := &r.Repo{
|
2014-06-07 20:46:05 +00:00
|
|
|
Name: task.Repo.Host + task.Repo.Owner + task.Repo.Name,
|
2014-02-25 01:51:25 +00:00
|
|
|
Path: task.Repo.URL,
|
|
|
|
Branch: task.Commit.Branch,
|
2014-06-04 21:25:38 +00:00
|
|
|
Commit: task.Commit.Sha,
|
2014-02-25 01:51:25 +00:00
|
|
|
PR: task.Commit.PullRequest,
|
2014-06-04 21:25:38 +00:00
|
|
|
//TODO the builder should handle this
|
2014-06-07 20:46:05 +00:00
|
|
|
Dir: filepath.Join("/var/cache/drone/src", task.Repo.Host, task.Repo.Owner, task.Repo.Name),
|
2014-06-04 21:25:38 +00:00
|
|
|
Depth: git.GitDepth(task.Script.Git),
|
2014-02-25 01:32:40 +00:00
|
|
|
}
|
2014-02-25 00:53:28 +00:00
|
|
|
|
2014-02-25 01:51:25 +00:00
|
|
|
return w.runner.Run(
|
2014-06-04 21:25:38 +00:00
|
|
|
task.Script,
|
2014-02-25 01:32:40 +00:00
|
|
|
repo,
|
2014-02-25 01:51:25 +00:00
|
|
|
[]byte(task.Repo.PrivateKey),
|
2014-03-27 05:40:17 +00:00
|
|
|
task.Repo.Privileged,
|
2014-02-25 01:32:40 +00:00
|
|
|
buf,
|
|
|
|
)
|
2014-02-25 00:53:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// updateGitHubStatus is a helper function that will send
|
|
|
|
// the build status to GitHub using the Status API.
|
|
|
|
// see https://github.com/blog/1227-commit-status-api
|
2014-06-04 21:44:05 +00:00
|
|
|
func updateGitHubStatus(repo *repo.Repo, commit *commit.Commit) error {
|
|
|
|
/*
|
|
|
|
// convert from drone status to github status
|
|
|
|
var message, status string
|
|
|
|
switch commit.Status {
|
|
|
|
case "Success":
|
|
|
|
status = "success"
|
|
|
|
message = "The build succeeded on drone.io"
|
|
|
|
case "Failure":
|
|
|
|
status = "failure"
|
|
|
|
message = "The build failed on drone.io"
|
|
|
|
case "Started":
|
|
|
|
status = "pending"
|
|
|
|
message = "The build is pending on drone.io"
|
|
|
|
default:
|
|
|
|
status = "error"
|
|
|
|
message = "The build errored on drone.io"
|
|
|
|
}
|
2014-02-25 00:53:28 +00:00
|
|
|
|
2014-06-04 21:44:05 +00:00
|
|
|
// get the system settings
|
|
|
|
settings, _ := database.GetSettings()
|
2014-02-25 00:53:28 +00:00
|
|
|
|
2014-06-04 21:44:05 +00:00
|
|
|
// get the user from the database
|
|
|
|
// since we need his / her GitHub token
|
|
|
|
user, err := database.GetUser(repo.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-02-25 00:53:28 +00:00
|
|
|
|
2014-06-04 21:44:05 +00:00
|
|
|
client := github.New(user.GithubToken)
|
|
|
|
client.ApiUrl = settings.GitHubApiUrl
|
|
|
|
buildUrl := getBuildUrl(settings.URL().String(), repo, commit)
|
2014-02-25 00:53:28 +00:00
|
|
|
|
2014-06-04 21:44:05 +00:00
|
|
|
return client.Repos.CreateStatus(repo.Owner, repo.Name, status, buildUrl, message, commit.Hash)
|
|
|
|
*/
|
|
|
|
return nil
|
2014-04-17 22:57:13 +00:00
|
|
|
}
|
2014-02-25 00:53:28 +00:00
|
|
|
|
2014-06-04 21:44:05 +00:00
|
|
|
/*
|
|
|
|
func getBuildUrl(host string, repo *repo.Repo, commit *commit.Commit) string {
|
2014-04-17 22:57:13 +00:00
|
|
|
branchQuery := url.Values{}
|
|
|
|
branchQuery.Set("branch", commit.Branch)
|
|
|
|
buildUrl := fmt.Sprintf("%s/%s/commit/%s?%s", host, repo.Slug, commit.Hash, branchQuery.Encode())
|
|
|
|
return buildUrl
|
2014-02-25 00:53:28 +00:00
|
|
|
}
|
2014-06-04 21:44:05 +00:00
|
|
|
*/
|
2014-02-25 00:53:28 +00:00
|
|
|
|
|
|
|
type bufferWrapper struct {
|
|
|
|
buf bytes.Buffer
|
|
|
|
|
|
|
|
// name of the channel
|
|
|
|
channel string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *bufferWrapper) Write(p []byte) (n int, err error) {
|
|
|
|
n, err = b.buf.Write(p)
|
|
|
|
channel.SendBytes(b.channel, p)
|
|
|
|
return
|
|
|
|
}
|