From 7abe695a5ca1e9e6e8552c74078e9cf73cfeca23 Mon Sep 17 00:00:00 2001 From: Brad Date: Wed, 11 Jun 2014 17:42:49 -0700 Subject: [PATCH] hooked the build queue back up --- .drone.yml | 18 ++++++------ Makefile | 10 +++++-- debian/drone/etc/init/drone.conf | 2 +- server/handler/commit.go | 49 ++++++++++++++++++++++++++++++-- server/handler/hook.go | 9 +++--- server/main.go | 21 ++++++++++---- server/queue/worker.go | 9 +++++- server/resource/repo/model.go | 18 ++++++++++-- server/static/styles/drone.css | 16 ----------- shared/build/script/script.go | 8 +++--- 10 files changed, 111 insertions(+), 49 deletions(-) diff --git a/.drone.yml b/.drone.yml index 564344056..1e3db35d5 100644 --- a/.drone.yml +++ b/.drone.yml @@ -12,12 +12,12 @@ notify: recipients: - brad@drone.io -publish: - s3: - acl: public-read - region: us-east-1 - bucket: downloads.drone.io - access_key: $AWS_KEY - secret_key: $AWS_SECRET - source: deb/drone.deb - target: exp/ +#publish: +# s3: +# acl: public-read +# region: us-east-1 +# bucket: downloads.drone.io +# access_key: $AWS_KEY +# secret_key: $AWS_SECRET +# source: deb/drone.deb +# target: exp/ diff --git a/Makefile b/Makefile index e13305401..b0c8f68de 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ SHA := $(shell git rev-parse --short HEAD) -all: build +all: rice amberc lessc build deps: go list github.com/drone/drone/... | xargs go get -t @@ -23,6 +23,10 @@ clean: #cd cmd/droned/static && rice clean #cd cmd/droned/template && rice clean +rice: + cd server && rice embed + #cd server/template/html && rice embed + amberc: @for f in server/template/*.amber; do $$GOPATH/bin/amberc -pp=true "$$f" > "$${f%.amber}.html"; done @mkdir -p server/template/html @@ -40,9 +44,9 @@ uglify: # creates a debian package for drone # to install `sudo dpkg -i drone.deb` -dpkg: build +dpkg: mkdir -p debian/drone/usr/local/bin -dpkg-deb --build debian/drone run: - @cd server && go run main.go conf.go \ No newline at end of file + @cd server && go run main.go conf.gomake \ No newline at end of file diff --git a/debian/drone/etc/init/drone.conf b/debian/drone/etc/init/drone.conf index 55962c454..8940bfea5 100644 --- a/debian/drone/etc/init/drone.conf +++ b/debian/drone/etc/init/drone.conf @@ -1,6 +1,6 @@ start on (filesystem and net-device-up) -chdir /var/lib/drone +chdir /root/.drone console log script diff --git a/server/handler/commit.go b/server/handler/commit.go index 13dbd2674..271234838 100644 --- a/server/handler/commit.go +++ b/server/handler/commit.go @@ -4,6 +4,7 @@ import ( "encoding/json" "net/http" + "github.com/drone/drone/server/queue" "github.com/drone/drone/server/resource/commit" "github.com/drone/drone/server/resource/perm" "github.com/drone/drone/server/resource/repo" @@ -16,10 +17,11 @@ type CommitHandler struct { repos repo.RepoManager commits commit.CommitManager sess session.Session + queue *queue.Queue } -func NewCommitHandler(repos repo.RepoManager, commits commit.CommitManager, perms perm.PermManager, sess session.Session) *CommitHandler { - return &CommitHandler{perms, repos, commits, sess} +func NewCommitHandler(repos repo.RepoManager, commits commit.CommitManager, perms perm.PermManager, sess session.Session, queue *queue.Queue) *CommitHandler { + return &CommitHandler{perms, repos, commits, sess, queue} } // GetFeed gets recent commits for the repository and branch @@ -117,6 +119,49 @@ func (h *CommitHandler) GetCommitOutput(w http.ResponseWriter, r *http.Request) // PostCommit gets the commit for the repository and schedules to re-build. // GET /v1/repos/{host}/{owner}/{name}/branches/{branch}/commits/{commit} func (h *CommitHandler) PostCommit(w http.ResponseWriter, r *http.Request) error { + var host, owner, name = parseRepo(r) + var branch = r.FormValue(":branch") + var sha = r.FormValue(":commit") + + // get the user form the session. + user := h.sess.User(r) + if user == nil { + return notAuthorized{} + } + + // get the repo from the database + repo, err := h.repos.FindName(host, owner, name) + if err != nil { + return notFound{err} + } + + // user must have admin access to the repository. + if ok, _ := h.perms.Admin(user, repo); !ok { + return notFound{err} + } + + c, err := h.commits.FindSha(repo.ID, branch, sha) + if err != nil { + return notFound{err} + } + + // we can't start an already started build + if c.Status == commit.StatusStarted || c.Status == commit.StatusEnqueue { + return badRequest{} + } + + c.Status = commit.StatusEnqueue + c.Started = 0 + c.Finished = 0 + c.Duration = 0 + if err := h.commits.Update(c); err != nil { + return internalServerError{err} + } + + // drop the items on the queue + h.queue.Add(&queue.BuildTask{Repo: repo, Commit: c}) + return nil + return notImplemented{} } diff --git a/server/handler/hook.go b/server/handler/hook.go index d8131140a..2a78dbad8 100644 --- a/server/handler/hook.go +++ b/server/handler/hook.go @@ -70,7 +70,7 @@ func (h *HookHandler) PostHook(w http.ResponseWriter, r *http.Request) error { // featch the .drone.yml file from the database client := remote.GetClient(user.Access, user.Secret) - script, err := client.GetScript(hook) + yml, err := client.GetScript(hook) if err != nil { return badRequest{err} } @@ -82,7 +82,8 @@ func (h *HookHandler) PostHook(w http.ResponseWriter, r *http.Request) error { Branch: hook.Branch, PullRequest: hook.PullRequest, Timestamp: hook.Timestamp, - Message: hook.Message} + Message: hook.Message, + Config: yml} c.SetAuthor(hook.Author) // inser the commit into the database if err := h.commits.Insert(&c); err != nil { @@ -90,10 +91,10 @@ func (h *HookHandler) PostHook(w http.ResponseWriter, r *http.Request) error { } fmt.Printf("%#v", hook) - fmt.Printf("%s", script) + fmt.Printf("%s", yml) // drop the items on the queue - //h.queue.Add(&queue.BuildTask{Repo: repo, Commit: &c, Script: script}) + h.queue.Add(&queue.BuildTask{Repo: repo, Commit: &c}) return nil } diff --git a/server/main.go b/server/main.go index 91a2cbe4c..86e289299 100644 --- a/server/main.go +++ b/server/main.go @@ -24,6 +24,7 @@ import ( "github.com/gorilla/pat" //"github.com/justinas/nosurf" + "github.com/GeertJohan/go.rice" _ "github.com/mattn/go-sqlite3" "github.com/russross/meddler" ) @@ -74,9 +75,17 @@ func main() { // parse the template files // TODO we need to retrieve these from go.rice - templ := template.Must( - template.New("_").Funcs(render.FuncMap).ParseGlob("template/html/*.html"), - ).ExecuteTemplate + //templ := template.Must( + // template.New("_").Funcs(render.FuncMap).ParseGlob("template/html/*.html"), + //).ExecuteTemplate + + templateBox := rice.MustFindBox("template/html") + templateFiles := []string{"login.html", "repo_branch.html", "repo_commit.html", "repo_conf.html", "repo_feed.html", "user_conf.html", "user_feed.html", "user_login.html", "user_repos.html", "404.html", "400.html"} + templ := template.New("_").Funcs(render.FuncMap) + for _, file := range templateFiles { + templateData, _ := templateBox.String(file) + templ, _ = templ.New(file).Parse(templateData) + } // setup the database meddler.Default = meddler.SQLite @@ -105,16 +114,16 @@ func main() { handler.NewUserHandler(users, repos, commits, sess).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.NewCommitHandler(repos, commits, perms, sess, queue).Register(router) handler.NewBranchHandler(repos, commits, perms, sess).Register(router) handler.NewRepoHandler(repos, commits, perms, sess, &conf).Register(router) handler.NewBadgeHandler(repos, commits).Register(router) handler.NewConfigHandler(conf, sess).Register(router) - handler.NewSiteHandler(users, repos, commits, perms, sess, templ).Register(router) + handler.NewSiteHandler(users, repos, commits, perms, sess, templ.ExecuteTemplate).Register(router) // serve static assets // TODO we need to replace this with go.rice - http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) + http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(rice.MustFindBox("static/").HTTPBox()))) // server websocket data http.Handle("/feed", websocket.Handler(channel.Read)) diff --git a/server/queue/worker.go b/server/queue/worker.go index 58b752d33..1ade6041e 100644 --- a/server/queue/worker.go +++ b/server/queue/worker.go @@ -6,7 +6,7 @@ import ( "github.com/drone/drone/server/channel" "github.com/drone/drone/shared/build/git" r "github.com/drone/drone/shared/build/repo" - //"github.com/drone/drone/pkg/plugin/notify" + "github.com/drone/drone/shared/build/script" "io" "path/filepath" "time" @@ -53,6 +53,13 @@ func (w *worker) execute(task *BuildTask) error { } }() + // parse the build script + params, err := task.Repo.ParamMap() + task.Script, err = script.ParseBuild(task.Commit.Config, params) + if err != nil { + return err + } + // update commit and build status task.Commit.Status = commit.StatusStarted task.Commit.Started = time.Now().Unix() diff --git a/server/resource/repo/model.go b/server/resource/repo/model.go index 7935fb13e..795476519 100644 --- a/server/resource/repo/model.go +++ b/server/resource/repo/model.go @@ -1,9 +1,15 @@ package repo -var DefaultBranch = "master" +import ( + "gopkg.in/yaml.v1" +) -// default build timeout, in seconds -var DefaultTimeout int64 = 7200 +var ( + DefaultBranch = "master" + + // default build timeout, in seconds + DefaultTimeout int64 = 7200 +) const ( HostGitlab = "gitlab.com" @@ -65,3 +71,9 @@ func New(remote, owner, name string) (*Repo, error) { repo.Timeout = DefaultTimeout return &repo, nil } + +func (r *Repo) ParamMap() (map[string]string, error) { + out := map[string]string{} + err := yaml.Unmarshal([]byte(r.Params), out) + return out, err +} diff --git a/server/static/styles/drone.css b/server/static/styles/drone.css index 32e61af97..1ff8896f5 100644 --- a/server/static/styles/drone.css +++ b/server/static/styles/drone.css @@ -3317,22 +3317,6 @@ article.pure-g > .pure-u-3-4 { border: 1px solid #FEF0B8; padding-bottom: 40px; position: relative; - /* not sure if this makes sense here - &:before { - text-transform: uppercase; - content: attr(data-status); - position: absolute; - top: 0px; - right: 0px; - font-family: 'Open Sans'; - font-size:14px; - letter-spacing: 0; - background: #fef9e5; - color: #CBB45B; - padding: 5px 10px; - margin:0px; - } - */ } .commit-item[data-status="Started"]:after, .commit-item[data-status="Pending"]:after { diff --git a/shared/build/script/script.go b/shared/build/script/script.go index fb0c74156..fd4eca737 100644 --- a/shared/build/script/script.go +++ b/shared/build/script/script.go @@ -6,7 +6,7 @@ import ( "io/ioutil" "strings" - "launchpad.net/goyaml" + "gopkg.in/yaml.v1" "github.com/drone/drone/shared/build/buildfile" "github.com/drone/drone/shared/build/git" @@ -16,11 +16,11 @@ import ( "github.com/drone/drone/shared/publish" ) -func ParseBuild(data []byte, params map[string]string) (*Build, error) { +func ParseBuild(data string, params map[string]string) (*Build, error) { build := Build{} // parse the build configuration file - err := goyaml.Unmarshal(injectParams(data, params), &build) + err := yaml.Unmarshal(injectParams([]byte(data), params), &build) return &build, err } @@ -30,7 +30,7 @@ func ParseBuildFile(filename string) (*Build, error) { return nil, err } - return ParseBuild(data, nil) + return ParseBuild(string(data), nil) } // injectParams injects params into data.