From 91e581462b9581967137b3cade4adc003092740c Mon Sep 17 00:00:00 2001 From: Achilleas Koutsou Date: Wed, 7 Sep 2022 13:43:15 +0200 Subject: [PATCH] common: move VersionLessThan() util function Move VersionLessThan() utility function to the common package to be reused. --- internal/common/distro.go | 30 ++++++++++++++++++++++ internal/distro/rhel8/distro.go | 36 +++------------------------ internal/distro/rhel8/package_sets.go | 3 ++- 3 files changed, 35 insertions(+), 34 deletions(-) diff --git a/internal/common/distro.go b/internal/common/distro.go index ade88be36..9f8500719 100644 --- a/internal/common/distro.go +++ b/internal/common/distro.go @@ -51,3 +51,33 @@ func readOSRelease(r io.Reader) (map[string]string, error) { return osrelease, nil } + +// Returns true if the version represented by the first argument is +// semantically older than the second. +// Meant to be used for comparing distro versions for differences between minor +// releases. +// Evaluates to false if a and b are equal. +// Assumes any missing components are 0, so 8 < 8.1. +func VersionLessThan(a, b string) bool { + aParts := strings.Split(a, ".") + bParts := strings.Split(b, ".") + + // pad shortest argument with zeroes + for len(aParts) < len(bParts) { + aParts = append(aParts, "0") + } + for len(bParts) < len(aParts) { + bParts = append(bParts, "0") + } + + for idx := 0; idx < len(aParts); idx++ { + if aParts[idx] < bParts[idx] { + return true + } else if aParts[idx] > bParts[idx] { + return false + } + } + + // equal + return false +} diff --git a/internal/distro/rhel8/distro.go b/internal/distro/rhel8/distro.go index c4a86a722..7140a0f0a 100644 --- a/internal/distro/rhel8/distro.go +++ b/internal/distro/rhel8/distro.go @@ -19,36 +19,6 @@ import ( "github.com/osbuild/osbuild-composer/internal/rpmmd" ) -// Returns true if the version represented by the first argument is -// semantically older than the second. -// Meant to be used for comparing RHEL versions for differences between minor -// releases. -// Evaluates to false if a and b are equal. -// Assumes any missing components are 0, so 8 < 8.1. -func versionLessThan(a, b string) bool { - aParts := strings.Split(a, ".") - bParts := strings.Split(b, ".") - - // pad shortest argument with zeroes - for len(aParts) < len(bParts) { - aParts = append(aParts, "0") - } - for len(bParts) < len(aParts) { - bParts = append(bParts, "0") - } - - for idx := 0; idx < len(aParts); idx++ { - if aParts[idx] < bParts[idx] { - return true - } else if aParts[idx] > bParts[idx] { - return false - } - } - - // equal - return false -} - const ( // package set names @@ -668,7 +638,7 @@ func (t *imageType) checkOptions(customizations *blueprint.Customizations, optio if osc := customizations.GetOpenSCAP(); osc != nil { // only add support for RHEL 8.7 and above. centos not supported. - if !t.arch.distro.isRHEL() || versionLessThan(t.arch.distro.osVersion, "8.7") { + if !t.arch.distro.isRHEL() || common.VersionLessThan(t.arch.distro.osVersion, "8.7") { return fmt.Errorf(fmt.Sprintf("OpenSCAP unsupported os version: %s", t.arch.distro.osVersion)) } supported := oscap.IsProfileAllowed(osc.ProfileID, oscapProfileAllowList) @@ -788,7 +758,7 @@ func newDistro(distroName string) distro.Distro { } - if !(rd.isRHEL() && versionLessThan(rd.osVersion, "8.6")) { + if !(rd.isRHEL() && common.VersionLessThan(rd.osVersion, "8.6")) { // enable fdo-client only on RHEL 8.6+ and CS8 // TODO(runcom): move fdo-client-linuxapp.service to presets? @@ -1954,7 +1924,7 @@ func newDistro(distroName string) distro.Distro { ) if rd.isRHEL() { - if !versionLessThan(rd.osVersion, "8.6") { + if !common.VersionLessThan(rd.osVersion, "8.6") { // image types only available on 8.6 and later on RHEL // These edge image types require FDO which aren't available on older versions x86_64.addImageTypes(edgeSimplifiedInstallerImgType, edgeRawImgType) diff --git a/internal/distro/rhel8/package_sets.go b/internal/distro/rhel8/package_sets.go index 9517a1954..de1ef85c8 100644 --- a/internal/distro/rhel8/package_sets.go +++ b/internal/distro/rhel8/package_sets.go @@ -5,6 +5,7 @@ package rhel8 import ( "fmt" + "github.com/osbuild/osbuild-composer/internal/common" "github.com/osbuild/osbuild-composer/internal/distro" "github.com/osbuild/osbuild-composer/internal/rpmmd" ) @@ -828,7 +829,7 @@ func edgeCommitPackageSet(t *imageType) rpmmd.PackageSet { ps = ps.Append(aarch64EdgeCommitPackageSet(t)) } - if t.arch.distro.isRHEL() && versionLessThan(t.arch.distro.osVersion, "8.6") { + if t.arch.distro.isRHEL() && common.VersionLessThan(t.arch.distro.osVersion, "8.6") { ps = ps.Append(rpmmd.PackageSet{ Include: []string{ "greenboot-grub2",