/
end_of_life_helper.go
354 lines (290 loc) · 10.9 KB
/
end_of_life_helper.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
346
347
348
349
350
351
352
353
354
package builder
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"time"
contracts "github.com/estafette/estafette-ci-contracts"
"github.com/opentracing-contrib/go-stdlib/nethttp"
"github.com/opentracing/opentracing-go"
tracingLog "github.com/opentracing/opentracing-go/log"
"github.com/rs/zerolog/log"
"github.com/sethgrid/pester"
)
// EndOfLifeHelper has methods to shutdown the runner after a fatal or successful run
type EndOfLifeHelper interface {
HandleFatal(context.Context, contracts.BuildLog, error, string)
SendBuildStartedEvent(ctx context.Context) error
SendBuildFinishedEvent(ctx context.Context, buildStatus contracts.LogStatus) error
SendBuildCleanEvent(ctx context.Context, buildStatus contracts.LogStatus) error
SendBuildJobLogEvent(ctx context.Context, buildLog contracts.BuildLog) error
CancelJob(ctx context.Context) error
}
type endOfLifeHelper struct {
runAsJob bool
config contracts.BuilderConfig
podName string
}
// NewEndOfLifeHelper returns a new EndOfLifeHelper
func NewEndOfLifeHelper(runAsJob bool, config contracts.BuilderConfig, podName string) EndOfLifeHelper {
return &endOfLifeHelper{
runAsJob: runAsJob,
config: config,
podName: podName,
}
}
func (elh *endOfLifeHelper) HandleFatal(ctx context.Context, buildLog contracts.BuildLog, err error, message string) {
// add error messages as step to show in logs
fatalStep := contracts.BuildLogStep{
Step: "init",
LogLines: []contracts.BuildLogLine{},
ExitCode: -1,
Status: "FAILED",
}
lineNumber := 1
if err != nil {
fatalStep.LogLines = append(fatalStep.LogLines, contracts.BuildLogLine{
LineNumber: lineNumber,
Timestamp: time.Now().UTC(),
StreamType: "stderr",
Text: err.Error(),
})
lineNumber++
}
if message != "" {
fatalStep.LogLines = append(fatalStep.LogLines, contracts.BuildLogLine{
LineNumber: lineNumber,
Timestamp: time.Now().UTC(),
StreamType: "stderr",
Text: message,
})
}
buildLog.Steps = append(buildLog.Steps, &fatalStep)
_ = elh.SendBuildFinishedEvent(ctx, contracts.LogStatusFailed)
_ = elh.SendBuildJobLogEvent(ctx, buildLog)
_ = elh.SendBuildCleanEvent(ctx, contracts.LogStatusFailed)
if elh.runAsJob {
log.Error().Err(err).Msg(message)
os.Exit(0)
} else {
log.Fatal().Err(err).Msg(message)
}
}
func (elh *endOfLifeHelper) SendBuildJobLogEvent(ctx context.Context, buildLog contracts.BuildLog) (err error) {
err = elh.SendBuildJobLogEventCore(ctx, buildLog)
if err == nil {
return
}
// strip log lines from successful steps to reduce size of the logs and still keep the useful information
slimBuildLog := buildLog
slimBuildLog.Steps = []*contracts.BuildLogStep{}
for _, s := range buildLog.Steps {
slimBuildLogStep := s
if s.Status == contracts.LogStatusSucceeded {
if len(s.LogLines) > 0 {
slimBuildLogStep.LogLines = []contracts.BuildLogLine{
{
LineNumber: s.LogLines[0].LineNumber,
Timestamp: s.LogLines[0].Timestamp,
StreamType: "stdout",
Text: "Truncated logs for reducing total log size; to prevent this use less verbose logging",
},
}
}
}
slimBuildLog.Steps = append(slimBuildLog.Steps, slimBuildLogStep)
}
return elh.SendBuildJobLogEventCore(ctx, slimBuildLog)
}
func (elh *endOfLifeHelper) SendBuildJobLogEventCore(ctx context.Context, buildLog contracts.BuildLog) (err error) {
span, _ := opentracing.StartSpanFromContext(ctx, "SendLog")
defer span.Finish()
ciServerBuilderPostLogsURL := elh.config.CIServer.PostLogsURL
jwt := elh.config.CIServer.JWT
jobName := *elh.config.JobName
if ciServerBuilderPostLogsURL != "" && jwt != "" && jobName != "" {
// convert BuildJobLogs to json
var requestBody io.Reader
var data []byte
if elh.config.JobType == contracts.JobTypeRelease {
// copy buildLog to releaseLog and marshal that
releaseLog := contracts.ReleaseLog{
ID: buildLog.ID,
RepoSource: buildLog.RepoSource,
RepoOwner: buildLog.RepoOwner,
RepoName: buildLog.RepoName,
ReleaseID: elh.config.Release.ID,
Steps: buildLog.Steps,
InsertedAt: buildLog.InsertedAt,
}
data, err = json.Marshal(releaseLog)
if err != nil {
log.Error().Err(err).Msgf("Failed marshalling ReleaseLog for job %v", jobName)
return
}
} else if elh.config.JobType == contracts.JobTypeBot {
// copy buildLog to botLog and marshal that
botLog := contracts.BotLog{
ID: buildLog.ID,
RepoSource: buildLog.RepoSource,
RepoOwner: buildLog.RepoOwner,
RepoName: buildLog.RepoName,
BotID: elh.config.Bot.ID,
Steps: buildLog.Steps,
InsertedAt: buildLog.InsertedAt,
}
data, err = json.Marshal(botLog)
if err != nil {
log.Error().Err(err).Msgf("Failed marshalling BotLog for job %v", jobName)
return
}
} else {
data, err = json.Marshal(buildLog)
if err != nil {
log.Error().Err(err).Msgf("Failed marshalling BuildLog for job %v", jobName)
return
}
}
requestBody = bytes.NewReader(data)
// create client, in order to add headers
client := pester.NewExtendedClient(&http.Client{Transport: &nethttp.Transport{}})
client.MaxRetries = 1
client.Backoff = pester.DefaultBackoff
client.KeepLog = true
client.Timeout = time.Second * 60
request, err := http.NewRequest("POST", ciServerBuilderPostLogsURL, requestBody)
if err != nil {
log.Error().Err(err).Msgf("Failed creating http client for job %v", jobName)
return err
}
// add tracing context
request = request.WithContext(opentracing.ContextWithSpan(request.Context(), span))
// collect additional information on setting up connections
request, ht := nethttp.TraceRequest(span.Tracer(), request)
// add headers
request.Header.Add("Authorization", fmt.Sprintf("Bearer %v", jwt))
request.Header.Add("Content-Type", "application/json")
// perform actual request
response, err := client.Do(request)
if err != nil {
log.Error().Err(err).Str("logs", client.LogString()).Msgf("Failed shipping logs to %v for job %v: %v", ciServerBuilderPostLogsURL, jobName, client.LogString())
return err
}
defer response.Body.Close()
ht.Finish()
log.Debug().Str("logs", client.LogString()).Msgf("Successfully shipped logs to %v for job %v", ciServerBuilderPostLogsURL, jobName)
}
return nil
}
func (elh *endOfLifeHelper) SendBuildStartedEvent(ctx context.Context) error {
buildStatus := contracts.LogStatusRunning
return elh.sendBuilderEvent(ctx, buildStatus, contracts.BuildEventTypeUpdateStatus)
}
func (elh *endOfLifeHelper) SendBuildFinishedEvent(ctx context.Context, buildStatus contracts.LogStatus) error {
return elh.sendBuilderEvent(ctx, buildStatus, contracts.BuildEventTypeUpdateStatus)
}
func (elh *endOfLifeHelper) SendBuildCleanEvent(ctx context.Context, buildStatus contracts.LogStatus) error {
return elh.sendBuilderEvent(ctx, buildStatus, contracts.BuildEventTypeClean)
}
func (elh *endOfLifeHelper) sendBuilderEvent(ctx context.Context, buildStatus contracts.LogStatus, buildEventType contracts.BuildEventType) (err error) {
span, _ := opentracing.StartSpanFromContext(ctx, "SendBuildStatus")
defer span.Finish()
span.SetTag("build-status", buildStatus.ToStatus())
ciServerBuilderEventsURL := elh.config.CIServer.BuilderEventsURL
jwt := elh.config.CIServer.JWT
jobName := *elh.config.JobName
if ciServerBuilderEventsURL != "" && jwt != "" && jobName != "" {
// convert EstafetteCiBuilderEvent to json
var requestBody io.Reader
ciBuilderEvent := contracts.EstafetteCiBuilderEvent{
BuildEventType: buildEventType,
JobType: elh.config.JobType,
Build: elh.config.Build,
Release: elh.config.Release,
Bot: elh.config.Bot,
Git: elh.config.Git,
JobName: jobName,
PodName: elh.podName,
}
// update status
ciBuilderEvent.SetStatus(buildStatus.ToStatus())
data, err := json.Marshal(ciBuilderEvent)
if err != nil {
log.Error().Err(err).Msgf("Failed marshalling EstafetteCiBuilderEvent for job %v", jobName)
return err
}
requestBody = bytes.NewReader(data)
// create client, in order to add headers
client := pester.NewExtendedClient(&http.Client{Transport: &nethttp.Transport{}})
client.MaxRetries = 3
client.Backoff = pester.ExponentialJitterBackoff
client.KeepLog = true
client.Timeout = time.Second * 10
request, err := http.NewRequest("POST", ciServerBuilderEventsURL, requestBody)
if err != nil {
log.Error().Err(err).Msgf("Failed creating http client for job %v", jobName)
return err
}
// add tracing context
request = request.WithContext(opentracing.ContextWithSpan(request.Context(), span))
// collect additional information on setting up connections
request, ht := nethttp.TraceRequest(span.Tracer(), request)
// add headers
request.Header.Add("X-Estafette-Event-Job-Name", jobName)
request.Header.Add("Authorization", fmt.Sprintf("Bearer %v", jwt))
// perform actual request
response, err := client.Do(request)
if err != nil {
span.SetTag("error", true)
span.LogFields(
tracingLog.String("error", err.Error()),
)
log.Error().Err(err).Str("pesterLogs", client.LogString()).Msgf("Failed performing http request to %v for job %v: %v", ciServerBuilderEventsURL, jobName, client.LogString())
return err
}
defer response.Body.Close()
ht.Finish()
log.Debug().Str("pesterLogs", client.LogString()).Str("url", ciServerBuilderEventsURL).Msgf("Succesfully sent build event type '%v' to api", buildEventType)
}
return nil
}
func (elh *endOfLifeHelper) CancelJob(ctx context.Context) error {
span, _ := opentracing.StartSpanFromContext(ctx, "CancelJob")
defer span.Finish()
ciServerBuilderCancelJobURL := elh.config.CIServer.CancelJobURL
jwt := elh.config.CIServer.JWT
jobName := *elh.config.JobName
if ciServerBuilderCancelJobURL != "" && jwt != "" && jobName != "" {
// create client, in order to add headers
client := pester.NewExtendedClient(&http.Client{Transport: &nethttp.Transport{}})
client.MaxRetries = 1
client.Backoff = pester.DefaultBackoff
client.KeepLog = true
client.Timeout = time.Second * 60
request, err := http.NewRequest("DELETE", ciServerBuilderCancelJobURL, nil)
if err != nil {
log.Error().Err(err).Msgf("Failed creating http client for job %v", jobName)
return err
}
// add tracing context
request = request.WithContext(opentracing.ContextWithSpan(request.Context(), span))
// collect additional information on setting up connections
request, ht := nethttp.TraceRequest(span.Tracer(), request)
// add headers
request.Header.Add("Authorization", fmt.Sprintf("Bearer %v", jwt))
// perform actual request
response, err := client.Do(request)
if err != nil {
log.Error().Err(err).Str("logs", client.LogString()).Msgf("Failed canceling job at %v for job %v: %v", ciServerBuilderCancelJobURL, jobName, client.LogString())
return err
}
defer response.Body.Close()
ht.Finish()
log.Debug().Str("logs", client.LogString()).Msgf("Successfully canceled job at %v for job %v", ciServerBuilderCancelJobURL, jobName)
}
return nil
}