2015-04-07 08:20:55 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-04-25 23:43:51 +00:00
|
|
|
"html/template"
|
2015-04-25 23:16:56 +00:00
|
|
|
"net/http"
|
2015-04-07 08:20:55 +00:00
|
|
|
|
2015-08-06 16:22:31 +00:00
|
|
|
"github.com/drone/drone/Godeps/_workspace/src/github.com/namsral/flag"
|
2015-08-06 15:54:47 +00:00
|
|
|
|
2015-05-22 18:37:40 +00:00
|
|
|
"github.com/drone/drone/Godeps/_workspace/src/github.com/gin-gonic/gin"
|
2015-04-08 23:19:25 +00:00
|
|
|
|
2015-05-22 18:37:40 +00:00
|
|
|
"github.com/drone/drone/Godeps/_workspace/src/github.com/elazarl/go-bindata-assetfs"
|
2015-07-23 17:36:23 +00:00
|
|
|
"github.com/drone/drone/pkg/remote"
|
2015-05-17 21:25:04 +00:00
|
|
|
"github.com/drone/drone/pkg/server"
|
2015-04-24 21:25:03 +00:00
|
|
|
|
2015-05-22 18:37:40 +00:00
|
|
|
log "github.com/drone/drone/Godeps/_workspace/src/github.com/Sirupsen/logrus"
|
2015-05-17 18:10:43 +00:00
|
|
|
eventbus "github.com/drone/drone/pkg/bus/builtin"
|
2015-05-17 18:42:56 +00:00
|
|
|
queue "github.com/drone/drone/pkg/queue/builtin"
|
2015-05-17 21:25:04 +00:00
|
|
|
runner "github.com/drone/drone/pkg/runner/builtin"
|
2015-06-15 06:26:49 +00:00
|
|
|
"github.com/drone/drone/pkg/store"
|
|
|
|
|
2015-07-24 19:44:02 +00:00
|
|
|
_ "github.com/drone/drone/pkg/remote/builtin/github"
|
2015-07-26 12:32:49 +00:00
|
|
|
_ "github.com/drone/drone/pkg/remote/builtin/gitlab"
|
2015-06-15 06:26:49 +00:00
|
|
|
_ "github.com/drone/drone/pkg/store/builtin"
|
2015-04-07 08:20:55 +00:00
|
|
|
)
|
|
|
|
|
2015-05-18 17:05:58 +00:00
|
|
|
var (
|
|
|
|
// commit sha for the current build, set by
|
|
|
|
// the compile process.
|
|
|
|
version string
|
|
|
|
revision string
|
|
|
|
)
|
|
|
|
|
2015-08-06 15:54:47 +00:00
|
|
|
var conf = struct {
|
|
|
|
debug bool
|
|
|
|
|
|
|
|
server struct {
|
|
|
|
addr string
|
|
|
|
cert string
|
|
|
|
key string
|
|
|
|
}
|
|
|
|
|
|
|
|
docker struct {
|
|
|
|
host string
|
|
|
|
cert string
|
|
|
|
key string
|
|
|
|
ca string
|
|
|
|
}
|
|
|
|
|
|
|
|
remote struct {
|
|
|
|
driver string
|
|
|
|
config string
|
|
|
|
}
|
|
|
|
|
|
|
|
database struct {
|
|
|
|
driver string
|
|
|
|
config string
|
|
|
|
}
|
|
|
|
|
|
|
|
plugin struct {
|
|
|
|
filter string
|
|
|
|
}
|
|
|
|
}{}
|
2015-04-07 08:20:55 +00:00
|
|
|
|
|
|
|
func main() {
|
2015-08-06 15:54:47 +00:00
|
|
|
|
2015-08-26 08:49:08 +00:00
|
|
|
flag.StringVar(&conf.docker.host, "docker-host", "unix:///var/run/docker.sock", "")
|
2015-08-06 15:54:47 +00:00
|
|
|
flag.StringVar(&conf.docker.cert, "docker-cert", "", "")
|
|
|
|
flag.StringVar(&conf.docker.key, "docker-key", "", "")
|
|
|
|
flag.StringVar(&conf.docker.ca, "docker-ca", "", "")
|
|
|
|
flag.StringVar(&conf.server.addr, "server-addr", ":8080", "")
|
|
|
|
flag.StringVar(&conf.server.cert, "server-cert", "", "")
|
|
|
|
flag.StringVar(&conf.server.key, "server-key", "", "")
|
|
|
|
flag.StringVar(&conf.remote.driver, "remote-driver", "github", "")
|
|
|
|
flag.StringVar(&conf.remote.config, "remote-config", "https://github.com", "")
|
|
|
|
flag.StringVar(&conf.database.driver, "database-driver", "sqlite3", "")
|
|
|
|
flag.StringVar(&conf.database.config, "database-config", "drone.sqlite", "")
|
|
|
|
flag.StringVar(&conf.plugin.filter, "plugin-filter", "plugins/*", "")
|
|
|
|
flag.BoolVar(&conf.debug, "debug", false, "")
|
|
|
|
|
|
|
|
flag.String("config", "", "")
|
2015-04-08 23:59:57 +00:00
|
|
|
flag.Parse()
|
2015-04-08 23:19:25 +00:00
|
|
|
|
2015-08-06 15:54:47 +00:00
|
|
|
store, err := store.New(conf.database.driver, conf.database.config)
|
2015-06-15 06:26:49 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2015-05-05 08:04:20 +00:00
|
|
|
|
2015-08-06 15:54:47 +00:00
|
|
|
remote, err := remote.New(conf.remote.driver, conf.remote.config)
|
2015-07-23 17:36:23 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2015-08-04 18:20:37 +00:00
|
|
|
|
2015-05-05 08:04:20 +00:00
|
|
|
eventbus_ := eventbus.New()
|
|
|
|
queue_ := queue.New()
|
2015-05-10 03:46:32 +00:00
|
|
|
updater := runner.NewUpdater(eventbus_, store, remote)
|
2015-05-05 08:07:35 +00:00
|
|
|
runner_ := runner.Runner{Updater: updater}
|
2015-05-19 03:44:44 +00:00
|
|
|
|
|
|
|
// launch the local queue runner if the system
|
|
|
|
// is not conifugred to run in agent mode
|
2015-09-08 21:02:48 +00:00
|
|
|
go run(&runner_, queue_)
|
2015-04-08 23:19:25 +00:00
|
|
|
|
|
|
|
r := gin.Default()
|
|
|
|
|
|
|
|
api := r.Group("/api")
|
|
|
|
api.Use(server.SetHeaders())
|
2015-05-05 08:04:20 +00:00
|
|
|
api.Use(server.SetBus(eventbus_))
|
|
|
|
api.Use(server.SetDatastore(store))
|
2015-04-08 23:19:25 +00:00
|
|
|
api.Use(server.SetRemote(remote))
|
2015-05-05 08:04:20 +00:00
|
|
|
api.Use(server.SetQueue(queue_))
|
2015-09-09 21:05:52 +00:00
|
|
|
api.Use(server.SetUser())
|
2015-05-06 08:24:55 +00:00
|
|
|
api.Use(server.SetRunner(&runner_))
|
2015-06-23 18:35:21 +00:00
|
|
|
api.OPTIONS("/*path", func(c *gin.Context) {})
|
2015-04-08 23:19:25 +00:00
|
|
|
|
|
|
|
user := api.Group("/user")
|
|
|
|
{
|
|
|
|
user.Use(server.MustUser())
|
|
|
|
|
|
|
|
user.GET("", server.GetUserCurr)
|
2015-04-15 05:04:38 +00:00
|
|
|
user.PATCH("", server.PutUserCurr)
|
2015-08-18 23:09:27 +00:00
|
|
|
user.GET("/feed", server.GetUserFeed)
|
2015-04-08 23:19:25 +00:00
|
|
|
user.GET("/repos", server.GetUserRepos)
|
2015-09-09 21:05:52 +00:00
|
|
|
user.POST("/token", server.PostUserToken)
|
2015-04-08 23:19:25 +00: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 05:04:38 +00:00
|
|
|
users.PATCH("/:name", server.PutUser)
|
2015-04-08 23:19:25 +00: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 05:04:38 +00:00
|
|
|
repo.PATCH("", server.PutRepo)
|
2015-04-08 23:19:25 +00:00
|
|
|
repo.DELETE("", server.DeleteRepo)
|
2015-08-19 06:59:30 +00:00
|
|
|
repo.POST("/encrypt", server.Encrypt)
|
2015-04-15 05:04:38 +00:00
|
|
|
repo.POST("/watch", server.Subscribe)
|
|
|
|
repo.DELETE("/unwatch", server.Unsubscribe)
|
|
|
|
|
2015-08-29 22:18:45 +00:00
|
|
|
repo.GET("/builds", server.GetBuilds)
|
|
|
|
repo.GET("/builds/:number", server.GetBuild)
|
2015-04-17 05:07:13 +00:00
|
|
|
repo.POST("/builds/:number", server.RunBuild)
|
|
|
|
repo.DELETE("/builds/:number", server.KillBuild)
|
2015-05-11 07:45:31 +00:00
|
|
|
repo.GET("/logs/:number/:task", server.GetLogs)
|
|
|
|
// repo.POST("/status/:number", server.PostBuildStatus)
|
2015-04-08 23:19:25 +00: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-06 03:26:57 +00:00
|
|
|
stream := api.Group("/stream")
|
2015-04-25 00:06:46 +00:00
|
|
|
{
|
2015-05-06 03:26:57 +00: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 02:57:43 +00:00
|
|
|
|
2015-04-25 00:06:46 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 23:19:25 +00:00
|
|
|
auth := r.Group("/authorize")
|
|
|
|
{
|
|
|
|
auth.Use(server.SetHeaders())
|
2015-05-05 08:04:20 +00:00
|
|
|
auth.Use(server.SetDatastore(store))
|
2015-04-08 23:19:25 +00:00
|
|
|
auth.Use(server.SetRemote(remote))
|
|
|
|
auth.GET("", server.GetLogin)
|
|
|
|
auth.POST("", server.GetLogin)
|
|
|
|
}
|
|
|
|
|
2015-09-02 02:19:11 +00:00
|
|
|
gitlab := r.Group("/gitlab/:owner/:name")
|
2015-08-22 03:32:22 +00:00
|
|
|
{
|
2015-09-02 02:19:11 +00:00
|
|
|
gitlab.Use(server.SetDatastore(store))
|
|
|
|
gitlab.Use(server.SetRepo())
|
2015-08-22 03:32:22 +00:00
|
|
|
|
2015-09-02 02:19:11 +00:00
|
|
|
gitlab.GET("/commits/:sha", server.GetCommit)
|
|
|
|
gitlab.GET("/pulls/:number", server.GetPullRequest)
|
|
|
|
|
|
|
|
redirects := gitlab.Group("/redirect")
|
|
|
|
{
|
|
|
|
redirects.GET("/commits/:sha", server.RedirectSha)
|
|
|
|
redirects.GET("/pulls/:number", server.RedirectPullRequest)
|
|
|
|
}
|
2015-08-22 03:32:22 +00:00
|
|
|
}
|
|
|
|
|
2015-04-25 23:43:51 +00:00
|
|
|
r.SetHTMLTemplate(index())
|
2015-04-08 23:19:25 +00:00
|
|
|
r.NoRoute(func(c *gin.Context) {
|
2015-04-25 23:43:51 +00:00
|
|
|
c.HTML(200, "index.html", nil)
|
2015-04-08 23:19:25 +00:00
|
|
|
})
|
2015-04-25 23:16:56 +00:00
|
|
|
|
|
|
|
http.Handle("/static/", static())
|
|
|
|
http.Handle("/", r)
|
2015-05-19 21:26:35 +00:00
|
|
|
|
2015-09-08 21:19:39 +00:00
|
|
|
if len(conf.server.cert) == 0 {
|
|
|
|
err = http.ListenAndServe(conf.server.addr, nil)
|
|
|
|
} else {
|
|
|
|
err = http.ListenAndServeTLS(conf.server.addr, conf.server.cert, conf.server.key, nil)
|
|
|
|
}
|
2015-05-19 21:26:35 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Cannot start server: ", err)
|
|
|
|
}
|
2015-09-08 21:19:39 +00:00
|
|
|
|
2015-04-25 23:16:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// static is a helper function that will setup handlers
|
|
|
|
// for serving static files.
|
|
|
|
func static() http.Handler {
|
2015-05-21 07:08:33 +00:00
|
|
|
// default file server is embedded
|
|
|
|
var handler = http.FileServer(&assetfs.AssetFS{
|
2015-04-25 23:16:56 +00:00
|
|
|
Asset: Asset,
|
|
|
|
AssetDir: AssetDir,
|
2015-05-17 21:25:04 +00:00
|
|
|
Prefix: "cmd/drone-server/static",
|
2015-05-21 07:08:33 +00:00
|
|
|
})
|
2015-08-06 15:54:47 +00:00
|
|
|
if conf.debug {
|
2015-05-21 07:08:33 +00:00
|
|
|
handler = http.FileServer(
|
|
|
|
http.Dir("cmd/drone-server/static"),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return http.StripPrefix("/static/", handler)
|
2015-04-07 08:20:55 +00:00
|
|
|
}
|
2015-04-25 23:43:51 +00: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 21:25:04 +00:00
|
|
|
file := MustAsset("cmd/drone-server/static/index.html")
|
2015-04-25 23:43:51 +00:00
|
|
|
filestr := string(file)
|
|
|
|
return template.Must(template.New("index.html").Parse(filestr))
|
|
|
|
}
|
2015-05-05 08:04:20 +00: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)
|
|
|
|
}
|