From 3ce01b316d79c7b1485ae387cae6b6cf89797f82 Mon Sep 17 00:00:00 2001 From: Jason Dahlke Date: Mon, 30 Dec 2024 17:34:15 +0000 Subject: [PATCH 1/2] add utility to interrogate os-release file --- .../java/emissary/util/os/OSReleaseUtil.java | 59 +++++++++++++++++++ .../emissary/util/os/OSReleaseUtilTest.java | 44 ++++++++++++++ src/test/resources/emissary/util/os/centos7 | 15 +++++ src/test/resources/emissary/util/os/rhel8 | 17 ++++++ src/test/resources/emissary/util/os/ubuntu | 8 +++ 5 files changed, 143 insertions(+) create mode 100644 src/main/java/emissary/util/os/OSReleaseUtil.java create mode 100644 src/test/java/emissary/util/os/OSReleaseUtilTest.java create mode 100644 src/test/resources/emissary/util/os/centos7 create mode 100644 src/test/resources/emissary/util/os/rhel8 create mode 100644 src/test/resources/emissary/util/os/ubuntu diff --git a/src/main/java/emissary/util/os/OSReleaseUtil.java b/src/main/java/emissary/util/os/OSReleaseUtil.java new file mode 100644 index 0000000000..e3735a43c9 --- /dev/null +++ b/src/main/java/emissary/util/os/OSReleaseUtil.java @@ -0,0 +1,59 @@ +package emissary.util.os; + +import org.apache.commons.lang3.StringUtils; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Optional; +import java.util.stream.Stream; + +public class OSReleaseUtil { + + private static final Path OS_RELEASE_PATH = Path.of("/etc/os-release"); + + private OSReleaseUtil() {} + + public static String getVersionId() { + return getVersionId(OS_RELEASE_PATH); + } + + static String getVersionId(Path osReleasePath) { + String versionId = "UNKNOWN"; + + if (Files.exists(osReleasePath)) { + try (Stream lines = Files.lines(osReleasePath)) { + Optional versionIdOptional = lines.filter(line -> StringUtils.startsWith(line, "VERSION_ID")).findFirst(); + if (versionIdOptional.isPresent()) { + String versionIdLine = versionIdOptional.get().replace("\"", ""); + versionId = versionIdLine.substring(versionIdLine.indexOf("=") + 1); + } + } catch (Exception e) { + // ignore + } + } + return versionId; + } + + public static String getMajorVersion() { + return getMajorVersion(OS_RELEASE_PATH); + } + + static String getMajorVersion(Path osReleasePath) { + return String.format("%.0f", Float.parseFloat(getVersionId(osReleasePath))); + } + + public static boolean isUbuntu() { + return isUbuntu(OS_RELEASE_PATH); + } + + static boolean isUbuntu(Path osReleasePath) { + if (Files.exists(osReleasePath)) { + try (Stream lines = Files.lines(osReleasePath)) { + return lines.anyMatch(s -> StringUtils.containsIgnoreCase(s, "ubuntu")); + } catch (Exception e) { + // ignore + } + } + return false; + } +} diff --git a/src/test/java/emissary/util/os/OSReleaseUtilTest.java b/src/test/java/emissary/util/os/OSReleaseUtilTest.java new file mode 100644 index 0000000000..f86388f0da --- /dev/null +++ b/src/test/java/emissary/util/os/OSReleaseUtilTest.java @@ -0,0 +1,44 @@ +package emissary.util.os; + +import emissary.util.io.ResourceReader; + +import org.junit.jupiter.api.Test; + +import java.nio.file.Path; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class OSReleaseUtilTest { + + @Test + void testGetVersionId() throws Exception { + ResourceReader rr = new ResourceReader(); + + assertEquals("7", OSReleaseUtil.getVersionId(Path.of(rr.getResource(rr.getResourceName(this.getClass().getPackage(), "centos7")).toURI()))); + assertEquals("8.10", OSReleaseUtil.getVersionId(Path.of(rr.getResource(rr.getResourceName(this.getClass().getPackage(), "rhel8")).toURI()))); + assertEquals("20.04", + OSReleaseUtil.getVersionId(Path.of(rr.getResource(rr.getResourceName(this.getClass().getPackage(), "ubuntu")).toURI()))); + } + + @Test + void testGetMajorVersion() throws Exception { + ResourceReader rr = new ResourceReader(); + + assertEquals("7", + OSReleaseUtil.getMajorVersion(Path.of(rr.getResource(rr.getResourceName(this.getClass().getPackage(), "centos7")).toURI()))); + assertEquals("8", OSReleaseUtil.getMajorVersion(Path.of(rr.getResource(rr.getResourceName(this.getClass().getPackage(), "rhel8")).toURI()))); + assertEquals("20", + OSReleaseUtil.getMajorVersion(Path.of(rr.getResource(rr.getResourceName(this.getClass().getPackage(), "ubuntu")).toURI()))); + } + + @Test + void testIsUbuntu() throws Exception { + ResourceReader rr = new ResourceReader(); + + assertFalse(OSReleaseUtil.isUbuntu(Path.of(rr.getResource(rr.getResourceName(this.getClass().getPackage(), "centos7")).toURI()))); + assertFalse(OSReleaseUtil.isUbuntu(Path.of(rr.getResource(rr.getResourceName(this.getClass().getPackage(), "rhel8")).toURI()))); + assertTrue(OSReleaseUtil.isUbuntu(Path.of(rr.getResource(rr.getResourceName(this.getClass().getPackage(), "ubuntu")).toURI()))); + } +} diff --git a/src/test/resources/emissary/util/os/centos7 b/src/test/resources/emissary/util/os/centos7 new file mode 100644 index 0000000000..21d7826199 --- /dev/null +++ b/src/test/resources/emissary/util/os/centos7 @@ -0,0 +1,15 @@ +NAME="CentOS Linux" +VERSION="7 (Core)" +ID="centos" +ID_LIKE="rhel fedora" +VERSION_ID="7" +PRETTY_NAME="CentOS Linux 7 (Core)" +ANSI_COLOR="0;31" +CPE_NAME="cpe:/o:centos:centos:7" +HOME_URL="https://www.centos.org/" +BUG_REPORT_URL="https://bugs.centos.org/" + +CENTOS_MANTISBT_PROJECT="CentOS-7" +CENTOS_MANTISBT_PROJECT_VERSION="7" +REDHAT_SUPPORT_PRODUCT="centos" +REDHAT_SUPPORT_PRODUCT_VERSION="7" \ No newline at end of file diff --git a/src/test/resources/emissary/util/os/rhel8 b/src/test/resources/emissary/util/os/rhel8 new file mode 100644 index 0000000000..6df97f9886 --- /dev/null +++ b/src/test/resources/emissary/util/os/rhel8 @@ -0,0 +1,17 @@ +NAME="Red Hat Enterprise Linux" +VERSION="8.10 (Ootpa)" +ID="rhel" +ID_LIKE="fedora" +VERSION_ID="8.10" +PLATFORM_ID="platform:el8" +PRETTY_NAME="Red Hat Enterprise Linux 8.10 (Ootpa)" +ANSI_COLOR="0;31" +CPE_NAME="cpe:/o:redhat:enterprise_linux:8::baseos" +HOME_URL="https://www.redhat.com/" +DOCUMENTATION_URL="https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/8" +BUG_REPORT_URL="https://issues.redhat.com/" + +REDHAT_BUGZILLA_PRODUCT="Red Hat Enterprise Linux 8" +REDHAT_BUGZILLA_PRODUCT_VERSION=8.10 +REDHAT_SUPPORT_PRODUCT="Red Hat Enterprise Linux" +REDHAT_SUPPORT_PRODUCT_VERSION="8.10" \ No newline at end of file diff --git a/src/test/resources/emissary/util/os/ubuntu b/src/test/resources/emissary/util/os/ubuntu new file mode 100644 index 0000000000..e40b3315f9 --- /dev/null +++ b/src/test/resources/emissary/util/os/ubuntu @@ -0,0 +1,8 @@ +NAME="Ubuntu" +VERSION="20.04.3 LTS (Focal Fossa)" +ID="ubuntu" +ID_LIKE="debian" +VERSION_ID="20.04" +PRETTY_NAME="Ubuntu 20.04.3 LTS" +HOME_URL="https://www.ubuntu.com/" +BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/" \ No newline at end of file From 7bcc489ca421015e01a1c696cc914d83cba0165d Mon Sep 17 00:00:00 2001 From: Jason Dahlke Date: Mon, 30 Dec 2024 18:21:30 +0000 Subject: [PATCH 2/2] add javadoc --- src/main/java/emissary/util/os/OSReleaseUtil.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/main/java/emissary/util/os/OSReleaseUtil.java b/src/main/java/emissary/util/os/OSReleaseUtil.java index e3735a43c9..6a70169fc4 100644 --- a/src/main/java/emissary/util/os/OSReleaseUtil.java +++ b/src/main/java/emissary/util/os/OSReleaseUtil.java @@ -13,6 +13,11 @@ public class OSReleaseUtil { private OSReleaseUtil() {} + /** + * Finds and parses the VERSION_ID entry in the /etc/os-release file + * + * @return the VERSION_ID value + */ public static String getVersionId() { return getVersionId(OS_RELEASE_PATH); } @@ -34,6 +39,11 @@ static String getVersionId(Path osReleasePath) { return versionId; } + /** + * Uses the VERSION_ID entry in the /etc/os-release file to determine the major OS version + * + * @return the major OS version + */ public static String getMajorVersion() { return getMajorVersion(OS_RELEASE_PATH); } @@ -42,6 +52,11 @@ static String getMajorVersion(Path osReleasePath) { return String.format("%.0f", Float.parseFloat(getVersionId(osReleasePath))); } + /** + * Use the /etc/os-release file to determine if the OS is Ubuntu + * + * @return true if ubuntu is found, false otherwise + */ public static boolean isUbuntu() { return isUbuntu(OS_RELEASE_PATH); }