2015-04-07 10:20:55 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-04-09 01:19:25 +02:00
|
|
|
"flag"
|
2015-04-26 01:43:51 +02:00
|
|
|
"html/template"
|
2015-04-26 01:16:56 +02:00
|
|
|
"net/http"
|
2015-04-07 10:20:55 +02:00
|
|
|
|
2015-04-09 01:19:25 +02:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
2015-05-17 20:45:09 +02:00
|
|
|
"github.com/drone/drone/pkg/remote/github"
|
2015-05-17 23:25:04 +02:00
|
|
|
"github.com/drone/drone/pkg/server"
|
|
|
|
"github.com/drone/drone/pkg/server/session"
|
|
|
|
"github.com/drone/drone/pkg/settings"
|
2015-04-26 01:16:56 +02:00
|
|
|
"github.com/elazarl/go-bindata-assetfs"
|
2015-04-24 23:25:03 +02:00
|
|
|
|
2015-05-19 05:44:44 +02:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2015-05-17 20:10:43 +02:00
|
|
|
eventbus "github.com/drone/drone/pkg/bus/builtin"
|
2015-05-17 20:42:56 +02:00
|
|
|
queue "github.com/drone/drone/pkg/queue/builtin"
|
2015-05-17 23:25:04 +02:00
|
|
|
runner "github.com/drone/drone/pkg/runner/builtin"
|
2015-05-17 20:10:43 +02:00
|
|
|
store "github.com/drone/drone/pkg/store/builtin"
|
2015-05-06 05:59:07 +02:00
|
|
|
|
|
|
|
_ "net/http/pprof"
|
2015-04-07 10:20:55 +02:00
|
|
|
)
|
|
|
|
|
2015-05-18 19:05:58 +02:00
|
|
|
var (
|
|
|
|
// commit sha for the current build, set by
|
|
|
|
// the compile process.
|
|
|
|
version string
|
|
|
|
revision string
|
|
|
|
)
|
|
|
|
|
2015-05-21 09:08:33 +02:00
|
|
|
var (
|
|
|
|
conf = flag.String("config", "drone.toml", "")
|
|
|
|
debug = flag.Bool("debug", false, "")
|
|
|
|
)
|
2015-04-07 10:20:55 +02:00
|
|
|
|
|
|
|
func main() {
|
2015-04-09 01:59:57 +02:00
|
|
|
flag.Parse()
|
2015-04-09 01:19:25 +02:00
|
|
|
|
2015-04-26 01:16:56 +02:00
|
|
|
settings, err := settings.Parse(*conf)
|
2015-04-09 01:19:25 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2015-05-05 10:04:20 +02:00
|
|
|
|
2015-05-11 09:45:31 +02:00
|
|
|
db := store.MustConnect(settings.Database.Driver, settings.Database.Datasource)
|
|
|
|
store := store.New(db)
|
|
|
|
defer db.Close()
|
2015-05-05 10:04:20 +02:00
|
|
|
|
2015-04-09 01:19:25 +02:00
|
|
|
remote := github.New(settings.Service)
|
|
|
|
session := session.New(settings.Session)
|
2015-05-05 10:04:20 +02:00
|
|
|
eventbus_ := eventbus.New()
|
|
|
|
queue_ := queue.New()
|
2015-05-10 05:46:32 +02:00
|
|
|
updater := runner.NewUpdater(eventbus_, store, remote)
|
2015-05-05 10:07:35 +02:00
|
|
|
runner_ := runner.Runner{Updater: updater}
|
2015-05-19 05:44:44 +02:00
|
|
|
|
|
|
|
// launch the local queue runner if the system
|
|
|
|
// is not conifugred to run in agent mode
|
|
|
|
if settings.Agents != nil && settings.Agents.Secret != "" {
|
|
|
|
log.Infof("Run builds using remote build agents")
|
|
|
|
} else {
|
|
|
|
log.Infof("Run builds using the embedded build runner")
|
|
|
|
go run(&runner_, queue_)
|
|
|
|
}
|
2015-04-09 01:19:25 +02:00
|
|
|
|
|
|
|
r := gin.Default()
|
|
|
|
|
|
|
|
api := r.Group("/api")
|
|
|
|
api.Use(server.SetHeaders())
|
2015-05-05 10:04:20 +02:00
|
|
|
api.Use(server.SetBus(eventbus_))
|
|
|
|
api.Use(server.SetDatastore(store))
|
2015-04-09 01:19:25 +02:00
|
|
|
api.Use(server.SetRemote(remote))
|
2015-05-05 10:04:20 +02:00
|
|
|
api.Use(server.SetQueue(queue_))
|
2015-04-09 01:19:25 +02:00
|
|
|
api.Use(server.SetSettings(settings))
|
2015-04-30 23:23:46 +02:00
|
|
|
api.Use(server.SetSession(session))
|
2015-04-09 01:19:25 +02:00
|
|
|
api.Use(server.SetUser(session))
|
2015-05-06 10:24:55 +02:00
|
|
|
api.Use(server.SetRunner(&runner_))
|
2015-04-09 01:19:25 +02:00
|
|
|
|
|
|
|
user := api.Group("/user")
|
|
|
|
{
|
|
|
|
user.Use(server.MustUser())
|
|
|
|
|
|
|
|
user.GET("", server.GetUserCurr)
|
2015-04-15 07:04:38 +02:00
|
|
|
user.PATCH("", server.PutUserCurr)
|
2015-04-09 01:19:25 +02:00
|
|
|
user.GET("/repos", server.GetUserRepos)
|
2015-04-13 10:22:51 +02:00
|
|
|
user.GET("/tokens", server.GetUserTokens)
|
|
|
|
user.POST("/tokens", server.PostToken)
|
2015-04-14 01:33:29 +02:00
|
|
|
user.DELETE("/tokens/:label", server.DelToken)
|
2015-04-09 01:19:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
users := api.Group("/users")
|
|
|
|
{
|
|
|
|
users.Use(server.MustAdmin())
|
|
|
|
|
|
|
|
users.GET("", server.GetUsers)
|
|
|
|
users.GET("/:name", server.GetUser)
|
|
|
|
users.POST("/:name", server.PostUser)
|
2015-04-15 07:04:38 +02:00
|
|
|
users.PATCH("/:name", server.PutUser)
|
2015-04-09 01:19:25 +02:00
|
|
|
users.DELETE("/:name", server.DeleteUser)
|
|
|
|
}
|
|
|
|
|
|
|
|
repos := api.Group("/repos/:owner/:name")
|
|
|
|
{
|
|
|
|
repos.POST("", server.PostRepo)
|
|
|
|
|
|
|
|
repo := repos.Group("")
|
|
|
|
{
|
|
|
|
repo.Use(server.SetRepo())
|
|
|
|
repo.Use(server.SetPerm())
|
|
|
|
repo.Use(server.CheckPull())
|
|
|
|
repo.Use(server.CheckPush())
|
|
|
|
|
|
|
|
repo.GET("", server.GetRepo)
|
2015-04-15 07:04:38 +02:00
|
|
|
repo.PATCH("", server.PutRepo)
|
2015-04-09 01:19:25 +02:00
|
|
|
repo.DELETE("", server.DeleteRepo)
|
2015-04-15 07:04:38 +02:00
|
|
|
repo.POST("/watch", server.Subscribe)
|
|
|
|
repo.DELETE("/unwatch", server.Unsubscribe)
|
|
|
|
|
2015-05-11 09:45:31 +02:00
|
|
|
repo.GET("/builds", server.GetCommits)
|
|
|
|
repo.GET("/builds/:number", server.GetCommit)
|
2015-04-17 07:07:13 +02:00
|
|
|
repo.POST("/builds/:number", server.RunBuild)
|
|
|
|
repo.DELETE("/builds/:number", server.KillBuild)
|
2015-05-11 09:45:31 +02:00
|
|
|
repo.GET("/logs/:number/:task", server.GetLogs)
|
|
|
|
// repo.POST("/status/:number", server.PostBuildStatus)
|
2015-04-09 01:19:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
badges := api.Group("/badges/:owner/:name")
|
|
|
|
{
|
|
|
|
badges.Use(server.SetRepo())
|
|
|
|
|
|
|
|
badges.GET("/status.svg", server.GetBadge)
|
|
|
|
badges.GET("/cc.xml", server.GetCC)
|
|
|
|
}
|
|
|
|
|
|
|
|
hooks := api.Group("/hook")
|
|
|
|
{
|
|
|
|
hooks.POST("", server.PostHook)
|
|
|
|
}
|
|
|
|
|
2015-05-19 00:47:13 +02:00
|
|
|
queue := api.Group("/queue")
|
|
|
|
{
|
|
|
|
queue.Use(server.MustAgent())
|
2015-05-19 05:44:44 +02:00
|
|
|
queue.Use(server.SetSettings(settings))
|
2015-05-19 00:47:13 +02:00
|
|
|
queue.Use(server.SetUpdater(updater))
|
|
|
|
queue.POST("/pull", server.PollBuild)
|
|
|
|
|
|
|
|
push := queue.Group("/push/:owner/:name")
|
|
|
|
{
|
|
|
|
push.Use(server.SetRepo())
|
|
|
|
push.POST("", server.PushCommit)
|
|
|
|
push.POST("/:commit", server.PushBuild)
|
|
|
|
push.POST("/:commit/:build/logs", server.PushLogs)
|
|
|
|
}
|
|
|
|
}
|
2015-04-24 23:25:03 +02:00
|
|
|
|
2015-05-06 05:26:57 +02:00
|
|
|
stream := api.Group("/stream")
|
2015-04-25 02:06:46 +02:00
|
|
|
{
|
2015-05-06 05:26:57 +02:00
|
|
|
stream.Use(server.SetRepo())
|
|
|
|
stream.Use(server.SetPerm())
|
|
|
|
stream.GET("/:owner/:name", server.GetRepoEvents)
|
|
|
|
stream.GET("/:owner/:name/:build/:number", server.GetStream)
|
2015-04-30 04:57:43 +02:00
|
|
|
|
2015-04-25 02:06:46 +02:00
|
|
|
}
|
|
|
|
|
2015-04-09 01:19:25 +02:00
|
|
|
auth := r.Group("/authorize")
|
|
|
|
{
|
|
|
|
auth.Use(server.SetHeaders())
|
2015-05-05 10:04:20 +02:00
|
|
|
auth.Use(server.SetDatastore(store))
|
2015-04-09 01:19:25 +02:00
|
|
|
auth.Use(server.SetRemote(remote))
|
|
|
|
auth.Use(server.SetSettings(settings))
|
|
|
|
auth.Use(server.SetSession(session))
|
|
|
|
auth.GET("", server.GetLogin)
|
|
|
|
auth.POST("", server.GetLogin)
|
|
|
|
}
|
|
|
|
|
2015-04-26 01:43:51 +02:00
|
|
|
r.SetHTMLTemplate(index())
|
2015-04-09 01:19:25 +02:00
|
|
|
r.NoRoute(func(c *gin.Context) {
|
2015-04-26 01:43:51 +02:00
|
|
|
c.HTML(200, "index.html", nil)
|
2015-04-09 01:19:25 +02:00
|
|
|
})
|
2015-04-26 01:16:56 +02:00
|
|
|
|
|
|
|
http.Handle("/static/", static())
|
|
|
|
http.Handle("/", r)
|
2015-05-19 23:26:35 +02:00
|
|
|
|
|
|
|
err = http.ListenAndServe(settings.Server.Addr, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Cannot start server: ", err)
|
|
|
|
}
|
2015-04-26 01:16:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// static is a helper function that will setup handlers
|
|
|
|
// for serving static files.
|
|
|
|
func static() http.Handler {
|
2015-05-21 09:08:33 +02:00
|
|
|
// default file server is embedded
|
|
|
|
var handler = http.FileServer(&assetfs.AssetFS{
|
2015-04-26 01:16:56 +02:00
|
|
|
Asset: Asset,
|
|
|
|
AssetDir: AssetDir,
|
2015-05-17 23:25:04 +02:00
|
|
|
Prefix: "cmd/drone-server/static",
|
2015-05-21 09:08:33 +02:00
|
|
|
})
|
|
|
|
if *debug {
|
|
|
|
handler = http.FileServer(
|
|
|
|
http.Dir("cmd/drone-server/static"),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return http.StripPrefix("/static/", handler)
|
2015-04-07 10:20:55 +02:00
|
|
|
}
|
2015-04-26 01:43:51 +02:00
|
|
|
|
|
|
|
// index is a helper function that will setup a template
|
|
|
|
// for rendering the main angular index.html file.
|
|
|
|
func index() *template.Template {
|
2015-05-17 23:25:04 +02:00
|
|
|
file := MustAsset("cmd/drone-server/static/index.html")
|
2015-04-26 01:43:51 +02:00
|
|
|
filestr := string(file)
|
|
|
|
return template.Must(template.New("index.html").Parse(filestr))
|
|
|
|
}
|
2015-05-05 10:04:20 +02:00
|
|
|
|
|
|
|
// run is a helper function for initializing the
|
|
|
|
// built-in build runner, if not running in remote
|
|
|
|
// mode.
|
|
|
|
func run(r *runner.Runner, q *queue.Queue) {
|
|
|
|
defer func() {
|
|
|
|
recover()
|
|
|
|
}()
|
|
|
|
r.Poll(q)
|
|
|
|
}
|