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

Refactor distros so we don't have to add a new one whenever it gets branched #104

Closed
wants to merge 7 commits into from
Closed
4 changes: 2 additions & 2 deletions cmd/build/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/osbuild/images/pkg/blueprint"
"github.com/osbuild/images/pkg/container"
"github.com/osbuild/images/pkg/distro"
"github.com/osbuild/images/pkg/distroregistry"
"github.com/osbuild/images/pkg/distrolist"
"github.com/osbuild/images/pkg/manifest"
"github.com/osbuild/images/pkg/osbuild"
"github.com/osbuild/images/pkg/ostree"
Expand Down Expand Up @@ -327,7 +327,7 @@ func main() {

seedArg := int64(0)
darm := readRepos()
distroReg := distroregistry.NewDefault()
distroReg := distrolist.NewDefault()

config := loadConfig(configFile)

Expand Down
6 changes: 3 additions & 3 deletions cmd/gen-manifests/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import (
"github.com/gobwas/glob"

"github.com/osbuild/images/internal/dnfjson"
"github.com/osbuild/images/internal/testdistrolist"
"github.com/osbuild/images/pkg/blueprint"
"github.com/osbuild/images/pkg/container"
"github.com/osbuild/images/pkg/distro"
"github.com/osbuild/images/pkg/distroregistry"
"github.com/osbuild/images/pkg/manifest"
"github.com/osbuild/images/pkg/ostree"
"github.com/osbuild/images/pkg/rhsm/facts"
Expand Down Expand Up @@ -628,7 +628,7 @@ func main() {

seedArg := int64(0)
darm := readRepos()
distroReg := distroregistry.NewDefault()
distroReg := testdistrolist.New()
jobs := make([]manifestJob, 0)

contentResolve := map[string]bool{
Expand All @@ -644,7 +644,7 @@ func main() {
}

fmt.Println("Collecting jobs")
distros, invalidDistros := resolveArgValues(distros, distroReg.List())
distros, invalidDistros := resolveArgValues(distros, distroReg.ListTested())
if len(invalidDistros) > 0 {
fmt.Fprintf(os.Stderr, "WARNING: invalid distro names: [%s]\n", strings.Join(invalidDistros, ","))
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/list-images/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (
"strings"

"github.com/gobwas/glob"
"github.com/osbuild/images/pkg/distroregistry"

"github.com/osbuild/images/internal/testdistrolist"
)

type multiValue []string
Expand Down Expand Up @@ -74,8 +75,8 @@ func main() {
flag.BoolVar(&json, "json", false, "print configs as json")
flag.Parse()

distroReg := distroregistry.NewDefault()
distros, invalidDistros := resolveArgValues(distros, distroReg.List())
distroReg := testdistrolist.New()
distros, invalidDistros := resolveArgValues(distros, distroReg.ListTested())
if len(invalidDistros) > 0 {
fmt.Fprintf(os.Stderr, "WARNING: invalid distro names: [%s]\n", strings.Join(invalidDistros, ","))
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/osbuild-composer-image-definitions/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
"fmt"
"os"

"github.com/osbuild/images/pkg/distroregistry"
"github.com/osbuild/images/internal/testdistrolist"
)

func main() {
definitions := map[string]map[string][]string{}
distroRegistry := distroregistry.NewDefault()
distroRegistry := testdistrolist.New()

for _, distroName := range distroRegistry.List() {
for _, distroName := range distroRegistry.ListTested() {
distro := distroRegistry.GetDistro(distroName)
for _, archName := range distro.ListArches() {
arch, err := distro.GetArch(archName)
Expand Down
4 changes: 2 additions & 2 deletions cmd/osbuild-package-sets/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"fmt"
"os"

"github.com/osbuild/images/internal/testdistrolist"
"github.com/osbuild/images/pkg/blueprint"
"github.com/osbuild/images/pkg/distro"
"github.com/osbuild/images/pkg/distroregistry"
"github.com/osbuild/images/pkg/ostree"
)

Expand All @@ -29,7 +29,7 @@ func main() {
os.Exit(1)
}

dr := distroregistry.NewDefault()
dr := testdistrolist.New()

d := dr.GetDistro(distroName)
if d == nil {
Expand Down
10 changes: 6 additions & 4 deletions cmd/osbuild-pipeline/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import (

"github.com/osbuild/images/internal/common"
"github.com/osbuild/images/internal/dnfjson"
"github.com/osbuild/images/internal/testdistrolist"
"github.com/osbuild/images/pkg/container"
"github.com/osbuild/images/pkg/distro"
"github.com/osbuild/images/pkg/distroregistry"
distrolist2 "github.com/osbuild/images/pkg/distrolist"
"github.com/osbuild/images/pkg/ostree"

"github.com/osbuild/images/pkg/blueprint"
Expand Down Expand Up @@ -111,11 +112,12 @@ func main() {
}
}

distros := distroregistry.NewDefault()
distros := distrolist2.NewDefault()
d := distros.GetDistro(composeRequest.Distro)
if d == nil {
_, _ = fmt.Fprintf(os.Stderr, "The provided distribution '%s' is not supported. Use one of these:\n", composeRequest.Distro)
for _, d := range distros.List() {
_, _ = fmt.Fprintf(os.Stderr, "The provided distribution '%s' is not supported. Use one of these (non-exhaustive list):\n", composeRequest.Distro)
dl := testdistrolist.New()
for _, d := range dl.ListTested() {
_, _ = fmt.Fprintln(os.Stderr, " *", d)
}
return
Expand Down
11 changes: 6 additions & 5 deletions cmd/osbuild-playground/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

"github.com/osbuild/images/internal/common"
"github.com/osbuild/images/pkg/distro"
"github.com/osbuild/images/pkg/distroregistry"
"github.com/osbuild/images/pkg/distrolist"
"github.com/osbuild/images/pkg/image"
"github.com/osbuild/images/pkg/rpmmd"
)
Expand Down Expand Up @@ -72,12 +72,13 @@ func main() {
}
}

distros := distroregistry.NewDefault()
distros := distrolist.NewDefault()
var d distro.Distro
var err error
if distroArg == "host" {
d = distros.FromHost()
if d == nil {
panic("host distro not supported")
distroArg, _, _, err = common.GetHostDistroName()
if err != nil {
panic(fmt.Sprintf("cannot infer host distro: %v", err))
}
} else {
d = distros.GetDistro(distroArg)
Expand Down
38 changes: 38 additions & 0 deletions internal/testdistrolist/distrolist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package testdistrolist

import (
"fmt"
"os"
"strings"

"github.com/osbuild/images/pkg/distrolist"
)

type DistroList struct {
distrolist.List
}

func New() DistroList {
return DistroList{
distrolist.NewDefault(),
}
}

func (l *DistroList) ListTested() []string {
files, err := os.ReadDir("test/data/repositories")
if err != nil {
panic(fmt.Sprintf("error when enumerating the test repositories: %v", err))
}

var distros []string
for _, f := range files {
if f.IsDir() {
continue
}

d := strings.TrimSuffix(f.Name(), ".json")
distros = append(distros, d)
}

return distros
}
13 changes: 13 additions & 0 deletions pkg/distro/fedora/distro.go
Original file line number Diff line number Diff line change
Expand Up @@ -750,3 +750,16 @@ func newDistro(version int) distro.Distro {
rd.addArches(x86_64, aarch64)
return &rd
}

func New(idStr string) distro.Distro {
id, err := distro.ParseName(idStr)
if err != nil {
return nil
}

if id.Name != "fedora" {
return nil
}

return newDistro(id.MajorVersion)
}
66 changes: 66 additions & 0 deletions pkg/distro/id.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package distro

import (
"fmt"
"strconv"
"strings"
)

type ID struct {
Name string
MajorVersion int
MinorVersion int
}

type ParseError struct {
ToParse string
Msg string
Inner error
}

func (e ParseError) Error() string {
msg := fmt.Sprintf("error when parsing distro name (%s): %v", e.ToParse, e.Msg)

if e.Inner != nil {
msg += fmt.Sprintf(", inner error:\n%v", e.Inner)
}

return msg
}

func ParseName(id string) (ID, error) {
idParts := strings.Split(id, "-")

if len(idParts) > 2 {
return ID{}, ParseError{ToParse: id, Msg: fmt.Sprintf("too many dashes (%d)", len(idParts)-1)}
}

name := idParts[0]
version := idParts[1]

versionParts := strings.Split(version, ".")

if len(versionParts) > 2 {
return ID{}, ParseError{ToParse: id, Msg: fmt.Sprintf("too many dots in the version (%d)", len(versionParts)-1)}
}

majorVersion, err := strconv.Atoi(versionParts[0])
if err != nil {
return ID{}, ParseError{ToParse: id, Msg: "parsing major version failed", Inner: err}
}

var minorVersion int

if len(versionParts) > 1 {
minorVersion, err = strconv.Atoi(versionParts[1])
if err != nil {
return ID{}, ParseError{ToParse: id, Msg: "parsing minor version failed", Inner: err}
}
}

return ID{
Name: name,
MajorVersion: majorVersion,
MinorVersion: minorVersion,
}, nil
}
16 changes: 16 additions & 0 deletions pkg/distro/rhel7/distro.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,19 @@ func newDistro(distroName string) distro.Distro {

return &rd
}

func NewFromID(idStr string) distro.Distro {
id, err := distro.ParseName(idStr)
if err != nil {
return nil
}
if id.Name != "rhel" {
return nil
}

if id.MajorVersion != 7 {
return nil
}

return newDistro("rhel-7")
}
27 changes: 27 additions & 0 deletions pkg/distro/rhel8/distro.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,3 +512,30 @@ func newDistro(name string, minor int) *distribution {
rd.addArches(x86_64, aarch64, ppc64le)
return &rd
}

func NewFromID(idStr string) distro.Distro {
id, err := distro.ParseName(idStr)
if err != nil {
return nil
}

// compat for rhel-84 and friends
if id.MajorVersion >= 80 && id.MajorVersion < 90 {
id.MinorVersion = id.MajorVersion % 10
id.MajorVersion = 8
}

if id.MajorVersion != 8 {
return nil
}

if id.Name == "centos" {
return NewCentos()
}

if id.Name != "rhel" {
return nil
}

return newDistro("rhel", id.MinorVersion)
}
27 changes: 27 additions & 0 deletions pkg/distro/rhel9/distro.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,3 +487,30 @@ func newDistro(name string, minor int) *distribution {
rd.addArches(x86_64, aarch64, ppc64le, s390x)
return &rd
}

func NewFromID(idStr string) distro.Distro {
id, err := distro.ParseName(idStr)
if err != nil {
return nil
}

// compat for rhel-94 and friends
if id.MajorVersion >= 90 && id.MajorVersion < 100 {
id.MinorVersion = id.MajorVersion % 10
id.MajorVersion = 9
}

if id.MajorVersion != 9 {
return nil
}

if id.Name == "centos" {
return NewCentOS9()
}

if id.Name != "rhel" {
return nil
}

return newDistro("rhel", id.MinorVersion)
}
Loading