Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add utility to interrogate os-release file #1036

Merged
merged 2 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions src/main/java/emissary/util/os/OSReleaseUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
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() {}

/**
* 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);
}

static String getVersionId(Path osReleasePath) {
String versionId = "UNKNOWN";

if (Files.exists(osReleasePath)) {
try (Stream<String> lines = Files.lines(osReleasePath)) {
Optional<String> 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;
}

/**
* 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);
}

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);
}

static boolean isUbuntu(Path osReleasePath) {
if (Files.exists(osReleasePath)) {
try (Stream<String> lines = Files.lines(osReleasePath)) {
return lines.anyMatch(s -> StringUtils.containsIgnoreCase(s, "ubuntu"));
jpdahlke marked this conversation as resolved.
Show resolved Hide resolved
} catch (Exception e) {
// ignore
}
}
return false;
}
}
44 changes: 44 additions & 0 deletions src/test/java/emissary/util/os/OSReleaseUtilTest.java
Original file line number Diff line number Diff line change
@@ -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())));
}
}
15 changes: 15 additions & 0 deletions src/test/resources/emissary/util/os/centos7
Original file line number Diff line number Diff line change
@@ -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"
17 changes: 17 additions & 0 deletions src/test/resources/emissary/util/os/rhel8
Original file line number Diff line number Diff line change
@@ -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"
8 changes: 8 additions & 0 deletions src/test/resources/emissary/util/os/ubuntu
Original file line number Diff line number Diff line change
@@ -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/"
Loading