-
Notifications
You must be signed in to change notification settings - Fork 4
/
stage.go
249 lines (227 loc) · 10.1 KB
/
stage.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
package executor
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"time"
"github.com/dadosjusbr/executor/status"
)
const (
defaultRunSuccessCode int = 0 // default value when the list of codes meaning success is not defines.
)
// StageExecutionResult represents information about the execution of a stage.
type StageExecutionResult struct {
Stage Stage `json:"stage" bson:"stage,omitempty"` // Name of stage.
CommitID string `json:"commit" bson:"commit,omitempty"` // Commit of the stage repo when executing the stage.
StartTime time.Time `json:"start" bson:"start,omitempty"` // Time at start of stage.
FinalTime time.Time `json:"end" bson:"end,omitempty"` // Time at the end of stage.
BuildResult CmdResult `json:"buildResult" bson:"buildResult,omitempty"` // Build result.
RunResult CmdResult `json:"runResult" bson:"runResult,omitempty"` // Run result.
SetupResult CmdResult `json:"setupResult" bson:"setupResult,omitempty"` // Setup result.
TeardownResult CmdResult `json:"teardownResult" bson:"teardownResult,omitempty"` // Teardown result.
Status status.Code `json:"status" bson:"status,omitempty"` // Final execution status of the stage.
}
// Stage is a phase of data release process.
type Stage struct {
Name string `json:"name" bson:"name,omitempt"` // Stage's name.
Dir string `json:"dir" bson:"dir,omitempt"` // Directory to be concatenated with default base directory or with the base directory specified here in 'BaseDir'. This field is used to name the image built.
Image string `json:"image" bson:"image,omitempt"` // Docker image ID, e.g., ghcr.io/dadosjusbr/coletor-cnj:main
Repo string `json:"repo" bson:"repo,omitempt"` // Repository URL from where to clone the pipeline stage.
BaseDir string `json:"base-dir" bson:"base-dir,omitempt"` // Base directory for the stage. This field overwrites the DefaultBaseDir in pipeline's definition.
BuildEnv map[string]string `json:"build-env" bson:"build-env,omitempt"` // Variables to be used in the stage build. They will be concatenated with the default variables defined in the pipeline, overwriting them if repeated.
RunEnv map[string]string `json:"run-env" bson:"run-env,omitempt"` // Variables to be used in the stage run. They will be concatenated with the default variables defined in the pipeline, overwriting them if repeated.
RepoVersionEnvVar string `json:"repo_version_env_var" bson:"repo_version_env_var,omitempt"` // Name of the environment variable passed to build and run that represents the stage commit id.
ContainerID string `json:"container-id" bson:"container-id,omitempty"` // ID of the container running used to run the stage.
VolumeName string `json:"volume-name" bson:"volume-name,omitempty"` // Name of the shared volume.
VolumeDir string `json:"volume-dir" bson:"volume-dir,omitempty"` // Directory of the shared volume.
RunSuccessCodes []int `json:"run-success-codes" bson:"run-success-codes,omitempty"` // List of exit codes that mean the stage has been successfully excecuted.
internalID string // Stage internal identification.
index int // Stage position in the pipeline.
}
func (stage *Stage) run(index int, pipeline Pipeline, stdin string) (StageExecutionResult, error) {
var ser StageExecutionResult
stage.index = index
stage.internalID = fmt.Sprintf("%s/%s", pipeline.Name, stage.Name)
// 'index+1' because the index starts from 0.
log.Printf("## Executing Stage %s [%d/%d]\n\n", stage.internalID, stage.index+1, len(pipeline.Stages))
defer func(ser *StageExecutionResult) {
ser.FinalTime = time.Now()
}(&ser)
// AQUI FIREMAN:
// Garantir que o setup e o tear down são corretamente referenciados dentro do SER, dessa forma, eles poderão
// Opções: campo SetupResult e TeardownResult string
// Precisa adicionar uma variável que permita identificar se houve sucesso ou não
ser.StartTime = time.Now()
{
log.Printf("### [%s] Setting up ...\n", stage.internalID)
c, err := stage.setup(pipeline)
ser.SetupResult = c
if err != nil {
ser.Status = status.SetupError
log.Printf("### Error setting up stage %s:%v\n\n", stage.internalID, err)
return ser, err
}
log.Printf("### [%s] Set up completed successfully!\n\n", stage.internalID)
}
ser.Stage = *stage
{
log.Printf("### [%s] Building/Pulling image %s from %s ...\n", stage.internalID, stage.ContainerID, filepath.Join(stage.BaseDir, stage.Dir))
c, err := stage.buildImage()
ser.BuildResult = c
if err != nil {
ser.Status = status.BuildError
log.Printf("### Error building stage %s:%v\n\n", stage.internalID, err)
return ser, err
}
log.Printf("### [%s] Image %s built/pulled sucessfully!\n\n", stage.internalID, stage.ContainerID)
}
{
log.Printf("### [%s] Running ...\n", stage.internalID)
c, err := stage.runImage(stdin)
ser.RunResult = c
if err != nil {
ser.Status = status.RunError
log.Printf("### Error running stage %s:%v\n\n", stage.internalID, err)
return ser, err
}
log.Printf("### [%s] Run completed successfully!\n\n", stage.internalID)
}
{
log.Printf("### [%s] Tearing down ...\n", stage.internalID)
c, err := stage.teardown()
ser.TeardownResult = c
if err != nil {
ser.Status = status.TeardownError
log.Printf("### Error tearing down stage %s:%v\n\n", stage.internalID, err)
return ser, err
}
log.Printf("### [%s] Tear down completed successfully!\n\n", stage.internalID)
}
// 'index+1' because the index starts from 0.
log.Printf("## Stage %s [%d/%d] executed successfully!\n\n", stage.internalID, stage.index, len(pipeline.Stages))
ser.Status = status.OK
return ser, nil
}
func (stage *Stage) setup(pipeline Pipeline) (CmdResult, error) {
// Even though this stage uses libraries to execute its commands, we wrap
// those in a CmdResult to comply with the stage execution steps interface.
if stage.BaseDir == "" {
stage.BaseDir = pipeline.DefaultBaseDir
}
if stage.ContainerID == "" {
stage.ContainerID = strings.ReplaceAll(strings.ToLower(stage.Name), " ", "-")
}
if stage.VolumeName == "" {
stage.VolumeName = pipeline.VolumeName
}
if stage.VolumeDir == "" {
stage.VolumeDir = pipeline.VolumeDir
}
if len(stage.RunSuccessCodes) == 0 {
stage.RunSuccessCodes = []int{defaultRunSuccessCode}
}
// if there the field "repo" is set for the stage, clone it and update
// its baseDir and commit id.
if stage.Repo != "" {
rr, err := setupRepo(stage.Repo, stage.BaseDir, stage.Dir)
if err != nil {
e := fmt.Errorf("error in setting up repo(%s) for stage %s setup: %w", stage.Repo, stage.Name, err)
return CmdResult{
Stderr: err.Error(),
ExitStatus: int(status.SystemError),
}, e
}
// specifying commit id as environment variable.
if stage.RepoVersionEnvVar != "" {
if stage.BuildEnv == nil {
stage.BuildEnv = make(map[string]string)
}
stage.BuildEnv[stage.RepoVersionEnvVar] = rr.commitID
if stage.RunEnv == nil {
stage.RunEnv = make(map[string]string)
}
stage.RunEnv[stage.RepoVersionEnvVar] = rr.commitID
}
stage.BaseDir = rr.dir
}
// Fill up enviroment variable maps.
stage.BuildEnv = mergeEnv(pipeline.DefaultBuildEnv, stage.BuildEnv)
stage.RunEnv = mergeEnv(pipeline.DefaultRunEnv, stage.RunEnv)
return CmdResult{
ExitStatus: int(status.OK),
}, nil
}
func (stage *Stage) buildImage() (CmdResult, error) {
var err error
var r CmdResult
switch {
case stage.Image != "":
r, err = pullImage(stage.Image, stage.BaseDir, stage.Dir)
default:
r, err = buildImage(stage.ContainerID, stage.BaseDir, stage.Dir, stage.BuildEnv)
}
if err != nil {
return r, fmt.Errorf("error when building image: %w", err)
}
if status.Code(r.ExitStatus) != status.OK {
return r, fmt.Errorf("error when building image: status code %d(%s) when building image for %s", r.ExitStatus, status.Text(status.Code(r.ExitStatus)), stage.internalID)
}
return r, nil
}
func (stage *Stage) runImage(stdin string) (CmdResult, error) {
image := stage.Image
if image == "" {
image = stage.ContainerID
}
r, _ := runImage(image, stage.BaseDir, stage.Dir, stage.VolumeName, stage.VolumeDir, stdin, stage.RunEnv)
if !contains(stage.RunSuccessCodes, r.ExitStatus) {
return r, fmt.Errorf("error when running image: Status code %d(%s) when running image for %s", r.ExitStatus, status.Text(status.Code(r.ExitStatus)), stage.internalID)
}
return r, nil
}
// Removing temporary directories created from cloned repositories.
func (stage *Stage) teardown() (CmdResult, error) {
// Even though this stage uses libraries to execute its commands, we wrap
// those in a CmdResult to comply with the stage execution steps interface.
if stage.Repo != "" {
log.Printf("Removing cloned repo %s\n", stage.BaseDir)
if err := os.RemoveAll(stage.BaseDir); err != nil {
e := fmt.Errorf("error removing temp dir(%s): %w", stage.BaseDir, err)
return CmdResult{
Stderr: err.Error(),
ExitStatus: int(status.SystemError),
}, e
}
log.Printf("Cloned repo temp dir removed successfully!\n")
}
return CmdResult{
ExitStatus: int(status.OK),
}, nil
}
// validate checks whether the stage specification is valid.
func (stage *Stage) validateSpec() error {
if stage.Repo != "" && stage.Image != "" {
return fmt.Errorf("invalid stage configuration: repo and image can not be set at the same time")
}
return nil
}
func mergeEnv(defaultEnv, stageEnv map[string]string) map[string]string {
env := make(map[string]string)
for k, v := range defaultEnv {
env[k] = v
}
for k, v := range stageEnv {
env[k] = v
}
return env
}
func contains(succCodes []int, s int) bool {
for _, c := range succCodes {
if s == c {
return true
}
}
return false
}