koji: use nvra as the filename for images

We have the same thing for AWS. The AWS target also specifies under what name
should be the image available in EC2.

As requested by Brew maintainers Tomáš Kopeček and Lubomír Sedlář.
This commit is contained in:
Ondřej Budai 2020-10-22 14:54:59 +02:00 committed by Tom Gundersen
parent b2ed59c385
commit befeef34a5
4 changed files with 64 additions and 5 deletions

View file

@ -231,7 +231,7 @@ func RunJob(job worker.Job, store string, kojiServers map[string]koji.GSSAPICred
continue continue
} }
hash, filesize, err := k.Upload(f, options.UploadDirectory, options.Filename) hash, filesize, err := k.Upload(f, options.UploadDirectory, options.KojiFilename)
if err != nil { if err != nil {
r = append(r, err) r = append(r, err)
continue continue
@ -274,7 +274,7 @@ func RunJob(job worker.Job, store string, kojiServers map[string]koji.GSSAPICred
output := []koji.Image{ output := []koji.Image{
{ {
BuildRootID: 1, BuildRootID: 1,
Filename: options.Filename, Filename: options.KojiFilename,
FileSize: uint64(filesize), FileSize: uint64(filesize),
Arch: common.CurrentArch(), Arch: common.CurrentArch(),
ChecksumType: "md5", ChecksumType: "md5",

View file

@ -8,9 +8,11 @@ import (
"log" "log"
"net/http" "net/http"
"net/url" "net/url"
"strings"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"github.com/osbuild/osbuild-composer/internal/blueprint" "github.com/osbuild/osbuild-composer/internal/blueprint"
"github.com/osbuild/osbuild-composer/internal/common" "github.com/osbuild/osbuild-composer/internal/common"
"github.com/osbuild/osbuild-composer/internal/distro" "github.com/osbuild/osbuild-composer/internal/distro"
@ -88,6 +90,7 @@ func (h *apiHandlers) PostCompose(ctx echo.Context) error {
manifest distro.Manifest manifest distro.Manifest
arch string arch string
filename string filename string
kojiFilename string
} }
imageRequests := make([]imageRequest, len(request.ImageRequests)) imageRequests := make([]imageRequest, len(request.ImageRequests))
@ -131,6 +134,17 @@ func (h *apiHandlers) PostCompose(ctx echo.Context) error {
imageRequests[i].manifest = manifest imageRequests[i].manifest = manifest
imageRequests[i].arch = arch.Name() imageRequests[i].arch = arch.Name()
imageRequests[i].filename = imageType.Filename() imageRequests[i].filename = imageType.Filename()
filename := fmt.Sprintf(
"%s-%s-%s.%s%s",
request.Name,
request.Version,
request.Release,
ir.Architecture,
splitExtension(imageType.Filename()),
)
imageRequests[i].kojiFilename = filename
} }
var ir imageRequest var ir imageRequest
@ -176,6 +190,7 @@ func (h *apiHandlers) PostCompose(ctx echo.Context) error {
Filename: ir.filename, Filename: ir.filename,
UploadDirectory: "osbuild-composer-koji-" + uuid.New().String(), UploadDirectory: "osbuild-composer-koji-" + uuid.New().String(),
Server: request.Koji.Server, Server: request.Koji.Server,
KojiFilename: ir.kojiFilename,
}), }),
}) })
if err != nil { if err != nil {
@ -189,6 +204,25 @@ func (h *apiHandlers) PostCompose(ctx echo.Context) error {
}) })
} }
// splitExtension returns the extension of the given file. If there's
// a multipart extension (e.g. file.tar.gz), it returns all parts (e.g.
// .tar.gz). If there's no extension in the input, it returns an empty
// string. If the filename starts with dot, the part before the second dot
// is not considered as an extension.
func splitExtension(filename string) string {
filenameParts := strings.Split(filename, ".")
if len(filenameParts) > 0 && filenameParts[0] == "" {
filenameParts = filenameParts[1:]
}
if len(filenameParts) <= 1 {
return ""
}
return "." + strings.Join(filenameParts[1:], ".")
}
func composeStateToStatus(state common.ComposeState) string { func composeStateToStatus(state common.ComposeState) string {
switch state { switch state {
case common.CFailed: case common.CFailed:

View file

@ -0,0 +1,24 @@
package kojiapi
import (
"github.com/stretchr/testify/require"
"testing"
)
func TestSplitExtension(t *testing.T) {
tests := []struct {
filename string
extension string
}{
{filename: "image.qcow2", extension: ".qcow2"},
{filename: "image.tar.gz", extension: ".tar.gz"},
{filename: "", extension: ""},
{filename: ".htaccess", extension: ""},
{filename: ".weirdfile.txt", extension: ".txt"},
}
for _, tt := range tests {
t.Run(tt.filename, func(t *testing.T) {
require.Equal(t, tt.extension, splitExtension(tt.filename))
})
}
}

View file

@ -10,6 +10,7 @@ type KojiTargetOptions struct {
Filename string `json:"filename"` Filename string `json:"filename"`
UploadDirectory string `json:"upload_directory"` UploadDirectory string `json:"upload_directory"`
Server string `json:"server"` Server string `json:"server"`
KojiFilename string `json:"koji_filename"`
} }
func (KojiTargetOptions) isTargetOptions() {} func (KojiTargetOptions) isTargetOptions() {}