@@ -30,14 +30,65 @@ ADDITIONAL_VERSIONS="${ADDITIONALVERSIONS:-""}"
3030
3131set -e
3232
33- # Clean up
34- rm -rf /var/lib/apt/lists/*
35-
3633if [ " $( 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
3936fi
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.
4293rm -f /etc/profile.d/00-restore-env.sh
4394echo " 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
61112fi
62113
63114updaterc () {
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
84173check_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
143242fi
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
149259if [ ! -d " ${SDKMAN_DIR} " ]; then
@@ -178,26 +288,26 @@ if [ ! -z "${ADDITIONAL_VERSIONS}" ]; then
178288fi
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}
183293fi
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}
188298fi
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}
193303fi
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} "
198308fi
199309
200310# Clean up
201- rm -rf /var/lib/apt/lists/ *
311+ clean_up
202312
203313echo " Done!"
0 commit comments