2014-06-21 21:22:38 +00:00
|
|
|
package worker
|
|
|
|
|
|
|
|
import (
|
2014-07-11 23:00:04 +00:00
|
|
|
"log"
|
2014-06-21 21:22:38 +00:00
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
2014-08-14 04:37:56 +00:00
|
|
|
"github.com/drone/drone/plugin/notify"
|
2014-06-21 21:22:38 +00:00
|
|
|
"github.com/drone/drone/server/database"
|
|
|
|
"github.com/drone/drone/server/pubsub"
|
|
|
|
"github.com/drone/drone/shared/build"
|
|
|
|
"github.com/drone/drone/shared/build/docker"
|
|
|
|
"github.com/drone/drone/shared/build/git"
|
|
|
|
"github.com/drone/drone/shared/build/repo"
|
|
|
|
"github.com/drone/drone/shared/build/script"
|
|
|
|
"github.com/drone/drone/shared/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Worker interface {
|
|
|
|
Start() // Start instructs the worker to start processing requests
|
|
|
|
Stop() // Stop instructions the worker to stop processing requests
|
|
|
|
}
|
|
|
|
|
|
|
|
type worker struct {
|
|
|
|
users database.UserManager
|
|
|
|
repos database.RepoManager
|
|
|
|
commits database.CommitManager
|
2014-07-13 02:01:58 +00:00
|
|
|
//config database.ConfigManager
|
|
|
|
pubsub *pubsub.PubSub
|
|
|
|
server *model.Server
|
2014-06-21 21:22:38 +00:00
|
|
|
|
2014-07-13 02:01:58 +00:00
|
|
|
request chan *model.Request
|
|
|
|
dispatch chan chan *model.Request
|
2014-06-21 21:22:38 +00:00
|
|
|
quit chan bool
|
|
|
|
}
|
|
|
|
|
2014-07-13 02:01:58 +00:00
|
|
|
func NewWorker(dispatch chan chan *model.Request, users database.UserManager, repos database.RepoManager, commits database.CommitManager /*config database.ConfigManager,*/, pubsub *pubsub.PubSub, server *model.Server) Worker {
|
2014-06-21 21:22:38 +00:00
|
|
|
return &worker{
|
2014-07-13 02:01:58 +00:00
|
|
|
users: users,
|
|
|
|
repos: repos,
|
|
|
|
commits: commits,
|
|
|
|
//config: config,
|
2014-06-21 21:22:38 +00:00
|
|
|
pubsub: pubsub,
|
|
|
|
server: server,
|
|
|
|
dispatch: dispatch,
|
2014-07-13 02:01:58 +00:00
|
|
|
request: make(chan *model.Request),
|
2014-06-21 21:22:38 +00:00
|
|
|
quit: make(chan bool),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start tells the worker to start listening and
|
|
|
|
// accepting new work requests.
|
|
|
|
func (w *worker) Start() {
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
// register our queue with the dispatch
|
|
|
|
// queue to start accepting work.
|
|
|
|
go func() { w.dispatch <- w.request }()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case r := <-w.request:
|
|
|
|
// handle the request
|
2014-07-13 02:01:58 +00:00
|
|
|
r.Server = w.server
|
2014-06-21 21:22:38 +00:00
|
|
|
w.Execute(r)
|
|
|
|
|
|
|
|
case <-w.quit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop tells the worker to stop listening for new
|
|
|
|
// work requests.
|
|
|
|
func (w *worker) Stop() {
|
|
|
|
go func() { w.quit <- true }()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute executes the work Request, persists the
|
|
|
|
// results to the database, and sends event messages
|
|
|
|
// to the pubsub (for websocket updates on the website).
|
2014-07-13 02:01:58 +00:00
|
|
|
func (w *worker) Execute(r *model.Request) {
|
2014-06-21 21:22:38 +00:00
|
|
|
// mark the build as Started and update the database
|
|
|
|
r.Commit.Status = model.StatusStarted
|
2014-07-11 23:52:22 +00:00
|
|
|
r.Commit.Started = time.Now().UTC().Unix()
|
2014-06-21 21:22:38 +00:00
|
|
|
w.commits.Update(r.Commit)
|
|
|
|
|
|
|
|
// notify all listeners that the build is started
|
2014-06-22 09:04:07 +00:00
|
|
|
commitc := w.pubsub.Register("_global")
|
2014-06-21 21:22:38 +00:00
|
|
|
commitc.Publish(r)
|
2014-06-22 09:04:07 +00:00
|
|
|
stdoutc := w.pubsub.RegisterOpts(r.Commit.ID, pubsub.ConsoleOpts)
|
2014-06-21 21:22:38 +00:00
|
|
|
defer stdoutc.Close()
|
|
|
|
|
|
|
|
// create a special buffer that will also
|
|
|
|
// write to a websocket channel
|
|
|
|
buf := pubsub.NewBuffer(stdoutc)
|
|
|
|
|
|
|
|
// parse the parameters and build script. The script has already
|
|
|
|
// been parsed in the hook, so we can be confident it will succeed.
|
|
|
|
// that being said, we should clean this up
|
|
|
|
params, _ := r.Repo.ParamMap()
|
2014-07-11 23:00:04 +00:00
|
|
|
script, err := script.ParseBuild(r.Commit.Config, params)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error parsing YAML for %s/%s, Err: %s", r.Repo.Owner, r.Repo.Name, err.Error())
|
|
|
|
}
|
2014-06-21 21:22:38 +00:00
|
|
|
|
2014-08-14 04:37:56 +00:00
|
|
|
// append private parameters to the environment
|
|
|
|
// variable section of the .drone.yml file, iff
|
|
|
|
// this is not a pull request (for security purposes)
|
|
|
|
if params != nil && len(r.Commit.PullRequest) == 0 {
|
|
|
|
for k, v := range params {
|
|
|
|
script.Env = append(script.Env, k+"="+v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-21 21:22:38 +00:00
|
|
|
path := r.Repo.Host + "/" + r.Repo.Owner + "/" + r.Repo.Name
|
|
|
|
repo := &repo.Repo{
|
|
|
|
Name: path,
|
|
|
|
Path: r.Repo.CloneURL,
|
|
|
|
Branch: r.Commit.Branch,
|
|
|
|
Commit: r.Commit.Sha,
|
|
|
|
PR: r.Commit.PullRequest,
|
|
|
|
Dir: filepath.Join("/var/cache/drone/src", git.GitPath(script.Git, path)),
|
|
|
|
Depth: git.GitDepth(script.Git),
|
|
|
|
}
|
|
|
|
|
2014-07-11 22:39:17 +00:00
|
|
|
// Instantiate a new Docker client
|
|
|
|
var dockerClient *docker.Client
|
|
|
|
switch {
|
|
|
|
case len(w.server.Host) == 0:
|
|
|
|
dockerClient = docker.New()
|
|
|
|
default:
|
|
|
|
dockerClient = docker.NewHost(w.server.Host)
|
|
|
|
}
|
|
|
|
|
2014-07-13 02:01:58 +00:00
|
|
|
// send all "started" notifications
|
2014-08-14 18:56:11 +00:00
|
|
|
if script.Notifications == nil {
|
2014-08-14 04:37:56 +00:00
|
|
|
script.Notifications = ¬ify.Notification{}
|
2014-07-13 02:01:58 +00:00
|
|
|
}
|
2014-08-14 04:37:56 +00:00
|
|
|
script.Notifications.Send(r)
|
2014-07-13 02:01:58 +00:00
|
|
|
|
2014-06-21 21:22:38 +00:00
|
|
|
// create an instance of the Docker builder
|
2014-07-11 22:39:17 +00:00
|
|
|
builder := build.New(dockerClient)
|
2014-06-21 21:22:38 +00:00
|
|
|
builder.Build = script
|
|
|
|
builder.Repo = repo
|
|
|
|
builder.Stdout = buf
|
|
|
|
builder.Key = []byte(r.Repo.PrivateKey)
|
2014-07-11 23:52:22 +00:00
|
|
|
builder.Timeout = time.Duration(r.Repo.Timeout) * time.Second
|
2014-06-21 21:22:38 +00:00
|
|
|
builder.Privileged = r.Repo.Privileged
|
|
|
|
|
2014-07-11 22:29:31 +00:00
|
|
|
// run the build
|
2014-07-11 23:00:04 +00:00
|
|
|
err = builder.Run()
|
2014-06-21 21:22:38 +00:00
|
|
|
|
|
|
|
// update the build status based on the results
|
|
|
|
// from the build runner.
|
|
|
|
switch {
|
|
|
|
case err != nil:
|
|
|
|
r.Commit.Status = model.StatusError
|
2014-07-11 23:52:22 +00:00
|
|
|
log.Printf("Error building %s, Err: %s", r.Commit.Sha, err)
|
2014-06-21 21:22:38 +00:00
|
|
|
buf.WriteString(err.Error())
|
|
|
|
case builder.BuildState == nil:
|
|
|
|
r.Commit.Status = model.StatusFailure
|
|
|
|
case builder.BuildState.ExitCode != 0:
|
|
|
|
r.Commit.Status = model.StatusFailure
|
|
|
|
default:
|
|
|
|
r.Commit.Status = model.StatusSuccess
|
|
|
|
}
|
|
|
|
|
|
|
|
// calcualte the build finished and duration details and
|
|
|
|
// update the commit
|
2014-07-11 23:52:22 +00:00
|
|
|
r.Commit.Finished = time.Now().UTC().Unix()
|
2014-06-21 21:22:38 +00:00
|
|
|
r.Commit.Duration = (r.Commit.Finished - r.Commit.Started)
|
|
|
|
w.commits.Update(r.Commit)
|
|
|
|
w.commits.UpdateOutput(r.Commit, buf.Bytes())
|
|
|
|
|
|
|
|
// notify all listeners that the build is finished
|
|
|
|
commitc.Publish(r)
|
|
|
|
|
2014-07-13 02:01:58 +00:00
|
|
|
// send all "finished" notifications
|
2014-08-14 04:37:56 +00:00
|
|
|
script.Notifications.Send(r)
|
2014-06-21 21:22:38 +00:00
|
|
|
}
|