Skip to content

Commit

Permalink
Refactored scheduler out of the service singleton provider (#237)
Browse files Browse the repository at this point in the history
* Refactored scheduler out of the service singleton provider

* Refactored the rest of the tests

* Linter and test fixes

* Fixed the agent test

* Fixed the agent tests

* Keep em coming

* Ehhh

* Removed unused service

* A little renaming

* Fixed import rename to workers2

* Fixed merge conflict

* Was missing Init() call

* Fixed test

* Enabling the test back

* This now also fixes the cron issue

* Added nil check
  • Loading branch information
Skarlso authored Apr 27, 2020
1 parent 8913a7e commit ec28241
Show file tree
Hide file tree
Showing 34 changed files with 895 additions and 388 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require (
github.com/emirpasic/gods v1.9.0 // indirect
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 // indirect
github.com/gaia-pipeline/flag v1.7.4-pre
github.com/gaia-pipeline/gosdk v0.0.0-20180909192508-cc9f89055777
github.com/gaia-pipeline/protobuf v0.0.0-20180812091451-7be8a901b55a
github.com/gliderlabs/ssh v0.1.1 // indirect
github.com/gofrs/uuid v3.2.0+incompatible
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 h1:BHsljHzVlRcyQhjr
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
github.com/gaia-pipeline/flag v1.7.4-pre h1:/TAmHVYVQGE4Mw9xl0Qs0D5UruVDMF95thexyEFbTAY=
github.com/gaia-pipeline/flag v1.7.4-pre/go.mod h1:rLpsWzqOEPa2K0Yl4aC34nmblLpIYjGqjP/srZbYvEk=
github.com/gaia-pipeline/gosdk v0.0.0-20180909192508-cc9f89055777 h1:Gn3nmETr4IE44pIFLeTDNNBBRhNXXh53PqFV23CsVok=
github.com/gaia-pipeline/gosdk v0.0.0-20180909192508-cc9f89055777/go.mod h1:e3TkvPdcdSdHZTgwiS89fs8lJrveYHsGLlz/Q0oXlN8=
github.com/gaia-pipeline/protobuf v0.0.0-20180812091451-7be8a901b55a h1:/5XAmdAyGl4yL9BugdPdBLaXquif1zw6Hih6go8E7Xs=
github.com/gaia-pipeline/protobuf v0.0.0-20180812091451-7be8a901b55a/go.mod h1:H0w7MofSuW53Nz7kesnBdVkvr437flf5B7D9Lcsb+lQ=
github.com/gliderlabs/ssh v0.1.1 h1:j3L6gSLQalDETeEg/Jg0mGY0/y/N6zI2xX1978P0Uqw=
Expand Down
1 change: 1 addition & 0 deletions handlers/errors/handler_errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package errors
85 changes: 39 additions & 46 deletions handlers/handler.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,25 @@
package handlers

import (
"errors"
"net/http"

rice "github.com/GeertJohan/go.rice"
"github.com/gaia-pipeline/gaia"
"github.com/gaia-pipeline/gaia/helper/rolehelper"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"

"github.com/gaia-pipeline/gaia"
"github.com/gaia-pipeline/gaia/handlers/providers/pipelines"
"github.com/gaia-pipeline/gaia/handlers/providers/workers"
"github.com/gaia-pipeline/gaia/helper/rolehelper"
)

var (
// errPipelineNotFound is thrown when a pipeline was not found with the given id
errPipelineNotFound = errors.New("pipeline not found with the given id")

// errInvalidPipelineID is thrown when the given pipeline id is not valid
errInvalidPipelineID = errors.New("the given pipeline id is not valid")

// errPipelineRunNotFound is thrown when a pipeline run was not found with the given id
errPipelineRunNotFound = errors.New("pipeline run not found with the given id")

// errPipelineDelete is thrown when a pipeline binary could not be deleted
errPipelineDelete = errors.New("pipeline could not be deleted. Perhaps you don't have the right permissions")

// errPipelineRename is thrown when a pipeline binary could not be renamed
errPipelineRename = errors.New("pipeline could not be renamed")

// errWrongDockerValue is thrown when docker has been specified for a pipeline run but the value is invalid
errWrongDockerValue = errors.New("invalid value for docker parameter")

// List of secret keys which cannot be modified via the normal Vault API.
ignoredVaultKeys []string
)

// InitHandlers initializes(registers) all handlers.
func InitHandlers(e *echo.Echo) error {
func (s *GaiaHandler) InitHandlers(e *echo.Echo) error {
// Define prefix
p := "/api/" + gaia.APIVersion + "/"

Expand All @@ -57,32 +41,37 @@ func InitHandlers(e *echo.Echo) error {
perms.GET("", PermissionGetAll)

// Pipelines
e.POST(p+"pipeline", CreatePipeline)
e.POST(p+"pipeline/gitlsremote", PipelineGitLSRemote)
e.GET(p+"pipeline/name", PipelineNameAvailable)
// Create pipeline provider
pipelineProvider := pipelines.NewPipelineProvider(pipelines.Dependencies{
Scheduler: s.deps.Scheduler,
PipelineService: s.deps.PipelineService,
})
e.POST(p+"pipeline", pipelineProvider.CreatePipeline)
e.POST(p+"pipeline/gitlsremote", pipelineProvider.PipelineGitLSRemote)
e.GET(p+"pipeline/name", pipelineProvider.PipelineNameAvailable)
e.POST(p+"pipeline/githook", GitWebHook)
e.GET(p+"pipeline/created", CreatePipelineGetAll)
e.GET(p+"pipeline", PipelineGetAll)
e.GET(p+"pipeline/:pipelineid", PipelineGet)
e.PUT(p+"pipeline/:pipelineid", PipelineUpdate)
e.DELETE(p+"pipeline/:pipelineid", PipelineDelete)
e.POST(p+"pipeline/:pipelineid/start", PipelineStart)
e.POST(p+"pipeline/:pipelineid/:pipelinetoken/trigger", PipelineTrigger)
e.PUT(p+"pipeline/:pipelineid/reset-trigger-token", PipelineResetToken)
e.GET(p+"pipeline/latest", PipelineGetAllWithLatestRun)
e.POST(p+"pipeline/periodicschedules", PipelineCheckPeriodicSchedules)
e.GET(p+"pipeline/created", pipelineProvider.CreatePipelineGetAll)
e.GET(p+"pipeline", pipelineProvider.PipelineGetAll)
e.GET(p+"pipeline/:pipelineid", pipelineProvider.PipelineGet)
e.PUT(p+"pipeline/:pipelineid", pipelineProvider.PipelineUpdate)
e.DELETE(p+"pipeline/:pipelineid", pipelineProvider.PipelineDelete)
e.POST(p+"pipeline/:pipelineid/start", pipelineProvider.PipelineStart)
e.POST(p+"pipeline/:pipelineid/:pipelinetoken/trigger", pipelineProvider.PipelineTrigger)
e.PUT(p+"pipeline/:pipelineid/reset-trigger-token", pipelineProvider.PipelineResetToken)
e.GET(p+"pipeline/latest", pipelineProvider.PipelineGetAllWithLatestRun)
e.POST(p+"pipeline/periodicschedules", pipelineProvider.PipelineCheckPeriodicSchedules)

// Settings
e.POST(p+"settings/poll/on", SettingsPollOn)
e.POST(p+"settings/poll/off", SettingsPollOff)
e.GET(p+"settings/poll", SettingsPollGet)

// PipelineRun
e.POST(p+"pipelinerun/:pipelineid/:runid/stop", PipelineStop)
e.GET(p+"pipelinerun/:pipelineid/:runid", PipelineRunGet)
e.GET(p+"pipelinerun/:pipelineid", PipelineGetAllRuns)
e.GET(p+"pipelinerun/:pipelineid/latest", PipelineGetLatestRun)
e.GET(p+"pipelinerun/:pipelineid/:runid/log", GetJobLogs)
e.POST(p+"pipelinerun/:pipelineid/:runid/stop", pipelineProvider.PipelineStop)
e.GET(p+"pipelinerun/:pipelineid/:runid", pipelineProvider.PipelineRunGet)
e.GET(p+"pipelinerun/:pipelineid", pipelineProvider.PipelineGetAllRuns)
e.GET(p+"pipelinerun/:pipelineid/latest", pipelineProvider.PipelineGetLatestRun)
e.GET(p+"pipelinerun/:pipelineid/:runid/log", pipelineProvider.GetJobLogs)

// Secrets
e.GET(p+"secrets", ListSecrets)
Expand All @@ -92,12 +81,16 @@ func InitHandlers(e *echo.Echo) error {
}

// Worker
e.GET(p+"worker/secret", GetWorkerRegisterSecret)
e.POST(p+"worker/register", RegisterWorker)
e.GET(p+"worker/status", GetWorkerStatusOverview)
e.GET(p+"worker", GetWorker)
e.DELETE(p+"worker/:workerid", DeregisterWorker)
e.POST(p+"worker/secret", ResetWorkerRegisterSecret)
// initialize the worker provider
workerProvider := workers.NewWorkerProvider(workers.Dependencies{
Scheduler: s.deps.Scheduler,
})
e.GET(p+"worker/secret", workerProvider.GetWorkerRegisterSecret)
e.POST(p+"worker/register", workerProvider.RegisterWorker)
e.GET(p+"worker/status", workerProvider.GetWorkerStatusOverview)
e.GET(p+"worker", workerProvider.GetWorker)
e.DELETE(p+"worker/:workerid", workerProvider.DeregisterWorker)
e.POST(p+"worker/secret", workerProvider.ResetWorkerRegisterSecret)

// Middleware
e.Use(middleware.Recover())
Expand Down
9 changes: 8 additions & 1 deletion handlers/hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,14 @@ func TestHookReceive(t *testing.T) {
if err != nil {
t.Fatalf("error creating data dir %v", err.Error())
}
pipelineService := pipeline.NewGaiaPipelineService(pipeline.Dependencies{
Scheduler: &mockScheduleService{},
})

handlerService := NewGaiaHandler(Dependencies{
Scheduler: &mockScheduleService{},
PipelineService: pipelineService,
})
defer func() {
gaia.Cfg = nil
_ = os.RemoveAll(dataDir)
Expand Down Expand Up @@ -84,7 +91,7 @@ func TestHookReceive(t *testing.T) {

ap.Append(p)

_ = InitHandlers(e)
_ = handlerService.InitHandlers(e)

t.Run("successfully extracting path information from payload", func(t *testing.T) {
payload, _ := ioutil.ReadFile(filepath.Join("fixtures", "hook_basic_push_payload.json"))
Expand Down
Loading

0 comments on commit ec28241

Please sign in to comment.