forked from gaia-pipeline/gaia
/
pipeline.go
206 lines (169 loc) · 5.54 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
package handlers
import (
"net/http"
"strconv"
"strings"
"time"
"github.com/gaia-pipeline/gaia"
"github.com/gaia-pipeline/gaia/pipeline"
"github.com/labstack/echo"
uuid "github.com/satori/go.uuid"
)
const (
// Split char to separate path from pipeline and name
pipelinePathSplitChar = "/"
)
// 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 {
p := &gaia.CreatePipeline{}
if err := c.Bind(p); 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()
// 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 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
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")
// The name could contain a path. Split it up
path := strings.Split(pName, pipelinePathSplitChar)
// Iterate all objects
for _, s := range path {
// Length should be correct
if len(s) < 1 || len(s) > 50 {
return c.String(http.StatusBadRequest, errPathLength.Error())
}
// TODO check if pipeline name is already in use
}
return nil
}
// PipelineGetAll returns all registered pipelines.
func PipelineGetAll(c echo.Context) error {
var pipelines []gaia.Pipeline
// Get all active pipelines
for pipeline := range pipeline.GlobalActivePipelines.Iter() {
pipelines = append(pipelines, pipeline)
}
// 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
var foundPipeline gaia.Pipeline
for pipeline := range pipeline.GlobalActivePipelines.Iter() {
if pipeline.ID == pipelineID {
foundPipeline = pipeline
}
}
if foundPipeline.Name != "" {
return c.JSON(http.StatusOK, foundPipeline)
}
// Pipeline not found
return c.String(http.StatusNotFound, errPipelineNotFound.Error())
}
// PipelineStart starts a pipeline by the given id.
// Afterwards it returns the created/scheduled pipeline run.
func PipelineStart(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
var foundPipeline gaia.Pipeline
for pipeline := range pipeline.GlobalActivePipelines.Iter() {
if pipeline.ID == pipelineID {
foundPipeline = pipeline
}
}
if foundPipeline.Name != "" {
pipelineRun, err := schedulerService.SchedulePipeline(&foundPipeline)
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
var pipelines []gaia.Pipeline
for pipeline := range pipeline.GlobalActivePipelines.Iter() {
pipelines = append(pipelines, pipeline)
}
// Iterate all pipelines
var pipelinesWithLatestRun []getAllWithLatestRun
for _, pipeline := range pipelines {
// Get the latest run by the given pipeline id
run, err := storeService.PipelineGetLatestRun(pipeline.ID)
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
// Append run if one exists
g := getAllWithLatestRun{}
g.Pipeline = pipeline
if run != nil {
g.PipelineRun = *run
}
// Append
pipelinesWithLatestRun = append(pipelinesWithLatestRun, g)
}
return c.JSON(http.StatusOK, pipelinesWithLatestRun)
}