-
Notifications
You must be signed in to change notification settings - Fork 414
/
workflow_template_bulk.go
345 lines (311 loc) · 10.6 KB
/
workflow_template_bulk.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
package api
import (
"context"
"fmt"
"strconv"
"time"
"github.com/ovh/cds/engine/api/application"
"github.com/ovh/cds/engine/api/ascode"
"github.com/ovh/cds/engine/api/authentication"
"github.com/ovh/cds/engine/api/operation"
"github.com/ovh/cds/engine/api/project"
"github.com/ovh/cds/engine/api/workflow"
"github.com/ovh/cds/engine/api/workflowtemplate"
"github.com/ovh/cds/sdk"
"github.com/ovh/cds/sdk/exportentities"
"github.com/ovh/cds/sdk/telemetry"
"github.com/rockbears/log"
"go.opencensus.io/trace"
)
func (api *API) WorkflowTemplateBulk(ctx context.Context, tick time.Duration, chanOperation chan WorkflowTemplateBulkOperation) {
ticker := time.NewTicker(tick)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
if ctx.Err() != nil {
log.Error(ctx, "%v", ctx.Err())
}
return
case <-ticker.C:
bs, err := workflowtemplate.GetBulksPending(ctx, api.mustDBWithCtx(ctx))
if err != nil {
log.ErrorWithStackTrace(ctx, err)
continue
}
for _, b := range bs {
api.GoRoutines.Exec(api.Router.Background, "workflowTemplateBulk-"+strconv.FormatInt(b.ID, 10),
func(ctx context.Context) {
ctx = telemetry.New(ctx, api, "api.workflowTemplateBulk", nil, trace.SpanKindUnspecified)
if err := api.workflowTemplateBulk(ctx, b.ID, chanOperation); err != nil {
log.ErrorWithStackTrace(ctx, sdk.WrapError(err, "error on workflow template bulk %d", b.ID))
}
},
)
}
}
}
}
type WorkflowTemplateBulkOperations []WorkflowTemplateBulkOperation
func (o *WorkflowTemplateBulkOperations) Push(op WorkflowTemplateBulkOperation) {
*o = append(*o, op)
}
func (o *WorkflowTemplateBulkOperations) Pop() WorkflowTemplateBulkOperation {
old := *o
n := len(old)
x := old[n-1]
*o = old[0 : n-1]
return x
}
type WorkflowTemplateBulkOperation struct {
BulkID int64
AuthConsumerID string
WorkflowTemplateID int64
Operation sdk.WorkflowTemplateBulkOperation
chanResult chan sdk.WorkflowTemplateBulkOperation
}
func (api *API) workflowTemplateBulk(ctx context.Context, bulkID int64, chanOperation chan WorkflowTemplateBulkOperation) error {
tx, err := api.mustDB().Begin()
if err != nil {
return sdk.WithStack(err)
}
defer tx.Rollback() // nolint
b, err := workflowtemplate.GetAndLockBulkByID(ctx, tx, bulkID)
if err != nil {
return err
}
b.Status = sdk.OperationStatusProcessing
if err := workflowtemplate.UpdateBulk(tx, b); err != nil {
return err
}
if err := tx.Commit(); err != nil {
return sdk.WithStack(err)
}
chanResult := make(chan sdk.WorkflowTemplateBulkOperation)
defer close(chanResult)
operationTodo := make(WorkflowTemplateBulkOperations, 0, len(b.Operations))
// Enqueue operations
for _, o := range b.Operations {
if o.Status != sdk.OperationStatusPending && o.Status != sdk.OperationStatusProcessing {
continue
}
operationTodo.Push(WorkflowTemplateBulkOperation{
BulkID: b.ID,
AuthConsumerID: b.AuthConsumerID,
WorkflowTemplateID: b.WorkflowTemplateID,
Operation: o,
chanResult: chanResult,
})
}
if len(operationTodo) == 0 {
b.Status = sdk.OperationStatusDone
return workflowtemplate.UpdateBulk(api.mustDBWithCtx(ctx), b)
}
if b.Parallel {
for i := 0; i < int(api.Config.Workflow.TemplateBulkRunnerCount) && len(operationTodo) > 0; i++ {
chanOperation <- operationTodo.Pop()
}
} else {
chanOperation <- operationTodo.Pop()
}
// Wait for all operation to complete, if context is cancelled, set bulk status back to pending so antother API will be able to end the process
for {
select {
case res := <-chanResult:
log.Info(ctx, "workflowTemplateBulk> processing bulk with %d: operation result %s/%s with status %s", b.ID, res.Request.ProjectKey, res.Request.WorkflowName, res.Status)
b.UpdateOperation(res)
log.Info(ctx, "workflowTemplateBulk> processing bulk with %d: updating status to %s", b.ID, b.Status)
if err := workflowtemplate.UpdateBulk(api.mustDBWithCtx(ctx), b); err != nil {
return err
}
if b.Status == sdk.OperationStatusDone {
return nil
}
if (res.Status == sdk.OperationStatusDone || res.Status == sdk.OperationStatusError) && len(operationTodo) > 0 {
if b.Parallel {
for i := 0; i < int(api.Config.Workflow.TemplateBulkRunnerCount) && len(operationTodo) > 0; i++ {
chanOperation <- operationTodo.Pop()
}
} else {
chanOperation <- operationTodo.Pop()
}
}
case <-ctx.Done():
b.Status = sdk.OperationStatusPending
return workflowtemplate.UpdateBulk(api.mustDBWithCtx(ctx), b)
}
}
}
func (api *API) WorkflowTemplateBulkOperation(ctx context.Context, chanOperation chan WorkflowTemplateBulkOperation) {
for i := int64(0); i < api.Config.Workflow.TemplateBulkRunnerCount; i++ {
api.GoRoutines.RunWithRestart(ctx, fmt.Sprintf("api.WorkflowTemplateBulkOperation-%d", i), func(ctx context.Context) {
for {
select {
case <-ctx.Done():
return
case op := <-chanOperation:
api.workflowTemplateBulkOperation(ctx, op)
}
}
})
}
}
func (api *API) workflowTemplateBulkOperation(ctx context.Context, op WorkflowTemplateBulkOperation) {
if op.Operation.Status != sdk.OperationStatusPending {
return
}
op.Operation.Status = sdk.OperationStatusProcessing
op.chanResult <- op.Operation
errorDefer := func(err error) error {
if err != nil {
err = sdk.WrapError(err, "error occurred in template bulk with id %d", op.BulkID)
ctx = sdk.ContextWithStacktrace(ctx, err)
log.Error(ctx, "%v", err)
op.Operation.Status = sdk.OperationStatusError
op.Operation.Error = fmt.Sprintf("%s", sdk.Cause(err))
op.chanResult <- op.Operation
}
return nil
}
consumer, err := authentication.LoadUserConsumerByID(ctx, api.mustDB(), op.AuthConsumerID,
authentication.LoadUserConsumerOptions.WithAuthentifiedUserWithContacts,
authentication.LoadUserConsumerOptions.WithConsumerGroups)
if err != nil {
if errD := errorDefer(err); errD != nil {
log.Error(ctx, "%v", errD)
}
return
}
// load project with key
p, err := project.Load(ctx, api.mustDB(), op.Operation.Request.ProjectKey,
project.LoadOptions.WithGroups,
project.LoadOptions.WithApplications,
project.LoadOptions.WithEnvironments,
project.LoadOptions.WithPipelines,
project.LoadOptions.WithApplicationWithDeploymentStrategies,
project.LoadOptions.WithIntegrations,
project.LoadOptions.WithClearKeys,
)
if err != nil {
if errD := errorDefer(err); errD != nil {
log.Error(ctx, "%v", errD)
}
return
}
wt, err := workflowtemplate.LoadByID(ctx, api.mustDB(), op.WorkflowTemplateID, workflowtemplate.LoadOptions.Default)
if err != nil {
if errD := errorDefer(err); errD != nil {
log.Error(ctx, "%v", errD)
}
return
}
// apply and import workflow
data := exportentities.WorkflowComponents{
Template: exportentities.TemplateInstance{
Name: op.Operation.Request.WorkflowName,
From: wt.PathWithVersion(),
Parameters: op.Operation.Request.Parameters,
},
}
// In case we want to update a workflow that is ascode, we want to create a PR instead of pushing directly the new workflow.
wti, err := workflowtemplate.LoadInstanceByTemplateIDAndProjectIDAndRequestWorkflowName(ctx, api.mustDB(), wt.ID, p.ID, data.Template.Name)
if err != nil && !sdk.ErrorIs(err, sdk.ErrNotFound) {
if errD := errorDefer(err); errD != nil {
log.Error(ctx, "%v", errD)
}
return
}
if wti != nil && wti.WorkflowID != nil {
existingWorkflow, err := workflow.LoadByID(ctx, api.mustDB(), api.Cache, *p, *wti.WorkflowID, workflow.LoadOptions{})
if err != nil {
if errD := errorDefer(err); errD != nil {
log.Error(ctx, "%v", errD)
}
return
}
if existingWorkflow.FromRepository != "" {
var rootApp *sdk.Application
if existingWorkflow.WorkflowData.Node.Context != nil && existingWorkflow.WorkflowData.Node.Context.ApplicationID != 0 {
rootApp, err = application.LoadByIDWithClearVCSStrategyPassword(ctx, api.mustDB(), existingWorkflow.WorkflowData.Node.Context.ApplicationID)
if err != nil {
if errD := errorDefer(err); errD != nil {
log.Error(ctx, "%v", errD)
}
return
}
}
if rootApp == nil {
if errD := errorDefer(sdk.NewErrorFrom(sdk.ErrWrongRequest, "cannot find the root application of the workflow")); errD != nil {
log.Error(ctx, "%v", errD)
}
return
}
if op.Operation.Request.Branch == "" || op.Operation.Request.Message == "" {
if errD := errorDefer(sdk.NewErrorFrom(sdk.ErrWrongRequest, "missing branch or message data")); errD != nil {
log.Error(ctx, "%v", errD)
}
return
}
tx, err := api.mustDB().Begin()
if err != nil {
if errD := errorDefer(err); errD != nil {
log.Error(ctx, "%v", errD)
}
return
}
ope, err := operation.PushOperationUpdate(ctx, tx, api.Cache, *p, data, rootApp.VCSServer, rootApp.RepositoryFullname, op.Operation.Request.Branch, op.Operation.Request.Message, rootApp.RepositoryStrategy, consumer)
if err != nil {
tx.Rollback() // nolint
if errD := errorDefer(err); errD != nil {
log.Error(ctx, "%v", errD)
}
return
}
if err := tx.Commit(); err != nil {
tx.Rollback() // nolint
if errD := errorDefer(err); errD != nil {
log.Error(ctx, "%v", errD)
}
return
}
ed := ascode.EntityData{
Name: existingWorkflow.Name,
ID: existingWorkflow.ID,
Type: ascode.WorkflowEvent,
FromRepo: existingWorkflow.FromRepository,
OperationUUID: ope.UUID,
}
ascode.UpdateAsCodeResult(ctx, api.mustDB(), api.Cache, api.GoRoutines, *p, *existingWorkflow, *rootApp, ed, consumer)
op.Operation.Status = sdk.OperationStatusDone
op.chanResult <- op.Operation
}
}
mods := []workflowtemplate.TemplateRequestModifierFunc{
workflowtemplate.TemplateRequestModifiers.DefaultKeys(*p),
}
_, wti, err = workflowtemplate.CheckAndExecuteTemplate(ctx, api.mustDB(), api.Cache, *consumer, *p, &data, mods...)
if err != nil {
if errD := errorDefer(err); errD != nil {
log.Error(ctx, "%v", errD)
}
return
}
_, wkf, _, _, err := workflow.Push(ctx, api.mustDB(), api.Cache, p, data, nil, consumer, project.DecryptWithBuiltinKey)
if err != nil {
if sdk.ErrorIs(err, sdk.ErrEnvironmentExist) {
err = sdk.NewErrorFrom(err, "conflict when creating an environment with same name on multiple workflow")
}
if errD := errorDefer(sdk.WrapError(err, "cannot push generated workflow")); errD != nil {
log.Error(ctx, "%v", errD)
}
return
}
if err := workflowtemplate.UpdateTemplateInstanceWithWorkflow(ctx, api.mustDB(), *wkf, *consumer, wti); err != nil {
if errD := errorDefer(err); errD != nil {
log.Error(ctx, "%v", errD)
}
return
}
op.Operation.Status = sdk.OperationStatusDone
op.chanResult <- op.Operation
}