@@ -35,6 +35,8 @@ import (
3535 "fmt"
3636 "log"
3737 "os"
38+ "sort"
39+ "strconv"
3840 "time"
3941
4042 "github.com/google/uuid"
@@ -49,15 +51,15 @@ var taskLogs = &cobra.Command{
4951}
5052
5153var (
52- architecture string
53- cwd string
54- combined bool
54+ architecture string
55+ cwd string
56+ combined bool
5557 taskLogFileName string
56- attrs int
58+ attrs int
5759)
5860
5961func init () {
60- taskLogs .Flags ().StringVarP (& architecture , "architecture" , "a" , "" , "filter by architecture" )
62+ taskLogs .Flags ().StringVarP (& architecture , "architecture" , "a" , "" , "(inop) filter by architecture" )
6163 taskLogs .Flags ().BoolVarP (& combined , "combined" , "c" , false , "dump all logs to one file" )
6264 taskLogs .Flags ().StringVarP (& cwd , "cwd" , "C" , "" , "change working directory for ouput" )
6365}
@@ -66,57 +68,86 @@ func taskLogsMn(_ *cobra.Command, args []string) {
6668 // Ensure project id exists
6769 projectId := mustGetProjectID ()
6870
69- // buildCl := getClient(serviceBuild).(peridotopenapi.BuildServiceApi)
70- // body := peridotopenapi.BuildServiceSubmitBuildBody{
71- // PackageName: &args[0],
72- // DisableChecks: &disableChecks,
73- // Branches: &branches,
74- // ModuleVariant: &moduleVariant,
75- // SideNvrs: &sideNvrs,
76- // SetInactive: &setInactive,
77- // }
78- // if scmHash != "" {
79- // body.ScmHash = &scmHash
80- // }
81- // req := buildCl.SubmitBuild(getContext(), projectId).Body(body)
82- // buildRes, _, err := req.Execute()
83- // errFatal(err)
84-
8571 buildIdOrPackageName := args [0 ]
72+ var buildId string
8673
87- err := uuid .Validate (buildIdOrPackageName )
88- if err == nil {
89- // argument is not a uuid, try to look up the most recent build for a package with said name
74+ err := uuid .Validate (buildIdOrPackageName )
75+ if err == nil {
76+ buildId = buildIdOrPackageName
77+ } else {
78+ // argument is not a uuid, try to look up the most recent build for a package with said name
9079 // projectCl := getClient(serviceProject).(peridotopenapi.ProjectServiceApi)
9180 packageCl := getClient (servicePackage ).(peridotopenapi.PackageServiceApi )
81+ buildCl := getClient (serviceBuild ).(peridotopenapi.BuildServiceApi )
9282
93- res := packageCl .GetPackage (getContext (), projectId , "name" , buildIdOrPackageName )
94- log .Printf ("%s" , res )
95- }
83+ _ , _ , err := packageCl .GetPackage (getContext (), projectId , "name" , buildIdOrPackageName ).Execute ()
84+ if err != nil {
85+ errFatal (err )
86+ }
87+ // var pkg peridotopenapi.V1Package = *res.Package
88+ // pkgId := pkg.GetId()
9689
97- // Wait for build to finish
98- taskCl := getClient (serviceTask ).(peridotopenapi.TaskServiceApi )
99- log .Printf ("Waiting for build %s to finish\n " , buildIdOrPackageName )
90+ // try to get the latest builds for the package
91+ res , _ , err := buildCl .ListBuilds (
92+ getContext (),
93+ projectId ).FiltersStatus (string (peridotopenapi .SUCCEEDED )).FiltersPackageName (buildIdOrPackageName ).Execute ()
94+ if err != nil {
95+ errFatal (err )
96+ }
97+
98+ // TODO(neil): why is Total a string?
99+ total , err := strconv .Atoi (* res .Total )
100+ if err != nil {
101+ errFatal (err )
102+ }
103+
104+ // TODO(neil): support pagination
105+ if total > int (* res .Size ) {
106+ panic ("result set larger than one page" )
107+ }
108+
109+ if total > 0 {
110+ builds := * res .Builds
111+
112+ // sort the result set so we're looking at the latest build
113+ sort .Slice (builds , func (i , j int ) bool {
114+ return builds [i ].CreatedAt .After (* builds [j ].CreatedAt )
115+ })
116+
117+ // for _, build := range builds {
118+ // buildjson, _ := build.MarshalJSON()
119+ // log.Printf("build: %s", buildjson)
120+ // }
121+
122+ // after sorting, the first build is the latest
123+ buildId = builds [0 ].GetTaskId ()
124+ }
125+ }
100126
101127 if cwd != "" {
102128 os .Chdir (cwd )
103129 }
104130
105131 if combined {
106- // open and close the file to truncate it
107- taskLogFileName = fmt .Sprintf ("%s.log" , buildIdOrPackageName )
108- attrs = os .O_RDWR | os .O_APPEND
109- if _ , err := os .Stat (taskLogFileName ); errors .Is (err , os .ErrNotExist ) {
132+ // open and close the file to truncate it
133+ taskLogFileName = fmt .Sprintf ("%s.log" , buildId )
134+ attrs = os .O_RDWR | os .O_APPEND | os . O_CREATE
135+ if _ , err := os .Stat (taskLogFileName ); ! errors .Is (err , os .ErrNotExist ) {
110136 file , err := os .OpenFile (taskLogFileName , os .O_RDWR | os .O_TRUNC , 0666 )
111137 if err != nil {
112- panic (err )
138+ errFatal (err )
113139 }
114140 defer file .Close ()
141+ log .Printf ("Truncating %s because combined logs were requested" , taskLogFileName )
115142 }
116143 }
117144
145+ // Wait for build to finish
146+ taskCl := getClient (serviceTask ).(peridotopenapi.TaskServiceApi )
147+ log .Printf ("Checking if build %s is finished\n " , buildId )
148+
118149 for {
119- res , _ , err := taskCl .GetTask (getContext (), projectId , buildIdOrPackageName ).Execute ()
150+ res , _ , err := taskCl .GetTask (getContext (), projectId , buildId ).Execute ()
120151 if err != nil {
121152 log .Printf ("Error getting task: %s" , err .Error ())
122153 time .Sleep (5 * time .Second )
@@ -132,31 +163,29 @@ func taskLogsMn(_ *cobra.Command, args []string) {
132163
133164 switch * taskType {
134165 case peridotopenapi .BUILD_ARCH :
135- // log.Printf("subtask %s (%s) finished successfully with status %s\n", t.GetId(), t.GetArch(), t.GetStatus())
136166 // NOTE(neil): 2024-07-25 - ignore error as it tries to unsuccessfully unmarshall json from logs
137167 _ , resp , _ := taskCl .StreamTaskLogs (getContext (), projectId , t .GetId ()).Execute ()
138168
139- defer resp .Body .Close ()
140- if resp != nil && resp .Status == " 200 OK" {
141- // log.Printf("%v", resp.Status)
169+ defer resp .Body .Close ()
170+ if resp != nil && resp .StatusCode == 200 {
171+ // log.Printf("%v", resp.Status)
142172 if ! combined {
143- taskLogFileName = fmt .Sprintf ("%s-%s.log" , buildIdOrPackageName , t .GetArch ())
144- attrs = os .O_RDWR | os .O_CREATE | os .O_TRUNC
173+ taskLogFileName = fmt .Sprintf ("%s_% s-%s.log" , buildId , t . GetId () , t .GetArch ())
174+ attrs = os .O_RDWR | os .O_CREATE | os .O_TRUNC
145175 }
176+ log .Printf ("Writing logs for task (arch=%s,tid=%s) to %v" , t .GetArch (), t .GetId (), taskLogFileName )
146177
147- log .Printf ("Writing to %v with %v" , taskLogFileName , attrs )
148-
149- file , err := os .OpenFile (taskLogFileName , attrs , 0666 )
150- if err != nil {
151- panic (err )
152- }
153- defer file .Close ()
178+ file , err := os .OpenFile (taskLogFileName , attrs , 0666 )
179+ if err != nil {
180+ errFatal (err )
181+ }
182+ defer file .Close ()
154183
155- _ , err = file .ReadFrom (resp .Body )
156- if err != nil {
157- panic (err )
158- }
159- }
184+ _ , err = file .ReadFrom (resp .Body )
185+ if err != nil {
186+ errFatal (err )
187+ }
188+ }
160189 }
161190 }
162191 break
0 commit comments