From 0b98eb41866724cc3fd0c911b759a6930be72752 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Thu, 6 Aug 2015 08:54:47 -0700 Subject: [PATCH] improving database & remote setup --- cmd/drone-server/drone.go | 66 ++++++++++++++++++++++++++--- pkg/remote/builtin/github/github.go | 3 +- pkg/remote/remote.go | 34 +++++++-------- pkg/store/builtin/store.go | 12 +----- pkg/store/store.go | 19 ++++----- 5 files changed, 87 insertions(+), 47 deletions(-) diff --git a/cmd/drone-server/drone.go b/cmd/drone-server/drone.go index 7ab426d27..2c2a98a35 100644 --- a/cmd/drone-server/drone.go +++ b/cmd/drone-server/drone.go @@ -1,10 +1,12 @@ package main import ( - "flag" + "fmt" "html/template" "net/http" + "github.com/namsral/flag" + "github.com/drone/drone/Godeps/_workspace/src/github.com/gin-gonic/gin" "github.com/drone/drone/Godeps/_workspace/src/github.com/elazarl/go-bindata-assetfs" @@ -33,11 +35,61 @@ var ( revision string ) -var ( - debug = flag.Bool("debug", false, "") -) +var conf = struct { + debug bool + + server struct { + addr string + cert string + key string + } + + session struct { + expiry string + secret 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 + } +}{} func main() { + + flag.StringVar(&conf.docker.host, "docker-host", "unix:///var/run/docker/docker.sock", "") + 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.session.expiry, "session-expiry", "", "") + flag.StringVar(&conf.session.secret, "session-secret", "", "") + 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", "", "") flag.Parse() settings, err := config.Load("") @@ -45,12 +97,12 @@ func main() { panic(err) } - store, err := store.New(settings.Database.Driver + "://" + settings.Database.Datasource) + store, err := store.New(conf.database.driver, conf.database.config) if err != nil { panic(err) } - remote, err := remote.New(settings.Remote.Driver, settings) + remote, err := remote.New(conf.remote.driver, conf.remote.config) if err != nil { panic(err) } @@ -204,7 +256,7 @@ func static() http.Handler { AssetDir: AssetDir, Prefix: "cmd/drone-server/static", }) - if *debug { + if conf.debug { handler = http.FileServer( http.Dir("cmd/drone-server/static"), ) diff --git a/pkg/remote/builtin/github/github.go b/pkg/remote/builtin/github/github.go index cb5b7901f..e08e8351a 100644 --- a/pkg/remote/builtin/github/github.go +++ b/pkg/remote/builtin/github/github.go @@ -41,7 +41,8 @@ func init() { remote.Register("github", NewDriver) } -func NewDriver(conf *config.Config) (remote.Remote, error) { +func NewDriver(config string) (remote.Remote, error) { + //conf *config.Config var github = GitHub{ API: conf.Github.API, URL: conf.Github.Host, diff --git a/pkg/remote/remote.go b/pkg/remote/remote.go index 0da719816..1fbdd2dfd 100644 --- a/pkg/remote/remote.go +++ b/pkg/remote/remote.go @@ -1,12 +1,12 @@ package remote import ( - "fmt" "net/http" - "github.com/drone/drone/pkg/config" "github.com/drone/drone/pkg/oauth2" - common "github.com/drone/drone/pkg/types" + "github.com/drone/drone/pkg/types" + + log "github.com/drone/drone/Godeps/_workspace/src/github.com/Sirupsen/logrus" ) var drivers = make(map[string]DriverFunc) @@ -26,55 +26,55 @@ func Register(name string, driver DriverFunc) { // DriverFunc returns a new connection to the remote. // Config is a struct, with base remote configuration. -type DriverFunc func(conf *config.Config) (Remote, error) +type DriverFunc func(config string) (Remote, error) // New creates a new remote connection. -func New(driver string, conf *config.Config) (Remote, error) { +func New(driver, config string) (Remote, error) { fn, ok := drivers[driver] if !ok { - return nil, fmt.Errorf("remote: unknown driver %q", driver) + log.Fatalf("remote: unknown driver %q", driver) } - return fn(conf) + return fn(config) } type Remote interface { // Login authenticates the session and returns the // remote user details. - Login(token, secret string) (*common.User, error) + Login(token, secret string) (*types.User, error) // Orgs fetches the organizations for the given user. - Orgs(u *common.User) ([]string, error) + Orgs(u *types.User) ([]string, error) // Repo fetches the named repository from the remote system. - Repo(u *common.User, owner, repo string) (*common.Repo, error) + Repo(u *types.User, owner, repo string) (*types.Repo, error) // Perm fetches the named repository permissions from // the remote system for the specified user. - Perm(u *common.User, owner, repo string) (*common.Perm, error) + Perm(u *types.User, owner, repo string) (*types.Perm, error) // Script fetches the build script (.drone.yml) from the remote // repository and returns in string format. - Script(u *common.User, r *common.Repo, b *common.Build) ([]byte, error) + Script(u *types.User, r *types.Repo, b *types.Build) ([]byte, error) // Status sends the commit status to the remote system. // An example would be the GitHub pull request status. - Status(u *common.User, r *common.Repo, b *common.Build) error + Status(u *types.User, r *types.Repo, b *types.Build) error // Netrc returns a .netrc file that can be used to clone // private repositories from a remote system. - Netrc(u *common.User) (*common.Netrc, error) + Netrc(u *types.User) (*types.Netrc, error) // Activate activates a repository by creating the post-commit hook and // adding the SSH deploy key, if applicable. - Activate(u *common.User, r *common.Repo, k *common.Keypair, link string) error + Activate(u *types.User, r *types.Repo, k *types.Keypair, link string) error // Deactivate removes a repository by removing all the post-commit hooks // which are equal to link and removing the SSH deploy key. - Deactivate(u *common.User, r *common.Repo, link string) error + Deactivate(u *types.User, r *types.Repo, link string) error // Hook parses the post-commit hook from the Request body // and returns the required data in a standard format. - Hook(r *http.Request) (*common.Hook, error) + Hook(r *http.Request) (*types.Hook, error) // Oauth2Transport Oauth2Transport(r *http.Request) *oauth2.Transport diff --git a/pkg/store/builtin/store.go b/pkg/store/builtin/store.go index 33800f2db..78977f828 100644 --- a/pkg/store/builtin/store.go +++ b/pkg/store/builtin/store.go @@ -2,7 +2,6 @@ package builtin import ( "database/sql" - "net/url" "os" "github.com/drone/drone/pkg/store" @@ -25,16 +24,7 @@ func init() { store.Register("postgres", NewDriver) } -func NewDriver(dsn string) (store.Store, error) { - uri, err := url.Parse(dsn) - if err != nil { - return nil, err - } - driver := uri.Scheme - if uri.Scheme == "sqlite3" { - uri.Scheme = "" - } - datasource := uri.String() +func NewDriver(driver, datasource string) (store.Store, error) { conn, err := Connect(driver, datasource) if err != nil { return nil, err diff --git a/pkg/store/store.go b/pkg/store/store.go index d71f0ced1..92470571f 100644 --- a/pkg/store/store.go +++ b/pkg/store/store.go @@ -1,11 +1,11 @@ package store import ( - "fmt" "io" - "net/url" "github.com/drone/drone/pkg/types" + + log "github.com/drone/drone/Godeps/_workspace/src/github.com/Sirupsen/logrus" ) var drivers = make(map[string]DriverFunc) @@ -25,22 +25,19 @@ func Register(name string, driver DriverFunc) { // DriverFunc returns a new connection to the datastore. // The name is a string in a driver-specific format. -type DriverFunc func(name string) (Store, error) +type DriverFunc func(driver, datasource string) (Store, error) // New creates a new database connection specified by its database driver // name and a driver-specific data source name, usually consisting of at // least a database name and connection information. -func New(dsn string) (Store, error) { - uri, err := url.Parse(dsn) - if err != nil { - return nil, err - } - driver := uri.Scheme +func New(driver, datasource string) (Store, error) { fn, ok := drivers[driver] if !ok { - return nil, fmt.Errorf("datastore: unknown driver %q", driver) + log.Fatalf("datastore: unknown driver %q", driver) } - return fn(dsn) + log.Infof("datastore: loading driver %s", driver) + log.Infof("datastore: loading config %s", datasource) + return fn(driver, datasource) } type Store interface {