forked from go-vela/worker
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.go
More file actions
621 lines (522 loc) · 17.4 KB
/
build.go
File metadata and controls
621 lines (522 loc) · 17.4 KB
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
// Copyright (c) 2022 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.
package linux
import (
"context"
"encoding/json"
"fmt"
"sync"
"time"
"golang.org/x/sync/errgroup"
"github.com/go-vela/types/constants"
"github.com/go-vela/worker/internal/build"
"github.com/go-vela/worker/internal/step"
)
// CreateBuild configures the build for execution.
func (c *client) CreateBuild(ctx context.Context) error {
// defer taking a snapshot of the build
//
// https://pkg.go.dev/github.com/go-vela/worker/internal/build#Snapshot
defer func() { build.Snapshot(c.build, c.Vela, c.err, c.Logger, c.repo) }()
// update the build fields
c.build.SetStatus(constants.StatusRunning)
c.build.SetStarted(time.Now().UTC().Unix())
c.build.SetHost(c.Hostname)
c.build.SetDistribution(c.Driver())
c.build.SetRuntime(c.Runtime.Driver())
c.Logger.Info("uploading build state")
// send API call to update the build
//
// https://pkg.go.dev/github.com/go-vela/sdk-go/vela?tab=doc#BuildService.Update
c.build, _, c.err = c.Vela.Build.Update(c.repo.GetOrg(), c.repo.GetName(), c.build)
if c.err != nil {
return fmt.Errorf("unable to upload build state: %w", c.err)
}
// setup the runtime build
c.err = c.Runtime.SetupBuild(ctx, c.pipeline)
if c.err != nil {
return fmt.Errorf("unable to setup build %s: %w", c.pipeline.ID, c.err)
}
// load the init step from the pipeline
//
// https://pkg.go.dev/github.com/go-vela/worker/internal/step#LoadInit
c.init, c.err = step.LoadInit(c.pipeline)
if c.err != nil {
return fmt.Errorf("unable to load init step from pipeline: %w", c.err)
}
c.Logger.Infof("creating %s step", c.init.Name)
// create the step
c.err = c.CreateStep(ctx, c.init)
if c.err != nil {
return fmt.Errorf("unable to create %s step: %w", c.init.Name, c.err)
}
c.Logger.Infof("planning %s step", c.init.Name)
// plan the step
c.err = c.PlanStep(ctx, c.init)
if c.err != nil {
return fmt.Errorf("unable to plan %s step: %w", c.init.Name, c.err)
}
return c.err
}
// PlanBuild prepares the build for execution.
func (c *client) PlanBuild(ctx context.Context) error {
// defer taking a snapshot of the build
//
// https://pkg.go.dev/github.com/go-vela/worker/internal/build#Snapshot
defer func() { build.Snapshot(c.build, c.Vela, c.err, c.Logger, c.repo) }()
// load the init step from the client
//
// https://pkg.go.dev/github.com/go-vela/worker/internal/step#Load
_init, err := step.Load(c.init, &c.steps)
if err != nil {
return err
}
// load the logs for the init step from the client
//
// https://pkg.go.dev/github.com/go-vela/worker/internal/step#LoadLogs
_log, err := step.LoadLogs(c.init, &c.stepLogs)
if err != nil {
return err
}
// defer taking a snapshot of the init step
//
// https://pkg.go.dev/github.com/go-vela/worker/internal/step#SnapshotInit
defer func() { step.SnapshotInit(c.init, c.build, c.Vela, c.Logger, c.repo, _init, _log) }()
c.Logger.Info("creating network")
// create the runtime network for the pipeline
c.err = c.Runtime.CreateNetwork(ctx, c.pipeline)
if c.err != nil {
return fmt.Errorf("unable to create network: %w", c.err)
}
// update the init log with progress
//
// https://pkg.go.dev/github.com/go-vela/types/library?tab=doc#Log.AppendData
_log.AppendData([]byte("> Inspecting runtime network...\n"))
// inspect the runtime network for the pipeline
network, err := c.Runtime.InspectNetwork(ctx, c.pipeline)
if err != nil {
c.err = err
return fmt.Errorf("unable to inspect network: %w", err)
}
// update the init log with network information
//
// https://pkg.go.dev/github.com/go-vela/types/library?tab=doc#Log.AppendData
_log.AppendData(network)
c.Logger.Info("creating volume")
// create the runtime volume for the pipeline
c.err = c.Runtime.CreateVolume(ctx, c.pipeline)
if c.err != nil {
return fmt.Errorf("unable to create volume: %w", c.err)
}
// update the init log with progress
//
// https://pkg.go.dev/github.com/go-vela/types/library?tab=doc#Log.AppendData
_log.AppendData([]byte("> Inspecting runtime volume...\n"))
// inspect the runtime volume for the pipeline
volume, err := c.Runtime.InspectVolume(ctx, c.pipeline)
if err != nil {
c.err = err
return fmt.Errorf("unable to inspect volume: %w", err)
}
// update the init log with volume information
//
// https://pkg.go.dev/github.com/go-vela/types/library?tab=doc#Log.AppendData
_log.AppendData(volume)
// update the init log with progress
//
// https://pkg.go.dev/github.com/go-vela/types/library?tab=doc#Log.AppendData
_log.AppendData([]byte("> Preparing secrets...\n"))
// iterate through each secret provided in the pipeline
for _, secret := range c.pipeline.Secrets {
// ignore pulling secrets coming from plugins
if !secret.Origin.Empty() {
continue
}
c.Logger.Infof("pulling %s %s secret %s", secret.Engine, secret.Type, secret.Name)
s, err := c.secret.pull(secret)
if err != nil {
c.err = err
return fmt.Errorf("unable to pull secrets: %w", err)
}
_log.AppendData([]byte(
fmt.Sprintf("$ vela view secret --secret.engine %s --secret.type %s --org %s --repo %s --name %s \n",
secret.Engine, secret.Type, s.GetOrg(), s.GetRepo(), s.GetName())))
sRaw, err := json.MarshalIndent(s.Sanitize(), "", " ")
if err != nil {
c.err = err
return fmt.Errorf("unable to decode secret: %w", err)
}
_log.AppendData(append(sRaw, "\n"...))
// add secret to the map
c.Secrets[secret.Name] = s
}
return nil
}
// AssembleBuild prepares the containers within a build for execution.
//
// nolint: funlen // ignore function length due to comments and logging messages
func (c *client) AssembleBuild(ctx context.Context) error {
// defer taking a snapshot of the build
//
// https://pkg.go.dev/github.com/go-vela/worker/internal/build#Snapshot
defer func() { build.Snapshot(c.build, c.Vela, c.err, c.Logger, c.repo) }()
// load the init step from the client
//
// https://pkg.go.dev/github.com/go-vela/worker/internal/step#Load
_init, err := step.Load(c.init, &c.steps)
if err != nil {
return err
}
// load the logs for the init step from the client
//
// https://pkg.go.dev/github.com/go-vela/worker/internal/step#LoadLogs
_log, err := step.LoadLogs(c.init, &c.stepLogs)
if err != nil {
return err
}
// defer an upload of the init step
//
// https://pkg.go.dev/github.com/go-vela/worker/internal/step#Upload
defer func() { step.Upload(c.init, c.build, c.Vela, c.Logger, c.repo, _init) }()
defer func() {
c.Logger.Infof("uploading %s step logs", c.init.Name)
// send API call to update the logs for the step
//
// https://pkg.go.dev/github.com/go-vela/sdk-go/vela?tab=doc#LogService.UpdateStep
_log, _, err = c.Vela.Log.UpdateStep(c.repo.GetOrg(), c.repo.GetName(), c.build.GetNumber(), c.init.Number, _log)
if err != nil {
c.Logger.Errorf("unable to upload %s logs: %v", c.init.Name, err)
}
}()
// update the init log with progress
//
// https://pkg.go.dev/github.com/go-vela/types/library?tab=doc#Log.AppendData
_log.AppendData([]byte("> Preparing service images...\n"))
// create the services for the pipeline
for _, s := range c.pipeline.Services {
// TODO: remove this; but we need it for tests
s.Detach = true
c.Logger.Infof("creating %s service", s.Name)
// create the service
c.err = c.CreateService(ctx, s)
if c.err != nil {
return fmt.Errorf("unable to create %s service: %w", s.Name, c.err)
}
c.Logger.Infof("inspecting %s service", s.Name)
// inspect the service image
image, err := c.Runtime.InspectImage(ctx, s)
if err != nil {
c.err = err
return fmt.Errorf("unable to inspect %s service: %w", s.Name, err)
}
// update the init log with service image info
//
// https://pkg.go.dev/github.com/go-vela/types/library?tab=doc#Log.AppendData
_log.AppendData(image)
}
// update the init log with progress
//
// https://pkg.go.dev/github.com/go-vela/types/library?tab=doc#Log.AppendData
_log.AppendData([]byte("> Preparing stage images...\n"))
// create the stages for the pipeline
for _, s := range c.pipeline.Stages {
// TODO: remove hardcoded reference
//
// nolint: goconst // ignore making a constant for now
if s.Name == "init" {
continue
}
c.Logger.Infof("creating %s stage", s.Name)
// create the stage
c.err = c.CreateStage(ctx, s)
if c.err != nil {
return fmt.Errorf("unable to create %s stage: %w", s.Name, c.err)
}
}
// update the init log with progress
//
// https://pkg.go.dev/github.com/go-vela/types/library?tab=doc#Log.AppendData
_log.AppendData([]byte("> Preparing step images...\n"))
// create the steps for the pipeline
for _, s := range c.pipeline.Steps {
// TODO: remove hardcoded reference
if s.Name == "init" {
continue
}
c.Logger.Infof("creating %s step", s.Name)
// create the step
c.err = c.CreateStep(ctx, s)
if c.err != nil {
return fmt.Errorf("unable to create %s step: %w", s.Name, c.err)
}
c.Logger.Infof("inspecting %s step", s.Name)
// inspect the step image
image, err := c.Runtime.InspectImage(ctx, s)
if err != nil {
c.err = err
return fmt.Errorf("unable to inspect %s step: %w", s.Name, c.err)
}
// update the init log with step image info
//
// https://pkg.go.dev/github.com/go-vela/types/library?tab=doc#Log.AppendData
_log.AppendData(image)
}
// update the init log with progress
//
// https://pkg.go.dev/github.com/go-vela/types/library?tab=doc#Log.AppendData
_log.AppendData([]byte("> Preparing secret images...\n"))
// create the secrets for the pipeline
for _, s := range c.pipeline.Secrets {
// skip over non-plugin secrets
if s.Origin.Empty() {
continue
}
c.Logger.Infof("creating %s secret", s.Origin.Name)
// create the service
c.err = c.secret.create(ctx, s.Origin)
if c.err != nil {
return fmt.Errorf("unable to create %s secret: %w", s.Origin.Name, c.err)
}
c.Logger.Infof("inspecting %s secret", s.Origin.Name)
// inspect the service image
image, err := c.Runtime.InspectImage(ctx, s.Origin)
if err != nil {
c.err = err
return fmt.Errorf("unable to inspect %s secret: %w", s.Origin.Name, err)
}
// update the init log with secret image info
//
// https://pkg.go.dev/github.com/go-vela/types/library?tab=doc#Log.AppendData
_log.AppendData(image)
}
// inspect the runtime build (eg a kubernetes pod) for the pipeline
buildOutput, err := c.Runtime.InspectBuild(ctx, c.pipeline)
if err != nil {
c.err = err
return fmt.Errorf("unable to inspect build: %w", err)
}
if len(buildOutput) > 0 {
// update the init log with progress
// (an empty value allows the runtime to opt out of providing this)
//
// https://pkg.go.dev/github.com/go-vela/types/library?tab=doc#Log.AppendData
_log.AppendData(buildOutput)
}
// assemble runtime build just before any containers execute
c.err = c.Runtime.AssembleBuild(ctx, c.pipeline)
if c.err != nil {
return fmt.Errorf("unable to assemble runtime build %s: %w", c.pipeline.ID, c.err)
}
// update the init log with progress
//
// https://pkg.go.dev/github.com/go-vela/types/library?tab=doc#Log.AppendData
_log.AppendData([]byte("> Executing secret images...\n"))
c.Logger.Info("executing secret images")
// execute the secret
c.err = c.secret.exec(ctx, &c.pipeline.Secrets)
if c.err != nil {
return fmt.Errorf("unable to execute secret: %w", c.err)
}
return c.err
}
// ExecBuild runs a pipeline for a build.
func (c *client) ExecBuild(ctx context.Context) error {
// defer an upload of the build
//
// https://pkg.go.dev/github.com/go-vela/worker/internal/build#Upload
defer func() { build.Upload(c.build, c.Vela, c.err, c.Logger, c.repo) }()
// execute the services for the pipeline
for _, _service := range c.pipeline.Services {
c.Logger.Infof("planning %s service", _service.Name)
// plan the service
c.err = c.PlanService(ctx, _service)
if c.err != nil {
return fmt.Errorf("unable to plan service: %w", c.err)
}
c.Logger.Infof("executing %s service", _service.Name)
// execute the service
c.err = c.ExecService(ctx, _service)
if c.err != nil {
return fmt.Errorf("unable to execute service: %w", c.err)
}
}
// execute the steps for the pipeline
for _, _step := range c.pipeline.Steps {
// TODO: remove hardcoded reference
if _step.Name == "init" {
continue
}
// check if the step should be skipped
//
// https://pkg.go.dev/github.com/go-vela/worker/internal/step#Skip
if step.Skip(_step, c.build, c.repo) {
continue
}
c.Logger.Infof("planning %s step", _step.Name)
// plan the step
c.err = c.PlanStep(ctx, _step)
if c.err != nil {
return fmt.Errorf("unable to plan step: %w", c.err)
}
c.Logger.Infof("executing %s step", _step.Name)
// execute the step
c.err = c.ExecStep(ctx, _step)
if c.err != nil {
return fmt.Errorf("unable to execute step: %w", c.err)
}
}
// create an error group with the context for each stage
//
// https://pkg.go.dev/golang.org/x/sync/errgroup?tab=doc#WithContext
stages, stageCtx := errgroup.WithContext(ctx)
// create a map to track the progress of each stage
stageMap := new(sync.Map)
// iterate through each stage in the pipeline
for _, _stage := range c.pipeline.Stages {
// TODO: remove hardcoded reference
if _stage.Name == "init" {
continue
}
// https://golang.org/doc/faq#closures_and_goroutines
stage := _stage
// create a new channel for each stage in the map
stageMap.Store(stage.Name, make(chan error))
// spawn errgroup routine for the stage
//
// https://pkg.go.dev/golang.org/x/sync/errgroup?tab=doc#Group.Go
stages.Go(func() error {
c.Logger.Infof("planning %s stage", stage.Name)
// plan the stage
c.err = c.PlanStage(stageCtx, stage, stageMap)
if c.err != nil {
return fmt.Errorf("unable to plan stage: %w", c.err)
}
c.Logger.Infof("executing %s stage", stage.Name)
// execute the stage
c.err = c.ExecStage(stageCtx, stage, stageMap)
if c.err != nil {
return fmt.Errorf("unable to execute stage: %w", c.err)
}
return nil
})
}
c.Logger.Debug("waiting for stages completion")
// wait for the stages to complete or return an error
//
// https://pkg.go.dev/golang.org/x/sync/errgroup?tab=doc#Group.Wait
c.err = stages.Wait()
if c.err != nil {
return fmt.Errorf("unable to wait for stages: %w", c.err)
}
return c.err
}
// StreamBuild receives a StreamRequest and then
// runs StreamService or StreamStep in a goroutine.
func (c *client) StreamBuild(ctx context.Context) error {
// create an error group with the parent context
//
// https://pkg.go.dev/golang.org/x/sync/errgroup?tab=doc#WithContext
streams, streamCtx := errgroup.WithContext(ctx)
defer func() {
c.Logger.Trace("waiting for stream functions to return")
err := streams.Wait()
if err != nil {
c.Logger.Errorf("error in a stream request, %v", err)
}
c.Logger.Trace("all stream functions have returned")
}()
for {
select {
case req := <-c.streamRequests:
streams.Go(func() error {
// update engine logger with step metadata
//
// https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithField
logger := c.Logger.WithField(req.Key, req.Container.Name)
logger.Debugf("streaming %s container %s", req.Key, req.Container.ID)
err := req.Stream(streamCtx, req.Container)
if err != nil {
logger.Error(err)
}
return nil
})
case <-ctx.Done():
// build done or canceled
return nil
}
}
}
// DestroyBuild cleans up the build after execution.
func (c *client) DestroyBuild(ctx context.Context) error {
var err error
defer func() {
c.Logger.Info("deleting runtime build")
// remove the runtime build for the pipeline
err = c.Runtime.RemoveBuild(ctx, c.pipeline)
if err != nil {
c.Logger.Errorf("unable to remove runtime build: %v", err)
}
}()
// destroy the steps for the pipeline
for _, _step := range c.pipeline.Steps {
// TODO: remove hardcoded reference
if _step.Name == "init" {
continue
}
c.Logger.Infof("destroying %s step", _step.Name)
// destroy the step
err = c.DestroyStep(ctx, _step)
if err != nil {
c.Logger.Errorf("unable to destroy step: %v", err)
}
}
// destroy the stages for the pipeline
for _, _stage := range c.pipeline.Stages {
// TODO: remove hardcoded reference
if _stage.Name == "init" {
continue
}
c.Logger.Infof("destroying %s stage", _stage.Name)
// destroy the stage
err = c.DestroyStage(ctx, _stage)
if err != nil {
c.Logger.Errorf("unable to destroy stage: %v", err)
}
}
// destroy the services for the pipeline
for _, _service := range c.pipeline.Services {
c.Logger.Infof("destroying %s service", _service.Name)
// destroy the service
err = c.DestroyService(ctx, _service)
if err != nil {
c.Logger.Errorf("unable to destroy service: %v", err)
}
}
// destroy the secrets for the pipeline
for _, _secret := range c.pipeline.Secrets {
// skip over non-plugin secrets
if _secret.Origin.Empty() {
continue
}
c.Logger.Infof("destroying %s secret", _secret.Name)
// destroy the secret
err = c.secret.destroy(ctx, _secret.Origin)
if err != nil {
c.Logger.Errorf("unable to destroy secret: %v", err)
}
}
c.Logger.Info("deleting volume")
// remove the runtime volume for the pipeline
err = c.Runtime.RemoveVolume(ctx, c.pipeline)
if err != nil {
c.Logger.Errorf("unable to remove volume: %v", err)
}
c.Logger.Info("deleting network")
// remove the runtime network for the pipeline
err = c.Runtime.RemoveNetwork(ctx, c.pipeline)
if err != nil {
c.Logger.Errorf("unable to remove network: %v", err)
}
return err
}