Skip to content

Commit

Permalink
add utility to interrogate os-release file
Browse files Browse the repository at this point in the history
  • Loading branch information
jpdahlke committed Dec 30, 2024
1 parent 5c56e40 commit 3ce01b3
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/main/java/emissary/util/os/OSReleaseUtil.java
Original file line number Diff line number Diff line change
@@ -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<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;
}

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<String> lines = Files.lines(osReleasePath)) {
return lines.anyMatch(s -> StringUtils.containsIgnoreCase(s, "ubuntu"));
} 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/"

0 comments on commit 3ce01b3

Please sign in to comment.