Skip to content

Commit

Permalink
handle package by name, check for errors, fix assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
NeilHanlon committed Jul 25, 2024
1 parent f6a0821 commit 1308522
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 55 deletions.
2 changes: 1 addition & 1 deletion peridot/cmd/v1/peridot/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pkg_rpm(
license = "MIT",
summary = "Peridot Command Line Interface",
version = "0.2.2",
release = "1",
release = "2",
architecture = "x86_64",
description = "A command line interface to interact with the Peridot build system",
source_date_epoch = 0,
Expand Down
137 changes: 83 additions & 54 deletions peridot/cmd/v1/peridot/task_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import (
"fmt"
"log"
"os"
"sort"
"strconv"
"time"

"github.com/google/uuid"
Expand All @@ -49,15 +51,15 @@ var taskLogs = &cobra.Command{
}

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

func init() {
taskLogs.Flags().StringVarP(&architecture, "architecture", "a", "", "filter by architecture")
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")
}
Expand All @@ -66,57 +68,86 @@ 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]
var buildId string

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
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)

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

// Wait for build to finish
taskCl := getClient(serviceTask).(peridotopenapi.TaskServiceApi)
log.Printf("Waiting for build %s to finish\n", buildIdOrPackageName)
// 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", buildIdOrPackageName)
attrs = os.O_RDWR|os.O_APPEND
if _, err := os.Stat(taskLogFileName); errors.Is(err, os.ErrNotExist) {
// 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 {
panic(err)
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, buildIdOrPackageName).Execute()
res, _, err := taskCl.GetTask(getContext(), projectId, buildId).Execute()
if err != nil {
log.Printf("Error getting task: %s", err.Error())
time.Sleep(5 * time.Second)
Expand All @@ -132,31 +163,29 @@ func taskLogsMn(_ *cobra.Command, args []string) {

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)
defer resp.Body.Close()
if resp != nil && resp.StatusCode == 200 {
// 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
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)

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

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

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

0 comments on commit 1308522

Please sign in to comment.