-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add utility to interrogate os-release file
- Loading branch information
Showing
5 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/" |