From c7e5e3c9c2a34e48470caca15aaaa17dbea5aef6 Mon Sep 17 00:00:00 2001 From: Tomas Hozza Date: Thu, 2 Jun 2022 10:28:50 +0200 Subject: [PATCH] Move `GetRedHatRelease()` and `GetHostDistroName()` to `common` package The `distro` package is now used for distro definitions supported by osbuild-composer, not for introspecting the Host system. Move `GetRedHatRelease()` and `GetHostDistroName()` functions to the `common` package. --- cmd/osbuild-worker/jobimpl-osbuild-koji.go | 3 +- internal/boot/context-managers.go | 2 +- internal/common/distro.go | 79 +++++++++++++++++++ .../distro_test.go} | 2 +- internal/distro/distro.go | 74 ----------------- internal/distroregistry/distroregistry.go | 3 +- internal/weldr/api.go | 2 +- 7 files changed, 85 insertions(+), 80 deletions(-) create mode 100644 internal/common/distro.go rename internal/{distro/osrelease_test.go => common/distro_test.go} (98%) diff --git a/cmd/osbuild-worker/jobimpl-osbuild-koji.go b/cmd/osbuild-worker/jobimpl-osbuild-koji.go index 0a656ac4e..2bb4d0875 100644 --- a/cmd/osbuild-worker/jobimpl-osbuild-koji.go +++ b/cmd/osbuild-worker/jobimpl-osbuild-koji.go @@ -10,7 +10,6 @@ import ( "github.com/sirupsen/logrus" "github.com/osbuild/osbuild-composer/internal/common" - "github.com/osbuild/osbuild-composer/internal/distro" "github.com/osbuild/osbuild-composer/internal/upload/koji" "github.com/osbuild/osbuild-composer/internal/worker" "github.com/osbuild/osbuild-composer/internal/worker/clienterrors" @@ -101,7 +100,7 @@ func (impl *OSBuildKojiJobImpl) Run(job worker.Job) error { } result.Arch = common.CurrentArch() - result.HostOS, err = distro.GetRedHatRelease() + result.HostOS, err = common.GetRedHatRelease() if err != nil { return err } diff --git a/internal/boot/context-managers.go b/internal/boot/context-managers.go index 88b2a1206..29444360a 100644 --- a/internal/boot/context-managers.go +++ b/internal/boot/context-managers.go @@ -115,7 +115,7 @@ func WithBootedQemuImage(image string, ns NetNS, f func() error) error { var qemuCmd *exec.Cmd if common.CurrentArch() == "x86_64" { - hostDistroName, _, _, err := distro.GetHostDistroName() + hostDistroName, _, _, err := common.GetHostDistroName() if err != nil { return fmt.Errorf("cannot determing the current distro: %v", err) } diff --git a/internal/common/distro.go b/internal/common/distro.go new file mode 100644 index 000000000..bd3c66bfb --- /dev/null +++ b/internal/common/distro.go @@ -0,0 +1,79 @@ +package common + +import ( + "bufio" + "errors" + "fmt" + "io" + "io/ioutil" + "os" + "strings" +) + +func GetHostDistroName() (string, bool, bool, error) { + f, err := os.Open("/etc/os-release") + if err != nil { + return "", false, false, err + } + defer f.Close() + osrelease, err := readOSRelease(f) + if err != nil { + return "", false, false, err + } + + isStream := osrelease["NAME"] == "CentOS Stream" + + // NOTE: We only consider major releases up until rhel 8.4 + version := strings.Split(osrelease["VERSION_ID"], ".") + name := osrelease["ID"] + "-" + version[0] + if osrelease["ID"] == "rhel" && ((version[0] == "8" && version[1] >= "4") || version[0] == "9") { + name = name + version[1] + } + + // TODO: We should probably index these things by the full CPE + beta := strings.Contains(osrelease["CPE_NAME"], "beta") + return name, beta, isStream, nil +} + +// GetRedHatRelease returns the content of /etc/redhat-release +// without the trailing new-line. +func GetRedHatRelease() (string, error) { + raw, err := ioutil.ReadFile("/etc/redhat-release") + if err != nil { + return "", fmt.Errorf("cannot read /etc/redhat-release: %v", err) + } + + //Remove the trailing new-line. + redHatRelease := strings.TrimSpace(string(raw)) + + return redHatRelease, nil +} + +func readOSRelease(r io.Reader) (map[string]string, error) { + osrelease := make(map[string]string) + scanner := bufio.NewScanner(r) + for scanner.Scan() { + line := strings.TrimSpace(scanner.Text()) + if len(line) == 0 { + continue + } + + parts := strings.SplitN(line, "=", 2) + if len(parts) != 2 { + return nil, errors.New("readOSRelease: invalid input") + } + + key := strings.TrimSpace(parts[0]) + value := strings.TrimSpace(parts[1]) + if value[0] == '"' { + if len(value) < 2 || value[len(value)-1] != '"' { + return nil, errors.New("readOSRelease: invalid input") + } + value = value[1 : len(value)-1] + } + + osrelease[key] = value + } + + return osrelease, nil +} diff --git a/internal/distro/osrelease_test.go b/internal/common/distro_test.go similarity index 98% rename from internal/distro/osrelease_test.go rename to internal/common/distro_test.go index 490040bf3..376874052 100644 --- a/internal/distro/osrelease_test.go +++ b/internal/common/distro_test.go @@ -1,4 +1,4 @@ -package distro +package common import ( "reflect" diff --git a/internal/distro/distro.go b/internal/distro/distro.go index 207b7dd83..f931f60b6 100644 --- a/internal/distro/distro.go +++ b/internal/distro/distro.go @@ -1,14 +1,8 @@ package distro import ( - "bufio" "encoding/json" - "errors" "fmt" - "io" - "io/ioutil" - "os" - "strings" "github.com/osbuild/osbuild-composer/internal/blueprint" "github.com/osbuild/osbuild-composer/internal/disk" @@ -186,74 +180,6 @@ func (m Manifest) Version() (string, error) { } } -func GetHostDistroName() (string, bool, bool, error) { - f, err := os.Open("/etc/os-release") - if err != nil { - return "", false, false, err - } - defer f.Close() - osrelease, err := readOSRelease(f) - if err != nil { - return "", false, false, err - } - - isStream := osrelease["NAME"] == "CentOS Stream" - - // NOTE: We only consider major releases up until rhel 8.4 - version := strings.Split(osrelease["VERSION_ID"], ".") - name := osrelease["ID"] + "-" + version[0] - if osrelease["ID"] == "rhel" && ((version[0] == "8" && version[1] >= "4") || version[0] == "9") { - name = name + version[1] - } - - // TODO: We should probably index these things by the full CPE - beta := strings.Contains(osrelease["CPE_NAME"], "beta") - return name, beta, isStream, nil -} - -// GetRedHatRelease returns the content of /etc/redhat-release -// without the trailing new-line. -func GetRedHatRelease() (string, error) { - raw, err := ioutil.ReadFile("/etc/redhat-release") - if err != nil { - return "", fmt.Errorf("cannot read /etc/redhat-release: %v", err) - } - - //Remove the trailing new-line. - redHatRelease := strings.TrimSpace(string(raw)) - - return redHatRelease, nil -} - -func readOSRelease(r io.Reader) (map[string]string, error) { - osrelease := make(map[string]string) - scanner := bufio.NewScanner(r) - for scanner.Scan() { - line := strings.TrimSpace(scanner.Text()) - if len(line) == 0 { - continue - } - - parts := strings.SplitN(line, "=", 2) - if len(parts) != 2 { - return nil, errors.New("readOSRelease: invalid input") - } - - key := strings.TrimSpace(parts[0]) - value := strings.TrimSpace(parts[1]) - if value[0] == '"' { - if len(value) < 2 || value[len(value)-1] != '"' { - return nil, errors.New("readOSRelease: invalid input") - } - value = value[1 : len(value)-1] - } - - osrelease[key] = value - } - - return osrelease, nil -} - // Fallbacks: When a new method is added to an interface to provide to provide // information that isn't available for older implementations, the older // methods should return a fallback/default value by calling the appropriate diff --git a/internal/distroregistry/distroregistry.go b/internal/distroregistry/distroregistry.go index 6ddabc3e1..2b5be06ee 100644 --- a/internal/distroregistry/distroregistry.go +++ b/internal/distroregistry/distroregistry.go @@ -5,6 +5,7 @@ import ( "sort" "strings" + "github.com/osbuild/osbuild-composer/internal/common" "github.com/osbuild/osbuild-composer/internal/distro" "github.com/osbuild/osbuild-composer/internal/distro/fedora" "github.com/osbuild/osbuild-composer/internal/distro/rhel8" @@ -69,7 +70,7 @@ func NewDefault() *Registry { // If there was an error, then the hostDistroName will be an empty string // and as a result, the hostDistro will have a nil value when calling New(). // Getting the host distro later using FromHost() will return nil as well. - hostDistroName, hostDistroIsBeta, hostDistroIsStream, _ := distro.GetHostDistroName() + hostDistroName, hostDistroIsBeta, hostDistroIsStream, _ := common.GetHostDistroName() for _, supportedDistro := range supportedDistros { distro := supportedDistro.defaultDistro() diff --git a/internal/weldr/api.go b/internal/weldr/api.go index 8be0cd220..78cc38ab2 100644 --- a/internal/weldr/api.go +++ b/internal/weldr/api.go @@ -151,7 +151,7 @@ func New(repoPaths []string, stateDir string, solver *dnfjson.BaseSolver, dr *di logger = log.New(os.Stdout, "", 0) } - hostDistroName, _, _, err := distro.GetHostDistroName() + hostDistroName, _, _, err := common.GetHostDistroName() if err != nil { return nil, fmt.Errorf("failed to read host distro information") }