Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Service provider. Removes global services and passing services around. #61

Merged
merged 15 commits into from
Aug 2, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 8 additions & 20 deletions cmd/gaia/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ import (
"github.com/gaia-pipeline/gaia"
"github.com/gaia-pipeline/gaia/handlers"
"github.com/gaia-pipeline/gaia/pipeline"
"github.com/gaia-pipeline/gaia/plugin"
scheduler "github.com/gaia-pipeline/gaia/scheduler"
"github.com/gaia-pipeline/gaia/security"
"github.com/gaia-pipeline/gaia/store"
"github.com/gaia-pipeline/gaia/services"
hclog "github.com/hashicorp/go-hclog"
"github.com/labstack/echo"
)
Expand Down Expand Up @@ -142,34 +140,24 @@ func main() {
// Initialize echo instance
echoInstance = echo.New()

// Initialize store
store := store.NewStore()
err = store.Init()
if err != nil {
gaia.Cfg.Logger.Error("cannot initialize store", "error", err.Error())
os.Exit(1)
}
// Create a service provider
p := new(services.Provider)

// Create new plugin system
pS := &plugin.Plugin{}
// Initialize store
p.StorageService()

// Initialize scheduler
scheduler := scheduler.NewScheduler(store, pS)
err = scheduler.Init()
if err != nil {
gaia.Cfg.Logger.Error("cannot initialize scheduler:", "error", err.Error())
os.Exit(1)
}
p.SchedulerService()

// Initialize handlers
err = handlers.InitHandlers(echoInstance, store, scheduler)
err = handlers.InitHandlers(echoInstance)
if err != nil {
gaia.Cfg.Logger.Error("cannot initialize handlers", "error", err.Error())
os.Exit(1)
}

// Start ticker. Periodic job to check for new plugins.
pipeline.InitTicker(store, scheduler)
pipeline.InitTicker()

// Start listen
echoInstance.Logger.Fatal(echoInstance.Start(":" + gaia.Cfg.ListenPort))
Expand Down
14 changes: 1 addition & 13 deletions handlers/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (

jwt "github.com/dgrijalva/jwt-go"
"github.com/gaia-pipeline/gaia"
scheduler "github.com/gaia-pipeline/gaia/scheduler"
"github.com/gaia-pipeline/gaia/store"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
)
Expand Down Expand Up @@ -49,18 +47,8 @@ var (
errPipelineRename = errors.New("pipeline could not be renamed")
)

// storeService is an instance of store.
// Use this to talk to the store.
var storeService *store.Store

var schedulerService *scheduler.Scheduler

// InitHandlers initializes(registers) all handlers
func InitHandlers(e *echo.Echo, store *store.Store, scheduler *scheduler.Scheduler) error {
// Set instances
storeService = store
schedulerService = scheduler

func InitHandlers(e *echo.Echo) error {
// Define prefix
p := "/api/" + apiVersion + "/"

Expand Down
13 changes: 13 additions & 0 deletions handlers/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/gaia-pipeline/gaia"
"github.com/gaia-pipeline/gaia/pipeline"
"github.com/gaia-pipeline/gaia/services"
"github.com/labstack/echo"
uuid "github.com/satori/go.uuid"
)
Expand Down Expand Up @@ -38,6 +39,8 @@ func PipelineGitLSRemote(c echo.Context) error {
// CreatePipeline accepts all data needed to create a pipeline.
// It then starts the create pipeline execution process async.
func CreatePipeline(c echo.Context) error {
sp := new(services.Provider)
storeService := sp.StorageService()
p := &gaia.CreatePipeline{}
if err := c.Bind(p); err != nil {
return c.String(http.StatusBadRequest, err.Error())
Expand Down Expand Up @@ -66,6 +69,8 @@ func CreatePipeline(c echo.Context) error {
// all pipelines which have been compiled.
func CreatePipelineGetAll(c echo.Context) error {
// Get all create pipelines
sp := new(services.Provider)
storeService := sp.StorageService()
pipelineList, err := storeService.CreatePipelineGet()
if err != nil {
gaia.Cfg.Logger.Debug("cannot get create pipelines from store", "error", err.Error())
Expand Down Expand Up @@ -138,6 +143,8 @@ func PipelineGet(c echo.Context) error {

// PipelineUpdate updates the given pipeline.
func PipelineUpdate(c echo.Context) error {
sp := new(services.Provider)
storeService := sp.StorageService()
p := gaia.Pipeline{}
if err := c.Bind(&p); err != nil {
return c.String(http.StatusBadRequest, err.Error())
Expand Down Expand Up @@ -188,6 +195,8 @@ func PipelineUpdate(c echo.Context) error {
// PipelineDelete accepts a pipeline id and deletes it from the
// store. It also removes the binary inside the pipeline folder.
func PipelineDelete(c echo.Context) error {
sp := new(services.Provider)
storeService := sp.StorageService()
pipelineIDStr := c.Param("pipelineid")

pipelineID, err := strconv.Atoi(pipelineIDStr)
Expand Down Expand Up @@ -232,6 +241,8 @@ func PipelineDelete(c echo.Context) error {
// PipelineStart starts a pipeline by the given id.
// Afterwards it returns the created/scheduled pipeline run.
func PipelineStart(c echo.Context) error {
sp := new(services.Provider)
schedulerService := sp.SchedulerService()
pipelineIDStr := c.Param("pipelineid")

// Convert string to int because id is int
Expand Down Expand Up @@ -270,6 +281,8 @@ type getAllWithLatestRun struct {
// included with the latest run.
func PipelineGetAllWithLatestRun(c echo.Context) error {
// Get all active pipelines
sp := new(services.Provider)
storeService := sp.StorageService()
var pipelines []gaia.Pipeline
for pipeline := range pipeline.GlobalActivePipelines.Iter() {
pipelines = append(pipelines, pipeline)
Expand Down
9 changes: 9 additions & 0 deletions handlers/pipeline_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strconv"

"github.com/gaia-pipeline/gaia"
"github.com/gaia-pipeline/gaia/services"
"github.com/labstack/echo"
)

Expand All @@ -26,6 +27,8 @@ type jobLogs struct {
// Required parameters are pipelineid and runid.
func PipelineRunGet(c echo.Context) error {
// Convert string to int because id is int
sp := new(services.Provider)
storeService := sp.StorageService()
pipelineID, err := strconv.Atoi(c.Param("pipelineid"))
if err != nil {
return c.String(http.StatusBadRequest, errInvalidPipelineID.Error())
Expand All @@ -52,6 +55,8 @@ func PipelineRunGet(c echo.Context) error {
// PipelineGetAllRuns returns all runs about the given pipeline.
func PipelineGetAllRuns(c echo.Context) error {
// Convert string to int because id is int
sp := new(services.Provider)
storeService := sp.StorageService()
pipelineID, err := strconv.Atoi(c.Param("pipelineid"))
if err != nil {
return c.String(http.StatusBadRequest, errInvalidPipelineID.Error())
Expand All @@ -69,6 +74,8 @@ func PipelineGetAllRuns(c echo.Context) error {
// PipelineGetLatestRun returns the latest run of a pipeline, given by id.
func PipelineGetLatestRun(c echo.Context) error {
// Convert string to int because id is int
sp := new(services.Provider)
storeService := sp.StorageService()
pipelineID, err := strconv.Atoi(c.Param("pipelineid"))
if err != nil {
return c.String(http.StatusBadRequest, errInvalidPipelineID.Error())
Expand All @@ -90,6 +97,8 @@ func PipelineGetLatestRun(c echo.Context) error {
// pipelinerunid - Related pipeline run id
func GetJobLogs(c echo.Context) error {
// Get parameters and validate
sp := new(services.Provider)
storeService := sp.StorageService()
pipelineID := c.Param("pipelineid")
pipelineRunID := c.Param("runid")

Expand Down
26 changes: 9 additions & 17 deletions handlers/pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import (
"testing"
"time"

"github.com/gaia-pipeline/gaia/services"

"github.com/gaia-pipeline/gaia"
"github.com/gaia-pipeline/gaia/pipeline"
"github.com/gaia-pipeline/gaia/store"
hclog "github.com/hashicorp/go-hclog"
"github.com/labstack/echo"
)
Expand All @@ -33,14 +34,8 @@ func TestPipelineGitLSRemote(t *testing.T) {
DataPath: dataDir,
}

dataStore := store.NewStore()
err = dataStore.Init()
if err != nil {
t.Fatalf("cannot initialize store: %v", err.Error())
}

e := echo.New()
InitHandlers(e, dataStore, nil)
InitHandlers(e)

t.Run("fails with invalid data", func(t *testing.T) {
req := httptest.NewRequest(echo.POST, "/api/"+apiVersion+"/pipeline/gitlsremote", nil)
Expand Down Expand Up @@ -109,19 +104,15 @@ func TestPipelineUpdate(t *testing.T) {
}

// Initialize store
dataStore := store.NewStore()
err = dataStore.Init()
if err != nil {
t.Fatalf("cannot initialize store: %v", err.Error())
}

p := new(services.Provider)
dataStore := p.StorageService()
// Initialize global active pipelines
ap := pipeline.NewActivePipelines()
pipeline.GlobalActivePipelines = ap

// Initialize echo
e := echo.New()
InitHandlers(e, dataStore, nil)
InitHandlers(e)

pipeline1 := gaia.Pipeline{
ID: 1,
Expand Down Expand Up @@ -200,7 +191,8 @@ func TestPipelineDelete(t *testing.T) {
}

// Initialize store
dataStore := store.NewStore()
provider := new(services.Provider)
dataStore := provider.StorageService()
err = dataStore.Init()
if err != nil {
t.Fatalf("cannot initialize store: %v", err.Error())
Expand All @@ -212,7 +204,7 @@ func TestPipelineDelete(t *testing.T) {

// Initialize echo
e := echo.New()
InitHandlers(e, dataStore, nil)
InitHandlers(e)

p := gaia.Pipeline{
ID: 1,
Expand Down
13 changes: 11 additions & 2 deletions handlers/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/dgrijalva/jwt-go"
"github.com/gaia-pipeline/gaia"
"github.com/gaia-pipeline/gaia/services"
)

// jwtExpiry defines how long the produced jwt tokens
Expand All @@ -24,6 +25,8 @@ type jwtCustomClaims struct {
// UserLogin authenticates the user with
// the given credentials.
func UserLogin(c echo.Context) error {
sp := new(services.Provider)
storeService := sp.StorageService()
u := &gaia.User{}
if err := c.Bind(u); err != nil {
gaia.Cfg.Logger.Debug("error reading json during UserLogin", "error", err.Error())
Expand Down Expand Up @@ -75,6 +78,8 @@ func UserLogin(c echo.Context) error {
// UserGetAll returns all users stored in store.
func UserGetAll(c echo.Context) error {
// Get all users
sp := new(services.Provider)
storeService := sp.StorageService()
users, err := storeService.UserGetAll()
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
Expand All @@ -94,6 +99,8 @@ type changePasswordRequest struct {
func UserChangePassword(c echo.Context) error {
// Get required parameters
r := &changePasswordRequest{}
sp := new(services.Provider)
storeService := sp.StorageService()
if err := c.Bind(r); err != nil {
return c.String(http.StatusBadRequest, "Invalid parameters given for password change request")
}
Expand Down Expand Up @@ -134,7 +141,8 @@ func UserDelete(c echo.Context) error {
if u == "" {
return c.String(http.StatusBadRequest, "Invalid username given")
}

sp := new(services.Provider)
storeService := sp.StorageService()
// Delete user
err := storeService.UserDelete(u)
if err != nil {
Expand All @@ -151,7 +159,8 @@ func UserAdd(c echo.Context) error {
if err := c.Bind(u); err != nil {
return c.String(http.StatusBadRequest, "Invalid parameters given for add user request")
}

sp := new(services.Provider)
storeService := sp.StorageService()
// Add user
u.LastLogin = time.Now()
err := storeService.UserPut(u, true)
Expand Down
17 changes: 2 additions & 15 deletions handlers/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (

jwt "github.com/dgrijalva/jwt-go"
"github.com/gaia-pipeline/gaia"
"github.com/gaia-pipeline/gaia/store"
"github.com/hashicorp/go-hclog"
"github.com/labstack/echo"
)
Expand All @@ -43,14 +42,8 @@ func TestUserLoginHMACKey(t *testing.T) {
DataPath: dataDir,
}

dataStore := store.NewStore()
err = dataStore.Init()
if err != nil {
t.Fatalf("cannot initialize store: %v", err.Error())
}

e := echo.New()
InitHandlers(e, dataStore, nil)
InitHandlers(e)

body := map[string]string{
"username": "admin",
Expand Down Expand Up @@ -105,14 +98,8 @@ func TestUserLoginRSAKey(t *testing.T) {
DataPath: dataDir,
}

dataStore := store.NewStore()
err = dataStore.Init()
if err != nil {
t.Fatalf("cannot initialize store: %v", err.Error())
}

e := echo.New()
InitHandlers(e, dataStore, nil)
InitHandlers(e)

body := map[string]string{
"username": "admin",
Expand Down
5 changes: 4 additions & 1 deletion pipeline/build_golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/gaia-pipeline/gaia"
"github.com/gaia-pipeline/gaia/services"
"github.com/satori/go.uuid"
)

Expand Down Expand Up @@ -129,7 +130,9 @@ func (b *BuildPipelineGolang) SavePipeline(p *gaia.Pipeline) error {
p.Name = strings.TrimSuffix(filepath.Base(dest), typeDelimiter+gaia.PTypeGolang.String())
p.Created = time.Now()
// Our pipeline is finished constructing. Save it.
return storeService.PipelinePut(p)
sp := new(services.Provider)
ss := sp.StorageService()
return ss.PipelinePut(p)
}

// copyFileContents copies the content from source to destination.
Expand Down
4 changes: 0 additions & 4 deletions pipeline/build_golang_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"testing"

"github.com/gaia-pipeline/gaia"
"github.com/gaia-pipeline/gaia/store"
hclog "github.com/hashicorp/go-hclog"
)

Expand Down Expand Up @@ -246,9 +245,6 @@ func TestCopyBinarySrcDoesNotExist(t *testing.T) {
}

func TestSavePipeline(t *testing.T) {
s := store.NewStore()
s.Init()
storeService = s
defer os.Remove("gaia.db")
gaia.Cfg = new(gaia.Config)
gaia.Cfg.HomePath = "/tmp"
Expand Down
Loading