From 3c4583080df926f8d4dfa08fd5950d5b6fa95d6e Mon Sep 17 00:00:00 2001 From: sidis-zafran Date: Wed, 13 Nov 2024 10:53:24 +0200 Subject: [PATCH 1/4] Enhance version regex to support both standard major.minor.patch formats and SUSE-specific SP notation for minor versions. --- providers/linux/os.go | 2 +- providers/linux/os_test.go | 32 +++++++++++++++++++ .../linux/testdata/sled15sp5/etc/os-release | 9 ++++++ .../linux/testdata/sles15sp5/etc/os-release | 9 ++++++ 4 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 providers/linux/testdata/sled15sp5/etc/os-release create mode 100644 providers/linux/testdata/sles15sp5/etc/os-release diff --git a/providers/linux/os.go b/providers/linux/os.go index ece0f37..3516157 100644 --- a/providers/linux/os.go +++ b/providers/linux/os.go @@ -35,7 +35,7 @@ const ( osRelease = "/etc/os-release" lsbRelease = "/etc/lsb-release" distribRelease = "/etc/*-release" - versionGrok = `(?P(?P[0-9]+)\.?(?P[0-9]+)?\.?(?P\w+)?)(?: \((?P[-\w ]+)\))?` + versionGrok = `(?P(?P[0-9]+)(?:[.-]?(?:SP)?(?P[0-9]+))?(?:[.-](?P[0-9]+|\w+))?)(?: \((?P[-\w ]+)\))?` ) var ( diff --git a/providers/linux/os_test.go b/providers/linux/os_test.go index 7f3f677..e780886 100644 --- a/providers/linux/os_test.go +++ b/providers/linux/os_test.go @@ -418,4 +418,36 @@ func TestOperatingSystem(t *testing.T) { }, *os) t.Logf("%#v", os) }) + t.Run("sles15sp5", func(t *testing.T) { + os, err := getOSInfo("testdata/sles15sp5") + if err != nil { + t.Fatal(err) + } + assert.Equal(t, types.OSInfo{ + Type: "linux", + Family: "suse", + Platform: "sles", + Name: "SLES", + Version: "15-SP5", + Major: 15, + Minor: 5, + }, *os) + t.Logf("%#v", os) + }) + t.Run("sled15sp5", func(t *testing.T) { + os, err := getOSInfo("testdata/sled15sp5") + if err != nil { + t.Fatal(err) + } + assert.Equal(t, types.OSInfo{ + Type: "linux", + Family: "suse", + Platform: "sled", + Name: "SLED", + Version: "15-SP5", + Major: 15, + Minor: 5, + }, *os) + t.Logf("%#v", os) + }) } diff --git a/providers/linux/testdata/sled15sp5/etc/os-release b/providers/linux/testdata/sled15sp5/etc/os-release new file mode 100644 index 0000000..84cea18 --- /dev/null +++ b/providers/linux/testdata/sled15sp5/etc/os-release @@ -0,0 +1,9 @@ +NAME="SLED" +VERSION="15-SP5" +VERSION_ID="15.5" +PRETTY_NAME="SUSE Linux Enterprise Desktop 15 SP5" +ID="sled" +ID_LIKE="suse" +ANSI_COLOR="0;32" +CPE_NAME="cpe:/o:suse:sled:15:sp5" +DOCUMENTATION_URL="https://documentation.suse.com/" diff --git a/providers/linux/testdata/sles15sp5/etc/os-release b/providers/linux/testdata/sles15sp5/etc/os-release new file mode 100644 index 0000000..1d867e3 --- /dev/null +++ b/providers/linux/testdata/sles15sp5/etc/os-release @@ -0,0 +1,9 @@ +NAME="SLES" +VERSION="15-SP5" +VERSION_ID="15.5" +PRETTY_NAME="SUSE Linux Enterprise Server 15 SP5" +ID="sles" +ID_LIKE="suse" +ANSI_COLOR="0;32" +CPE_NAME="cpe:/o:suse:sles:15:sp5" +DOCUMENTATION_URL="https://documentation.suse.com/" From 02a6cab94674c5d61e0737ea1892c41cb261fcd7 Mon Sep 17 00:00:00 2001 From: sidis-zafran Date: Wed, 20 Nov 2024 17:07:13 +0200 Subject: [PATCH 2/4] Use a seperated regex per SUSE dist Use extractVersionDetails method --- providers/linux/os.go | 51 +++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/providers/linux/os.go b/providers/linux/os.go index 3516157..f2e366a 100644 --- a/providers/linux/os.go +++ b/providers/linux/os.go @@ -32,10 +32,11 @@ import ( ) const ( - osRelease = "/etc/os-release" - lsbRelease = "/etc/lsb-release" - distribRelease = "/etc/*-release" - versionGrok = `(?P(?P[0-9]+)(?:[.-]?(?:SP)?(?P[0-9]+))?(?:[.-](?P[0-9]+|\w+))?)(?: \((?P[-\w ]+)\))?` + osRelease = "/etc/os-release" + lsbRelease = "/etc/lsb-release" + distribRelease = "/etc/*-release" + versionGrok = `(?P(?P[0-9]+)\.?(?P[0-9]+)?\.?(?P\w+)?)(?: \((?P[-\w ]+)\))?` + versionGrokSuse = `(?P(?P[0-9]+)(?:[.-]?(?:SP)?(?P[0-9]+))?(?:[.-](?P[0-9]+|\w+))?)(?: \((?P[-\w ]+)\))?` ) var ( @@ -44,6 +45,9 @@ var ( // versionRegexp parses version numbers (e.g. 6 or 6.1 or 6.1.0 or 6.1.0_20150102). versionRegexp = regexp.MustCompile(versionGrok) + + // versionRegexpSuse parses version numbers for SUSE (e.g. 15-SP1). + versionRegexpSuse = regexp.MustCompile(versionGrokSuse) ) // familyMap contains a mapping of family -> []platforms. @@ -186,28 +190,33 @@ func makeOSInfo(osRelease map[string]string) (*types.OSInfo, error) { } } - if os.Version != "" { - // Try parsing info from the version. - keys := versionRegexp.SubexpNames() - for i, m := range versionRegexp.FindStringSubmatch(os.Version) { - switch keys[i] { - case "major": - os.Major, _ = strconv.Atoi(m) - case "minor": - os.Minor, _ = strconv.Atoi(m) - case "patch": - os.Patch, _ = strconv.Atoi(m) - case "codename": - if os.Codename == "" { - os.Codename = m - } - } - } + if osRelease["ID_LIKE"] == "suse" { + extractVersionDetails(os, os.Version, versionRegexpSuse) + } else if os.Version != "" { + extractVersionDetails(os, os.Version, versionRegexp) } return os, nil } +func extractVersionDetails(os *types.OSInfo, version string, re *regexp.Regexp) { + keys := re.SubexpNames() + for i, match := range re.FindStringSubmatch(version) { + switch keys[i] { + case "major": + os.Major, _ = strconv.Atoi(match) + case "minor": + os.Minor, _ = strconv.Atoi(match) + case "patch": + os.Patch, _ = strconv.Atoi(match) + case "codename": + if os.Codename == "" { + os.Codename = match + } + } + } +} + func findDistribRelease(baseDir string) (*types.OSInfo, error) { matches, err := filepath.Glob(filepath.Join(baseDir, distribRelease)) if err != nil { From b5e97a50e4ec83da11c7619ebedc5bd38a615f62 Mon Sep 17 00:00:00 2001 From: sidis-zafran Date: Sun, 1 Dec 2024 13:00:56 +0200 Subject: [PATCH 3/4] Add release-note --- .changelog/248.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/248.txt diff --git a/.changelog/248.txt b/.changelog/248.txt new file mode 100644 index 0000000..74bdbab --- /dev/null +++ b/.changelog/248.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +Support SLES/SLED version extraction +``` From 6afc152fdeb44574f24f06e1368dfec4aaaef8cb Mon Sep 17 00:00:00 2001 From: sidis-zafran Date: Sun, 1 Dec 2024 21:42:44 +0200 Subject: [PATCH 4/4] Update .changelog/248.txt Co-authored-by: Andrew Kroh --- .changelog/248.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changelog/248.txt b/.changelog/248.txt index 74bdbab..5dd3c3d 100644 --- a/.changelog/248.txt +++ b/.changelog/248.txt @@ -1,3 +1,3 @@ ```release-note:enhancement -Support SLES/SLED version extraction +linux: Support SLES/SLED OS version extraction. ```