added worker handlers

This commit is contained in:
Brad Rydzewski 2014-09-29 21:34:30 -07:00
parent 99fef2b4ea
commit 58f287a541
6 changed files with 90 additions and 35 deletions

57
server/handler/worker.go Normal file
View file

@ -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())
}

View file

@ -8,7 +8,7 @@ import (
"github.com/drone/drone/server/datastore" "github.com/drone/drone/server/datastore"
"github.com/drone/drone/server/pubsub" "github.com/drone/drone/server/pubsub"
"github.com/drone/drone/shared/model" "github.com/drone/drone/server/worker"
"github.com/goji/context" "github.com/goji/context"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
@ -60,7 +60,7 @@ func WsUser(c web.C, w http.ResponseWriter, r *http.Request) {
for { for {
select { select {
case msg := <-sub.Read(): case msg := <-sub.Read():
work, ok := msg.(*model.Request) work, ok := msg.(*worker.Work)
if !ok { if !ok {
break break
} }

View file

@ -122,9 +122,9 @@ func main() {
goji.Get("/api/auth/:host", handler.GetLogin) 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/status.svg", handler.GetBadge)
goji.Get("/api/badge/:host/:owner/:name/cc.xml", handler.GetCC) goji.Get("/api/badge/:host/:owner/:name/cc.xml", handler.GetCC)
//goji.Get("/api/hook", handler.PostHook) goji.Get("/api/hook/:hook", handler.PostHook)
//goji.Put("/api/hook", handler.PostHook) goji.Put("/api/hook/:hook", handler.PostHook)
//goji.Post("/api/hook", handler.PostHook) goji.Post("/api/hook/:hook", handler.PostHook)
repos := web.New() repos := web.New()
repos.Use(middleware.SetRepo) repos.Use(middleware.SetRepo)
@ -156,6 +156,14 @@ func main() {
user.Put("/api/user", handler.PutUser) user.Put("/api/user", handler.PutUser)
goji.Handle("/api/user*", user) 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 // Add middleware and serve
goji.Use(ContextMiddleware) goji.Use(ContextMiddleware)
goji.Use(middleware.SetHeaders) goji.Use(middleware.SetHeaders)

View file

@ -1,7 +1,6 @@
package docker package docker
import ( import (
"bytes"
"log" "log"
"path/filepath" "path/filepath"
"runtime/debug" "runtime/debug"
@ -12,6 +11,7 @@ import (
"github.com/drone/drone/plugin/notify" "github.com/drone/drone/plugin/notify"
"github.com/drone/drone/server/blobstore" "github.com/drone/drone/server/blobstore"
"github.com/drone/drone/server/datastore" "github.com/drone/drone/server/datastore"
"github.com/drone/drone/server/pubsub"
"github.com/drone/drone/server/worker" "github.com/drone/drone/server/worker"
"github.com/drone/drone/shared/build" "github.com/drone/drone/shared/build"
"github.com/drone/drone/shared/build/docker" "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) datastore.PutCommit(c, r.Commit)
// notify all listeners that the build is started // notify all listeners that the build is started
//commitc := w.pubsub.Register("_global") commitc := pubsub.Register(c, "_global")
//commitc.Publish(r) commitc.Publish(r)
//stdoutc := w.pubsub.RegisterOpts(r.Commit.ID, pubsub.ConsoleOpts) stdoutc := pubsub.RegisterOpts(c, r.Commit.ID, pubsub.ConsoleOpts)
//defer stdoutc.Close() defer stdoutc.Close()
// create a special buffer that will also // create a special buffer that will also
// write to a websocket channel // 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 // parse the parameters and build script. The script has already
// been parsed in the hook, so we can be confident it will succeed. // 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 { if script.Notifications == nil {
script.Notifications = &notify.Notification{} script.Notifications = &notify.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 // create an instance of the Docker builder
builder := build.New(d.docker) builder := build.New(d.docker)
builder.Build = script builder.Build = script
builder.Repo = repo builder.Repo = repo
builder.Stdout = &buf builder.Stdout = buf
builder.Key = []byte(r.Repo.PrivateKey) builder.Key = []byte(r.Repo.PrivateKey)
builder.Timeout = time.Duration(r.Repo.Timeout) * time.Second builder.Timeout = time.Duration(r.Repo.Timeout) * time.Second
builder.Privileged = r.Repo.Privileged 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()) 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 // notify all listeners that the build is finished
//commitc.Publish(r) commitc.Publish(r)
// send all "finished" notifications // 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,
})
} }

View file

@ -5,5 +5,4 @@ type Request struct {
User *User `json:"-"` User *User `json:"-"`
Repo *Repo `json:"repo"` Repo *Repo `json:"repo"`
Commit *Commit `json:"commit"` Commit *Commit `json:"commit"`
Server *Server `json:"-"`
} }

View file

@ -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"`
}