Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return compose request details as part of metadata response #4451

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 57 additions & 2 deletions internal/cloudapi/v2/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"encoding/json"
"fmt"
"net/http"
"os"
"path"
"slices"
"sort"
"strconv"
Expand All @@ -22,6 +24,7 @@ import (
"github.com/osbuild/images/pkg/sbom"
"github.com/osbuild/osbuild-composer/internal/blueprint"
"github.com/osbuild/osbuild-composer/internal/common"
"github.com/osbuild/osbuild-composer/internal/jsondb"
"github.com/osbuild/osbuild-composer/internal/target"
"github.com/osbuild/osbuild-composer/internal/worker"
"github.com/osbuild/osbuild-composer/internal/worker/clienterrors"
Expand Down Expand Up @@ -150,6 +153,11 @@ func (h *apiHandlers) PostCompose(ctx echo.Context) error {

ctx.Logger().Infof("Job ID %s enqueued for operationID %s", id, ctx.Get(common.OperationIDKey))

// Save the request in the artifacts directory, log errors but continue
if err := saveComposeRequest(h.server.workers.ArtifactsDir(), id, request); err != nil {
ctx.Logger().Warnf("Failed to save compose request: %v", err)
}

return ctx.JSON(http.StatusCreated, &ComposeId{
ObjectReference: ObjectReference{
Href: "/api/image-builder-composer/v2/compose",
Expand Down Expand Up @@ -616,25 +624,33 @@ func (h *apiHandlers) getComposeMetadataImpl(ctx echo.Context, id string) error
return HTTPErrorWithInternal(ErrorComposeNotFound, err)
}

// Get the original compose request, if present
request, err := readComposeRequest(h.server.workers.ArtifactsDir(), jobId)
if err != nil {
ctx.Logger().Warnf("Failed to read compose request: %v", err)
}

if buildInfo.JobStatus.Finished.IsZero() {
// job still running: empty response
return ctx.JSON(200, ComposeMetadata{
ObjectReference: ObjectReference{
Href: fmt.Sprintf("/api/image-builder-composer/v2/%v/metadata", jobId),
Href: fmt.Sprintf("/api/image-builder-composer/v2/composes/%v/metadata", jobId),
Id: jobId.String(),
Kind: "ComposeMetadata",
},
Request: request,
})
}

if buildInfo.JobStatus.Canceled || !result.Success {
// job canceled or failed, empty response
return ctx.JSON(200, ComposeMetadata{
ObjectReference: ObjectReference{
Href: fmt.Sprintf("/api/image-builder-composer/v2/%v/metadata", jobId),
Href: fmt.Sprintf("/api/image-builder-composer/v2/composes/%v/metadata", jobId),
Id: jobId.String(),
Kind: "ComposeMetadata",
},
Request: request,
})
}

Expand Down Expand Up @@ -669,6 +685,7 @@ func (h *apiHandlers) getComposeMetadataImpl(ctx echo.Context, id string) error
Kind: "ComposeMetadata",
},
Packages: &packages,
Request: request,
}

if ostreeCommitMetadata != nil {
Expand Down Expand Up @@ -1514,3 +1531,41 @@ func (h *apiHandlers) GetDistributionList(ctx echo.Context) error {

return ctx.JSON(http.StatusOK, distros)
}

// saveComposeRequest stores the compose request's json on disk
// This is saved in the ComposeRequest directory of the artifacts directory
// If no artifacts directory has been configured it saves nothing and silently returns
func saveComposeRequest(artifactsDir string, id uuid.UUID, request ComposeRequest) error {
if artifactsDir == "" {
return nil
}
p := path.Join(artifactsDir, "ComposeRequest")
err := os.MkdirAll(p, 0700)
if err != nil {
return err
}
db := jsondb.New(p, 0700)
return db.Write(id.String(), request)
}

// readComposeRequest reads the compose request's json on disk
// This reads the original compose request json from the ComposeRequest directory of
// the artifacts directory.
// If no artifacts directory had been setup it silently returns nothing
func readComposeRequest(artifactsDir string, id uuid.UUID) (*ComposeRequest, error) {
if artifactsDir == "" {
return nil, nil
}
p := path.Join(artifactsDir, "ComposeRequest")
err := os.MkdirAll(p, 0700)
if err != nil {
return nil, err
}
db := jsondb.New(p, 0700)
var request ComposeRequest
exists, err := db.Read(id.String(), &request)
if !exists {
return nil, err
}
return &request, err
}
Loading
Loading