/
step.go
195 lines (160 loc) · 4.45 KB
/
step.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
// SPDX-License-Identifier: Apache-2.0
package local
import (
"bufio"
"context"
"fmt"
"time"
"github.com/go-vela/types/constants"
"github.com/go-vela/types/library"
"github.com/go-vela/types/pipeline"
"github.com/go-vela/worker/internal/message"
"github.com/go-vela/worker/internal/step"
)
// create a step logging pattern.
const stepPattern = "[step: %s]"
// CreateStep configures the step for execution.
func (c *client) CreateStep(ctx context.Context, ctn *pipeline.Container) error {
// TODO: remove hardcoded reference
if ctn.Name == "init" {
return nil
}
// setup the runtime container
err := c.Runtime.SetupContainer(ctx, ctn)
if err != nil {
return err
}
// substitute container configuration
//
// https://pkg.go.dev/github.com/go-vela/types/pipeline#Container.Substitute
err = ctn.Substitute()
if err != nil {
return err
}
return nil
}
// PlanStep prepares the step for execution.
func (c *client) PlanStep(ctx context.Context, ctn *pipeline.Container) error {
// early exit if container is nil
if ctn.Empty() {
return fmt.Errorf("empty container provided")
}
// create the library step object
_step := library.StepFromBuildContainer(c.build, ctn)
_step.SetStatus(constants.StatusRunning)
_step.SetStarted(time.Now().UTC().Unix())
// add the step to the client map
c.steps.Store(ctn.ID, _step)
return nil
}
// ExecStep runs a step.
func (c *client) ExecStep(ctx context.Context, ctn *pipeline.Container) error {
// TODO: remove hardcoded reference
if ctn.Name == "init" {
return nil
}
// load the step from the client
//
// https://pkg.go.dev/github.com/go-vela/worker/internal/step#Load
_step, err := step.Load(ctn, &c.steps)
if err != nil {
return err
}
// defer taking a snapshot of the step
//
// https://pkg.go.dev/github.com/go-vela/worker/internal/step#Snapshot
defer func() { step.Snapshot(ctn, c.build, nil, nil, nil, _step) }()
// run the runtime container
err = c.Runtime.RunContainer(ctx, ctn, c.pipeline)
if err != nil {
// set step status to error and step error
_step.SetStatus(constants.StatusError)
_step.SetError(err.Error())
return err
}
// trigger StreamStep goroutine with logging context
c.streamRequests <- message.StreamRequest{
Key: "step",
Stream: c.StreamStep,
Container: ctn,
}
// do not wait for detached containers
if ctn.Detach {
return nil
}
// wait for the runtime container
err = c.Runtime.WaitContainer(ctx, ctn)
if err != nil {
return err
}
// inspect the runtime container
err = c.Runtime.InspectContainer(ctx, ctn)
if err != nil {
return err
}
return nil
}
// StreamStep tails the output for a step.
func (c *client) StreamStep(ctx context.Context, ctn *pipeline.Container) error {
// TODO: remove hardcoded reference
if ctn.Name == "init" {
return nil
}
// tail the runtime container
rc, err := c.Runtime.TailContainer(ctx, ctn)
if err != nil {
return err
}
defer rc.Close()
// create a step pattern for log output
_pattern := fmt.Sprintf(stepPattern, ctn.Name)
// check if the container provided is for stages
_stage, ok := ctn.Environment["VELA_STEP_STAGE"]
if ok {
// check if the stage name is set
if len(_stage) > 0 {
// create a stage pattern for log output
_pattern = fmt.Sprintf(stagePattern, _stage, ctn.Name)
}
}
// create new scanner from the container output
scanner := bufio.NewScanner(rc)
// scan entire container output
for scanner.Scan() {
// ensure we output to stdout
fmt.Fprintln(c.stdout, _pattern, scanner.Text())
}
return scanner.Err()
}
// DestroyStep cleans up steps after execution.
func (c *client) DestroyStep(ctx context.Context, ctn *pipeline.Container) error {
// TODO: remove hardcoded reference
if ctn.Name == "init" {
return nil
}
// load the step from the client
//
// https://pkg.go.dev/github.com/go-vela/worker/internal/step#Load
_step, err := step.Load(ctn, &c.steps)
if err != nil {
// create the step from the container
//
// https://pkg.go.dev/github.com/go-vela/types/library#StepFromContainerEnvironment
_step = library.StepFromContainerEnvironment(ctn)
}
// defer an upload of the step
//
// https://pkg.go.dev/github.com/go-vela/worker/internal/step#Upload
defer func() { step.Upload(ctn, c.build, nil, nil, nil, _step) }()
// inspect the runtime container
err = c.Runtime.InspectContainer(ctx, ctn)
if err != nil {
return err
}
// remove the runtime container
err = c.Runtime.RemoveContainer(ctx, ctn)
if err != nil {
return err
}
return nil
}