Skip to content

Commit 3063f5f

Browse files
jdputschadiJeff Putschsamruddhikhandale
authored
Add RHEL support to Java feature (devcontainers#821)
* 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 <samruddhikhandale@github.com> * address PR feedback --------- Co-authored-by: Jeff Putsch <jputsch@analog.com> Co-authored-by: Samruddhi Khandale <samruddhikhandale@github.com>
1 parent 67de3c2 commit 3063f5f

21 files changed

Lines changed: 574 additions & 29 deletions

src/java/NOTES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ For the Java Feature from this repository, see [NOTICE.txt](https://github.com/d
55

66
## OS Support
77

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

1010
`bash` is required to execute the `install.sh` script.

src/java/devcontainer-feature.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"id": "java",
3-
"version": "1.3.0",
3+
"version": "1.4.0",
44
"name": "Java (via SDKMAN!)",
55
"documentationURL": "https://github.com/devcontainers/features/tree/main/src/java",
66
"description": "Installs Java, SDKMAN! (if not installed), and needed dependencies.",
@@ -111,4 +111,4 @@
111111
"installsAfter": [
112112
"ghcr.io/devcontainers/features/common-utils"
113113
]
114-
}
114+
}

src/java/install.sh

Lines changed: 135 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,65 @@ ADDITIONAL_VERSIONS="${ADDITIONALVERSIONS:-""}"
3030

3131
set -e
3232

33-
# Clean up
34-
rm -rf /var/lib/apt/lists/*
35-
3633
if [ "$(id -u)" -ne 0 ]; then
3734
echo -e 'Script must be run as root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.'
3835
exit 1
3936
fi
4037

38+
# Bring in ID, ID_LIKE, VERSION_ID, VERSION_CODENAME
39+
. /etc/os-release
40+
# Get an adjusted ID independent of distro variants
41+
MAJOR_VERSION_ID=$(echo ${VERSION_ID} | cut -d . -f 1)
42+
if [ "${ID}" = "debian" ] || [ "${ID_LIKE}" = "debian" ]; then
43+
ADJUSTED_ID="debian"
44+
elif [[ "${ID}" = "rhel" || "${ID}" = "fedora" || "${ID}" = "mariner" || "${ID_LIKE}" = *"rhel"* || "${ID_LIKE}" = *"fedora"* || "${ID_LIKE}" = *"mariner"* ]]; then
45+
ADJUSTED_ID="rhel"
46+
if [[ "${ID}" = "rhel" ]] || [[ "${ID}" = *"alma"* ]] || [[ "${ID}" = *"rocky"* ]]; then
47+
VERSION_CODENAME="rhel${MAJOR_VERSION_ID}"
48+
else
49+
VERSION_CODENAME="${ID}${MAJOR_VERSION_ID}"
50+
fi
51+
else
52+
echo "Linux distro ${ID} not supported."
53+
exit 1
54+
fi
55+
56+
# Setup INSTALL_CMD & PKG_MGR_CMD
57+
if type apt-get > /dev/null 2>&1; then
58+
PKG_MGR_CMD=apt-get
59+
INSTALL_CMD="${PKG_MGR_CMD} -y install --no-install-recommends"
60+
elif type microdnf > /dev/null 2>&1; then
61+
PKG_MGR_CMD=microdnf
62+
INSTALL_CMD="${PKG_MGR_CMD} -y install --refresh --best --nodocs --noplugins --setopt=install_weak_deps=0"
63+
elif type dnf > /dev/null 2>&1; then
64+
PKG_MGR_CMD=dnf
65+
INSTALL_CMD="${PKG_MGR_CMD} -y install"
66+
elif type yum > /dev/null 2>&1; then
67+
PKG_MGR_CMD=yum
68+
INSTALL_CMD="${PKG_MGR_CMD} -y install"
69+
else
70+
echo "(Error) Unable to find a supported package manager."
71+
exit 1
72+
fi
73+
74+
# Clean up
75+
clean_up() {
76+
local pkg
77+
case ${ADJUSTED_ID} in
78+
debian)
79+
rm -rf /var/lib/apt/lists/*
80+
;;
81+
rhel)
82+
for pkg in epel-release epel-release-latest packages-microsoft-prod; do
83+
${PKG_MGR_CMD} -y remove $pkg 2>/dev/null || /bin/true
84+
done
85+
rm -rf /var/cache/dnf/* /var/cache/yum/*
86+
rm -f /etc/yum.repos.d/docker-ce.repo
87+
;;
88+
esac
89+
}
90+
clean_up
91+
4192
# Ensure that login shells get the correct path if the user updated the PATH using ENV.
4293
rm -f /etc/profile.d/00-restore-env.sh
4394
echo "export PATH=${PATH//$(sh -lc 'echo $PATH')/\$PATH}" > /etc/profile.d/00-restore-env.sh
@@ -61,31 +112,79 @@ elif [ "${USERNAME}" = "none" ] || ! id -u ${USERNAME} > /dev/null 2>&1; then
61112
fi
62113

63114
updaterc() {
115+
local _bashrc
116+
local _zshrc
64117
if [ "${UPDATE_RC}" = "true" ]; then
65-
echo "Updating /etc/bash.bashrc and /etc/zsh/zshrc..."
66-
if [[ "$(cat /etc/bash.bashrc)" != *"$1"* ]]; then
67-
echo -e "$1" >> /etc/bash.bashrc
118+
case $ADJUSTED_ID in
119+
debian)
120+
_bashrc=/etc/bash.bashrc
121+
_zshrc=/etc/zsh/zshrc
122+
;;
123+
rhel)
124+
_bashrc=/etc/bashrc
125+
_zshrc=/etc/zshrc
126+
;;
127+
esac
128+
echo "Updating ${_bashrc} and ${_zshrc}..."
129+
if [[ "$(cat ${_bashrc})" != *"$1"* ]]; then
130+
echo -e "$1" >> "${_bashrc}"
68131
fi
69-
if [ -f "/etc/zsh/zshrc" ] && [[ "$(cat /etc/zsh/zshrc)" != *"$1"* ]]; then
70-
echo -e "$1" >> /etc/zsh/zshrc
132+
if [ -f "${_zshrc}" ] && [[ "$(cat ${_zshrc})" != *"$1"* ]]; then
133+
echo -e "$1" >> "${_zshrc}"
71134
fi
72135
fi
73136
}
74137

75-
apt_get_update()
76-
{
77-
if [ "$(find /var/lib/apt/lists/* | wc -l)" = "0" ]; then
78-
echo "Running apt-get update..."
79-
apt-get update -y
80-
fi
138+
139+
pkg_manager_update() {
140+
case $ADJUSTED_ID in
141+
debian)
142+
if [ "$(find /var/lib/apt/lists/* | wc -l)" = "0" ]; then
143+
echo "Running apt-get update..."
144+
${PKG_MGR_CMD} update -y
145+
fi
146+
;;
147+
rhel)
148+
if [ ${PKG_MGR_CMD} = "microdnf" ]; then
149+
if [ "$(ls /var/cache/yum/* 2>/dev/null | wc -l)" = 0 ]; then
150+
echo "Running ${PKG_MGR_CMD} makecache ..."
151+
${PKG_MGR_CMD} makecache
152+
fi
153+
else
154+
if [ "$(ls /var/cache/${PKG_MGR_CMD}/* 2>/dev/null | wc -l)" = 0 ]; then
155+
echo "Running ${PKG_MGR_CMD} check-update ..."
156+
set +e
157+
stderr_messages=$(${PKG_MGR_CMD} -q check-update 2>&1)
158+
rc=$?
159+
# centos 7 sometimes returns a status of 100 when it apears to work.
160+
if [ $rc != 0 ] && [ $rc != 100 ]; then
161+
echo "(Error) ${PKG_MGR_CMD} check-update produced the following error message(s):"
162+
echo "${stderr_messages}"
163+
exit 1
164+
fi
165+
set -e
166+
fi
167+
fi
168+
;;
169+
esac
81170
}
82171

83172
# Checks if packages are installed and installs them if not
84173
check_packages() {
85-
if ! dpkg -s "$@" > /dev/null 2>&1; then
86-
apt_get_update
87-
apt-get -y install --no-install-recommends "$@"
88-
fi
174+
case ${ADJUSTED_ID} in
175+
debian)
176+
if ! dpkg -s "$@" > /dev/null 2>&1; then
177+
pkg_manager_update
178+
${INSTALL_CMD} "$@"
179+
fi
180+
;;
181+
rhel)
182+
if ! rpm -q "$@" > /dev/null 2>&1; then
183+
pkg_manager_update
184+
${INSTALL_CMD} "$@"
185+
fi
186+
;;
187+
esac
89188
}
90189

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

145-
# Install dependencies
146-
check_packages curl ca-certificates zip unzip sed
244+
# Install dependencies,
245+
check_packages ca-certificates zip unzip sed findutils util-linux tar
246+
# Make sure passwd (Debian) and shadow-utils RHEL family is installed
247+
if [ ${ADJUSTED_ID} = "debian" ]; then
248+
check_packages passwd
249+
elif [ ${ADJUSTED_ID} = "rhel" ]; then
250+
check_packages shadow-utils
251+
fi
252+
# minimal RHEL installs may not include curl, or includes curl-minimal instead.
253+
# Install curl if the "curl" command is not present.
254+
if ! type curl > /dev/null 2>&1; then
255+
check_packages curl
256+
fi
147257

148258
# Install sdkman if not installed
149259
if [ ! -d "${SDKMAN_DIR}" ]; then
@@ -178,26 +288,26 @@ if [ ! -z "${ADDITIONAL_VERSIONS}" ]; then
178288
fi
179289

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

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

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

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

200310
# Clean up
201-
rm -rf /var/lib/apt/lists/*
311+
clean_up
202312

203313
echo "Done!"

test/java/alma-8-minimal.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
# Optional: Import test library
6+
source dev-container-features-test-lib
7+
8+
# Definition specific tests
9+
check "version" java --version
10+
11+
# Check env
12+
check "JAVA_HOME is set correctly" echo $JAVA_HOME | grep "/usr/local/sdkman/candidates/java/current"
13+
14+
# Report result
15+
reportResults

test/java/alma-8.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
# Optional: Import test library
6+
source dev-container-features-test-lib
7+
8+
# Definition specific tests
9+
check "version" java --version
10+
11+
# Check env
12+
check "JAVA_HOME is set correctly" echo $JAVA_HOME | grep "/usr/local/sdkman/candidates/java/current"
13+
14+
# Report result
15+
reportResults

test/java/alma-9-minimal.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
# Optional: Import test library
6+
source dev-container-features-test-lib
7+
8+
# Definition specific tests
9+
check "version" java --version
10+
11+
# Check env
12+
check "JAVA_HOME is set correctly" echo $JAVA_HOME | grep "/usr/local/sdkman/candidates/java/current"
13+
14+
# Report result
15+
reportResults

test/java/alma-9.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
# Optional: Import test library
6+
source dev-container-features-test-lib
7+
8+
# Definition specific tests
9+
check "version" java --version
10+
11+
# Check env
12+
check "JAVA_HOME is set correctly" echo $JAVA_HOME | grep "/usr/local/sdkman/candidates/java/current"
13+
14+
# Report result
15+
reportResults

test/java/centos-7.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
# Optional: Import test library
6+
source dev-container-features-test-lib
7+
8+
# Definition specific tests
9+
check "version" java --version
10+
11+
# Check env
12+
check "JAVA_HOME is set correctly" echo $JAVA_HOME | grep "/usr/local/sdkman/candidates/java/current"
13+
14+
# Report result
15+
reportResults

test/java/fedora.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
# Optional: Import test library
6+
source dev-container-features-test-lib
7+
8+
# Definition specific tests
9+
check "version" java --version
10+
11+
# Check env
12+
check "JAVA_HOME is set correctly" echo $JAVA_HOME | grep "/usr/local/sdkman/candidates/java/current"
13+
14+
# Report result
15+
reportResults
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
# Optional: Import test library
6+
source dev-container-features-test-lib
7+
8+
check "java version 11 installed as default" grep "11\." <(java --version)
9+
check "java version 17 installed" grep "^17\." <(ls /usr/local/sdkman/candidates/java)
10+
check "java version 8 installed" grep "^8\." <(ls /usr/local/sdkman/candidates/java)
11+
12+
# Report result
13+
reportResults

0 commit comments

Comments
 (0)