distro: get proper image size from distro

When a user does not define the image size for a compose the default
image size of that image type is used. In order to properly store the
compose's image size even if the default is used the store calls the
distro function GetSizeForOutputType. This function accepts an output
format and image size. If the image size is 0 then the default
value for the output format will be returned. Also, for vhd images the
size must be rounded. This is now handled in the distro function instead
of the api.
This commit is contained in:
Jacob Kozol 2020-02-06 13:49:58 +01:00 committed by Tom Gundersen
parent e30f40873d
commit abb5469b30
7 changed files with 37 additions and 16 deletions

View file

@ -31,6 +31,10 @@ type Distro interface {
// format. `outputFormat` must be one returned by
FilenameFromType(outputFormat string) (string, string, error)
// Returns the proper image size for a given output format. If the size
// is 0 the default value for the format will be returned.
GetSizeForOutputType(outputFormat string, size uint64) uint64
// Returns an osbuild pipeline that generates an image in the given
// output format with all packages and customizations specified in the
// given blueprint.

View file

@ -48,7 +48,8 @@ func TestDistro_Pipeline(t *testing.T) {
t.Errorf("unknown distro: %v", tt.Compose.Distro)
return
}
got, err := d.Pipeline(tt.Compose.Blueprint, nil, tt.Compose.Checksums, tt.Compose.Arch, tt.Compose.OutputFormat, 0)
size := d.GetSizeForOutputType(tt.Compose.OutputFormat, 0)
got, err := d.Pipeline(tt.Compose.Blueprint, nil, tt.Compose.Checksums, tt.Compose.Arch, tt.Compose.OutputFormat, size)
if (err != nil) != (tt.Pipeline == nil) {
t.Errorf("distro.Pipeline() error = %v", err)
return

View file

@ -321,6 +321,18 @@ func (r *Fedora30) FilenameFromType(outputFormat string) (string, string, error)
return "", "", errors.New("invalid output format: " + outputFormat)
}
func (r *Fedora30) GetSizeForOutputType(outputFormat string, size uint64) uint64 {
const MegaByte = 1024 * 1024
// Microsoft Azure requires vhd images to be rounded up to the nearest MB
if outputFormat == "vhd" && size%MegaByte != 0 {
size = (size/MegaByte + 1) * MegaByte
}
if size == 0 {
size = r.outputs[outputFormat].DefaultSize
}
return size
}
func (r *Fedora30) Pipeline(b *blueprint.Blueprint, additionalRepos []rpmmd.RepoConfig, checksums map[string]string, outputArchitecture, outputFormat string, size uint64) (*pipeline.Pipeline, error) {
output, exists := r.outputs[outputFormat]
if !exists {
@ -397,9 +409,6 @@ func (r *Fedora30) Pipeline(b *blueprint.Blueprint, additionalRepos []rpmmd.Repo
p.AddStage(pipeline.NewSELinuxStage(r.selinuxStageOptions()))
if size == 0 {
size = output.DefaultSize
}
p.Assembler = output.Assembler(arch.UEFI, size)
return p, nil

View file

@ -456,6 +456,18 @@ func (r *RHEL82) FilenameFromType(outputFormat string) (string, string, error) {
return "", "", errors.New("invalid output format: " + outputFormat)
}
func (r *RHEL82) GetSizeForOutputType(outputFormat string, size uint64) uint64 {
const MegaByte = 1024 * 1024
// Microsoft Azure requires vhd images to be rounded up to the nearest MB
if outputFormat == "vhd" && size%MegaByte != 0 {
size = (size/MegaByte + 1) * MegaByte
}
if size == 0 {
size = r.outputs[outputFormat].DefaultSize
}
return size
}
func (r *RHEL82) Pipeline(b *blueprint.Blueprint, additionalRepos []rpmmd.RepoConfig, checksums map[string]string, outputArchitecture, outputFormat string, size uint64) (*pipeline.Pipeline, error) {
output, exists := r.outputs[outputFormat]
if !exists {
@ -537,9 +549,6 @@ func (r *RHEL82) Pipeline(b *blueprint.Blueprint, additionalRepos []rpmmd.RepoCo
p.AddStage(pipeline.NewSELinuxStage(r.selinuxStageOptions()))
if size == 0 {
size = output.DefaultSize
}
p.Assembler = output.Assembler(arch.UEFI, size)
return p, nil

View file

@ -42,6 +42,10 @@ func (d *TestDistro) FilenameFromType(outputFormat string) (string, string, erro
}
}
func (r *TestDistro) GetSizeForOutputType(outputFormat string, size uint64) uint64 {
return 0
}
func (d *TestDistro) Pipeline(b *blueprint.Blueprint, additionalRepos []rpmmd.RepoConfig, checksums map[string]string, outputArch, outputFormat string, size uint64) (*pipeline.Pipeline, error) {
if outputFormat == "test_output" && outputArch == "test_arch" {
return &pipeline.Pipeline{}, nil

View file

@ -484,6 +484,8 @@ func (s *Store) PushCompose(composeID uuid.UUID, bp *blueprint.Blueprint, checks
repos = append(repos, source.RepoConfig())
}
size = s.distro.GetSizeForOutputType(composeType, size)
pipeline, err := s.distro.Pipeline(bp, repos, checksums, arch, composeType, size)
if err != nil {
return err

View file

@ -1211,7 +1211,6 @@ func (api *API) composeHandler(writer http.ResponseWriter, request *http.Request
BuildID uuid.UUID `json:"build_id"`
Status bool `json:"status"`
}
const MegaByte = 1024 * 1024
contentType := request.Header["Content-Type"]
if len(contentType) != 1 || contentType[0] != "application/json" {
@ -1257,13 +1256,6 @@ func (api *API) composeHandler(writer http.ResponseWriter, request *http.Request
bp := api.store.GetBlueprintCommitted(cr.BlueprintName)
size := cr.Size
// Microsoft Azure requires vhd images to be rounded up to the nearest MB
if cr.ComposeType == "vhd" && size%MegaByte != 0 {
size = (size/MegaByte + 1) * MegaByte
}
if bp != nil {
_, checksums, err := api.depsolveBlueprint(bp, true)
if err != nil {
@ -1275,7 +1267,7 @@ func (api *API) composeHandler(writer http.ResponseWriter, request *http.Request
return
}
err = api.store.PushCompose(reply.BuildID, bp, checksums, api.arch, cr.ComposeType, size, uploadTarget)
err = api.store.PushCompose(reply.BuildID, bp, checksums, api.arch, cr.ComposeType, cr.Size, uploadTarget)
// TODO: we should probably do some kind of blueprint validation in future
// for now, let's just 500 and bail out