mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2025-01-08 16:45:30 +00:00
setup store in main()
This commit is contained in:
parent
d4a1c22f85
commit
2f579e4b7d
10 changed files with 156 additions and 76 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,6 +1,7 @@
|
||||||
drone/drone
|
drone/drone
|
||||||
*.sqlite
|
*.sqlite
|
||||||
*_gen.go
|
*_gen.go
|
||||||
|
*_ee.go
|
||||||
!store/datastore/sql/sqlite/sql_gen.go
|
!store/datastore/sql/sqlite/sql_gen.go
|
||||||
!store/datastore/sql/mysql/sql_gen.go
|
!store/datastore/sql/mysql/sql_gen.go
|
||||||
!store/datastore/sql/postgres/sql_gen.go
|
!store/datastore/sql/postgres/sql_gen.go
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"time"
|
"time"
|
||||||
|
@ -8,8 +9,17 @@ import (
|
||||||
"golang.org/x/crypto/acme/autocert"
|
"golang.org/x/crypto/acme/autocert"
|
||||||
"golang.org/x/sync/errgroup"
|
"golang.org/x/sync/errgroup"
|
||||||
|
|
||||||
|
"github.com/cncd/logging"
|
||||||
|
"github.com/cncd/pubsub"
|
||||||
|
"github.com/cncd/queue"
|
||||||
|
"github.com/drone/drone/model"
|
||||||
|
"github.com/drone/drone/plugins/registry"
|
||||||
|
"github.com/drone/drone/plugins/secrets"
|
||||||
|
"github.com/drone/drone/plugins/sender"
|
||||||
"github.com/drone/drone/router"
|
"github.com/drone/drone/router"
|
||||||
"github.com/drone/drone/router/middleware"
|
"github.com/drone/drone/router/middleware"
|
||||||
|
droneserver "github.com/drone/drone/server"
|
||||||
|
"github.com/drone/drone/store"
|
||||||
|
|
||||||
"github.com/Sirupsen/logrus"
|
"github.com/Sirupsen/logrus"
|
||||||
"github.com/gin-gonic/contrib/ginrus"
|
"github.com/gin-gonic/contrib/ginrus"
|
||||||
|
@ -325,13 +335,16 @@ func server(c *cli.Context) error {
|
||||||
logrus.SetLevel(logrus.WarnLevel)
|
logrus.SetLevel(logrus.WarnLevel)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
s := setupStore(c)
|
||||||
|
setupEvilGlobals(c, s)
|
||||||
|
|
||||||
// setup the server and start the listener
|
// setup the server and start the listener
|
||||||
handler := router.Load(
|
handler := router.Load(
|
||||||
ginrus.Ginrus(logrus.StandardLogger(), time.RFC3339, true),
|
ginrus.Ginrus(logrus.StandardLogger(), time.RFC3339, true),
|
||||||
middleware.Version,
|
middleware.Version,
|
||||||
middleware.Config(c),
|
middleware.Config(c),
|
||||||
middleware.Cache(c),
|
middleware.Cache(c),
|
||||||
middleware.Store(c),
|
middleware.Store(c, s),
|
||||||
middleware.Remote(c),
|
middleware.Remote(c),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -370,3 +383,45 @@ func server(c *cli.Context) error {
|
||||||
|
|
||||||
return g.Wait()
|
return g.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HACK please excuse the message during this period of heavy refactoring.
|
||||||
|
// We are currently transitioning from storing services (ie database, queue)
|
||||||
|
// in the gin.Context to storing them in a struct. We are also moving away
|
||||||
|
// from gin to gorilla. We will temporarily use global during our refactoring
|
||||||
|
// which will be removing in the final implementation.
|
||||||
|
func setupEvilGlobals(c *cli.Context, v store.Store) {
|
||||||
|
|
||||||
|
// storage
|
||||||
|
droneserver.Config.Storage.Files = v
|
||||||
|
|
||||||
|
// services
|
||||||
|
droneserver.Config.Services.Queue = model.WithTaskStore(queue.New(), v)
|
||||||
|
droneserver.Config.Services.Logs = logging.New()
|
||||||
|
droneserver.Config.Services.Pubsub = pubsub.New()
|
||||||
|
droneserver.Config.Services.Pubsub.Create(context.Background(), "topic/events")
|
||||||
|
droneserver.Config.Services.Registries = registry.New(v)
|
||||||
|
droneserver.Config.Services.Secrets = secrets.New(v)
|
||||||
|
droneserver.Config.Services.Senders = sender.New(v)
|
||||||
|
if endpoint := c.String("registry-service"); endpoint != "" {
|
||||||
|
droneserver.Config.Services.Registries = registry.NewRemote(endpoint)
|
||||||
|
}
|
||||||
|
if endpoint := c.String("secret-service"); endpoint != "" {
|
||||||
|
droneserver.Config.Services.Secrets = secrets.NewRemote(endpoint)
|
||||||
|
}
|
||||||
|
if endpoint := c.String("gating-service"); endpoint != "" {
|
||||||
|
droneserver.Config.Services.Senders = sender.NewRemote(endpoint)
|
||||||
|
}
|
||||||
|
|
||||||
|
// server configuration
|
||||||
|
droneserver.Config.Server.Cert = c.String("server-cert")
|
||||||
|
droneserver.Config.Server.Key = c.String("server-key")
|
||||||
|
droneserver.Config.Server.Pass = c.String("agent-secret")
|
||||||
|
droneserver.Config.Server.Host = c.String("server-host")
|
||||||
|
droneserver.Config.Server.Port = c.String("server-addr")
|
||||||
|
droneserver.Config.Pipeline.Networks = c.StringSlice("network")
|
||||||
|
droneserver.Config.Pipeline.Volumes = c.StringSlice("volumes")
|
||||||
|
droneserver.Config.Pipeline.Privileged = c.StringSlice("escalate")
|
||||||
|
// droneserver.Config.Server.Open = cli.Bool("open")
|
||||||
|
// droneserver.Config.Server.Orgs = sliceToMap(cli.StringSlice("orgs"))
|
||||||
|
// droneserver.Config.Server.Admins = sliceToMap(cli.StringSlice("admin"))
|
||||||
|
}
|
||||||
|
|
24
drone/server/setup.go
Normal file
24
drone/server/setup.go
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
// +build !enterprise
|
||||||
|
|
||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/drone/drone/store"
|
||||||
|
"github.com/drone/drone/store/datastore"
|
||||||
|
|
||||||
|
"github.com/urfave/cli"
|
||||||
|
)
|
||||||
|
|
||||||
|
func setupStore(c *cli.Context) store.Store {
|
||||||
|
return datastore.New(
|
||||||
|
c.String("driver"),
|
||||||
|
c.String("datasource"),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func setupQueue(c *cli.Context) {}
|
||||||
|
func setupPubsub(c *cli.Context) {}
|
||||||
|
func setupStream(c *cli.Command) {}
|
||||||
|
func setupRegistryService(c *cli.Command) {}
|
||||||
|
func setupSecretService(c *cli.Command) {}
|
||||||
|
func setupGatingService(c *cli.Command) {}
|
|
@ -1,18 +1,7 @@
|
||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
|
|
||||||
"github.com/cncd/logging"
|
|
||||||
"github.com/cncd/pubsub"
|
|
||||||
"github.com/cncd/queue"
|
|
||||||
"github.com/drone/drone/model"
|
|
||||||
"github.com/drone/drone/plugins/registry"
|
|
||||||
"github.com/drone/drone/plugins/secrets"
|
|
||||||
"github.com/drone/drone/plugins/sender"
|
|
||||||
"github.com/drone/drone/server"
|
|
||||||
"github.com/drone/drone/store"
|
"github.com/drone/drone/store"
|
||||||
"github.com/drone/drone/store/datastore"
|
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
@ -20,64 +9,9 @@ import (
|
||||||
|
|
||||||
// Store is a middleware function that initializes the Datastore and attaches to
|
// Store is a middleware function that initializes the Datastore and attaches to
|
||||||
// the context of every http.Request.
|
// the context of every http.Request.
|
||||||
func Store(cli *cli.Context) gin.HandlerFunc {
|
func Store(cli *cli.Context, v store.Store) gin.HandlerFunc {
|
||||||
v := setupStore(cli)
|
|
||||||
|
|
||||||
// HACK during refactor period. Please ignore my mess.
|
|
||||||
|
|
||||||
// storage
|
|
||||||
server.Config.Storage.Files = v
|
|
||||||
|
|
||||||
// services
|
|
||||||
server.Config.Services.Queue = model.WithTaskStore(queue.New(), v)
|
|
||||||
server.Config.Services.Logs = logging.New()
|
|
||||||
server.Config.Services.Pubsub = pubsub.New()
|
|
||||||
server.Config.Services.Pubsub.Create(context.Background(), "topic/events")
|
|
||||||
server.Config.Services.Registries = registry.New(v)
|
|
||||||
server.Config.Services.Secrets = secrets.New(v)
|
|
||||||
server.Config.Services.Senders = sender.New(v)
|
|
||||||
if endpoint := cli.String("registry-service"); endpoint != "" {
|
|
||||||
server.Config.Services.Registries = registry.NewRemote(endpoint)
|
|
||||||
}
|
|
||||||
if endpoint := cli.String("secret-service"); endpoint != "" {
|
|
||||||
server.Config.Services.Secrets = secrets.NewRemote(endpoint)
|
|
||||||
}
|
|
||||||
if endpoint := cli.String("gating-service"); endpoint != "" {
|
|
||||||
server.Config.Services.Senders = sender.NewRemote(endpoint)
|
|
||||||
}
|
|
||||||
|
|
||||||
// server configuration
|
|
||||||
server.Config.Server.Cert = cli.String("server-cert")
|
|
||||||
server.Config.Server.Key = cli.String("server-key")
|
|
||||||
server.Config.Server.Pass = cli.String("agent-secret")
|
|
||||||
server.Config.Server.Host = cli.String("server-host")
|
|
||||||
server.Config.Server.Port = cli.String("server-addr")
|
|
||||||
server.Config.Pipeline.Networks = cli.StringSlice("network")
|
|
||||||
server.Config.Pipeline.Volumes = cli.StringSlice("volumes")
|
|
||||||
server.Config.Pipeline.Privileged = cli.StringSlice("escalate")
|
|
||||||
// server.Config.Server.Open = cli.Bool("open")
|
|
||||||
// server.Config.Server.Orgs = sliceToMap(cli.StringSlice("orgs"))
|
|
||||||
// server.Config.Server.Admins = sliceToMap(cli.StringSlice("admin"))
|
|
||||||
|
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
store.ToContext(c, v)
|
store.ToContext(c, v)
|
||||||
c.Next()
|
c.Next()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// helper function to create the datastore from the CLI context.
|
|
||||||
func setupStore(c *cli.Context) store.Store {
|
|
||||||
return datastore.New(
|
|
||||||
c.String("driver"),
|
|
||||||
c.String("datasource"),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// helper function to convert a string slice to a map.
|
|
||||||
func sliceToMap(s []string) map[string]struct{} {
|
|
||||||
v := map[string]struct{}{}
|
|
||||||
for _, ss := range s {
|
|
||||||
v[ss] = struct{}{}
|
|
||||||
}
|
|
||||||
return v
|
|
||||||
}
|
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/drone/drone/model"
|
"github.com/drone/drone/model"
|
||||||
|
"github.com/drone/drone/store/datastore/sql"
|
||||||
"github.com/russross/meddler"
|
"github.com/russross/meddler"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -38,10 +39,10 @@ func (db *datastore) GetRepoListOf(listof []*model.RepoLite) ([]*model.Repo, err
|
||||||
return repos, err
|
return repos, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *datastore) GetRepoCount() (int, error) {
|
func (db *datastore) GetRepoCount() (count int, err error) {
|
||||||
var count int
|
stmt := sql.Lookup(db.driver, "count-repos")
|
||||||
var err = db.QueryRow(rebind(repoCountQuery)).Scan(&count)
|
err = meddler.QueryAll(db, &count, stmt)
|
||||||
return count, err
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *datastore) CreateRepo(repo *model.Repo) error {
|
func (db *datastore) CreateRepo(repo *model.Repo) error {
|
||||||
|
|
14
store/datastore/sql/postgres/files/counts.sql
Normal file
14
store/datastore/sql/postgres/files/counts.sql
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
-- name: count-users
|
||||||
|
|
||||||
|
SELECT reltuples
|
||||||
|
FROM pg_class WHERE relname = 'users';
|
||||||
|
|
||||||
|
-- name: count-repos
|
||||||
|
|
||||||
|
SELECT reltuples
|
||||||
|
FROM pg_class WHERE relname = 'repos';
|
||||||
|
|
||||||
|
-- name: count-builds
|
||||||
|
|
||||||
|
SELECT reltuples
|
||||||
|
FROM pg_class WHERE relname = 'builds';
|
|
@ -6,6 +6,9 @@ func Lookup(name string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
var index = map[string]string{
|
var index = map[string]string{
|
||||||
|
"count-users": countUsers,
|
||||||
|
"count-repos": countRepos,
|
||||||
|
"count-builds": countBuilds,
|
||||||
"files-find-build": filesFindBuild,
|
"files-find-build": filesFindBuild,
|
||||||
"files-find-proc-name": filesFindProcName,
|
"files-find-proc-name": filesFindProcName,
|
||||||
"files-find-proc-name-data": filesFindProcNameData,
|
"files-find-proc-name-data": filesFindProcNameData,
|
||||||
|
@ -30,6 +33,21 @@ var index = map[string]string{
|
||||||
"task-delete": taskDelete,
|
"task-delete": taskDelete,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var countUsers = `
|
||||||
|
SELECT reltuples
|
||||||
|
FROM pg_class WHERE relname = 'users';
|
||||||
|
`
|
||||||
|
|
||||||
|
var countRepos = `
|
||||||
|
SELECT reltuples
|
||||||
|
FROM pg_class WHERE relname = 'repos';
|
||||||
|
`
|
||||||
|
|
||||||
|
var countBuilds = `
|
||||||
|
SELECT reltuples
|
||||||
|
FROM pg_class WHERE relname = 'builds';
|
||||||
|
`
|
||||||
|
|
||||||
var filesFindBuild = `
|
var filesFindBuild = `
|
||||||
SELECT
|
SELECT
|
||||||
file_id
|
file_id
|
||||||
|
|
14
store/datastore/sql/sqlite/files/counts.sql
Normal file
14
store/datastore/sql/sqlite/files/counts.sql
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
-- name: count-users
|
||||||
|
|
||||||
|
SELECT count(1)
|
||||||
|
FROM users
|
||||||
|
|
||||||
|
-- name: count-repos
|
||||||
|
|
||||||
|
SELECT count(1)
|
||||||
|
FROM repos
|
||||||
|
|
||||||
|
-- name: count-builds
|
||||||
|
|
||||||
|
SELECT count(1)
|
||||||
|
FROM builds
|
|
@ -6,6 +6,9 @@ func Lookup(name string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
var index = map[string]string{
|
var index = map[string]string{
|
||||||
|
"count-users": countUsers,
|
||||||
|
"count-repos": countRepos,
|
||||||
|
"count-builds": countBuilds,
|
||||||
"files-find-build": filesFindBuild,
|
"files-find-build": filesFindBuild,
|
||||||
"files-find-proc-name": filesFindProcName,
|
"files-find-proc-name": filesFindProcName,
|
||||||
"files-find-proc-name-data": filesFindProcNameData,
|
"files-find-proc-name-data": filesFindProcNameData,
|
||||||
|
@ -30,6 +33,21 @@ var index = map[string]string{
|
||||||
"task-delete": taskDelete,
|
"task-delete": taskDelete,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var countUsers = `
|
||||||
|
SELECT count(1)
|
||||||
|
FROM users
|
||||||
|
`
|
||||||
|
|
||||||
|
var countRepos = `
|
||||||
|
SELECT count(1)
|
||||||
|
FROM repos
|
||||||
|
`
|
||||||
|
|
||||||
|
var countBuilds = `
|
||||||
|
SELECT count(1)
|
||||||
|
FROM builds
|
||||||
|
`
|
||||||
|
|
||||||
var filesFindBuild = `
|
var filesFindBuild = `
|
||||||
SELECT
|
SELECT
|
||||||
file_id
|
file_id
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/drone/drone/model"
|
"github.com/drone/drone/model"
|
||||||
|
"github.com/drone/drone/store/datastore/sql"
|
||||||
"github.com/russross/meddler"
|
"github.com/russross/meddler"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -65,10 +66,10 @@ func (db *datastore) GetUserFeedLatest(listof []*model.RepoLite) ([]*model.Feed,
|
||||||
return feed, err
|
return feed, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *datastore) GetUserCount() (int, error) {
|
func (db *datastore) GetUserCount() (count int, err error) {
|
||||||
var count int
|
stmt := sql.Lookup(db.driver, "count-users")
|
||||||
var err = db.QueryRow(rebind(userCountQuery)).Scan(&count)
|
err = meddler.QueryAll(db, &count, stmt)
|
||||||
return count, err
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *datastore) CreateUser(user *model.User) error {
|
func (db *datastore) CreateUser(user *model.User) error {
|
||||||
|
|
Loading…
Reference in a new issue