got the build working correctly

This commit is contained in:
Brad Rydzewski 2014-06-12 12:44:19 -07:00
parent 7abe695a5c
commit aec9b33048
7 changed files with 39 additions and 15 deletions

View file

@ -1,7 +1,6 @@
package handler package handler
import ( import (
"fmt"
"net/http" "net/http"
"github.com/drone/drone/server/queue" "github.com/drone/drone/server/queue"
@ -49,6 +48,8 @@ func (h *HookHandler) PostHook(w http.ResponseWriter, r *http.Request) error {
return nil return nil
} }
//fmt.Printf("%#v", hook)
// fetch the repository from the database // fetch the repository from the database
repo, err := h.repos.FindName(remote.GetHost(), hook.Owner, hook.Repo) repo, err := h.repos.FindName(remote.GetHost(), hook.Owner, hook.Repo)
if err != nil { if err != nil {
@ -90,8 +91,7 @@ func (h *HookHandler) PostHook(w http.ResponseWriter, r *http.Request) error {
return badRequest{err} return badRequest{err}
} }
fmt.Printf("%#v", hook) //fmt.Printf("%s", yml)
fmt.Printf("%s", yml)
// drop the items on the queue // drop the items on the queue
h.queue.Add(&queue.BuildTask{Repo: repo, Commit: &c}) h.queue.Add(&queue.BuildTask{Repo: repo, Commit: &c})

View file

@ -21,6 +21,7 @@ import (
"github.com/drone/drone/server/resource/user" "github.com/drone/drone/server/resource/user"
"github.com/drone/drone/server/session" "github.com/drone/drone/server/session"
"github.com/drone/drone/shared/build/docker" "github.com/drone/drone/shared/build/docker"
"github.com/drone/drone/shared/build/log"
"github.com/gorilla/pat" "github.com/gorilla/pat"
//"github.com/justinas/nosurf" //"github.com/justinas/nosurf"
@ -63,6 +64,8 @@ var conf config.Config
func main() { func main() {
log.SetPriority(log.LOG_NOTICE)
// parse command line flags // parse command line flags
flag.StringVar(&port, "port", ":8080", "") flag.StringVar(&port, "port", ":8080", "")
flag.StringVar(&driver, "driver", "sqlite3", "") flag.StringVar(&driver, "driver", "sqlite3", "")
@ -92,10 +95,6 @@ func main() {
db, _ := sql.Open(driver, datasource) db, _ := sql.Open(driver, datasource)
database.Load(db) database.Load(db)
// setup the build queue
queueRunner := queue.NewBuildRunner(docker.New(), timeout)
queue := queue.Start(workers, queueRunner)
// setup the database managers // setup the database managers
repos := repo.NewManager(db) repos := repo.NewManager(db)
users := user.NewManager(db) users := user.NewManager(db)
@ -105,6 +104,10 @@ func main() {
// cancel all previously running builds // cancel all previously running builds
go commits.CancelAll() go commits.CancelAll()
// setup the build queue
queueRunner := queue.NewBuildRunner(docker.New(), timeout)
queue := queue.Start(workers, commits, queueRunner)
// setup the session managers // setup the session managers
sess := session.NewSession(users) sess := session.NewSession(users)

View file

@ -26,13 +26,14 @@ type BuildTask struct {
} }
// Start N workers with the given build runner. // 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) tasks := make(chan *BuildTask)
queue := &Queue{tasks: tasks} queue := &Queue{tasks: tasks}
for i := 0; i < workers; i++ { for i := 0; i < workers; i++ {
worker := worker{ worker := worker{
runner: runner, runner: runner,
commits: commits,
} }
go worker.work(tasks) go worker.work(tasks)

View file

@ -8,6 +8,7 @@ import (
r "github.com/drone/drone/shared/build/repo" r "github.com/drone/drone/shared/build/repo"
"github.com/drone/drone/shared/build/script" "github.com/drone/drone/shared/build/script"
"io" "io"
"log"
"path/filepath" "path/filepath"
"time" "time"
@ -34,7 +35,10 @@ func (w *worker) work(queue <-chan *BuildTask) {
} }
// execute the task // 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() params, err := task.Repo.ParamMap()
task.Script, err = script.ParseBuild(task.Commit.Config, params) task.Script, err = script.ParseBuild(task.Commit.Config, params)
if err != nil { if err != nil {
log.Printf("Error parsing repository params. %s\n", err)
return err return err
} }
@ -66,6 +71,7 @@ func (w *worker) execute(task *BuildTask) error {
// persist the commit to the database // persist the commit to the database
if err := w.commits.Update(task.Commit); err != nil { if err := w.commits.Update(task.Commit); err != nil {
log.Printf("Error updating commit. %s\n", err)
return 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) { func (w *worker) runBuild(task *BuildTask, buf io.Writer) (bool, error) {
repo := &r.Repo{ repo := &r.Repo{
Name: task.Repo.Host + task.Repo.Owner + task.Repo.Name, Name: task.Repo.Host + task.Repo.Owner + task.Repo.Name,
Path: task.Repo.URL, Path: task.Repo.CloneURL,
Branch: task.Commit.Branch, Branch: task.Commit.Branch,
Commit: task.Commit.Sha, Commit: task.Commit.Sha,
PR: task.Commit.PullRequest, 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), Depth: git.GitDepth(task.Script.Git),
} }
if task.Repo.Private {
repo.Path = task.Repo.SSHURL
}
return w.runner.Run( return w.runner.Run(
task.Script, task.Script,
repo, repo,

View file

@ -167,7 +167,7 @@ func (b *Builder) setup() error {
src := filepath.Join(dir, "src") src := filepath.Join(dir, "src")
cmd := exec.Command("cp", "-a", b.Repo.Path, src) cmd := exec.Command("cp", "-a", b.Repo.Path, src)
if err := cmd.Run(); err != nil { if err := cmd.Run(); err != nil {
return err return fmt.Errorf("Error: Unable to copy repository. %s", err)
} }
} }

View file

@ -56,6 +56,7 @@ func (b *Buildfile) WriteHost(mapping string) {
// code at the start. // code at the start.
var base = ` var base = `
#!/bin/bash #!/bin/bash
set +e
# drone configuration files are stored in /etc/drone.d # drone configuration files are stored in /etc/drone.d
# execute these files prior to our build to set global # execute these files prior to our build to set global

View file

@ -81,6 +81,10 @@ func (g *Github) GetHook(r *http.Request) (*remote.Hook, error) {
hook.Sha = data.Head.Id hook.Sha = data.Head.Id
hook.Branch = data.Branch() hook.Branch = data.Branch()
if len(hook.Owner) == 0 {
hook.Owner = data.Repo.Owner.Name
}
// extract the author and message from the commit // extract the author and message from the commit
// this is kind of experimental, since I don't know // this is kind of experimental, since I don't know
// what I'm doing here. // 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) // TODO we should also store the pull request branch (ie from x to y)
// we can find it here: data.PullRequest.Head.Ref // we can find it here: data.PullRequest.Head.Ref
hook := remote.Hook{
return &remote.Hook{
Owner: data.Repo.Owner.Login, Owner: data.Repo.Owner.Login,
Repo: data.Repo.Name, Repo: data.Repo.Name,
Sha: data.PullRequest.Head.Sha, Sha: data.PullRequest.Head.Sha,
@ -125,7 +128,13 @@ func (g *Github) GetPullRequestHook(r *http.Request) (*remote.Hook, error) {
Timestamp: time.Now().UTC().String(), Timestamp: time.Now().UTC().String(),
Message: data.PullRequest.Title, Message: data.PullRequest.Title,
PullRequest: strconv.Itoa(data.Number), 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 // GetLogin handles authentication to third party, remote services