From aec9b33048cc4ca87930716996d0f11cf73681cf Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Thu, 12 Jun 2014 12:44:19 -0700 Subject: [PATCH] got the build working correctly --- server/handler/hook.go | 6 +++--- server/main.go | 11 +++++++---- server/queue/queue.go | 5 +++-- server/queue/worker.go | 14 ++++++++++++-- shared/build/build.go | 2 +- shared/build/buildfile/buildfile.go | 1 + shared/remote/github/github.go | 15 ++++++++++++--- 7 files changed, 39 insertions(+), 15 deletions(-) diff --git a/server/handler/hook.go b/server/handler/hook.go index 2a78dbad..feac9070 100644 --- a/server/handler/hook.go +++ b/server/handler/hook.go @@ -1,7 +1,6 @@ package handler import ( - "fmt" "net/http" "github.com/drone/drone/server/queue" @@ -49,6 +48,8 @@ func (h *HookHandler) PostHook(w http.ResponseWriter, r *http.Request) error { return nil } + //fmt.Printf("%#v", hook) + // fetch the repository from the database repo, err := h.repos.FindName(remote.GetHost(), hook.Owner, hook.Repo) if err != nil { @@ -90,8 +91,7 @@ func (h *HookHandler) PostHook(w http.ResponseWriter, r *http.Request) error { return badRequest{err} } - fmt.Printf("%#v", hook) - fmt.Printf("%s", yml) + //fmt.Printf("%s", yml) // drop the items on the queue h.queue.Add(&queue.BuildTask{Repo: repo, Commit: &c}) diff --git a/server/main.go b/server/main.go index 86e28929..ce47b3d9 100644 --- a/server/main.go +++ b/server/main.go @@ -21,6 +21,7 @@ import ( "github.com/drone/drone/server/resource/user" "github.com/drone/drone/server/session" "github.com/drone/drone/shared/build/docker" + "github.com/drone/drone/shared/build/log" "github.com/gorilla/pat" //"github.com/justinas/nosurf" @@ -63,6 +64,8 @@ var conf config.Config func main() { + log.SetPriority(log.LOG_NOTICE) + // parse command line flags flag.StringVar(&port, "port", ":8080", "") flag.StringVar(&driver, "driver", "sqlite3", "") @@ -92,10 +95,6 @@ func main() { db, _ := sql.Open(driver, datasource) database.Load(db) - // setup the build queue - queueRunner := queue.NewBuildRunner(docker.New(), timeout) - queue := queue.Start(workers, queueRunner) - // setup the database managers repos := repo.NewManager(db) users := user.NewManager(db) @@ -105,6 +104,10 @@ func main() { // cancel all previously running builds go commits.CancelAll() + // setup the build queue + queueRunner := queue.NewBuildRunner(docker.New(), timeout) + queue := queue.Start(workers, commits, queueRunner) + // setup the session managers sess := session.NewSession(users) diff --git a/server/queue/queue.go b/server/queue/queue.go index 7fbc39c4..3b041ae5 100644 --- a/server/queue/queue.go +++ b/server/queue/queue.go @@ -26,13 +26,14 @@ type BuildTask struct { } // Start N workers with the given build runner. -func Start(workers int, runner BuildRunner) *Queue { +func Start(workers int, commits commit.CommitManager, runner BuildRunner) *Queue { tasks := make(chan *BuildTask) queue := &Queue{tasks: tasks} for i := 0; i < workers; i++ { worker := worker{ - runner: runner, + runner: runner, + commits: commits, } go worker.work(tasks) diff --git a/server/queue/worker.go b/server/queue/worker.go index 1ade6041..3fb11cd8 100644 --- a/server/queue/worker.go +++ b/server/queue/worker.go @@ -8,6 +8,7 @@ import ( r "github.com/drone/drone/shared/build/repo" "github.com/drone/drone/shared/build/script" "io" + "log" "path/filepath" "time" @@ -34,7 +35,10 @@ func (w *worker) work(queue <-chan *BuildTask) { } // execute the task - w.execute(task) + err := w.execute(task) + if err != nil { + log.Println(err) + } } } @@ -57,6 +61,7 @@ func (w *worker) execute(task *BuildTask) error { params, err := task.Repo.ParamMap() task.Script, err = script.ParseBuild(task.Commit.Config, params) if err != nil { + log.Printf("Error parsing repository params. %s\n", err) return err } @@ -66,6 +71,7 @@ func (w *worker) execute(task *BuildTask) error { // persist the commit to the database if err := w.commits.Update(task.Commit); err != nil { + log.Printf("Error updating commit. %s\n", err) return err } @@ -166,7 +172,7 @@ func (w *worker) execute(task *BuildTask) error { func (w *worker) runBuild(task *BuildTask, buf io.Writer) (bool, error) { repo := &r.Repo{ Name: task.Repo.Host + task.Repo.Owner + task.Repo.Name, - Path: task.Repo.URL, + Path: task.Repo.CloneURL, Branch: task.Commit.Branch, Commit: task.Commit.Sha, PR: task.Commit.PullRequest, @@ -175,6 +181,10 @@ func (w *worker) runBuild(task *BuildTask, buf io.Writer) (bool, error) { Depth: git.GitDepth(task.Script.Git), } + if task.Repo.Private { + repo.Path = task.Repo.SSHURL + } + return w.runner.Run( task.Script, repo, diff --git a/shared/build/build.go b/shared/build/build.go index cad8e7ce..05e03bac 100644 --- a/shared/build/build.go +++ b/shared/build/build.go @@ -167,7 +167,7 @@ func (b *Builder) setup() error { src := filepath.Join(dir, "src") cmd := exec.Command("cp", "-a", b.Repo.Path, src) if err := cmd.Run(); err != nil { - return err + return fmt.Errorf("Error: Unable to copy repository. %s", err) } } diff --git a/shared/build/buildfile/buildfile.go b/shared/build/buildfile/buildfile.go index 367fc083..da56fb6f 100644 --- a/shared/build/buildfile/buildfile.go +++ b/shared/build/buildfile/buildfile.go @@ -56,6 +56,7 @@ func (b *Buildfile) WriteHost(mapping string) { // code at the start. var base = ` #!/bin/bash +set +e # drone configuration files are stored in /etc/drone.d # execute these files prior to our build to set global diff --git a/shared/remote/github/github.go b/shared/remote/github/github.go index b9b924bf..968d1733 100644 --- a/shared/remote/github/github.go +++ b/shared/remote/github/github.go @@ -81,6 +81,10 @@ func (g *Github) GetHook(r *http.Request) (*remote.Hook, error) { hook.Sha = data.Head.Id hook.Branch = data.Branch() + if len(hook.Owner) == 0 { + hook.Owner = data.Repo.Owner.Name + } + // extract the author and message from the commit // this is kind of experimental, since I don't know // what I'm doing here. @@ -114,8 +118,7 @@ func (g *Github) GetPullRequestHook(r *http.Request) (*remote.Hook, error) { // TODO we should also store the pull request branch (ie from x to y) // we can find it here: data.PullRequest.Head.Ref - - return &remote.Hook{ + hook := remote.Hook{ Owner: data.Repo.Owner.Login, Repo: data.Repo.Name, Sha: data.PullRequest.Head.Sha, @@ -125,7 +128,13 @@ func (g *Github) GetPullRequestHook(r *http.Request) (*remote.Hook, error) { Timestamp: time.Now().UTC().String(), Message: data.PullRequest.Title, PullRequest: strconv.Itoa(data.Number), - }, nil + } + + if len(hook.Owner) == 0 { + hook.Owner = data.Repo.Owner.Name + } + + return &hook, nil } // GetLogin handles authentication to third party, remote services