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-09 22:47:35 +00:00
|
|
|
"runtime"
|
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-06-12 23:41:04 +00:00
|
|
|
"github.com/drone/drone/server/database"
|
2014-06-12 22:02:19 +00:00
|
|
|
"github.com/drone/drone/server/database/schema"
|
2014-06-04 21:25:38 +00:00
|
|
|
"github.com/drone/drone/server/handler"
|
2014-06-21 21:22:38 +00:00
|
|
|
"github.com/drone/drone/server/pubsub"
|
2014-06-04 21:25:38 +00:00
|
|
|
"github.com/drone/drone/server/session"
|
2014-06-21 21:22:38 +00:00
|
|
|
"github.com/drone/drone/server/worker"
|
2014-06-12 19:44:19 +00:00
|
|
|
"github.com/drone/drone/shared/build/log"
|
2014-06-21 21:22:38 +00:00
|
|
|
"github.com/drone/drone/shared/model"
|
2014-06-04 21:25:38 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/pat"
|
|
|
|
//"github.com/justinas/nosurf"
|
2014-06-12 00:42:49 +00:00
|
|
|
"github.com/GeertJohan/go.rice"
|
2014-06-04 21:25:38 +00:00
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
"github.com/russross/meddler"
|
2014-07-13 02:01:58 +00:00
|
|
|
|
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-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-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(&port, "port", ":8080", "")
|
|
|
|
flag.StringVar(&driver, "driver", "sqlite3", "")
|
|
|
|
flag.StringVar(&datasource, "datasource", "drone.sqlite", "")
|
|
|
|
flag.StringVar(&sslcert, "sslcert", "", "")
|
|
|
|
flag.StringVar(&sslkey, "sslkey", "", "")
|
2014-06-09 22:47:35 +00:00
|
|
|
flag.IntVar(&workers, "workers", runtime.NumCPU(), "")
|
2014-06-04 21:25:38 +00:00
|
|
|
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-06-04 21:25:38 +00:00
|
|
|
// setup the database
|
|
|
|
meddler.Default = meddler.SQLite
|
|
|
|
db, _ := sql.Open(driver, datasource)
|
2014-06-12 22:02:19 +00:00
|
|
|
schema.Load(db)
|
2014-06-04 21:25:38 +00:00
|
|
|
|
|
|
|
// setup the database managers
|
2014-06-12 23:41:04 +00:00
|
|
|
repos := database.NewRepoManager(db)
|
|
|
|
users := database.NewUserManager(db)
|
|
|
|
perms := database.NewPermManager(db)
|
|
|
|
commits := database.NewCommitManager(db)
|
2014-06-04 21:25:38 +00:00
|
|
|
|
2014-06-21 21:22:38 +00:00
|
|
|
// message broker
|
|
|
|
pubsub := pubsub.NewPubSub()
|
|
|
|
|
2014-06-11 19:39:29 +00:00
|
|
|
// cancel all previously running builds
|
|
|
|
go commits.CancelAll()
|
|
|
|
|
2014-07-13 02:01:58 +00:00
|
|
|
queue := make(chan *model.Request)
|
2014-07-14 00:15:09 +00:00
|
|
|
workerc := make(chan chan *model.Request)
|
|
|
|
worker.NewDispatch(queue, workerc).Start()
|
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
|
|
|
|
if nodes == nil || len(nodes) == 0 {
|
2014-07-14 00:15:09 +00:00
|
|
|
worker.NewWorker(workerc, users, repos, commits, pubsub, &model.Server{}).Start()
|
2014-09-07 21:55:37 +00:00
|
|
|
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-07-14 00:06:55 +00:00
|
|
|
}
|
2014-06-12 19:44:19 +00:00
|
|
|
|
2014-06-04 21:25:38 +00:00
|
|
|
// setup the session managers
|
|
|
|
sess := session.NewSession(users)
|
|
|
|
|
|
|
|
// setup the router and register routes
|
|
|
|
router := pat.New()
|
|
|
|
handler.NewUsersHandler(users, sess).Register(router)
|
|
|
|
handler.NewUserHandler(users, repos, commits, sess).Register(router)
|
2014-09-25 04:45:30 +00:00
|
|
|
handler.NewHookHandler(users, repos, commits, queue).Register(router)
|
2014-09-05 03:53:32 +00:00
|
|
|
handler.NewLoginHandler(users, repos, perms, sess, open).Register(router)
|
2014-08-14 19:49:45 +00:00
|
|
|
handler.NewCommitHandler(users, repos, commits, perms, sess, queue).Register(router)
|
2014-09-25 04:45:30 +00:00
|
|
|
handler.NewRepoHandler(repos, commits, perms, sess).Register(router)
|
2014-06-04 21:25:38 +00:00
|
|
|
handler.NewBadgeHandler(repos, commits).Register(router)
|
2014-06-21 21:22:38 +00:00
|
|
|
handler.NewWsHandler(repos, commits, perms, sess, pubsub).Register(router)
|
|
|
|
|
|
|
|
box := rice.MustFindBox("app/")
|
|
|
|
fserver := http.FileServer(box.HTTPBox())
|
|
|
|
index, _ := box.Bytes("index.html")
|
|
|
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(r.URL.Path, "/favicon.ico"),
|
|
|
|
strings.HasPrefix(r.URL.Path, "/scripts/"),
|
|
|
|
strings.HasPrefix(r.URL.Path, "/styles/"),
|
|
|
|
strings.HasPrefix(r.URL.Path, "/views/"):
|
2014-08-10 02:06:20 +00:00
|
|
|
// serve static conent
|
2014-06-21 21:22:38 +00:00
|
|
|
fserver.ServeHTTP(w, r)
|
|
|
|
case strings.HasPrefix(r.URL.Path, "/logout"),
|
|
|
|
strings.HasPrefix(r.URL.Path, "/login/"),
|
|
|
|
strings.HasPrefix(r.URL.Path, "/v1/"),
|
|
|
|
strings.HasPrefix(r.URL.Path, "/ws/"):
|
2014-08-10 02:06:20 +00:00
|
|
|
// standard header variables that should be set, for good measure.
|
|
|
|
w.Header().Add("Cache-Control", "no-cache, no-store, max-age=0, must-revalidate")
|
|
|
|
w.Header().Add("X-Frame-Options", "DENY")
|
|
|
|
w.Header().Add("X-Content-Type-Options", "nosniff")
|
|
|
|
w.Header().Add("X-XSS-Protection", "1; mode=block")
|
|
|
|
// serve dynamic content
|
2014-06-21 21:22:38 +00:00
|
|
|
router.ServeHTTP(w, r)
|
|
|
|
default:
|
|
|
|
w.Write(index)
|
|
|
|
}
|
|
|
|
})
|
2014-06-04 21:25:38 +00:00
|
|
|
|
|
|
|
// start webserver using HTTPS or HTTP
|
2014-06-21 21:22:38 +00:00
|
|
|
if len(sslcert) != 0 {
|
2014-06-04 21:25:38 +00:00
|
|
|
panic(http.ListenAndServeTLS(port, sslcert, sslkey, nil))
|
|
|
|
} else {
|
|
|
|
panic(http.ListenAndServe(port, nil))
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
}
|