diff --git a/server/handler/hook.go b/server/handler/hook.go index 6186c113c..d8131140a 100644 --- a/server/handler/hook.go +++ b/server/handler/hook.go @@ -4,6 +4,7 @@ import ( "fmt" "net/http" + "github.com/drone/drone/server/queue" "github.com/drone/drone/server/resource/commit" "github.com/drone/drone/server/resource/config" "github.com/drone/drone/server/resource/repo" @@ -15,11 +16,12 @@ type HookHandler struct { users user.UserManager repos repo.RepoManager commits commit.CommitManager + queue *queue.Queue conf *config.Config } -func NewHookHandler(users user.UserManager, repos repo.RepoManager, commits commit.CommitManager, conf *config.Config) *HookHandler { - return &HookHandler{users, repos, commits, conf} +func NewHookHandler(users user.UserManager, repos repo.RepoManager, commits commit.CommitManager, conf *config.Config, queue *queue.Queue) *HookHandler { + return &HookHandler{users, repos, commits, queue, conf} } // PostHook receives a post-commit hook from GitHub, Bitbucket, etc @@ -48,7 +50,7 @@ func (h *HookHandler) PostHook(w http.ResponseWriter, r *http.Request) error { } // fetch the repository from the database - repo, err := h.repos.FindName(host, hook.Owner, hook.Repo) + repo, err := h.repos.FindName(remote.GetHost(), hook.Owner, hook.Repo) if err != nil { return notFound{} } @@ -91,7 +93,7 @@ func (h *HookHandler) PostHook(w http.ResponseWriter, r *http.Request) error { fmt.Printf("%s", script) // drop the items on the queue - //h.queue.Add(&queue.BuildTask{Repo: repo, Commit: commit, Build: build, Script: script}) + //h.queue.Add(&queue.BuildTask{Repo: repo, Commit: &c, Script: script}) return nil } diff --git a/server/main.go b/server/main.go index 318575f5f..65c2202aa 100644 --- a/server/main.go +++ b/server/main.go @@ -5,11 +5,14 @@ import ( "flag" "html/template" "net/http" + "runtime" + "time" "code.google.com/p/go.net/websocket" "github.com/drone/drone/server/channel" "github.com/drone/drone/server/database" "github.com/drone/drone/server/handler" + "github.com/drone/drone/server/queue" "github.com/drone/drone/server/render" "github.com/drone/drone/server/resource/commit" "github.com/drone/drone/server/resource/config" @@ -17,6 +20,7 @@ import ( "github.com/drone/drone/server/resource/repo" "github.com/drone/drone/server/resource/user" "github.com/drone/drone/server/session" + "github.com/drone/drone/shared/build/docker" "github.com/gorilla/pat" //"github.com/justinas/nosurf" @@ -42,6 +46,14 @@ var ( // commit sha for the current build. version string = "0.2-dev" revision string + + // build will timeout after N milliseconds. + // this will default to 500 minutes (6 hours) + timeout time.Duration + + // Number of concurrent build workers to run + // default to number of CPUs on machine + workers int ) // drone cofiguration data, loaded from the @@ -56,6 +68,8 @@ func main() { flag.StringVar(&datasource, "datasource", "drone.sqlite", "") flag.StringVar(&sslcert, "sslcert", "", "") flag.StringVar(&sslkey, "sslkey", "", "") + flag.DurationVar(&timeout, "timeout", 300*time.Minute, "") + flag.IntVar(&workers, "workers", runtime.NumCPU(), "") flag.Parse() // parse the template files @@ -69,6 +83,10 @@ 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) @@ -82,7 +100,7 @@ func main() { router := pat.New() handler.NewUsersHandler(users, sess).Register(router) handler.NewUserHandler(users, repos, commits, sess).Register(router) - handler.NewHookHandler(users, repos, commits, &conf).Register(router) + handler.NewHookHandler(users, repos, commits, &conf, queue).Register(router) handler.NewLoginHandler(users, repos, perms, sess, &conf).Register(router) handler.NewCommitHandler(repos, commits, perms, sess).Register(router) handler.NewBranchHandler(repos, commits, perms, sess).Register(router) diff --git a/shared/remote/bitbucket/bitbucket.go b/shared/remote/bitbucket/bitbucket.go index 51e5e6fe9..21b621c24 100644 --- a/shared/remote/bitbucket/bitbucket.go +++ b/shared/remote/bitbucket/bitbucket.go @@ -3,6 +3,7 @@ package bitbucket import ( "fmt" "net/http" + "net/url" "time" "github.com/drone/drone/shared/remote" @@ -24,6 +25,15 @@ func (b *Bitbucket) GetName() string { return "bitbucket.org" } +// GetHost returns the url.Host of this remote system. +func (b *Bitbucket) GetHost() (host string) { + u, err := url.Parse(b.URL) + if err != nil { + return + } + return u.Host +} + // GetHook parses the post-commit hook from the Request body // and returns the required data in a standard format. func (b *Bitbucket) GetHook(r *http.Request) (*remote.Hook, error) { diff --git a/shared/remote/github/github.go b/shared/remote/github/github.go index d4e783bdf..b9b924bf2 100644 --- a/shared/remote/github/github.go +++ b/shared/remote/github/github.go @@ -3,6 +3,7 @@ package github import ( "fmt" "net/http" + "net/url" "strconv" "strings" "time" @@ -36,6 +37,15 @@ func (g *Github) GetName() string { } } +// GetHost returns the url.Host of this remote system. +func (g *Github) GetHost() (host string) { + u, err := url.Parse(g.URL) + if err != nil { + return + } + return u.Host +} + // GetHook parses the post-commit hook from the Request body // and returns the required data in a standard format. func (g *Github) GetHook(r *http.Request) (*remote.Hook, error) { diff --git a/shared/remote/gitlab/gitlab.go b/shared/remote/gitlab/gitlab.go index 43d45eeb3..c778d56a4 100644 --- a/shared/remote/gitlab/gitlab.go +++ b/shared/remote/gitlab/gitlab.go @@ -2,6 +2,7 @@ package gitlab import ( "net/http" + "net/url" "strings" "github.com/drone/drone/shared/remote" @@ -17,6 +18,15 @@ func (g *Gitlab) GetName() string { return "gitlab.com" } +// GetHost returns the url.Host of this remote system. +func (g *Gitlab) GetHost() (host string) { + u, err := url.Parse(g.URL) + if err != nil { + return + } + return u.Host +} + // GetHook parses the post-commit hook from the Request body // and returns the required data in a standard format. func (g *Gitlab) GetHook(*http.Request) (*remote.Hook, error) { diff --git a/shared/remote/remote.go b/shared/remote/remote.go index 1cee8fac7..8acecaa22 100644 --- a/shared/remote/remote.go +++ b/shared/remote/remote.go @@ -5,9 +5,12 @@ import ( ) type Remote interface { - // Name returns the name of this remote system. + // GetName returns the name of this remote system. GetName() string + // GetHost returns the URL hostname of this remote system. + GetHost() (host string) + // GetHook parses the post-commit hook from the Request body // and returns the required data in a standard format. GetHook(*http.Request) (*Hook, error) diff --git a/shared/remote/stash/stash.go b/shared/remote/stash/stash.go index 21b48ffc5..2e61fff7b 100644 --- a/shared/remote/stash/stash.go +++ b/shared/remote/stash/stash.go @@ -2,6 +2,7 @@ package stash import ( "net/http" + "net/url" "strings" "github.com/drone/drone/shared/remote" @@ -20,6 +21,15 @@ func (s *Stash) GetName() string { return "stash.atlassian.com" } +// GetHost returns the url.Host of this remote system. +func (s *Stash) GetHost() (host string) { + u, err := url.Parse(s.URL) + if err != nil { + return + } + return u.Host +} + // GetHook parses the post-commit hook from the Request body // and returns the required data in a standard format. func (s *Stash) GetHook(*http.Request) (*remote.Hook, error) {