forked from gaia-pipeline/gaia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline_run.go
180 lines (151 loc) · 5.07 KB
/
pipeline_run.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
package handlers
import (
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strconv"
"github.com/gaia-pipeline/gaia"
"github.com/gaia-pipeline/gaia/services"
"github.com/gaia-pipeline/gaia/workers/pipeline"
"github.com/labstack/echo"
)
const (
maxMaxBufferLen = 1024
)
// jobLogs represents the json format which is returned
// by GetJobLogs.
type jobLogs struct {
Log string `json:"log"`
Finished bool `json:"finished"`
}
// PipelineRunGet returns details about a specific pipeline run.
// Required parameters are pipelineid and runid.
func PipelineRunGet(c echo.Context) error {
// Convert string to int because id is int
storeService, _ := services.StorageService()
pipelineID, err := strconv.Atoi(c.Param("pipelineid"))
if err != nil {
return c.String(http.StatusBadRequest, errInvalidPipelineID.Error())
}
// Convert string to int because id is int
runID, err := strconv.Atoi(c.Param("runid"))
if err != nil {
return c.String(http.StatusBadRequest, errPipelineRunNotFound.Error())
}
// Find pipeline run in store
pipelineRun, err := storeService.PipelineGetRunByPipelineIDAndID(pipelineID, runID)
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
} else if pipelineRun == nil {
return c.String(http.StatusNotFound, errPipelineRunNotFound.Error())
}
// Return pipeline run
return c.JSON(http.StatusOK, pipelineRun)
}
// PipelineStop stops a running pipeline.
func PipelineStop(c echo.Context) error {
schedulerService, _ := services.SchedulerService()
// Get parameters and validate
pipelineID := c.Param("pipelineid")
pipelineRunID := c.Param("runid")
// Transform pipelineid to int
p, err := strconv.Atoi(pipelineID)
if err != nil {
return c.String(http.StatusBadRequest, "invalid pipeline id given")
}
// Transform pipelinerunid to int
r, err := strconv.Atoi(pipelineRunID)
if err != nil {
return c.String(http.StatusBadRequest, "invalid pipeline run id given")
}
// Look up pipeline for the given id
var foundPipeline gaia.Pipeline
for pipeline := range pipeline.GlobalActivePipelines.Iter() {
if pipeline.ID == p {
foundPipeline = pipeline
}
}
if foundPipeline.Name != "" {
err = schedulerService.StopPipelineRun(&foundPipeline, r)
if err != nil {
return c.String(http.StatusBadRequest, err.Error())
}
return c.String(http.StatusOK, "pipeline successfully stopped")
}
// Pipeline not found
return c.String(http.StatusNotFound, errPipelineNotFound.Error())
}
// PipelineGetAllRuns returns all runs about the given pipeline.
func PipelineGetAllRuns(c echo.Context) error {
// Convert string to int because id is int
storeService, _ := services.StorageService()
pipelineID, err := strconv.Atoi(c.Param("pipelineid"))
if err != nil {
return c.String(http.StatusBadRequest, errInvalidPipelineID.Error())
}
// Get all runs by the given pipeline id
runs, err := storeService.PipelineGetAllRuns(pipelineID)
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
return c.JSON(http.StatusOK, runs)
}
// 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
storeService, _ := services.StorageService()
pipelineID, err := strconv.Atoi(c.Param("pipelineid"))
if err != nil {
return c.String(http.StatusBadRequest, errInvalidPipelineID.Error())
}
// Get the latest run by the given pipeline id
run, err := storeService.PipelineGetLatestRun(pipelineID)
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
return c.JSON(http.StatusOK, run)
}
// GetJobLogs returns logs from a pipeline run.
//
// Required parameters:
// pipelineid - Related pipeline id
// pipelinerunid - Related pipeline run id
func GetJobLogs(c echo.Context) error {
// Get parameters and validate
storeService, _ := services.StorageService()
pipelineID := c.Param("pipelineid")
pipelineRunID := c.Param("runid")
// Transform pipelineid to int
p, err := strconv.Atoi(pipelineID)
if err != nil {
return c.String(http.StatusBadRequest, "invalid pipeline id given")
}
// Transform pipelinerunid to int
r, err := strconv.Atoi(pipelineRunID)
if err != nil {
return c.String(http.StatusBadRequest, "invalid pipeline run id given")
}
run, err := storeService.PipelineGetRunByPipelineIDAndID(p, r)
if err != nil {
return c.String(http.StatusBadRequest, "cannot find pipeline run with given pipeline id and pipeline run id")
}
// Create return object
jL := jobLogs{}
// Determine if job has been finished
if run.Status == gaia.RunFailed || run.Status == gaia.RunSuccess || run.Status == gaia.RunCancelled {
jL.Finished = true
}
// Check if log file exists
logFilePath := filepath.Join(gaia.Cfg.WorkspacePath, pipelineID, pipelineRunID, gaia.LogsFolderName, gaia.LogsFileName)
if _, err := os.Stat(logFilePath); err == nil {
content, err := ioutil.ReadFile(logFilePath)
if err != nil {
return c.String(http.StatusInternalServerError, "cannot read pipeline run log file")
}
// Convert logs
jL.Log = string(content)
}
// Return logs
return c.JSON(http.StatusOK, jL)
}