-
Notifications
You must be signed in to change notification settings - Fork 0
/
ci.go
505 lines (429 loc) · 14.1 KB
/
ci.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
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
package plasmactlmeta
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os/exec"
"strconv"
"strings"
"time"
"github.com/launchrctl/launchr"
)
const targetJobName = "platform:deploy"
// OAuthResponse is a struct to parse OAuth response.
type OAuthResponse struct {
AccessToken string `json:"access_token"`
}
// PipelineResponse is a struct to parse OAuth response.
type PipelineResponse struct {
ID int `json:"id"`
WebURL string `json:"web_url"`
ProjectID int `json:"project_id"`
DetailedStatus struct {
DetailsPath string `json:"details_path"`
} `json:"detailed_status"`
}
// Job struct for listing jobs in the pipeline
type Job struct {
ID int `json:"id"`
Name string `json:"name"`
Status string `json:"status"`
Stage string `json:"stage"`
AllowFailure bool `json:"allow_failure"`
}
func getOAuthToken(gitlabDomain, username, password string) (string, error) {
// Prepare the OAuth request
oauthURL := fmt.Sprintf("%s/oauth/token", gitlabDomain)
launchr.Log().Debug("OAuth token request URL", "url", oauthURL)
data := url.Values{}
data.Set("grant_type", "password")
data.Set("username", username)
data.Set("password", password)
// Create HTTP request for OAuth token
req, err := http.NewRequest("POST", oauthURL, strings.NewReader(data.Encode()))
if err != nil {
return "", err
}
req.SetBasicAuth(username, password)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
// Execute request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
// Read response body
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
launchr.Log().Debug("OAuth token response", "body", string(body))
// Parse JSON response to extract access token
var oauthResp OAuthResponse
err = json.Unmarshal(body, &oauthResp)
if err != nil {
return "", err
}
return oauthResp.AccessToken, nil
}
func getBranchName() (string, error) {
cmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD")
output, err := cmd.Output()
if err != nil {
return "", err
}
return strings.TrimSpace(string(output)), nil
}
func getRepoName() (string, error) {
cmd := exec.Command("git", "config", "--get", "remote.origin.url")
output, err := cmd.Output()
if err != nil {
return "", err
}
repoURL := strings.TrimSpace(string(output))
repoParts := strings.Split(repoURL, "/")
return strings.TrimSuffix(repoParts[len(repoParts)-1], ".git"), nil
}
func getProjectID(gitlabDomain, username, password, accessToken, repoName string) (string, error) {
apiURL := fmt.Sprintf("%s/api/v4/projects?search=%s&access_token=%s", gitlabDomain, url.QueryEscape(repoName), accessToken)
launchr.Log().Debug("GitLab API URL to get project ID", "url", apiURL)
// Create the request
req, err := http.NewRequest("GET", apiURL, nil)
if err != nil {
return "", err
}
req.SetBasicAuth(username, password)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
// Parse the response JSON
var projects []map[string]interface{}
err = json.Unmarshal(body, &projects)
if err != nil {
return "", err
}
if len(projects) == 0 {
return "", fmt.Errorf("project not found")
}
return fmt.Sprintf("%.0f", projects[0]["id"].(float64)), nil
}
func triggerPipeline(gitlabDomain, username, password, accessToken, projectID, branchName, buildEnv, buildResources, comparisonRef, verbosity string, ansibleDebug bool) (int, error) {
apiURL := fmt.Sprintf("%s/api/v4/projects/%s/pipeline?access_token=%s", gitlabDomain, projectID, accessToken)
launchr.Log().Debug("GitLab API URL for triggering pipeline", "url", apiURL)
// Prepare the variables to pass during pipeline creation
data := map[string]interface{}{
"ref": branchName,
"variables": []map[string]string{
{
"key": "PLASMA_BUILD_ENV",
"value": buildEnv,
},
{
"key": "PLASMA_BUILD_RESOURCES",
"value": buildResources,
},
},
}
// Only add OVERRIDDEN_COMPARISON_REF if provided
if comparisonRef != "" {
launchr.Log().Info("Appending OVERRIDDEN_COMPARISON_REF to pipelines variables")
data["variables"] = append(data["variables"].([]map[string]string), map[string]string{
"key": "OVERRIDDEN_COMPARISON_REF",
"value": comparisonRef,
})
}
// Only add BUILD_DEBUG_MODE if provided
if ansibleDebug {
launchr.Log().Info("Appending BUILD_DEBUG_MODE to pipelines variables")
data["variables"] = append(data["variables"].([]map[string]string), map[string]string{
"key": "BUILD_DEBUG_MODE",
"value": strconv.FormatBool(ansibleDebug),
})
}
// Only add VERBOSITY if provided
if verbosity != "" {
launchr.Log().Info("Appending VERBOSITY to pipelines variables")
data["variables"] = append(data["variables"].([]map[string]string), map[string]string{
"key": "VERBOSITY",
"value": verbosity,
})
}
jsonData, err := json.Marshal(data)
if err != nil {
return 0, err
}
launchr.Log().Debug("JSON data for triggering pipeline", "json", string(jsonData))
// Create the request
launchr.Term().Info().Printfln("Creating CI pipeline...")
req, err := http.NewRequest("POST", apiURL, bytes.NewBuffer(jsonData))
if err != nil {
return 0, err
}
req.SetBasicAuth(username, password)
req.Header.Set("Content-Type", "application/json")
launchr.Log().Debug("Request for triggering pipeline", "request", req)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return 0, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return 0, err
}
bodyStr := string(body)
launchr.Log().Debug("Response for triggering pipeline", "body", bodyStr)
// Check if the response contains "Reference not found"
if strings.Contains(strings.ToLower(bodyStr), "reference not found") {
return 0, fmt.Errorf("git branch not found: %s", bodyStr)
}
// Parse the response JSON to extract pipeline info
var pipelineResp PipelineResponse
err = json.Unmarshal(body, &pipelineResp)
if err != nil {
return 0, err
}
// Print Pipeline URL only once
launchr.Term().Printfln("Pipeline URL: %s", pipelineResp.WebURL)
// Return the pipeline ID
return pipelineResp.ID, nil
}
func getJobsInPipeline(gitlabDomain, username, password, accessToken, projectID string, pipelineID int) ([]Job, error) {
apiURL := fmt.Sprintf("%s/api/v4/projects/%s/pipelines/%d/jobs?access_token=%s", gitlabDomain, projectID, pipelineID, accessToken)
launchr.Log().Debug("GitLab API URL for retrieving jobs", "url", apiURL)
// Create the request
req, err := http.NewRequest("GET", apiURL, nil)
if err != nil {
return nil, err
}
req.SetBasicAuth(username, password)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
launchr.Log().Debug("GitLab API response for job", "body", string(body))
// Parse the jobs in the pipeline
var jobs []Job
err = json.Unmarshal(body, &jobs)
if err != nil {
return nil, err
}
return jobs, nil
}
// Function to retrieve and continuously print the job trace
func getJobTrace(gitlabDomain, username, password, accessToken, projectID string, jobID int) error {
apiURL := fmt.Sprintf("%s/api/v4/projects/%s/jobs/%d/trace?access_token=%s", gitlabDomain, projectID, jobID, accessToken)
launchr.Log().Debug("GitLab API URL for retrieving job trace", "url", apiURL)
const maxRetries = 20 // Retry up to 20 times to ensure job has started
const retryDelay = 10 * time.Second // Wait 10 seconds between retries
// Retry until job has started and trace is available
for i := 0; i < maxRetries; i++ {
traceContent, err := fetchTrace(apiURL, username, password)
if err != nil {
return err
}
if len(traceContent) > 0 {
launchr.Term().Println("Job has started!")
break
}
launchr.Term().Println("Waiting for job to start...")
time.Sleep(retryDelay)
}
// Start tailing the job trace
launchr.Term().Println("Now tailing job's trace:")
var lastLength int // Keeps track of how much trace has already been printed
for {
traceContent, err := fetchTrace(apiURL, username, password)
if err != nil {
return err
}
if len(traceContent) > lastLength {
// Print only new trace content
newContent := traceContent[lastLength:]
launchr.Term().Print(newContent)
lastLength = len(traceContent)
}
// If the trace has not changed for a while, assume it's still running
if len(traceContent) == lastLength {
launchr.Term().Print(".") // Indicate waiting
}
// Determine job completion status
statusCode, completed := jobCompleted(traceContent)
if completed {
if statusCode == 0 {
launchr.Term().Println("\nEnd of trace.")
return nil // Exit with code 0
}
// If the job failed, return an error with the corresponding exit code
return fmt.Errorf("job failed with exit code %d", statusCode)
}
// Sleep briefly before polling the trace again
time.Sleep(5 * time.Second)
}
}
// Helper function to determine if job has completed based on trace content and return exit code if failed
func jobCompleted(traceContent string) (int, bool) {
if strings.Contains(traceContent, "Job succeeded") {
return 0, true
}
if strings.Contains(traceContent, "Job failed") {
// Extract the exit code from the trace content
lines := strings.Split(traceContent, "\n")
for _, line := range lines {
if strings.Contains(line, "Job failed") {
parts := strings.Fields(line)
if len(parts) > 3 {
if code, err := strconv.Atoi(parts[len(parts)-1]); err == nil {
return code, true
}
}
}
}
}
return 1, false // Default to failed with exit code 1 if not found
}
// Helper function to fetch job trace from GitLab API
func fetchTrace(apiURL, username, password string) (string, error) {
// Create the request
req, err := http.NewRequest("GET", apiURL, nil)
if err != nil {
return "", err
}
req.SetBasicAuth(username, password)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
// Read the response body (the trace)
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(body), nil
}
func triggerManualJob(gitlabDomain, username, password, accessToken, projectID string, jobID int, pipelineID int) error {
apiURL := fmt.Sprintf("%s/api/v4/projects/%s/jobs/%d/play?access_token=%s", gitlabDomain, projectID, jobID, accessToken)
launchr.Log().Debug("GitLab API URL for triggering manual job", "url", apiURL)
// Retry parameters
const maxRetries = 100
const retryDelay = 30 * time.Second
var jobURL string // To store the Job URL
// Print the Job URL at the very end of the function
defer func() {
if jobURL != "" {
launchr.Term().Printfln("\nJob URL: %s", jobURL)
}
}()
// Retrieve all jobs to determine the stage of the target job
allJobs, err := getJobsInPipeline(gitlabDomain, username, password, accessToken, projectID, pipelineID)
if err != nil {
return err
}
// Find the stage of the target job
var targetJobStage string
for _, job := range allJobs {
if job.ID == jobID {
targetJobStage = job.Stage
break
}
}
if targetJobStage == "" {
return fmt.Errorf("stage of %s job not found", targetJobName)
}
for i := 0; i < maxRetries; i++ {
// Check the status of jobs in previous stages
jobs, err := getJobsInPipeline(gitlabDomain, username, password, accessToken, projectID, pipelineID)
if err != nil {
return err
}
inProgress := []string{}
failed := []string{}
for _, job := range jobs {
if job.Status == "running" || job.Status == "pending" {
if job.Stage != targetJobStage { // Exclude jobs in the same stage
inProgress = append(inProgress, job.Name)
}
} else if job.Status == "failed" && !job.AllowFailure {
if job.Stage != targetJobStage { // Exclude jobs in the same stage
failed = append(failed, job.Name)
}
}
}
// If there are failed jobs, no need to retry further
if len(failed) > 0 {
return fmt.Errorf("cannot trigger %s job due to failed jobs: %v", targetJobName, failed)
}
// If there are still jobs in progress, list them and wait before retrying
if len(inProgress) > 0 {
launchr.Term().Printfln("Waiting for previous jobs to finish: %v...", inProgress)
time.Sleep(retryDelay)
continue
}
// No failed jobs, no jobs in progress, proceed with triggering the manual job
// Create the request to trigger the job
req, err := http.NewRequest("POST", apiURL, nil)
if err != nil {
return err
}
req.SetBasicAuth(username, password)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
launchr.Log().Debug("GitLab API response for triggering manual job", "body", string(body), "http_code", resp.StatusCode)
// Check if response indicates success
if resp.StatusCode == http.StatusOK {
// Unmarshal the response body to extract the Job URL
var jsonResponse struct {
WebURL string `json:"web_url"`
}
if err := json.Unmarshal(body, &jsonResponse); err != nil {
return fmt.Errorf("failed to unmarshal job response: %v", err)
}
jobURL = jsonResponse.WebURL // Save the Job URL for later printing
// Print the Job URL
launchr.Term().Printfln("Job URL: %s", jobURL)
// Retrieve and print the job trace
err = getJobTrace(gitlabDomain, username, password, accessToken, projectID, jobID)
if err != nil {
return fmt.Errorf("failed to retrieve job trace: %v", err)
}
return nil // End the program after successfully retrieving job trace
}
// Handle unplayable job response
if strings.Contains(string(body), "Unplayable Job") {
launchr.Term().Printfln("%s job cannot be played yet. Retrying in %v...", targetJobName, retryDelay)
time.Sleep(retryDelay)
} else {
return fmt.Errorf("failed to trigger job: %s", resp.Status)
}
}
return fmt.Errorf("failed to trigger job after %d retries", maxRetries)
}