/
pipeline.go
498 lines (415 loc) · 14.2 KB
/
pipeline.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
package handlers
import (
"net/http"
"strconv"
"time"
"github.com/gaia-pipeline/gaia/helper/stringhelper"
"github.com/gaia-pipeline/gaia/security"
"github.com/gaia-pipeline/gaia"
"github.com/gaia-pipeline/gaia/services"
"github.com/gaia-pipeline/gaia/workers/pipeline"
"github.com/labstack/echo"
"github.com/robfig/cron"
uuid "github.com/satori/go.uuid"
)
// PipelineGitLSRemote checks for available git remote branches.
// This is the perfect way to check if we have access to a given repo.
func PipelineGitLSRemote(c echo.Context) error {
repo := &gaia.GitRepo{}
if err := c.Bind(repo); err != nil {
return c.String(http.StatusBadRequest, err.Error())
}
// Check for remote branches
err := pipeline.GitLSRemote(repo)
if err != nil {
return c.String(http.StatusForbidden, err.Error())
}
// Return branches
return c.JSON(http.StatusOK, repo.Branches)
}
// CreatePipeline accepts all data needed to create a pipeline.
// It then starts the create pipeline execution process async.
func CreatePipeline(c echo.Context) error {
storeService, _ := services.StorageService()
p := &gaia.CreatePipeline{}
if err := c.Bind(p); err != nil {
return c.String(http.StatusBadRequest, err.Error())
}
// Validate pipeline name
if err := pipeline.ValidatePipelineName(p.Pipeline.Name); err != nil {
return c.String(http.StatusBadRequest, err.Error())
}
// Set initial value
p.Created = time.Now()
p.StatusType = gaia.CreatePipelineRunning
p.ID = uuid.Must(uuid.NewV4(), nil).String()
// Add pipeline type tag if not already existent
if !stringhelper.IsContainedInSlice(p.Pipeline.Tags, p.Pipeline.Type.String(), true) {
p.Pipeline.Tags = append(p.Pipeline.Tags, p.Pipeline.Type.String())
}
// Save this pipeline to our store
err := storeService.CreatePipelinePut(p)
if err != nil {
gaia.Cfg.Logger.Debug("cannot put pipeline into store", "error", err.Error())
return c.String(http.StatusInternalServerError, err.Error())
}
// Cloning the repo and compiling the pipeline will be done async
go pipeline.CreatePipeline(p)
return c.JSON(http.StatusOK, nil)
}
// CreatePipelineGetAll returns a json array of
// all pipelines which are about to get compiled and
// all pipelines which have been compiled.
func CreatePipelineGetAll(c echo.Context) error {
// Get all create pipelines
storeService, _ := services.StorageService()
pipelineList, err := storeService.CreatePipelineGet()
if err != nil {
gaia.Cfg.Logger.Debug("cannot get create pipelines from store", "error", err.Error())
return c.String(http.StatusInternalServerError, err.Error())
}
// Return all create pipelines
return c.JSON(http.StatusOK, pipelineList)
}
// PipelineNameAvailable looks up if the given pipeline name is
// available and valid.
func PipelineNameAvailable(c echo.Context) error {
pName := c.QueryParam("name")
if err := pipeline.ValidatePipelineName(pName); err != nil {
return c.String(http.StatusBadRequest, err.Error())
}
return c.JSON(http.StatusOK, nil)
}
// PipelineGetAll returns all registered pipelines.
func PipelineGetAll(c echo.Context) error {
// Get all active pipelines
pipelines := pipeline.GlobalActivePipelines.GetAll()
// Obscure non-necessary information
for id := range pipelines {
obscurePipelineData(&pipelines[id])
}
// Return as json
return c.JSON(http.StatusOK, pipelines)
}
// PipelineGet accepts a pipeline id and returns the pipeline object.
func PipelineGet(c echo.Context) error {
pipelineIDStr := c.Param("pipelineid")
// Convert string to int because id is int
pipelineID, err := strconv.Atoi(pipelineIDStr)
if err != nil {
return c.String(http.StatusBadRequest, errInvalidPipelineID.Error())
}
// Look up pipeline for the given id
for _, p := range pipeline.GlobalActivePipelines.GetAll() {
if p.ID == pipelineID {
obscurePipelineData(&p)
return c.JSON(http.StatusOK, p)
}
}
return c.String(http.StatusNotFound, errPipelineNotFound.Error())
}
// PipelineUpdate updates the given pipeline.
func PipelineUpdate(c echo.Context) error {
storeService, _ := services.StorageService()
schedulerService, _ := services.SchedulerService()
p := gaia.Pipeline{}
if err := c.Bind(&p); err != nil {
return c.String(http.StatusBadRequest, err.Error())
}
// Look up pipeline for the given id
var foundPipeline gaia.Pipeline
for _, pipe := range pipeline.GlobalActivePipelines.GetAll() {
if pipe.ID == p.ID {
foundPipeline = pipe
break
}
}
if foundPipeline.Name == "" {
return c.String(http.StatusNotFound, errPipelineNotFound.Error())
}
// Check if the pipeline name was changed.
if foundPipeline.Name != p.Name {
// Pipeline name has been changed
currentName := foundPipeline.Name
// Rename binary
err := pipeline.RenameBinary(foundPipeline, p.Name)
if err != nil {
return c.String(http.StatusInternalServerError, errPipelineRename.Error())
}
// Update name and exec path
foundPipeline.Name = p.Name
foundPipeline.ExecPath = pipeline.GetExecPath(p)
// Update pipeline in store
err = storeService.PipelinePut(&foundPipeline)
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
// Update active pipelines
pipeline.GlobalActivePipelines.ReplaceByName(currentName, foundPipeline)
}
// Check if the periodic scheduling has been changed.
if !stringSliceEqual(foundPipeline.PeriodicSchedules, p.PeriodicSchedules) {
// We prevent side effects here and make sure
// that no scheduling is already running.
if foundPipeline.CronInst != nil {
foundPipeline.CronInst.Stop()
}
foundPipeline.CronInst = cron.New()
// Iterate over all cron schedules.
for _, schedule := range p.PeriodicSchedules {
err := foundPipeline.CronInst.AddFunc(schedule, func() {
_, err := schedulerService.SchedulePipeline(&foundPipeline, []*gaia.Argument{})
if err != nil {
gaia.Cfg.Logger.Error("cannot schedule pipeline from periodic schedule", "error", err, "pipeline", foundPipeline)
return
}
// Log scheduling information
gaia.Cfg.Logger.Info("pipeline has been automatically scheduled by periodic scheduling:", "name", foundPipeline.Name)
})
if err != nil {
return c.String(http.StatusBadRequest, err.Error())
}
}
// Update pipeline in store
err := storeService.PipelinePut(&foundPipeline)
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
// Start schedule process.
foundPipeline.CronInst.Start()
// Update active pipelines
pipeline.GlobalActivePipelines.Replace(foundPipeline)
}
return c.String(http.StatusOK, "Pipeline has been updated")
}
// stringSliceEqual is a small helper function
// which determines if two string slices are equal.
func stringSliceEqual(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
// 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 {
storeService, _ := services.StorageService()
pipelineIDStr := c.Param("pipelineid")
pipelineID, err := strconv.Atoi(pipelineIDStr)
if err != nil {
return c.String(http.StatusBadRequest, errInvalidPipelineID.Error())
}
// Look up pipeline for the given id
var foundPipeline gaia.Pipeline
var deletedPipelineIndex int
for index, p := range pipeline.GlobalActivePipelines.GetAll() {
if p.ID == pipelineID {
foundPipeline = p
deletedPipelineIndex = index
break
}
}
if foundPipeline.Name == "" {
return c.String(http.StatusNotFound, errPipelineNotFound.Error())
}
// Delete pipeline binary
err = pipeline.DeleteBinary(foundPipeline)
if err != nil {
return c.String(http.StatusInternalServerError, errPipelineDelete.Error())
}
// Delete pipeline from store
err = storeService.PipelineDelete(pipelineID)
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
// Remove from active pipelines
if err := pipeline.GlobalActivePipelines.Remove(deletedPipelineIndex); err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
return c.String(http.StatusOK, "Pipeline has been deleted")
}
// PipelineTrigger allows for a remote running of a pipeline.
// This endpoint does not require authentication. It will use a TOKEN
// that is specific to a pipeline. It can only be used by the `auto`
// user.
func PipelineTrigger(c echo.Context) error {
err := PipelineTriggerAuth(c)
if err != nil {
return c.String(http.StatusForbidden, "User rejected")
}
// Check here against the pipeline's token.
pipelineIDStr := c.Param("pipelineid")
pipelineToken := c.Param("pipelinetoken")
// Convert string to int because id is int
pipelineID, err := strconv.Atoi(pipelineIDStr)
if err != nil {
return c.String(http.StatusBadRequest, errInvalidPipelineID.Error())
}
// Look up pipeline for the given id
var foundPipeline gaia.Pipeline
for _, p := range pipeline.GlobalActivePipelines.GetAll() {
if p.ID == pipelineID {
foundPipeline = p
break
}
}
if foundPipeline.Name == "" {
return c.String(http.StatusBadRequest, "Pipeline not found.")
}
if foundPipeline.TriggerToken != pipelineToken {
return c.String(http.StatusForbidden, "Invalid remote trigger token.")
}
schedulerService, _ := services.SchedulerService()
var args []*gaia.Argument
_ = c.Bind(&args)
pipelineRun, err := schedulerService.SchedulePipeline(&foundPipeline, args)
if err != nil {
return c.String(http.StatusBadRequest, err.Error())
} else if pipelineRun != nil {
return c.String(http.StatusOK, "Trigger successful for pipeline: "+pipelineIDStr)
}
return c.String(http.StatusBadRequest, "Failed to trigger pipeline run.")
}
// PipelineResetToken generates a new remote trigger token for a given
// pipeline.
func PipelineResetToken(c echo.Context) error {
// Check here against the pipeline's token.
pipelineIDStr := c.Param("pipelineid")
// Convert string to int because id is int
pipelineID, err := strconv.Atoi(pipelineIDStr)
if err != nil {
return c.String(http.StatusBadRequest, errInvalidPipelineID.Error())
}
// Look up pipeline for the given id
var foundPipeline gaia.Pipeline
for _, p := range pipeline.GlobalActivePipelines.GetAll() {
if p.ID == pipelineID {
foundPipeline = p
break
}
}
if foundPipeline.Name == "" {
return c.String(http.StatusBadRequest, "Pipeline not found.")
}
foundPipeline.TriggerToken = security.GenerateRandomUUIDV5()
s, err := services.StorageService()
if err != nil {
return c.String(http.StatusInternalServerError, "Error getting store service.")
}
err = s.PipelinePut(&foundPipeline)
if err != nil {
return c.String(http.StatusInternalServerError, "Error while saving pipeline.")
}
return c.String(http.StatusOK, "Token successfully reset. To see, please open the pipeline's view.")
}
// PipelineTriggerAuth is a barrier before remote trigger which checks if
// the user is `auto`.
func PipelineTriggerAuth(c echo.Context) error {
// check headers
s, err := services.StorageService()
if err != nil {
return c.String(http.StatusInternalServerError, "Error getting store service.")
}
auto, err := s.UserGet("auto")
if err != nil {
return c.String(http.StatusInternalServerError, "Auto user not found.")
}
username, password, ok := c.Request().BasicAuth()
if !ok {
return c.String(http.StatusForbidden, "No authentication provided.")
}
if username != auto.Username || password != auto.TriggerToken {
return c.String(http.StatusBadRequest, "Auto username or password did not match.")
}
return nil
}
// PipelineStart starts a pipeline by the given id.
// It accepts arguments for the given pipeline.
// Afterwards it returns the created/scheduled pipeline run.
func PipelineStart(c echo.Context) error {
schedulerService, _ := services.SchedulerService()
pipelineIDStr := c.Param("pipelineid")
// Look for arguments.
// We do not check for errors here cause arguments are optional.
var args []*gaia.Argument
_ = c.Bind(&args)
// Convert string to int because id is int
pipelineID, err := strconv.Atoi(pipelineIDStr)
if err != nil {
return c.String(http.StatusBadRequest, errInvalidPipelineID.Error())
}
// Look up pipeline for the given id
var foundPipeline gaia.Pipeline
for _, p := range pipeline.GlobalActivePipelines.GetAll() {
if p.ID == pipelineID {
foundPipeline = p
break
}
}
if foundPipeline.Name != "" {
pipelineRun, err := schedulerService.SchedulePipeline(&foundPipeline, args)
if err != nil {
return c.String(http.StatusBadRequest, err.Error())
} else if pipelineRun != nil {
return c.JSON(http.StatusCreated, pipelineRun)
}
}
// Pipeline not found
return c.String(http.StatusNotFound, errPipelineNotFound.Error())
}
type getAllWithLatestRun struct {
Pipeline gaia.Pipeline `json:"p"`
PipelineRun gaia.PipelineRun `json:"r"`
}
// PipelineGetAllWithLatestRun returns the latest of all registered pipelines
// included with the latest run.
func PipelineGetAllWithLatestRun(c echo.Context) error {
// Get all active pipelines
storeService, _ := services.StorageService()
pipelines := pipeline.GlobalActivePipelines.GetAll()
// Iterate all pipelines
var pipelinesWithLatestRun []getAllWithLatestRun
for _, p := range pipelines {
// Get the latest run by the given pipeline id
run, err := storeService.PipelineGetLatestRun(p.ID)
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
// Append run if one exists
g := getAllWithLatestRun{}
obscurePipelineData(&p)
g.Pipeline = p
if run != nil {
g.PipelineRun = *run
}
// Append
pipelinesWithLatestRun = append(pipelinesWithLatestRun, g)
}
return c.JSON(http.StatusOK, pipelinesWithLatestRun)
}
// PipelineCheckPeriodicSchedules validates the added periodic schedules.
func PipelineCheckPeriodicSchedules(c echo.Context) error {
var pSchedules []string
if err := c.Bind(&pSchedules); err != nil {
return c.String(http.StatusBadRequest, err.Error())
}
// Create new test cron spec parser.
cr := cron.New()
// Check every cron entry.
for _, entry := range pSchedules {
if err := cr.AddFunc(entry, func() {}); err != nil {
return c.String(http.StatusBadRequest, err.Error())
}
}
// All entries are valid.
return c.JSON(http.StatusOK, nil)
}
// obscurePipelineData obscures pipeline data from the given pipeline object
func obscurePipelineData(p *gaia.Pipeline) {
p.ExecPath = ""
}