2014-06-04 21:25:38 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"flag"
|
2014-09-07 21:55:37 +00:00
|
|
|
"fmt"
|
2014-06-04 21:25:38 +00:00
|
|
|
"net/http"
|
2014-06-21 21:22:38 +00:00
|
|
|
"strings"
|
2014-06-04 21:25:38 +00:00
|
|
|
|
2014-09-03 07:23:36 +00:00
|
|
|
"github.com/drone/config"
|
2014-09-29 01:36:24 +00:00
|
|
|
//"github.com/drone/drone/server/database"
|
2014-06-04 21:25:38 +00:00
|
|
|
"github.com/drone/drone/server/handler"
|
2014-09-29 01:36:24 +00:00
|
|
|
"github.com/drone/drone/server/middleware"
|
|
|
|
//"github.com/drone/drone/server/pubsub"
|
|
|
|
//"github.com/drone/drone/server/session"
|
|
|
|
//"github.com/drone/drone/server/worker"
|
2014-06-12 19:44:19 +00:00
|
|
|
"github.com/drone/drone/shared/build/log"
|
2014-09-29 01:36:24 +00:00
|
|
|
//"github.com/drone/drone/shared/model"
|
2014-06-04 21:25:38 +00:00
|
|
|
|
2014-09-29 01:36:24 +00:00
|
|
|
//"github.com/GeertJohan/go.rice"
|
2014-07-13 02:01:58 +00:00
|
|
|
|
2014-09-29 01:36:24 +00:00
|
|
|
"code.google.com/p/go.net/context"
|
|
|
|
webcontext "github.com/goji/context"
|
|
|
|
"github.com/zenazn/goji"
|
|
|
|
"github.com/zenazn/goji/web"
|
|
|
|
|
|
|
|
_ "github.com/drone/drone/plugin/notify/email"
|
2014-09-03 07:23:36 +00:00
|
|
|
"github.com/drone/drone/plugin/remote/bitbucket"
|
|
|
|
"github.com/drone/drone/plugin/remote/github"
|
|
|
|
"github.com/drone/drone/plugin/remote/gitlab"
|
2014-09-29 01:36:24 +00:00
|
|
|
"github.com/drone/drone/server/blobstore"
|
|
|
|
"github.com/drone/drone/server/datastore"
|
|
|
|
"github.com/drone/drone/server/datastore/database"
|
2014-06-04 21:25:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// port the server will run on
|
|
|
|
port string
|
|
|
|
|
|
|
|
// database driver used to connect to the database
|
|
|
|
driver string
|
|
|
|
|
|
|
|
// driver specific connection information. In this
|
|
|
|
// case, it should be the location of the SQLite file
|
|
|
|
datasource string
|
|
|
|
|
|
|
|
// optional flags for tls listener
|
|
|
|
sslcert string
|
|
|
|
sslkey string
|
|
|
|
|
|
|
|
// commit sha for the current build.
|
2014-07-14 00:06:55 +00:00
|
|
|
version string = "0.3-dev"
|
2014-06-04 21:25:38 +00:00
|
|
|
revision string
|
2014-06-09 22:47:35 +00:00
|
|
|
|
|
|
|
// Number of concurrent build workers to run
|
|
|
|
// default to number of CPUs on machine
|
|
|
|
workers int
|
2014-09-03 07:23:36 +00:00
|
|
|
|
|
|
|
conf string
|
|
|
|
prefix string
|
2014-09-05 03:53:32 +00:00
|
|
|
|
|
|
|
open bool
|
2014-09-07 21:55:37 +00:00
|
|
|
|
|
|
|
nodes StringArr
|
2014-09-29 01:36:24 +00:00
|
|
|
|
|
|
|
db *sql.DB
|
2014-06-04 21:25:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2014-06-12 19:44:19 +00:00
|
|
|
log.SetPriority(log.LOG_NOTICE)
|
|
|
|
|
2014-09-03 07:23:36 +00:00
|
|
|
flag.StringVar(&conf, "config", "", "")
|
|
|
|
flag.StringVar(&prefix, "prefix", "DRONE_", "")
|
2014-06-04 21:25:38 +00:00
|
|
|
flag.StringVar(&driver, "driver", "sqlite3", "")
|
|
|
|
flag.StringVar(&datasource, "datasource", "drone.sqlite", "")
|
|
|
|
flag.Parse()
|
|
|
|
|
2014-09-07 21:55:37 +00:00
|
|
|
config.Var(&nodes, "worker-nodes")
|
2014-09-05 03:53:32 +00:00
|
|
|
config.BoolVar(&open, "registration-open", false)
|
2014-09-03 07:23:36 +00:00
|
|
|
config.SetPrefix(prefix)
|
2014-09-07 21:55:37 +00:00
|
|
|
if err := config.Parse(conf); err != nil {
|
|
|
|
fmt.Println("Error parsing config", err)
|
|
|
|
}
|
2014-09-03 07:23:36 +00:00
|
|
|
|
|
|
|
// setup the remote services
|
|
|
|
bitbucket.Register()
|
|
|
|
github.Register()
|
|
|
|
gitlab.Register()
|
|
|
|
|
2014-09-29 01:36:24 +00:00
|
|
|
// setup the database and cancel all pending
|
|
|
|
// commits in the system.
|
|
|
|
db = database.MustConnect(driver, datasource)
|
|
|
|
go database.NewCommitstore(db).KillCommits()
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
repos := web.New()
|
|
|
|
repos.Use(middleware.SetRepo)
|
|
|
|
repos.Use(middleware.RequireRepoRead)
|
|
|
|
repos.Use(middleware.RequireRepoAdmin)
|
|
|
|
repos.Get("/api/repos/:host/:owner/:name/branches/:branch/commits/:commit/console", handler.GetOutput)
|
|
|
|
repos.Get("/api/repos/:host/:owner/:name/branches/:branch/commits/:commit", handler.GetCommit)
|
|
|
|
repos.Post("/api/repos/:host/:owner/:name/branches/:branch/commits/:commit", handler.PostCommit)
|
|
|
|
repos.Get("/api/repos/:host/:owner/:name/commits", handler.GetCommitList)
|
|
|
|
repos.Get("/api/repos/:host/:owner/:name", handler.GetRepo)
|
|
|
|
repos.Put("/api/repos/:host/:owner/:name", handler.PutRepo)
|
|
|
|
repos.Post("/api/repos/:host/:owner/:name", handler.PostRepo)
|
|
|
|
repos.Delete("/api/repos/:host/:owner/:name", handler.DelRepo)
|
|
|
|
goji.Handle("/api/repos/:host/:owner/:name*", repos)
|
|
|
|
|
|
|
|
users := web.New()
|
|
|
|
users.Use(middleware.RequireUserAdmin)
|
|
|
|
users.Get("/api/users/:host/:login", handler.GetUser)
|
|
|
|
users.Post("/api/users/:host/:login", handler.PostUser)
|
|
|
|
users.Delete("/api/users/:host/:login", handler.DelUser)
|
|
|
|
users.Get("/api/users", handler.GetUserList)
|
|
|
|
goji.Handle("/api/users*", users)
|
|
|
|
|
|
|
|
user := web.New()
|
|
|
|
user.Use(middleware.RequireUser)
|
|
|
|
user.Get("/api/user/feed", handler.GetUserFeed)
|
|
|
|
user.Get("/api/user/repos", handler.GetUserRepos)
|
|
|
|
user.Get("/api/user", handler.GetUserCurrent)
|
|
|
|
user.Put("/api/user", handler.PutUser)
|
|
|
|
goji.Handle("/api/user*", user)
|
|
|
|
|
|
|
|
// Add middleware and serve
|
|
|
|
goji.Use(ContextMiddleware)
|
|
|
|
goji.Use(middleware.SetHeaders)
|
|
|
|
goji.Use(middleware.SetUser)
|
|
|
|
goji.Serve()
|
2014-07-14 00:06:55 +00:00
|
|
|
|
2014-09-07 21:55:37 +00:00
|
|
|
// if no worker nodes are specified than start 2 workers
|
|
|
|
// using the default DOCKER_HOST
|
2014-09-29 01:36:24 +00:00
|
|
|
/*
|
|
|
|
if nodes == nil || len(nodes) == 0 {
|
|
|
|
worker.NewWorker(workerc, users, repos, commits, pubsub, &model.Server{}).Start()
|
|
|
|
worker.NewWorker(workerc, users, repos, commits, pubsub, &model.Server{}).Start()
|
|
|
|
} else {
|
|
|
|
for _, node := range nodes {
|
|
|
|
println(node)
|
|
|
|
worker.NewWorker(workerc, users, repos, commits, pubsub, &model.Server{Host: node}).Start()
|
|
|
|
}
|
2014-09-07 21:55:37 +00:00
|
|
|
}
|
2014-09-29 01:36:24 +00:00
|
|
|
*/
|
2014-06-04 21:25:38 +00:00
|
|
|
|
|
|
|
// start webserver using HTTPS or HTTP
|
2014-09-29 01:36:24 +00:00
|
|
|
//if len(sslcert) != 0 {
|
|
|
|
// panic(http.ListenAndServeTLS(port, sslcert, sslkey, nil))
|
|
|
|
//} else {
|
|
|
|
//panic(http.ListenAndServe(port, nil))
|
|
|
|
//}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ContextMiddleware creates a new go.net/context and
|
|
|
|
// injects into the current goji context.
|
|
|
|
func ContextMiddleware(c *web.C, h http.Handler) http.Handler {
|
|
|
|
fn := func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var ctx = context.Background()
|
|
|
|
ctx = datastore.NewContext(ctx, database.NewDatastore(db))
|
|
|
|
ctx = blobstore.NewContext(ctx, database.NewBlobstore(db))
|
|
|
|
//ctx = pool.NewContext(ctx, workers)
|
|
|
|
//ctx = director.NewContext(ctx, worker)
|
|
|
|
|
|
|
|
// add the context to the goji web context
|
|
|
|
webcontext.Set(c, ctx)
|
|
|
|
h.ServeHTTP(w, r)
|
2014-06-04 21:25:38 +00:00
|
|
|
}
|
2014-09-29 01:36:24 +00:00
|
|
|
return http.HandlerFunc(fn)
|
2014-06-04 21:25:38 +00:00
|
|
|
}
|
2014-09-07 21:55:37 +00:00
|
|
|
|
|
|
|
type StringArr []string
|
|
|
|
|
|
|
|
func (s *StringArr) String() string {
|
|
|
|
return fmt.Sprint(*s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StringArr) Set(value string) error {
|
|
|
|
for _, str := range strings.Split(value, ",") {
|
|
|
|
str = strings.TrimSpace(str)
|
|
|
|
*s = append(*s, str)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|