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

cloudapi download #4590

Open
wants to merge 6 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
2 changes: 2 additions & 0 deletions internal/cloudapi/v2/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ const (
ErrorGettingJobType ServiceErrorCode = 1019
ErrorTenantNotInContext ServiceErrorCode = 1020
ErrorGettingComposeList ServiceErrorCode = 1021
ErrorArtifactNotFound ServiceErrorCode = 1022

// Errors contained within this file
ErrorUnspecified ServiceErrorCode = 10000
Expand Down Expand Up @@ -123,6 +124,7 @@ func getServiceErrors() serviceErrors {
serviceError{ErrorInvalidNumberOfImageBuilds, http.StatusBadRequest, "Compose request has unsupported number of image builds"},
serviceError{ErrorInvalidOSTreeParams, http.StatusBadRequest, "Invalid OSTree parameters or parameter combination"},
serviceError{ErrorTenantNotFound, http.StatusBadRequest, "Tenant not found in JWT claims"},
serviceError{ErrorArtifactNotFound, http.StatusBadRequest, "Artifact not found"},
serviceError{ErrorNoGPGKey, http.StatusBadRequest, "Invalid repository, when check_gpg is set, gpgkey must be specified"},
serviceError{ErrorValidationFailed, http.StatusBadRequest, "Request could not be validated"},
serviceError{ErrorComposeBadState, http.StatusBadRequest, "Compose is running or has failed"},
Expand Down
59 changes: 55 additions & 4 deletions internal/cloudapi/v2/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ func (h *apiHandlers) targetResultToUploadStatus(jobId uuid.UUID, t *target.Targ
workerServerOptions := t.Options.(*target.WorkerServerTargetResultOptions)
absPath, err := h.server.workers.JobArtifactLocation(jobId, workerServerOptions.ArtifactRelPath)
if err != nil {
return nil, fmt.Errorf("unable to find job artefact: %w", err)
return nil, fmt.Errorf("unable to find job artifact: %w", err)
bcl marked this conversation as resolved.
Show resolved Hide resolved
}
uploadOptions = LocalUploadStatus{
ArtifactPath: absPath,
Expand Down Expand Up @@ -365,7 +365,7 @@ func (h *apiHandlers) getJobIDComposeStatus(jobId uuid.UUID) (ComposeStatus, err
tr := result.TargetResults[idx]
us, err := h.targetResultToUploadStatus(jobId, tr)
if err != nil {
return ComposeStatus{}, HTTPError(ErrorUnknownUploadTarget)
return ComposeStatus{}, HTTPErrorWithInternal(ErrorUnknownUploadTarget, err)
}
us.Status = uploadStatusFromJobStatus(jobInfo.JobStatus, result.JobError)
statuses[idx] = *us
Expand Down Expand Up @@ -430,7 +430,7 @@ func (h *apiHandlers) getJobIDComposeStatus(jobId uuid.UUID) (ComposeStatus, err
if tr.Name != target.TargetNameKoji {
us, err := h.targetResultToUploadStatus(jobId, tr)
if err != nil {
return ComposeStatus{}, HTTPError(ErrorUnknownUploadTarget)
return ComposeStatus{}, HTTPErrorWithInternal(ErrorUnknownUploadTarget, err)
}
us.Status = uploadStatusFromJobStatus(buildInfo.JobStatus, result.JobError)
statuses = append(statuses, *us)
Expand Down Expand Up @@ -1191,7 +1191,7 @@ func (h *apiHandlers) postCloneComposeImpl(ctx echo.Context, id string) error {
var us *UploadStatus
us, err = h.targetResultToUploadStatus(jobId, osbuildResult.TargetResults[0])
if err != nil {
return HTTPError(ErrorUnknownUploadTarget)
return HTTPErrorWithInternal(ErrorUnknownUploadTarget, err)
}

var osbuildJob worker.OSBuildJob
Expand Down Expand Up @@ -1514,3 +1514,54 @@ func (h *apiHandlers) GetDistributionList(ctx echo.Context) error {

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

// GetComposeDownload downloads a compose artifact
func (h *apiHandlers) GetComposeDownload(ctx echo.Context, id string) error {
return h.server.EnsureJobChannel(h.getComposeDownloadImpl)(ctx, id)
}

func (h *apiHandlers) getComposeDownloadImpl(ctx echo.Context, id string) error {
jobId, err := uuid.Parse(id)
if err != nil {
return HTTPError(ErrorInvalidComposeId)
}

jobType, err := h.server.workers.JobType(jobId)
if err != nil {
return HTTPError(ErrorComposeNotFound)
}
if jobType != worker.JobTypeOSBuild {
return HTTPError(ErrorInvalidJobType)
}

var osbuildResult worker.OSBuildJobResult
jobInfo, err := h.server.workers.OSBuildJobInfo(jobId, &osbuildResult)
if err != nil {
return HTTPErrorWithInternal(ErrorGettingOSBuildJobStatus, err)
}

// Is it finished?
if jobInfo.JobStatus.Finished.IsZero() {
err := fmt.Errorf("Cannot access artifacts before job is finished: %s", jobId)
return HTTPErrorWithInternal(ErrorArtifactNotFound, err)
}

// Building only supports one target, but that may change, so make sure to check.
// NOTE: TargetResults isn't populated until it is finished
if len(osbuildResult.TargetResults) != 1 {
msg := fmt.Errorf("%#v", osbuildResult.TargetResults)
//return HTTPError(ErrorSeveralUploadTargets)
return HTTPErrorWithInternal(ErrorSeveralUploadTargets, msg)
}
tr := osbuildResult.TargetResults[0]
if tr.OsbuildArtifact == nil {
return HTTPError(ErrorArtifactNotFound)
}

// NOTE: This also returns an error if the job isn't finished or it cannot find the file
file, err := h.server.workers.JobArtifactLocation(jobId, tr.OsbuildArtifact.ExportFilename)
if err != nil {
return HTTPErrorWithInternal(ErrorArtifactNotFound, err)
}
return ctx.Attachment(file, fmt.Sprintf("%s-%s", jobId, tr.OsbuildArtifact.ExportFilename))
}
Loading
Loading