1 package middlechild
2
3 import (
4 "context"
5 "fmt"
6 "io"
7 "strings"
8
9 store "github.com/Shopify/go-storage"
10
11 "edge-infra.dev/pkg/lib/fog"
12 )
13
14 const argoLogsBucket = "edge-argo-logs"
15
16
17 func buildArgoResultPath(mcj *Job) string {
18 return fmt.Sprintf("argo/edge-infra/%s/%s/logs/logs.txt", mcj.JobData.Workflow, mcj.JobData.Number)
19 }
20
21
22 func buildArgoLogPath(mcj *Job, id string) string {
23 y, m, d := mcj.JobData.Started.Date()
24 return fmt.Sprintf("%d/%02d/%02d/%s/%s/main.log", y, int(m), d, mcj.JobData.Run, id)
25 }
26
27
28
29 func getArgoPodName(mcj *Job) (bool, []string) {
30 found := false
31 id := ""
32 for _, meta := range mcj.Metadata {
33 if meta.Key == "ID" {
34 found = true
35 id = meta.Value
36 }
37 }
38
39 if !found {
40 return found, []string{}
41 }
42
43 splitID := strings.Split(id, "-")
44 s := splitID[len(splitID)-1]
45
46 pods := []string{
47 strings.Replace(id, fmt.Sprint("-", s), fmt.Sprint("-rosa-", s), 1),
48 strings.Replace(id, fmt.Sprint("-", s), fmt.Sprint("-rosa-with-kubeconfig-", s), 1),
49 }
50
51 return found, pods
52 }
53
54
55 func argoLogs(ctx context.Context, mcj *Job, testStore, logStore store.FS) error {
56 log := fog.FromContext(ctx)
57
58 found, ids := getArgoPodName(mcj)
59 if !found {
60 log.Info("no pod found")
61 return nil
62 }
63
64 if len(ids) != 2 {
65 return fmt.Errorf("ids should always have a length of 2")
66 }
67
68 log.Info("pod found in metadata", "podID", ids)
69
70
71 pods := []string{buildArgoLogPath(mcj, ids[0]), buildArgoLogPath(mcj, ids[1])}
72
73 log.Info("attempting to copy the argo log files")
74 err := copyArgoLogs(ctx, testStore, logStore, pods, buildArgoResultPath(mcj))
75 if err != nil {
76 return err
77 }
78
79 return nil
80 }
81
82
83 func copyArgoLogs(ctx context.Context, testStore, logStore store.FS, sources []string, destination string) error {
84 log := fog.FromContext(ctx)
85 log.Info("copying log files", "sources", sources, "destination", destination)
86
87 log.Info("opening source log file")
88
89
90 f, err := logStore.Open(context.Background(), sources[0], nil)
91 if err != nil {
92
93 f, err = logStore.Open(context.Background(), sources[1], nil)
94 if err != nil {
95 return fmt.Errorf("Couldnt find rosa or rosa-with-kubeconfig log files: %w", err)
96 }
97 }
98
99 log.Info("creating destination log file")
100 wc, err := testStore.Create(ctx, destination, nil)
101 if err != nil {
102 return err
103 }
104
105 log.Info("copying file contents")
106 _, err = io.Copy(wc, f)
107 if err != nil {
108 return err
109 }
110
111 err = wc.Close()
112 if err != nil {
113 return err
114 }
115
116 return f.Close()
117 }
118
View as plain text