Skip to content

Commit

Permalink
Add RHEL support to Java feature (#821)
Browse files Browse the repository at this point in the history
* Initial rhel update

* RHEL support added.

* Add tests

* quiet down test for ant/gradle/maven/groovy when they are not yet installed.

* revert README -- it is autobuilt

* Update src/java/NOTES.md

add "`" around package manager names.

Co-authored-by: Samruddhi Khandale <[email protected]>

* address PR feedback

---------

Co-authored-by: Jeff Putsch <[email protected]>
Co-authored-by: Samruddhi Khandale <[email protected]>
  • Loading branch information
3 people authored Jan 31, 2024
1 parent 67de3c2 commit 3063f5f
Show file tree
Hide file tree
Showing 21 changed files with 574 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/java/NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ For the Java Feature from this repository, see [NOTICE.txt](https://github.com/d

## OS Support

This Feature should work on recent versions of Debian/Ubuntu-based distributions with the `apt` package manager installed.
Debian/Ubuntu, RedHat Enterprise Linux, Fedora, Alma, and RockyLinux distributions with the `apt`, `yum`, `dnf`, or `microdnf` package manager installed.

`bash` is required to execute the `install.sh` script.
4 changes: 2 additions & 2 deletions src/java/devcontainer-feature.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"id": "java",
"version": "1.3.0",
"version": "1.4.0",
"name": "Java (via SDKMAN!)",
"documentationURL": "https://github.com/devcontainers/features/tree/main/src/java",
"description": "Installs Java, SDKMAN! (if not installed), and needed dependencies.",
Expand Down Expand Up @@ -111,4 +111,4 @@
"installsAfter": [
"ghcr.io/devcontainers/features/common-utils"
]
}
}
160 changes: 135 additions & 25 deletions src/java/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,65 @@ ADDITIONAL_VERSIONS="${ADDITIONALVERSIONS:-""}"

set -e

# Clean up
rm -rf /var/lib/apt/lists/*

if [ "$(id -u)" -ne 0 ]; then
echo -e 'Script must be run as root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.'
exit 1
fi

# Bring in ID, ID_LIKE, VERSION_ID, VERSION_CODENAME
. /etc/os-release
# Get an adjusted ID independent of distro variants
MAJOR_VERSION_ID=$(echo ${VERSION_ID} | cut -d . -f 1)
if [ "${ID}" = "debian" ] || [ "${ID_LIKE}" = "debian" ]; then
ADJUSTED_ID="debian"
elif [[ "${ID}" = "rhel" || "${ID}" = "fedora" || "${ID}" = "mariner" || "${ID_LIKE}" = *"rhel"* || "${ID_LIKE}" = *"fedora"* || "${ID_LIKE}" = *"mariner"* ]]; then
ADJUSTED_ID="rhel"
if [[ "${ID}" = "rhel" ]] || [[ "${ID}" = *"alma"* ]] || [[ "${ID}" = *"rocky"* ]]; then
VERSION_CODENAME="rhel${MAJOR_VERSION_ID}"
else
VERSION_CODENAME="${ID}${MAJOR_VERSION_ID}"
fi
else
echo "Linux distro ${ID} not supported."
exit 1
fi

# Setup INSTALL_CMD & PKG_MGR_CMD
if type apt-get > /dev/null 2>&1; then
PKG_MGR_CMD=apt-get
INSTALL_CMD="${PKG_MGR_CMD} -y install --no-install-recommends"
elif type microdnf > /dev/null 2>&1; then
PKG_MGR_CMD=microdnf
INSTALL_CMD="${PKG_MGR_CMD} -y install --refresh --best --nodocs --noplugins --setopt=install_weak_deps=0"
elif type dnf > /dev/null 2>&1; then
PKG_MGR_CMD=dnf
INSTALL_CMD="${PKG_MGR_CMD} -y install"
elif type yum > /dev/null 2>&1; then
PKG_MGR_CMD=yum
INSTALL_CMD="${PKG_MGR_CMD} -y install"
else
echo "(Error) Unable to find a supported package manager."
exit 1
fi

# Clean up
clean_up() {
local pkg
case ${ADJUSTED_ID} in
debian)
rm -rf /var/lib/apt/lists/*
;;
rhel)
for pkg in epel-release epel-release-latest packages-microsoft-prod; do
${PKG_MGR_CMD} -y remove $pkg 2>/dev/null || /bin/true
done
rm -rf /var/cache/dnf/* /var/cache/yum/*
rm -f /etc/yum.repos.d/docker-ce.repo
;;
esac
}
clean_up

# Ensure that login shells get the correct path if the user updated the PATH using ENV.
rm -f /etc/profile.d/00-restore-env.sh
echo "export PATH=${PATH//$(sh -lc 'echo $PATH')/\$PATH}" > /etc/profile.d/00-restore-env.sh
Expand All @@ -61,31 +112,79 @@ elif [ "${USERNAME}" = "none" ] || ! id -u ${USERNAME} > /dev/null 2>&1; then
fi

updaterc() {
local _bashrc
local _zshrc
if [ "${UPDATE_RC}" = "true" ]; then
echo "Updating /etc/bash.bashrc and /etc/zsh/zshrc..."
if [[ "$(cat /etc/bash.bashrc)" != *"$1"* ]]; then
echo -e "$1" >> /etc/bash.bashrc
case $ADJUSTED_ID in
debian)
_bashrc=/etc/bash.bashrc
_zshrc=/etc/zsh/zshrc
;;
rhel)
_bashrc=/etc/bashrc
_zshrc=/etc/zshrc
;;
esac
echo "Updating ${_bashrc} and ${_zshrc}..."
if [[ "$(cat ${_bashrc})" != *"$1"* ]]; then
echo -e "$1" >> "${_bashrc}"
fi
if [ -f "/etc/zsh/zshrc" ] && [[ "$(cat /etc/zsh/zshrc)" != *"$1"* ]]; then
echo -e "$1" >> /etc/zsh/zshrc
if [ -f "${_zshrc}" ] && [[ "$(cat ${_zshrc})" != *"$1"* ]]; then
echo -e "$1" >> "${_zshrc}"
fi
fi
}

apt_get_update()
{
if [ "$(find /var/lib/apt/lists/* | wc -l)" = "0" ]; then
echo "Running apt-get update..."
apt-get update -y
fi

pkg_manager_update() {
case $ADJUSTED_ID in
debian)
if [ "$(find /var/lib/apt/lists/* | wc -l)" = "0" ]; then
echo "Running apt-get update..."
${PKG_MGR_CMD} update -y
fi
;;
rhel)
if [ ${PKG_MGR_CMD} = "microdnf" ]; then
if [ "$(ls /var/cache/yum/* 2>/dev/null | wc -l)" = 0 ]; then
echo "Running ${PKG_MGR_CMD} makecache ..."
${PKG_MGR_CMD} makecache
fi
else
if [ "$(ls /var/cache/${PKG_MGR_CMD}/* 2>/dev/null | wc -l)" = 0 ]; then
echo "Running ${PKG_MGR_CMD} check-update ..."
set +e
stderr_messages=$(${PKG_MGR_CMD} -q check-update 2>&1)
rc=$?
# centos 7 sometimes returns a status of 100 when it apears to work.
if [ $rc != 0 ] && [ $rc != 100 ]; then
echo "(Error) ${PKG_MGR_CMD} check-update produced the following error message(s):"
echo "${stderr_messages}"
exit 1
fi
set -e
fi
fi
;;
esac
}

# Checks if packages are installed and installs them if not
check_packages() {
if ! dpkg -s "$@" > /dev/null 2>&1; then
apt_get_update
apt-get -y install --no-install-recommends "$@"
fi
case ${ADJUSTED_ID} in
debian)
if ! dpkg -s "$@" > /dev/null 2>&1; then
pkg_manager_update
${INSTALL_CMD} "$@"
fi
;;
rhel)
if ! rpm -q "$@" > /dev/null 2>&1; then
pkg_manager_update
${INSTALL_CMD} "$@"
fi
;;
esac
}

# Use Microsoft JDK for everything but JDK 8 and 18 (unless specified differently with jdkDistro option)
Expand Down Expand Up @@ -142,8 +241,19 @@ if [ "${architecture}" != "amd64" ] && [ "${architecture}" != "x86_64" ] && [ "$
exit 1
fi

# Install dependencies
check_packages curl ca-certificates zip unzip sed
# Install dependencies,
check_packages ca-certificates zip unzip sed findutils util-linux tar
# Make sure passwd (Debian) and shadow-utils RHEL family is installed
if [ ${ADJUSTED_ID} = "debian" ]; then
check_packages passwd
elif [ ${ADJUSTED_ID} = "rhel" ]; then
check_packages shadow-utils
fi
# minimal RHEL installs may not include curl, or includes curl-minimal instead.
# Install curl if the "curl" command is not present.
if ! type curl > /dev/null 2>&1; then
check_packages curl
fi

# Install sdkman if not installed
if [ ! -d "${SDKMAN_DIR}" ]; then
Expand Down Expand Up @@ -178,26 +288,26 @@ if [ ! -z "${ADDITIONAL_VERSIONS}" ]; then
fi

# Install Ant
if [[ "${INSTALL_ANT}" = "true" ]] && ! ant -version > /dev/null; then
if [[ "${INSTALL_ANT}" = "true" ]] && ! ant -version > /dev/null 2>&1; then
sdk_install ant ${ANT_VERSION}
fi

# Install Gradle
if [[ "${INSTALL_GRADLE}" = "true" ]] && ! gradle --version > /dev/null; then
if [[ "${INSTALL_GRADLE}" = "true" ]] && ! gradle --version > /dev/null 2>&1; then
sdk_install gradle ${GRADLE_VERSION}
fi

# Install Maven
if [[ "${INSTALL_MAVEN}" = "true" ]] && ! mvn --version > /dev/null; then
if [[ "${INSTALL_MAVEN}" = "true" ]] && ! mvn --version > /dev/null 2>&1; then
sdk_install maven ${MAVEN_VERSION}
fi

# Install Groovy
if [[ "${INSTALL_GROOVY}" = "true" ]] && ! groovy --version > /dev/null; then
if [[ "${INSTALL_GROOVY}" = "true" ]] && ! groovy --version > /dev/null 2>&1; then
sdk_install groovy "${GROOVY_VERSION}"
fi

# Clean up
rm -rf /var/lib/apt/lists/*
clean_up

echo "Done!"
15 changes: 15 additions & 0 deletions test/java/alma-8-minimal.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

set -e

# Optional: Import test library
source dev-container-features-test-lib

# Definition specific tests
check "version" java --version

# Check env
check "JAVA_HOME is set correctly" echo $JAVA_HOME | grep "/usr/local/sdkman/candidates/java/current"

# Report result
reportResults
15 changes: 15 additions & 0 deletions test/java/alma-8.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

set -e

# Optional: Import test library
source dev-container-features-test-lib

# Definition specific tests
check "version" java --version

# Check env
check "JAVA_HOME is set correctly" echo $JAVA_HOME | grep "/usr/local/sdkman/candidates/java/current"

# Report result
reportResults
15 changes: 15 additions & 0 deletions test/java/alma-9-minimal.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

set -e

# Optional: Import test library
source dev-container-features-test-lib

# Definition specific tests
check "version" java --version

# Check env
check "JAVA_HOME is set correctly" echo $JAVA_HOME | grep "/usr/local/sdkman/candidates/java/current"

# Report result
reportResults
15 changes: 15 additions & 0 deletions test/java/alma-9.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

set -e

# Optional: Import test library
source dev-container-features-test-lib

# Definition specific tests
check "version" java --version

# Check env
check "JAVA_HOME is set correctly" echo $JAVA_HOME | grep "/usr/local/sdkman/candidates/java/current"

# Report result
reportResults
15 changes: 15 additions & 0 deletions test/java/centos-7.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

set -e

# Optional: Import test library
source dev-container-features-test-lib

# Definition specific tests
check "version" java --version

# Check env
check "JAVA_HOME is set correctly" echo $JAVA_HOME | grep "/usr/local/sdkman/candidates/java/current"

# Report result
reportResults
15 changes: 15 additions & 0 deletions test/java/fedora.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

set -e

# Optional: Import test library
source dev-container-features-test-lib

# Definition specific tests
check "version" java --version

# Check env
check "JAVA_HOME is set correctly" echo $JAVA_HOME | grep "/usr/local/sdkman/candidates/java/current"

# Report result
reportResults
13 changes: 13 additions & 0 deletions test/java/install_additional_java_rhel_family.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

set -e

# Optional: Import test library
source dev-container-features-test-lib

check "java version 11 installed as default" grep "11\." <(java --version)
check "java version 17 installed" grep "^17\." <(ls /usr/local/sdkman/candidates/java)
check "java version 8 installed" grep "^8\." <(ls /usr/local/sdkman/candidates/java)

# Report result
reportResults
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/bash

set -e

# Optional: Import test library
source dev-container-features-test-lib

check "user is vscode" grep vscode <(whoami)

check "java" java --version

check "ant" ant -version
cat << EOF > /tmp/build.xml
<project><target name="init"><mkdir dir="ant-src"/></target></project>
EOF
cd /tmp && ant init
check "ant-src exists" grep "ant-src" <(ls -la /tmp)

check "gradle" gradle --version
cd /tmp && gradle init --type basic --dsl groovy --incubating --project-name test
check "GRADLE_USER_HOME exists" grep ".gradle" <(ls -la /home/vscode)

check "maven" mvn --version
cd /tmp && mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false
check "m2 exists" grep ".m2" <(ls -la /home/vscode)

check "groovy" groovy --version
cat << EOF > /tmp/test.groovy
println("verify")
EOF
check "groovy works" test "$(groovy /tmp/test.groovy)" = "verify"

# Report result
reportResults
Loading

0 comments on commit 3063f5f

Please sign in to comment.