Skip to content

Commit d56d8f8

Browse files
committed
feature(peridot-cli): add task logs command
`peridot task logs` will accept either a build ID or package name. In the latter case, the project will be queried for the latest build for the given package name. With the build task ID in hand, the task can be queried for its BUILD_ARCH subtasks, which contain the logs for the binary RPM builds. Passing `-C` or `--cwd` with a directory will change into that directory before writing files. That directory **must** exist beforehand. Otherwise, $PWD is used. By default, a separate file will be created for each <task>-<subtask>-<architecture> tuple, which accounts for modular packages with multiple outputs. Passing the `-c` or `--combined` flag will combine all the logs into a single file, named <task id>.log
1 parent cfd9d9b commit d56d8f8

File tree

4 files changed

+243
-2
lines changed

4 files changed

+243
-2
lines changed

peridot/cmd/v1/peridot/BUILD.bazel

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ go_library(
1515
"project_list.go",
1616
"project_catalog_sync.go",
1717
"project_create_hashed_repos.go",
18+
"task.go",
19+
"task_logs.go",
1820
"utils.go",
1921
],
2022
data = [
@@ -26,6 +28,7 @@ go_library(
2628
"//vendor/github.com/sirupsen/logrus",
2729
"//vendor/github.com/spf13/cobra",
2830
"//vendor/github.com/spf13/viper",
31+
"//vendor/github.com/google/uuid",
2932
"//vendor/openapi.peridot.resf.org/peridotopenapi",
3033
"@org_golang_x_oauth2//:oauth2",
3134
"@org_golang_x_oauth2//clientcredentials",
@@ -54,8 +57,8 @@ pkg_rpm(
5457
srcs = [":peridot-files"],
5558
license = "MIT",
5659
summary = "Peridot Command Line Interface",
57-
version = "0.2.1",
58-
release = "1",
60+
version = "0.2.2",
61+
release = "2",
5962
architecture = "x86_64",
6063
description = "A command line interface to interact with the Peridot build system",
6164
source_date_epoch = 0,

peridot/cmd/v1/peridot/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ func init() {
5858
build.AddCommand(buildRpmImport)
5959
build.AddCommand(buildPackage)
6060

61+
root.AddCommand(task)
62+
task.AddCommand(taskLogs)
63+
6164
root.AddCommand(project)
6265
project.AddCommand(projectInfo)
6366
project.AddCommand(projectList)

peridot/cmd/v1/peridot/task.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) All respective contributors to the Peridot Project. All rights reserved.
2+
// Copyright (c) 2021-2022 Rocky Enterprise Software Foundation, Inc. All rights reserved.
3+
// Copyright (c) 2021-2022 Ctrl IQ, Inc. All rights reserved.
4+
//
5+
// Redistribution and use in source and binary forms, with or without
6+
// modification, are permitted provided that the following conditions are met:
7+
//
8+
// 1. Redistributions of source code must retain the above copyright notice,
9+
// this list of conditions and the following disclaimer.
10+
//
11+
// 2. Redistributions in binary form must reproduce the above copyright notice,
12+
// this list of conditions and the following disclaimer in the documentation
13+
// and/or other materials provided with the distribution.
14+
//
15+
// 3. Neither the name of the copyright holder nor the names of its contributors
16+
// may be used to endorse or promote products derived from this software without
17+
// specific prior written permission.
18+
//
19+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22+
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23+
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24+
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25+
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26+
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27+
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28+
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29+
// POSSIBILITY OF SUCH DAMAGE.
30+
31+
package main
32+
33+
import (
34+
"github.com/spf13/cobra"
35+
)
36+
37+
var task = &cobra.Command{
38+
Use: "task",
39+
}
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
// Copyright (c) All respective contributors to the Peridot Project. All rights reserved.
2+
// Copyright (c) 2021-2022 Rocky Enterprise Software Foundation, Inc. All rights reserved.
3+
// Copyright (c) 2021-2022 Ctrl IQ, Inc. All rights reserved.
4+
//
5+
// Redistribution and use in source and binary forms, with or without
6+
// modification, are permitted provided that the following conditions are met:
7+
//
8+
// 1. Redistributions of source code must retain the above copyright notice,
9+
// this list of conditions and the following disclaimer.
10+
//
11+
// 2. Redistributions in binary form must reproduce the above copyright notice,
12+
// this list of conditions and the following disclaimer in the documentation
13+
// and/or other materials provided with the distribution.
14+
//
15+
// 3. Neither the name of the copyright holder nor the names of its contributors
16+
// may be used to endorse or promote products derived from this software without
17+
// specific prior written permission.
18+
//
19+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22+
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23+
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24+
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25+
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26+
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27+
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28+
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29+
// POSSIBILITY OF SUCH DAMAGE.
30+
31+
package main
32+
33+
import (
34+
"errors"
35+
"fmt"
36+
"log"
37+
"os"
38+
"sort"
39+
"strconv"
40+
"time"
41+
42+
"github.com/google/uuid"
43+
"github.com/spf13/cobra"
44+
"openapi.peridot.resf.org/peridotopenapi"
45+
)
46+
47+
var taskLogs = &cobra.Command{
48+
Use: "logs [name-or-buildId]",
49+
Args: cobra.ExactArgs(1),
50+
Run: taskLogsMn,
51+
}
52+
53+
var (
54+
architecture string
55+
cwd string
56+
combined bool
57+
taskLogFileName string
58+
attrs int
59+
)
60+
61+
func init() {
62+
taskLogs.Flags().StringVarP(&architecture, "architecture", "a", "", "(inop) filter by architecture")
63+
taskLogs.Flags().BoolVarP(&combined, "combined", "c", false, "dump all logs to one file")
64+
taskLogs.Flags().StringVarP(&cwd, "cwd", "C", "", "change working directory for ouput")
65+
}
66+
67+
func taskLogsMn(_ *cobra.Command, args []string) {
68+
// Ensure project id exists
69+
projectId := mustGetProjectID()
70+
71+
buildIdOrPackageName := args[0]
72+
var buildId string
73+
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
79+
// projectCl := getClient(serviceProject).(peridotopenapi.ProjectServiceApi)
80+
packageCl := getClient(servicePackage).(peridotopenapi.PackageServiceApi)
81+
buildCl := getClient(serviceBuild).(peridotopenapi.BuildServiceApi)
82+
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()
89+
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+
}
126+
127+
if cwd != "" {
128+
os.Chdir(cwd)
129+
}
130+
131+
if combined {
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) {
136+
file, err := os.OpenFile(taskLogFileName, os.O_RDWR|os.O_TRUNC, 0666)
137+
if err != nil {
138+
errFatal(err)
139+
}
140+
defer file.Close()
141+
log.Printf("Truncating %s because combined logs were requested", taskLogFileName)
142+
}
143+
}
144+
145+
// Wait for build to finish
146+
taskCl := getClient(serviceTask).(peridotopenapi.TaskServiceApi)
147+
log.Printf("Checking if build %s is finished\n", buildId)
148+
149+
for {
150+
res, _, err := taskCl.GetTask(getContext(), projectId, buildId).Execute()
151+
if err != nil {
152+
log.Printf("Error getting task: %s", err.Error())
153+
time.Sleep(5 * time.Second)
154+
}
155+
task := res.GetTask()
156+
if task.GetDone() {
157+
for _, t := range task.GetSubtasks() {
158+
taskType, ok := t.GetTypeOk()
159+
160+
if !ok {
161+
continue
162+
}
163+
164+
switch *taskType {
165+
case peridotopenapi.BUILD_ARCH:
166+
// NOTE(neil): 2024-07-25 - ignore error as it tries to unsuccessfully unmarshall json from logs
167+
_, resp, _ := taskCl.StreamTaskLogs(getContext(), projectId, t.GetId()).Execute()
168+
169+
defer resp.Body.Close()
170+
if resp != nil && resp.StatusCode == 200 {
171+
// log.Printf("%v", resp.Status)
172+
if !combined {
173+
taskLogFileName = fmt.Sprintf("%s_%s-%s.log", buildId, t.GetId(), t.GetArch())
174+
attrs = os.O_RDWR | os.O_CREATE | os.O_TRUNC
175+
}
176+
log.Printf("Writing logs for task (arch=%s,tid=%s) to %v", t.GetArch(), t.GetId(), taskLogFileName)
177+
178+
file, err := os.OpenFile(taskLogFileName, attrs, 0666)
179+
if err != nil {
180+
errFatal(err)
181+
}
182+
defer file.Close()
183+
184+
_, err = file.ReadFrom(resp.Body)
185+
if err != nil {
186+
errFatal(err)
187+
}
188+
}
189+
}
190+
}
191+
break
192+
}
193+
194+
time.Sleep(5 * time.Second)
195+
}
196+
}

0 commit comments

Comments
 (0)