2014-06-04 21:25:38 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"flag"
|
2014-06-13 00:17:59 +00:00
|
|
|
"io/ioutil"
|
2014-06-04 21:25:38 +00:00
|
|
|
"net/http"
|
2014-06-13 00:17:59 +00:00
|
|
|
"os"
|
|
|
|
"os/user"
|
|
|
|
"path/filepath"
|
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-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"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2014-06-13 00:17:59 +00:00
|
|
|
// home directory for the application.
|
|
|
|
home string
|
|
|
|
|
2014-06-04 21:25:38 +00:00
|
|
|
// 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.
|
|
|
|
version string = "0.2-dev"
|
|
|
|
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-06-04 21:25:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
2014-06-12 19:44:19 +00:00
|
|
|
log.SetPriority(log.LOG_NOTICE)
|
|
|
|
|
2014-06-04 21:25:38 +00:00
|
|
|
// parse command line flags
|
|
|
|
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()
|
|
|
|
|
|
|
|
// 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-21 21:22:38 +00:00
|
|
|
servers := database.NewServerManager(db)
|
|
|
|
remotes := database.NewRemoteManager(db)
|
2014-06-13 00:17:59 +00:00
|
|
|
configs := database.NewConfigManager(filepath.Join(home, "config.toml"))
|
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-06-12 19:44:19 +00:00
|
|
|
// setup the build queue
|
2014-06-21 21:22:38 +00:00
|
|
|
//queueRunner := queue.NewBuildRunner(docker.New(), timeout)
|
|
|
|
//queue := queue.Start(work ers, commits, queueRunner)
|
|
|
|
|
|
|
|
queue := make(chan *worker.Request)
|
|
|
|
workers := make(chan chan *worker.Request)
|
|
|
|
worker.NewDispatch(queue, workers).Start()
|
|
|
|
worker.NewWorker(workers, users, repos, commits, configs, pubsub, &model.Server{Host: "unix:///tmp/sock.sock"}).Start()
|
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-06-13 00:17:59 +00:00
|
|
|
handler.NewHookHandler(users, repos, commits, configs, queue).Register(router)
|
|
|
|
handler.NewLoginHandler(users, repos, perms, sess, configs).Register(router)
|
2014-06-12 00:42:49 +00:00
|
|
|
handler.NewCommitHandler(repos, commits, perms, sess, queue).Register(router)
|
2014-06-04 21:25:38 +00:00
|
|
|
handler.NewBranchHandler(repos, commits, perms, sess).Register(router)
|
2014-06-13 00:17:59 +00:00
|
|
|
handler.NewRepoHandler(repos, commits, perms, sess, configs).Register(router)
|
2014-06-04 21:25:38 +00:00
|
|
|
handler.NewBadgeHandler(repos, commits).Register(router)
|
2014-06-13 00:17:59 +00:00
|
|
|
handler.NewConfigHandler(configs, sess).Register(router)
|
2014-06-21 21:22:38 +00:00
|
|
|
handler.NewServerHandler(servers, sess).Register(router)
|
|
|
|
handler.NewRemoteHandler(users, remotes, sess).Register(router)
|
|
|
|
handler.NewWsHandler(repos, commits, perms, sess, pubsub).Register(router)
|
|
|
|
//handler.NewSiteHandler(users, repos, commits, perms, sess, templ.ExecuteTemplate).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/"):
|
|
|
|
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/"):
|
|
|
|
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-06-13 00:17:59 +00:00
|
|
|
|
2014-06-21 21:22:38 +00:00
|
|
|
func setupDatabase() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupQueue() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupHandlers() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-06-13 00:17:59 +00:00
|
|
|
// initialize the .drone directory and create a skeleton config
|
|
|
|
// file if one does not already exist.
|
|
|
|
func init() {
|
|
|
|
// load the current user
|
|
|
|
u, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// set .drone home dir
|
|
|
|
home = filepath.Join(u.HomeDir, ".drone")
|
|
|
|
|
|
|
|
// create the .drone home directory
|
|
|
|
os.MkdirAll(home, 0777)
|
|
|
|
|
|
|
|
// check for the config file
|
|
|
|
filename := filepath.Join(u.HomeDir, ".drone", "config.toml")
|
|
|
|
if _, err := os.Stat(filename); err != nil {
|
|
|
|
// if not exists, create
|
|
|
|
ioutil.WriteFile(filename, []byte(defaultConfig), 0777)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var defaultConfig = `
|
|
|
|
# Enables user self-registration. If false, the system administrator
|
|
|
|
# will need to manually add users to the system.
|
|
|
|
registration = true
|
|
|
|
|
|
|
|
[smtp]
|
|
|
|
host = ""
|
|
|
|
port = ""
|
|
|
|
from = ""
|
|
|
|
username = ""
|
|
|
|
password = ""
|
|
|
|
|
|
|
|
[bitbucket]
|
|
|
|
url = "https://bitbucket.org"
|
|
|
|
api = "https://bitbucket.org"
|
|
|
|
client = ""
|
|
|
|
secret = ""
|
|
|
|
enabled = false
|
|
|
|
|
|
|
|
[github]
|
|
|
|
url = "https://github.com"
|
|
|
|
api = "https://api.github.com"
|
|
|
|
client = ""
|
|
|
|
secret = ""
|
|
|
|
enabled = false
|
|
|
|
|
|
|
|
[githubenterprise]
|
|
|
|
url = ""
|
|
|
|
api = ""
|
|
|
|
client = ""
|
|
|
|
secret = ""
|
|
|
|
enabled = false
|
|
|
|
|
|
|
|
[gitlab]
|
|
|
|
url = ""
|
|
|
|
api = ""
|
|
|
|
client = ""
|
|
|
|
secret = ""
|
|
|
|
enabled = false
|
|
|
|
|
|
|
|
[stash]
|
|
|
|
url = ""
|
|
|
|
api = ""
|
|
|
|
client = ""
|
|
|
|
secret = ""
|
|
|
|
enabled = false
|
|
|
|
`
|