Skip to content

Commit

Permalink
WIP: dump logs from a given parent task ID
Browse files Browse the repository at this point in the history
  • Loading branch information
NeilHanlon committed Jul 25, 2024
1 parent 8960b3a commit f6a0821
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 1 deletion.
5 changes: 4 additions & 1 deletion peridot/cmd/v1/peridot/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,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 @@ -36,6 +38,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 @@ -64,7 +67,7 @@ pkg_rpm(
srcs = [":peridot-files"],
license = "MIT",
summary = "Peridot Command Line Interface",
version = "0.2.1",
version = "0.2.2",
release = "1",
architecture = "x86_64",
description = "A command line interface to interact with the Peridot build system",
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 @@ -62,6 +62,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",
}
167 changes: 167 additions & 0 deletions peridot/cmd/v1/peridot/task_logs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
// 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"
"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", "", "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()

// buildCl := getClient(serviceBuild).(peridotopenapi.BuildServiceApi)
// body := peridotopenapi.BuildServiceSubmitBuildBody{
// PackageName: &args[0],
// DisableChecks: &disableChecks,
// Branches: &branches,
// ModuleVariant: &moduleVariant,
// SideNvrs: &sideNvrs,
// SetInactive: &setInactive,
// }
// if scmHash != "" {
// body.ScmHash = &scmHash
// }
// req := buildCl.SubmitBuild(getContext(), projectId).Body(body)
// buildRes, _, err := req.Execute()
// errFatal(err)

buildIdOrPackageName := args[0]

err := uuid.Validate(buildIdOrPackageName)
if err == nil {
// 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)

res := packageCl.GetPackage(getContext(), projectId, "name", buildIdOrPackageName)
log.Printf("%s", res)
}

// Wait for build to finish
taskCl := getClient(serviceTask).(peridotopenapi.TaskServiceApi)
log.Printf("Waiting for build %s to finish\n", buildIdOrPackageName)

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

if combined {
// open and close the file to truncate it
taskLogFileName = fmt.Sprintf("%s.log", buildIdOrPackageName)
attrs = os.O_RDWR|os.O_APPEND
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 {
panic(err)
}
defer file.Close()
}
}

for {
res, _, err := taskCl.GetTask(getContext(), projectId, buildIdOrPackageName).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:
// log.Printf("subtask %s (%s) finished successfully with status %s\n", t.GetId(), t.GetArch(), t.GetStatus())
// 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.Status == "200 OK" {
// log.Printf("%v", resp.Status)
if !combined {
taskLogFileName = fmt.Sprintf("%s-%s.log", buildIdOrPackageName, t.GetArch())
attrs = os.O_RDWR|os.O_CREATE|os.O_TRUNC
}

log.Printf("Writing to %v with %v", taskLogFileName, attrs)

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

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

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

0 comments on commit f6a0821

Please sign in to comment.