From c4e5ab5aca41c45956bff2f368d0ea3f96589d73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Hozza?= Date: Fri, 19 Jan 2024 21:05:46 +0100 Subject: [PATCH] Drop internal/common/distro.go in favor of osbuild/images MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Drop `common.GetHostDistroName()` implementation and use `distro.GetHostDistroName()` from the osbuild/images instead. Signed-off-by: Tomáš Hozza --- cmd/osbuild-worker/jobimpl-osbuild.go | 3 +- internal/boot/context-managers.go | 3 +- internal/common/distro.go | 53 -------------------------- internal/common/distro_test.go | 54 --------------------------- internal/weldr/api.go | 2 +- 5 files changed, 5 insertions(+), 110 deletions(-) delete mode 100644 internal/common/distro.go delete mode 100644 internal/common/distro_test.go diff --git a/cmd/osbuild-worker/jobimpl-osbuild.go b/cmd/osbuild-worker/jobimpl-osbuild.go index cc0169681..2255564c4 100644 --- a/cmd/osbuild-worker/jobimpl-osbuild.go +++ b/cmd/osbuild-worker/jobimpl-osbuild.go @@ -16,6 +16,7 @@ import ( "strings" "github.com/osbuild/images/pkg/container" + "github.com/osbuild/images/pkg/distro" "github.com/osbuild/images/pkg/osbuild" "github.com/osbuild/osbuild-composer/internal/upload/oci" @@ -351,7 +352,7 @@ func (impl *OSBuildJobImpl) Run(job worker.Job) error { Arch: common.CurrentArch(), } - hostOS, _, _, err := common.GetHostDistroName() + hostOS, err := distro.GetHostDistroName() if err != nil { logWithId.Warnf("Failed to get host distro name: %v", err) hostOS = "linux" diff --git a/internal/boot/context-managers.go b/internal/boot/context-managers.go index a68f4e44e..298a25ec2 100644 --- a/internal/boot/context-managers.go +++ b/internal/boot/context-managers.go @@ -14,6 +14,7 @@ import ( "time" "github.com/osbuild/images/pkg/arch" + "github.com/osbuild/images/pkg/distro" "github.com/osbuild/osbuild-composer/cmd/osbuild-image-tests/constants" "github.com/osbuild/osbuild-composer/internal/common" ) @@ -114,7 +115,7 @@ func WithBootedQemuImage(image string, ns NetNS, f func() error) error { var qemuCmd *exec.Cmd if common.CurrentArch() == "x86_64" { - hostDistroName, _, _, err := common.GetHostDistroName() + hostDistroName, err := distro.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 deleted file mode 100644 index ade88be36..000000000 --- a/internal/common/distro.go +++ /dev/null @@ -1,53 +0,0 @@ -package common - -import ( - "bufio" - "errors" - "io" - "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" - - version := strings.Split(osrelease["VERSION_ID"], ".") - name := osrelease["ID"] + "-" + strings.Join(version, "") - - // TODO: We should probably index these things by the full CPE - beta := strings.Contains(osrelease["CPE_NAME"], "beta") - return name, beta, isStream, 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]) - // drop all surrounding whitespace and double-quotes - value := strings.Trim(strings.TrimSpace(parts[1]), "\"") - osrelease[key] = value - } - - return osrelease, nil -} diff --git a/internal/common/distro_test.go b/internal/common/distro_test.go deleted file mode 100644 index 376874052..000000000 --- a/internal/common/distro_test.go +++ /dev/null @@ -1,54 +0,0 @@ -package common - -import ( - "reflect" - "strings" - "testing" -) - -func TestOSRelease(t *testing.T) { - var cases = []struct { - Input string - OSRelease map[string]string - }{ - { - ``, - map[string]string{}, - }, - { - `NAME=Fedora -VERSION="30 (Workstation Edition)" -ID=fedora -VERSION_ID=30 -VERSION_CODENAME="" -PLATFORM_ID="platform:f30" -PRETTY_NAME="Fedora 30 (Workstation Edition)" -VARIANT="Workstation Edition" -VARIANT_ID=workstation`, - map[string]string{ - "NAME": "Fedora", - "VERSION": "30 (Workstation Edition)", - "ID": "fedora", - "VERSION_ID": "30", - "VERSION_CODENAME": "", - "PLATFORM_ID": "platform:f30", - "PRETTY_NAME": "Fedora 30 (Workstation Edition)", - "VARIANT": "Workstation Edition", - "VARIANT_ID": "workstation", - }, - }, - } - - for i, c := range cases { - r := strings.NewReader(c.Input) - - osrelease, err := readOSRelease(r) - if err != nil { - t.Fatalf("%d: readOSRelease: %v", i, err) - } - - if !reflect.DeepEqual(osrelease, c.OSRelease) { - t.Fatalf("%d: readOSRelease returned unexpected result: %#v", i, osrelease) - } - } -} diff --git a/internal/weldr/api.go b/internal/weldr/api.go index 02a7c2caf..3093ff22d 100644 --- a/internal/weldr/api.go +++ b/internal/weldr/api.go @@ -161,7 +161,7 @@ func New(repoPaths []string, stateDir string, solver *dnfjson.BaseSolver, df *di logger = log.New(os.Stdout, "", 0) } - hostDistroName, _, _, err := common.GetHostDistroName() + hostDistroName, err := distro.GetHostDistroName() if err != nil { return nil, fmt.Errorf("failed to read host distro information") }