-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
pipeline_runs_controller.go
184 lines (160 loc) · 5.29 KB
/
pipeline_runs_controller.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
package web
import (
"encoding/json"
"io"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
uuid "github.com/satori/go.uuid"
"github.com/smartcontractkit/chainlink/v2/core/logger/audit"
"github.com/smartcontractkit/chainlink/v2/core/services/chainlink"
"github.com/smartcontractkit/chainlink/v2/core/services/job"
"github.com/smartcontractkit/chainlink/v2/core/services/pipeline"
"github.com/smartcontractkit/chainlink/v2/core/services/webhook"
"github.com/smartcontractkit/chainlink/v2/core/web/auth"
"github.com/smartcontractkit/chainlink/v2/core/web/presenters"
)
// PipelineRunsController manages V2 job run requests.
type PipelineRunsController struct {
App chainlink.Application
}
// Index returns all pipeline runs for a job.
// Example:
// "GET <application>/jobs/:ID/runs"
func (prc *PipelineRunsController) Index(c *gin.Context, size, page, offset int) {
id := c.Param("ID")
// Temporary: if no size is passed in, use a large page size. Remove once frontend can handle pagination
if c.Query("size") == "" {
size = 1000
}
var pipelineRuns []pipeline.Run
var count int
var err error
if id == "" {
pipelineRuns, count, err = prc.App.JobORM().PipelineRuns(nil, offset, size)
} else {
jobSpec := job.Job{}
err = jobSpec.SetID(c.Param("ID"))
if err != nil {
jsonAPIError(c, http.StatusUnprocessableEntity, err)
return
}
pipelineRuns, count, err = prc.App.JobORM().PipelineRuns(&jobSpec.ID, offset, size)
}
if err != nil {
jsonAPIError(c, http.StatusInternalServerError, err)
return
}
res := presenters.NewPipelineRunResources(pipelineRuns, prc.App.GetLogger())
paginatedResponse(c, "pipelineRun", size, page, res, count, err)
}
// Show returns a specified pipeline run.
// Example:
// "GET <application>/jobs/:ID/runs/:runID"
func (prc *PipelineRunsController) Show(c *gin.Context) {
pipelineRun := pipeline.Run{}
err := pipelineRun.SetID(c.Param("runID"))
if err != nil {
jsonAPIError(c, http.StatusUnprocessableEntity, err)
return
}
pipelineRun, err = prc.App.PipelineORM().FindRun(pipelineRun.ID)
if err != nil {
jsonAPIError(c, http.StatusInternalServerError, err)
return
}
res := presenters.NewPipelineRunResource(pipelineRun, prc.App.GetLogger())
jsonAPIResponse(c, res, "pipelineRun")
}
// Create triggers a pipeline run for a job.
// Example:
// "POST <application>/jobs/:ID/runs"
func (prc *PipelineRunsController) Create(c *gin.Context) {
respondWithPipelineRun := func(jobRunID int64) {
pipelineRun, err := prc.App.PipelineORM().FindRun(jobRunID)
if err != nil {
jsonAPIError(c, http.StatusInternalServerError, err)
return
}
res := presenters.NewPipelineRunResource(pipelineRun, prc.App.GetLogger())
jsonAPIResponse(c, res, "pipelineRun")
}
bodyBytes, err := io.ReadAll(c.Request.Body)
if err != nil {
jsonAPIError(c, http.StatusUnprocessableEntity, err)
return
}
idStr := c.Param("ID")
user, isUser := auth.GetAuthenticatedUser(c)
ei, _ := auth.GetAuthenticatedExternalInitiator(c)
authorizer := webhook.NewAuthorizer(prc.App.GetSqlxDB().DB, user, ei)
// Is it a UUID? Then process it as a webhook job
jobUUID, err := uuid.FromString(idStr)
if err == nil {
canRun, err2 := authorizer.CanRun(c.Request.Context(), prc.App.GetConfig(), jobUUID)
if err2 != nil {
jsonAPIError(c, http.StatusInternalServerError, err2)
return
}
if canRun {
jobRunID, err3 := prc.App.RunWebhookJobV2(c.Request.Context(), jobUUID, string(bodyBytes), pipeline.JSONSerializable{})
if errors.Is(err3, webhook.ErrJobNotExists) {
jsonAPIError(c, http.StatusNotFound, err3)
return
} else if err3 != nil {
jsonAPIError(c, http.StatusInternalServerError, err3)
return
}
respondWithPipelineRun(jobRunID)
} else {
jsonAPIError(c, http.StatusUnauthorized, errors.Errorf("external initiator %s is not allowed to run job %s", ei.Name, jobUUID))
}
return
}
// only users are allowed to run jobs using int IDs - EIs not allowed
if isUser {
// Is it an int32? Then process it regardless of type
var jobID int32
jobID64, err := strconv.ParseInt(idStr, 10, 32)
if err == nil {
jobID = int32(jobID64)
jobRunID, err := prc.App.RunJobV2(c.Request.Context(), jobID, nil)
if err != nil {
jsonAPIError(c, http.StatusInternalServerError, err)
return
}
respondWithPipelineRun(jobRunID)
return
}
}
jsonAPIError(c, http.StatusUnprocessableEntity, errors.New("bad job ID"))
}
// Resume finishes a task and resumes the pipeline run.
// Example:
// "PATCH <application>/jobs/:ID/runs/:runID"
func (prc *PipelineRunsController) Resume(c *gin.Context) {
taskID, err := uuid.FromString(c.Param("runID"))
if err != nil {
jsonAPIError(c, http.StatusUnprocessableEntity, err)
return
}
rr := pipeline.ResumeRequest{}
decoder := json.NewDecoder(c.Request.Body)
err = errors.Wrap(decoder.Decode(&rr), "failed to unmarshal JSON body")
if err != nil {
jsonAPIError(c, http.StatusUnprocessableEntity, err)
return
}
result, err := rr.ToResult()
if err != nil {
jsonAPIError(c, http.StatusUnprocessableEntity, err)
return
}
if err := prc.App.ResumeJobV2(c.Request.Context(), taskID, result); err != nil {
jsonAPIError(c, http.StatusInternalServerError, err)
return
}
prc.App.GetAuditLogger().Audit(audit.UnauthedRunResumed, map[string]interface{}{"runID": c.Param("runID")})
c.Status(http.StatusOK)
}