-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: dump logs from a given parent task ID
- Loading branch information
1 parent
8960b3a
commit f6a0821
Showing
4 changed files
with
213 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |