forked from gaia-pipeline/gaia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline_run.go
202 lines (170 loc) · 5.48 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package handlers
import (
"io/ioutil"
"net/http"
"os"
"path/filepath"
"sort"
"strconv"
"github.com/gaia-pipeline/gaia"
"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
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)
}
// PipelineGetAllRuns returns all runs about the given pipeline.
func PipelineGetAllRuns(c echo.Context) error {
// Convert string to int because id is int
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
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 jobs for a given job.
// If no jobID is given, a collection of all jobs logs will be returned.
//
// Required parameters:
// pipelineid - Related pipeline id
// pipelinerunid - Related pipeline run id
//
// Optional parameters:
// jobid - Job id
func GetJobLogs(c echo.Context) error {
// Get parameters and validate
pipelineID := c.Param("pipelineid")
pipelineRunID := c.Param("runid")
jobID := c.QueryParam("jobid")
// 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")
}
// Get pipeline run from store
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")
}
// jobID is not empty, just return the logs from this job
if jobID != "" {
for _, job := range run.Jobs {
if strconv.FormatUint(uint64(job.ID), 10) == jobID {
// Get logs
jL, err := getLogs(pipelineID, pipelineRunID, jobID, false)
if err != nil {
return c.String(http.StatusBadRequest, err.Error())
}
// Check if job is finished
if job.Status == gaia.JobSuccess || job.Status == gaia.JobFailed {
jL.Finished = true
}
// We always return an array.
// It makes a bit easier in the frontend.
jobLogsList := []jobLogs{}
jobLogsList = append(jobLogsList, *jL)
return c.JSON(http.StatusOK, jobLogsList)
}
}
// Logs for given job id not found
return c.String(http.StatusBadRequest, "cannot find job with given job id")
}
// Sort the slice. This is important for the order of the returned logs.
sort.Slice(run.Jobs, func(i, j int) bool {
return run.Jobs[i].Priority < run.Jobs[j].Priority
})
// Return a collection of all logs
jobs := []jobLogs{}
for _, job := range run.Jobs {
// Get logs
jL, err := getLogs(pipelineID, pipelineRunID, strconv.FormatUint(uint64(job.ID), 10), true)
if err != nil {
return c.String(http.StatusBadRequest, err.Error())
}
// No error but also no job logs. Job must be in the queue.
// We skip it so no error will break things.
if jL == nil {
continue
}
// Check if job is finished
if job.Status == gaia.JobSuccess || job.Status == gaia.JobFailed {
jL.Finished = true
}
jobs = append(jobs, *jL)
}
// Return logs
return c.JSON(http.StatusOK, jobs)
}
func getLogs(pipelineID, pipelineRunID, jobID string, getAllJobLogs bool) (*jobLogs, error) {
// Lookup log file
logFilePath := filepath.Join(gaia.Cfg.WorkspacePath, pipelineID, pipelineRunID, gaia.LogsFolderName, jobID)
// We only check if logs exist when a specific job log was requested.
// If we don't do this, get all job logs will fail during a pipeline run.
if _, err := os.Stat(logFilePath); os.IsNotExist(err) {
if !getAllJobLogs {
return nil, err
}
return nil, nil
}
// Read file
content, err := ioutil.ReadFile(logFilePath)
if err != nil {
return nil, err
}
// Create return struct
return &jobLogs{
Log: string(content),
}, nil
}