Skip to content

Commit

Permalink
feature(peridot-cli): add task logs command
Browse files Browse the repository at this point in the history
`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
  • Loading branch information
NeilHanlon committed Jul 25, 2024
1 parent cfd9d9b commit d56d8f8
Show file tree
Hide file tree
Showing 4 changed files with 243 additions and 2 deletions.
7 changes: 5 additions & 2 deletions peridot/cmd/v1/peridot/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ go_library(
"project_list.go",
"project_catalog_sync.go",
"project_create_hashed_repos.go",
"task.go",
"task_logs.go",
"utils.go",
],
data = [
Expand All @@ -26,6 +28,7 @@ go_library(
"//vendor/github.com/sirupsen/logrus",
"//vendor/github.com/spf13/cobra",
"//vendor/github.com/spf13/viper",
"//vendor/github.com/google/uuid",
"//vendor/openapi.peridot.resf.org/peridotopenapi",
"@org_golang_x_oauth2//:oauth2",
"@org_golang_x_oauth2//clientcredentials",
Expand Down Expand Up @@ -54,8 +57,8 @@ pkg_rpm(
srcs = [":peridot-files"],
license = "MIT",
summary = "Peridot Command Line Interface",
version = "0.2.1",
release = "1",
version = "0.2.2",
release = "2",
architecture = "x86_64",
description = "A command line interface to interact with the Peridot build system",
source_date_epoch = 0,
Expand Down
3 changes: 3 additions & 0 deletions peridot/cmd/v1/peridot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ func init() {
build.AddCommand(buildRpmImport)
build.AddCommand(buildPackage)

root.AddCommand(task)
task.AddCommand(taskLogs)

root.AddCommand(project)
project.AddCommand(projectInfo)
project.AddCommand(projectList)
Expand Down
39 changes: 39 additions & 0 deletions peridot/cmd/v1/peridot/task.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) All respective contributors to the Peridot Project. All rights reserved.
// Copyright (c) 2021-2022 Rocky Enterprise Software Foundation, Inc. All rights reserved.
// Copyright (c) 2021-2022 Ctrl IQ, Inc. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors
// may be used to endorse or promote products derived from this software without
// specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

package main

import (
"github.com/spf13/cobra"
)

var task = &cobra.Command{
Use: "task",
}
196 changes: 196 additions & 0 deletions peridot/cmd/v1/peridot/task_logs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
// Copyright (c) All respective contributors to the Peridot Project. All rights reserved.
// Copyright (c) 2021-2022 Rocky Enterprise Software Foundation, Inc. All rights reserved.
// Copyright (c) 2021-2022 Ctrl IQ, Inc. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors
// may be used to endorse or promote products derived from this software without
// specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

package main

import (
"errors"
"fmt"
"log"
"os"
"sort"
"strconv"
"time"

"github.com/google/uuid"
"github.com/spf13/cobra"
"openapi.peridot.resf.org/peridotopenapi"
)

var taskLogs = &cobra.Command{
Use: "logs [name-or-buildId]",
Args: cobra.ExactArgs(1),
Run: taskLogsMn,
}

var (
architecture string
cwd string
combined bool
taskLogFileName string
attrs int
)

func init() {
taskLogs.Flags().StringVarP(&architecture, "architecture", "a", "", "(inop) filter by architecture")
taskLogs.Flags().BoolVarP(&combined, "combined", "c", false, "dump all logs to one file")
taskLogs.Flags().StringVarP(&cwd, "cwd", "C", "", "change working directory for ouput")
}

func taskLogsMn(_ *cobra.Command, args []string) {
// Ensure project id exists
projectId := mustGetProjectID()

buildIdOrPackageName := args[0]
var buildId string

err := uuid.Validate(buildIdOrPackageName)
if err == nil {
buildId = buildIdOrPackageName
} else {
// argument is not a uuid, try to look up the most recent build for a package with said name
// projectCl := getClient(serviceProject).(peridotopenapi.ProjectServiceApi)
packageCl := getClient(servicePackage).(peridotopenapi.PackageServiceApi)
buildCl := getClient(serviceBuild).(peridotopenapi.BuildServiceApi)

_, _, err := packageCl.GetPackage(getContext(), projectId, "name", buildIdOrPackageName).Execute()
if err != nil {
errFatal(err)
}
// var pkg peridotopenapi.V1Package = *res.Package
// pkgId := pkg.GetId()

// try to get the latest builds for the package
res, _, err := buildCl.ListBuilds(
getContext(),
projectId).FiltersStatus(string(peridotopenapi.SUCCEEDED)).FiltersPackageName(buildIdOrPackageName).Execute()
if err != nil {
errFatal(err)
}

// TODO(neil): why is Total a string?
total, err := strconv.Atoi(*res.Total)
if err != nil {
errFatal(err)
}

// TODO(neil): support pagination
if total > int(*res.Size) {
panic("result set larger than one page")
}

if total > 0 {
builds := *res.Builds

// sort the result set so we're looking at the latest build
sort.Slice(builds, func(i, j int) bool {
return builds[i].CreatedAt.After(*builds[j].CreatedAt)
})

// for _, build := range builds {
// buildjson, _ := build.MarshalJSON()
// log.Printf("build: %s", buildjson)
// }

// after sorting, the first build is the latest
buildId = builds[0].GetTaskId()
}
}

if cwd != "" {
os.Chdir(cwd)
}

if combined {
// open and close the file to truncate it
taskLogFileName = fmt.Sprintf("%s.log", buildId)
attrs = os.O_RDWR | os.O_APPEND | os.O_CREATE
if _, err := os.Stat(taskLogFileName); !errors.Is(err, os.ErrNotExist) {
file, err := os.OpenFile(taskLogFileName, os.O_RDWR|os.O_TRUNC, 0666)
if err != nil {
errFatal(err)
}
defer file.Close()
log.Printf("Truncating %s because combined logs were requested", taskLogFileName)
}
}

// Wait for build to finish
taskCl := getClient(serviceTask).(peridotopenapi.TaskServiceApi)
log.Printf("Checking if build %s is finished\n", buildId)

for {
res, _, err := taskCl.GetTask(getContext(), projectId, buildId).Execute()
if err != nil {
log.Printf("Error getting task: %s", err.Error())
time.Sleep(5 * time.Second)
}
task := res.GetTask()
if task.GetDone() {
for _, t := range task.GetSubtasks() {
taskType, ok := t.GetTypeOk()

if !ok {
continue
}

switch *taskType {
case peridotopenapi.BUILD_ARCH:
// NOTE(neil): 2024-07-25 - ignore error as it tries to unsuccessfully unmarshall json from logs
_, resp, _ := taskCl.StreamTaskLogs(getContext(), projectId, t.GetId()).Execute()

defer resp.Body.Close()
if resp != nil && resp.StatusCode == 200 {
// log.Printf("%v", resp.Status)
if !combined {
taskLogFileName = fmt.Sprintf("%s_%s-%s.log", buildId, t.GetId(), t.GetArch())
attrs = os.O_RDWR | os.O_CREATE | os.O_TRUNC
}
log.Printf("Writing logs for task (arch=%s,tid=%s) to %v", t.GetArch(), t.GetId(), taskLogFileName)

file, err := os.OpenFile(taskLogFileName, attrs, 0666)
if err != nil {
errFatal(err)
}
defer file.Close()

_, err = file.ReadFrom(resp.Body)
if err != nil {
errFatal(err)
}
}
}
}
break
}

time.Sleep(5 * time.Second)
}
}

0 comments on commit d56d8f8

Please sign in to comment.