From 24b503b60bb9d4d390c9a0a6e258997de858f7af Mon Sep 17 00:00:00 2001 From: Christoph Hartmann Date: Wed, 2 Aug 2023 10:05:33 +0200 Subject: [PATCH] =?UTF-8?q?=E2=AD=90=EF=B8=8F=20add=20suse=2011=20platform?= =?UTF-8?q?=20detection=20(#1418)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This adds platform detection of suse systems without /etc/os-release --- motor/platform/detector/detector_test.go | 39 ++++++++++ .../platform/detector/parser_linux_version.go | 30 ++++++++ .../detector/parser_linux_version_test.go | 20 ++++++ motor/platform/detector/platform_specs.go | 72 +++++++++++++++---- .../detector/testdata/detect-opensuse-11.toml | 15 ++++ .../testdata/detect-suse-sles-10.toml | 15 ++++ .../testdata/detect-suse-sles-11.toml | 15 ++++ 7 files changed, 192 insertions(+), 14 deletions(-) create mode 100644 motor/platform/detector/testdata/detect-opensuse-11.toml create mode 100644 motor/platform/detector/testdata/detect-suse-sles-10.toml create mode 100644 motor/platform/detector/testdata/detect-suse-sles-11.toml diff --git a/motor/platform/detector/detector_test.go b/motor/platform/detector/detector_test.go index 83d0149648..284d8a4e9a 100644 --- a/motor/platform/detector/detector_test.go +++ b/motor/platform/detector/detector_test.go @@ -538,6 +538,45 @@ func TestOpenSuseTumbleweedDetector(t *testing.T) { assert.Equal(t, []string{"suse", "linux", "unix", "os"}, di.Family) } +func TestSuse10Detector(t *testing.T) { + detector, err := newDetector("./testdata/detect-suse-sles-10.toml") + assert.Nil(t, err, "was able to create the provider") + di, err := detector.Platform() + require.NoError(t, err) + + assert.Equal(t, "sles", di.Name, "os name should be identified") + assert.Equal(t, "SUSE Linux Enterprise Server 10", di.Title, "os title should be identified") + assert.Equal(t, "10.4", di.Version, "os version should be identified") + assert.Equal(t, "x86_64", di.Arch, "os arch should be identified") + assert.Equal(t, []string{"suse", "linux", "unix", "os"}, di.Family) +} + +func TestSuse11Detector(t *testing.T) { + detector, err := newDetector("./testdata/detect-suse-sles-11.toml") + assert.Nil(t, err, "was able to create the provider") + di, err := detector.Platform() + require.NoError(t, err) + + assert.Equal(t, "sles", di.Name, "os name should be identified") + assert.Equal(t, "SUSE Linux Enterprise Server 11", di.Title, "os title should be identified") + assert.Equal(t, "11.3", di.Version, "os version should be identified") + assert.Equal(t, "x86_64", di.Arch, "os arch should be identified") + assert.Equal(t, []string{"suse", "linux", "unix", "os"}, di.Family) +} + +func TestOpenSuse11Detector(t *testing.T) { + detector, err := newDetector("./testdata/detect-opensuse-11.toml") + assert.Nil(t, err, "was able to create the provider") + di, err := detector.Platform() + require.NoError(t, err) + + assert.Equal(t, "opensuse", di.Name, "os name should be identified") + assert.Equal(t, "openSUSE 11.4", di.Title, "os title should be identified") + assert.Equal(t, "11.4", di.Version, "os version should be identified") + assert.Equal(t, "x86_64", di.Arch, "os arch should be identified") + assert.Equal(t, []string{"suse", "linux", "unix", "os"}, di.Family) +} + func TestSuse12Detector(t *testing.T) { detector, err := newDetector("./testdata/detect-suse-sles-12.toml") assert.Nil(t, err, "was able to create the provider") diff --git a/motor/platform/detector/parser_linux_version.go b/motor/platform/detector/parser_linux_version.go index e1fbdfbe08..0922c12cdd 100644 --- a/motor/platform/detector/parser_linux_version.go +++ b/motor/platform/detector/parser_linux_version.go @@ -3,6 +3,7 @@ package detector import ( "errors" "regexp" + "strings" ) var ( @@ -46,3 +47,32 @@ func ParseRhelVersion(releaseDescription string) (string, string, error) { return name, release, nil } + +var ( + suseTitle = regexp.MustCompile(`^([\w\d\s\.]+)\(([\w\d\s\.]+)\)`) + suseVersion = regexp.MustCompile(`VERSION\s*=\s*"?([\d\.]+)"?`) + susePatchVersion = regexp.MustCompile(`VERSION = (\d+)\nPATCHLEVEL = (\d+)`) +) + +func ParseSuseTitle(v string) (string, string) { + m := suseTitle.FindStringSubmatch(v) + if len(m) == 0 { + return "", "" + } + + return strings.TrimSpace(m[1]), strings.TrimSpace(m[2]) +} + +func ParseSuseVersion(v string) string { + m := susePatchVersion.FindStringSubmatch(v) + if len(m) == 3 { + return m[1] + "." + m[2] + } + + m = suseVersion.FindStringSubmatch(v) + if len(m) == 0 { + return "" + } + + return m[1] +} diff --git a/motor/platform/detector/parser_linux_version_test.go b/motor/platform/detector/parser_linux_version_test.go index 69342b1fed..a3fafd2fc0 100644 --- a/motor/platform/detector/parser_linux_version_test.go +++ b/motor/platform/detector/parser_linux_version_test.go @@ -98,3 +98,23 @@ func TestRedhatRelease(t *testing.T) { assert.Equal(t, "Oracle Linux Server", name, "parse os name") assert.Equal(t, "7.4", release, "parse release version") } + +func TestSuseParser(t *testing.T) { + data := `openSUSE 11.4 (x86_64) +VERSION = 11.4 +CODENAME = Celadon` + title, arch := detector.ParseSuseTitle(data) + assert.Equal(t, "openSUSE 11.4", title) + assert.Equal(t, "x86_64", arch) + v := detector.ParseSuseVersion(data) + assert.Equal(t, "11.4", v) + + data = `SUSE Linux Enterprise Server 12 (x86_64) +VERSION = 12 +PATCHLEVEL = 0` + title, arch = detector.ParseSuseTitle(data) + assert.Equal(t, "SUSE Linux Enterprise Server 12", title) + assert.Equal(t, "x86_64", arch) + v = detector.ParseSuseVersion(data) + assert.Equal(t, "12.0", v) +} diff --git a/motor/platform/detector/platform_specs.go b/motor/platform/detector/platform_specs.go index 8ca93e82dd..fa2208a1bf 100644 --- a/motor/platform/detector/platform_specs.go +++ b/motor/platform/detector/platform_specs.go @@ -3,7 +3,6 @@ package detector import ( "bytes" "io" - "io/ioutil" "regexp" "strconv" "strings" @@ -29,7 +28,7 @@ var macOS = &PlatformResolver{ } defer f.Close() - c, err := ioutil.ReadAll(f) + c, err := io.ReadAll(f) if err != nil || len(c) == 0 { return false, nil } @@ -85,7 +84,7 @@ var alpine = &PlatformResolver{ } defer f.Close() - c, err := ioutil.ReadAll(f) + c, err := io.ReadAll(f) if err != nil || len(c) == 0 { return false, nil } @@ -129,7 +128,7 @@ var debian = &PlatformResolver{ } defer f.Close() - c, err := ioutil.ReadAll(f) + c, err := io.ReadAll(f) if err != nil || len(c) == 0 { return false, nil } @@ -229,7 +228,7 @@ var rhel = &PlatformResolver{ } defer f.Close() - c, err := ioutil.ReadAll(f) + c, err := io.ReadAll(f) if err != nil || len(c) == 0 { return false, nil } @@ -315,7 +314,7 @@ var fedora = &PlatformResolver{ } defer f.Close() - c, err := ioutil.ReadAll(f) + c, err := io.ReadAll(f) if err != nil || len(c) == 0 { return false, nil } @@ -345,7 +344,7 @@ var oracle = &PlatformResolver{ } defer f.Close() - c, err := ioutil.ReadAll(f) + c, err := io.ReadAll(f) if err != nil || len(c) == 0 { return false, nil } @@ -408,6 +407,28 @@ var opensuse = &PlatformResolver{ return true, nil } + // fallback to /etc/etc/SuSE-release file + f, err := p.FS().Open("/etc/SuSE-release") + if err != nil { + return false, nil + } + defer f.Close() + + c, err := io.ReadAll(f) + if err != nil || len(c) == 0 { + log.Debug().Err(err) + return false, nil + } + + if strings.Contains(strings.ToLower(string(c)), "opensuse") { + pf.Name = "opensuse" + title, arch := ParseSuseTitle(string(c)) + pf.Title = title + pf.Arch = arch + pf.Version = ParseSuseVersion(string(c)) + return true, nil + } + return false, nil }, } @@ -419,6 +440,29 @@ var sles = &PlatformResolver{ if pf.Name == "sles" { return true, nil } + + // fallback to /etc/etc/SuSE-release file + f, err := p.FS().Open("/etc/SuSE-release") + if err != nil { + return false, nil + } + defer f.Close() + + c, err := io.ReadAll(f) + if err != nil || len(c) == 0 { + log.Debug().Err(err) + return false, nil + } + + if strings.Contains(strings.ToLower(string(c)), "suse linux enterprise server") { + pf.Name = "sles" + title, arch := ParseSuseTitle(string(c)) + pf.Title = title + pf.Arch = arch + pf.Version = ParseSuseVersion(string(c)) + return true, nil + } + return false, nil }, } @@ -444,7 +488,7 @@ var gentoo = &PlatformResolver{ } defer f.Close() - c, err := ioutil.ReadAll(f) + c, err := io.ReadAll(f) if err != nil || len(c) == 0 { log.Debug().Err(err) return false, nil @@ -495,7 +539,7 @@ var busybox = &PlatformResolver{ } defer f.Close() - content, err := ioutil.ReadAll(f) + content, err := io.ReadAll(f) if err != nil { return false, err } @@ -549,7 +593,7 @@ var openwrt = &PlatformResolver{ } defer f.Close() - content, err := ioutil.ReadAll(f) + content, err := io.ReadAll(f) if err != nil { return false, err } @@ -807,7 +851,7 @@ var redhatFamily = &PlatformResolver{ } defer f.Close() - c, err := ioutil.ReadAll(f) + c, err := io.ReadAll(f) if err != nil || len(c) == 0 { log.Debug().Err(err) return false, nil @@ -866,7 +910,7 @@ var archFamily = &PlatformResolver{ } defer f.Close() - c, err := ioutil.ReadAll(f) + c, err := io.ReadAll(f) if err != nil { return false, nil } @@ -1038,7 +1082,7 @@ var solaris = &PlatformResolver{ } defer f.Close() - c, err := ioutil.ReadAll(f) + c, err := io.ReadAll(f) if err != nil { return false, nil } @@ -1065,7 +1109,7 @@ var esxi = &PlatformResolver{ log.Debug().Err(err).Msg("could not run command") return false, nil } - vmware_info, err := ioutil.ReadAll(cmd.Stdout) + vmware_info, err := io.ReadAll(cmd.Stdout) if err != nil { log.Debug().Err(err).Msg("could not run command") return false, err diff --git a/motor/platform/detector/testdata/detect-opensuse-11.toml b/motor/platform/detector/testdata/detect-opensuse-11.toml new file mode 100644 index 0000000000..81ab3cd3cd --- /dev/null +++ b/motor/platform/detector/testdata/detect-opensuse-11.toml @@ -0,0 +1,15 @@ +[commands."uname -s"] +stdout = "Linux" + +[commands."uname -m"] +stdout = "x86_64" + +[commands."uname -r"] +stdout = "2.6.37.6-24-desktop" + +[files."/etc/SuSE-release"] +content = """ +openSUSE 11.4 (x86_64) +VERSION = 11.4 +CODENAME = Celadon +""" \ No newline at end of file diff --git a/motor/platform/detector/testdata/detect-suse-sles-10.toml b/motor/platform/detector/testdata/detect-suse-sles-10.toml new file mode 100644 index 0000000000..b3bfe02935 --- /dev/null +++ b/motor/platform/detector/testdata/detect-suse-sles-10.toml @@ -0,0 +1,15 @@ +[commands."uname -s"] +stdout = "Linux" + +[commands."uname -m"] +stdout = "x86_64" + +[commands."uname -r"] +stdout = "2.6.37.6-24-desktop" + +[files."/etc/SuSE-release"] +content = """ +SUSE Linux Enterprise Server 10 (x86_64) +VERSION = 10 +PATCHLEVEL = 4 +""" \ No newline at end of file diff --git a/motor/platform/detector/testdata/detect-suse-sles-11.toml b/motor/platform/detector/testdata/detect-suse-sles-11.toml new file mode 100644 index 0000000000..ab9850feeb --- /dev/null +++ b/motor/platform/detector/testdata/detect-suse-sles-11.toml @@ -0,0 +1,15 @@ +[commands."uname -s"] +stdout = "Linux" + +[commands."uname -m"] +stdout = "x86_64" + +[commands."uname -r"] +stdout = "2.6.37.6-24-desktop" + +[files."/etc/SuSE-release"] +content = """ +SUSE Linux Enterprise Server 11 (x86_64) +VERSION = 11 +PATCHLEVEL = 3 +""" \ No newline at end of file