From 58f287a54124ed104d5591d8218749eb04c16821 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Mon, 29 Sep 2014 21:34:30 -0700 Subject: [PATCH] added worker handlers --- server/handler/worker.go | 57 ++++++++++++++++++++++++++++++++++ server/handler/ws.go | 4 +-- server/main.go | 14 +++++++-- server/worker/docker/docker.go | 30 ++++++++++++------ shared/model/request.go | 1 - shared/model/server.go | 19 ------------ 6 files changed, 90 insertions(+), 35 deletions(-) create mode 100644 server/handler/worker.go delete mode 100644 shared/model/server.go diff --git a/server/handler/worker.go b/server/handler/worker.go new file mode 100644 index 000000000..56fbe6f1c --- /dev/null +++ b/server/handler/worker.go @@ -0,0 +1,57 @@ +package handler + +import ( + "encoding/json" + "net/http" + + "github.com/drone/drone-dart/worker" + "github.com/drone/drone-dart/worker/director" + "github.com/drone/drone-dart/worker/pool" + "github.com/goji/context" + "github.com/zenazn/goji/web" +) + +// GetWorkers accepts a request to retrieve the list +// of registered workers and return the results +// in JSON format. +// +// GET /api/workers +// +func GetWorkers(c web.C, w http.ResponseWriter, r *http.Request) { + ctx := context.FromC(c) + workers := pool.FromContext(ctx).List() + json.NewEncoder(w).Encode(workers) +} + +// GetWorkPending accepts a request to retrieve the list +// of pending work and returns in JSON format. +// +// GET /api/work/pending +// +func GetWorkPending(c web.C, w http.ResponseWriter, r *http.Request) { + ctx := context.FromC(c) + d := worker.FromContext(ctx).(*director.Director) + json.NewEncoder(w).Encode(d.GetPending()) +} + +// GetWorkStarted accepts a request to retrieve the list +// of started work and returns in JSON format. +// +// GET /api/work/started +// +func GetWorkStarted(c web.C, w http.ResponseWriter, r *http.Request) { + ctx := context.FromC(c) + d := worker.FromContext(ctx).(*director.Director) + json.NewEncoder(w).Encode(d.GetStarted()) +} + +// GetWorkAssigned accepts a request to retrieve the list +// of started work and returns in JSON format. +// +// GET /api/work/assignments +// +func GetWorkAssigned(c web.C, w http.ResponseWriter, r *http.Request) { + ctx := context.FromC(c) + d := worker.FromContext(ctx).(*director.Director) + json.NewEncoder(w).Encode(d.GetAssignemnts()) +} diff --git a/server/handler/ws.go b/server/handler/ws.go index f5179b949..027d522e6 100644 --- a/server/handler/ws.go +++ b/server/handler/ws.go @@ -8,7 +8,7 @@ import ( "github.com/drone/drone/server/datastore" "github.com/drone/drone/server/pubsub" - "github.com/drone/drone/shared/model" + "github.com/drone/drone/server/worker" "github.com/goji/context" "github.com/gorilla/websocket" @@ -60,7 +60,7 @@ func WsUser(c web.C, w http.ResponseWriter, r *http.Request) { for { select { case msg := <-sub.Read(): - work, ok := msg.(*model.Request) + work, ok := msg.(*worker.Work) if !ok { break } diff --git a/server/main.go b/server/main.go index 4c65d3db9..3c158cabb 100644 --- a/server/main.go +++ b/server/main.go @@ -122,9 +122,9 @@ func main() { goji.Get("/api/auth/:host", handler.GetLogin) goji.Get("/api/badge/:host/:owner/:name/status.svg", handler.GetBadge) goji.Get("/api/badge/:host/:owner/:name/cc.xml", handler.GetCC) - //goji.Get("/api/hook", handler.PostHook) - //goji.Put("/api/hook", handler.PostHook) - //goji.Post("/api/hook", handler.PostHook) + goji.Get("/api/hook/:hook", handler.PostHook) + goji.Put("/api/hook/:hook", handler.PostHook) + goji.Post("/api/hook/:hook", handler.PostHook) repos := web.New() repos.Use(middleware.SetRepo) @@ -156,6 +156,14 @@ func main() { user.Put("/api/user", handler.PutUser) goji.Handle("/api/user*", user) + work := web.New() + work.Use(middleware.RequireUserAdmin) + work.Get("/api/work/started", handler.GetWorkStarted) + work.Get("/api/work/pending", handler.GetWorkPending) + work.Get("/api/work/assignments", handler.GetWorkAssigned) + work.Get("/api/workers", handler.GetWorkers) + goji.Handle("/api/work*", work) + // Add middleware and serve goji.Use(ContextMiddleware) goji.Use(middleware.SetHeaders) diff --git a/server/worker/docker/docker.go b/server/worker/docker/docker.go index 02168e642..b37f7323a 100644 --- a/server/worker/docker/docker.go +++ b/server/worker/docker/docker.go @@ -1,7 +1,6 @@ package docker import ( - "bytes" "log" "path/filepath" "runtime/debug" @@ -12,6 +11,7 @@ import ( "github.com/drone/drone/plugin/notify" "github.com/drone/drone/server/blobstore" "github.com/drone/drone/server/datastore" + "github.com/drone/drone/server/pubsub" "github.com/drone/drone/server/worker" "github.com/drone/drone/shared/build" "github.com/drone/drone/shared/build/docker" @@ -57,14 +57,14 @@ func (d *Docker) Do(c context.Context, r *worker.Work) { datastore.PutCommit(c, r.Commit) // notify all listeners that the build is started - //commitc := w.pubsub.Register("_global") - //commitc.Publish(r) - //stdoutc := w.pubsub.RegisterOpts(r.Commit.ID, pubsub.ConsoleOpts) - //defer stdoutc.Close() + commitc := pubsub.Register(c, "_global") + commitc.Publish(r) + stdoutc := pubsub.RegisterOpts(c, r.Commit.ID, pubsub.ConsoleOpts) + defer stdoutc.Close() // create a special buffer that will also // write to a websocket channel - var buf bytes.Buffer //:= pubsub.NewBuffer(stdoutc) + 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. @@ -102,13 +102,18 @@ func (d *Docker) Do(c context.Context, r *worker.Work) { if script.Notifications == nil { script.Notifications = ¬ify.Notification{} } - //script.Notifications.Send(r) + script.Notifications.Send(&model.Request{ + User: r.User, + Repo: r.Repo, + Commit: r.Commit, + Host: r.Host, + }) // create an instance of the Docker builder builder := build.New(d.docker) builder.Build = script builder.Repo = repo - builder.Stdout = &buf + builder.Stdout = buf builder.Key = []byte(r.Repo.PrivateKey) builder.Timeout = time.Duration(r.Repo.Timeout) * time.Second builder.Privileged = r.Repo.Privileged @@ -139,8 +144,13 @@ func (d *Docker) Do(c context.Context, r *worker.Work) { blobstore.Put(c, filepath.Join(r.Repo.Host, r.Repo.Owner, r.Repo.Name, r.Commit.Branch, r.Commit.Sha), buf.Bytes()) // notify all listeners that the build is finished - //commitc.Publish(r) + commitc.Publish(r) // send all "finished" notifications - //script.Notifications.Send(r) + script.Notifications.Send(&model.Request{ + User: r.User, + Repo: r.Repo, + Commit: r.Commit, + Host: r.Host, + }) } diff --git a/shared/model/request.go b/shared/model/request.go index ca3ec3fbc..0bf752513 100644 --- a/shared/model/request.go +++ b/shared/model/request.go @@ -5,5 +5,4 @@ type Request struct { User *User `json:"-"` Repo *Repo `json:"repo"` Commit *Commit `json:"commit"` - Server *Server `json:"-"` } diff --git a/shared/model/server.go b/shared/model/server.go deleted file mode 100644 index 07ff84d79..000000000 --- a/shared/model/server.go +++ /dev/null @@ -1,19 +0,0 @@ -package model - -type Server struct { - ID int64 `meddler:"server_id,pk" json:"id"` - Name string `meddler:"server_name" json:"name"` - Host string `meddler:"server_host" json:"host"` - User string `meddler:"server_user" json:"user"` - Pass string `meddler:"server_pass" json:"name"` - Cert string `meddler:"server_cert" json:"cert"` -} - -type SMTPServer struct { - ID int64 `meddler:"smtp_id,pk" json:"id"` - From string `meddler:"smtp_from" json:"from"` - Host string `meddler:"smtp_host" json:"host"` - Port string `meddler:"smtp_port" json:"port"` - User string `meddler:"smtp_user" json:"user"` - Pass string `meddler:"smtp_pass" json:"name"` -}