distro: introduce Arch and ImageType interfaces

Objects implementing these interfaces will represent the
architecture support for a given distro and the image type
support for a given architecture distro combination, respectively.

The idea is to always resolve to these objects early, and drop
the equilavent methods from the distro interface. This means that
we convert our input strings to real objects once, and then never
have to verify their correctness again.

Signed-off-by: Tom Gundersen <teg@jklm.no>
This commit is contained in:
Tom Gundersen 2020-03-22 13:08:09 +01:00 committed by msehnout
parent ff0ec01eb6
commit bbd8dc338d
8 changed files with 669 additions and 33 deletions

View file

@ -6,6 +6,7 @@ import (
"strconv"
"github.com/osbuild/osbuild-composer/internal/common"
"github.com/osbuild/osbuild-composer/internal/distro"
"github.com/osbuild/osbuild-composer/internal/osbuild"
"github.com/google/uuid"
@ -15,12 +16,6 @@ import (
"github.com/osbuild/osbuild-composer/internal/rpmmd"
)
type Fedora31 struct {
arches map[string]arch
outputs map[string]output
buildPackages []string
}
type arch struct {
Name string
BootloaderPackages []string
@ -44,6 +39,103 @@ type output struct {
const Distro = common.Fedora31
const ModulePlatformID = "platform:f31"
type Fedora31 struct {
arches map[string]arch
outputs map[string]output
buildPackages []string
}
type Fedora31Arch struct {
name string
distro *Fedora31
arch *arch
}
type Fedora31ImageType struct {
name string
arch *Fedora31Arch
output *output
}
func (d *Fedora31) GetArch(arch string) (distro.Arch, error) {
a, exists := d.arches[arch]
if !exists {
return nil, errors.New("invalid architecture: " + arch)
}
return &Fedora31Arch{
name: arch,
distro: d,
arch: &a,
}, nil
}
func (a *Fedora31Arch) Name() string {
return a.name
}
func (a *Fedora31Arch) ListImageTypes() []string {
return a.distro.ListOutputFormats()
}
func (a *Fedora31Arch) GetImageType(imageType string) (distro.ImageType, error) {
t, exists := a.distro.outputs[imageType]
if !exists {
return nil, errors.New("invalid image type: " + imageType)
}
return &Fedora31ImageType{
name: imageType,
arch: a,
output: &t,
}, nil
}
func (t *Fedora31ImageType) Name() string {
return t.name
}
func (t *Fedora31ImageType) Filename() string {
return t.output.Name
}
func (t *Fedora31ImageType) MIMEType() string {
return t.output.MimeType
}
func (t *Fedora31ImageType) Size(size uint64) uint64 {
return t.arch.distro.GetSizeForOutputType(t.name, size)
}
func (t *Fedora31ImageType) BasePackages() ([]string, []string) {
packages := t.output.Packages
if t.output.Bootable {
packages = append(packages, t.arch.arch.BootloaderPackages...)
}
return packages, t.output.ExcludedPackages
}
func (t *Fedora31ImageType) BuildPackages() []string {
return append(t.arch.distro.buildPackages, t.arch.arch.BuildPackages...)
}
func (t *Fedora31ImageType) Manifest(c *blueprint.Customizations,
repos []rpmmd.RepoConfig,
packageSpecs,
buildPackageSpecs []rpmmd.PackageSpec,
size uint64) (*osbuild.Manifest, error) {
pipeline, err := t.arch.distro.pipeline(c, repos, packageSpecs, buildPackageSpecs, t.arch.name, t.name, size)
if err != nil {
return nil, err
}
return &osbuild.Manifest{
Sources: *t.arch.distro.sources(append(packageSpecs, buildPackageSpecs...)),
Pipeline: *pipeline,
}, nil
}
func New() *Fedora31 {
const GigaByte = 1024 * 1024 * 1024