From: Matthew Watkins Date: Thu, 12 Jan 2023 00:25:35 +0000 (+0000) Subject: Fix: Update path/location of update-alternatives X-Git-Tag: v0.84.0~1^2 X-Git-Url: https://gerrit.linuxfoundation.org/infra/gitweb?a=commitdiff_plain;h=b6ea0d24c673d8af81164d2cd930b7d2f9445993;p=releng%2Fglobal-jjb.git Fix: Update path/location of update-alternatives The path is different between CentOS7/8 and was incorrect for Ubuntu Issue-ID: IT-25261 Signed-off-by: Matthew Watkins Change-Id: I91bfed027dcbe10c0dc39beac2ae71fb4f5f00ad Signed-off-by: Bengt Thuree --- diff --git a/releasenotes/notes/fix-java-alternative-cmd-path-aec5c0808a28e376.yaml b/releasenotes/notes/fix-java-alternative-cmd-path-aec5c0808a28e376.yaml new file mode 100644 index 00000000..60ac5bfd --- /dev/null +++ b/releasenotes/notes/fix-java-alternative-cmd-path-aec5c0808a28e376.yaml @@ -0,0 +1,7 @@ +--- +fixes: + - | + The path and command for update-alternatives/alternatives was + not being set correctly between CentOS7/8 and was incorrect under all + tested ubuntu versions. It did not seem to cause jobs to break, so was + perhaps not being detected in all cases. diff --git a/shell/update-java-alternatives.sh b/shell/update-java-alternatives.sh index fdd87c70..d6c069ec 100644 --- a/shell/update-java-alternatives.sh +++ b/shell/update-java-alternatives.sh @@ -14,11 +14,11 @@ echo "---> update-java-alternatives.sh" JAVA_ENV_FILE="/tmp/java.env" -JAVA_RELEASE=$(echo $SET_JDK_VERSION | sed 's/[a-zA-Z]//g') -JAVA_RELEASE_NBR=$(echo $SET_JDK_VERSION | sed 's/[a-zA-Z:-]//g') +JAVA_RELEASE=$(echo "$SET_JDK_VERSION" | sed 's/[a-zA-Z]//g') +JAVA_RELEASE_NBR=$(echo "$SET_JDK_VERSION" | sed 's/[a-zA-Z:-]//g') #TODO check whether is it worth keeping there 2 distinct variables update_java_redhat() { - if [ ${JAVA_RELEASE} -ge 9 ]; then + if [ "${JAVA_RELEASE}" -ge 9 ]; then # Java 9 or newer: new version format export JAVA_HOME="/usr/lib/jvm/java-${JAVA_RELEASE}-openjdk" else @@ -39,31 +39,35 @@ case "${OS}" in fedora|centos|redhat) echo "---> RedHat type system detected" update_java_redhat - alternatives="/usr/sbin/alternatives" + alternatives="/usr/sbin/alternatives" ;; ubuntu|debian) echo "---> Ubuntu/Debian system detected" update_java_ubuntu - alternatives="/usr/sbin/update-alternatives" + alternatives=$(which update-alternatives) ;; esac if ! [ -d "$JAVA_HOME" ]; then echo "$JAVA_HOME directory not found - trying to find an approaching one" if ls -d "$JAVA_HOME"*; then - export JAVA_HOME=$(ls -d "$JAVA_HOME"* | head -1) + JAVA_HOME=$(ls -d "$JAVA_HOME"* | head -1) + export JAVA_HOME else echo "no $JAVA_HOME directory nor candidate found -exiting " >&2 exit 17 fi fi -sudo $alternatives --install /usr/bin/java java "${JAVA_HOME}/bin/java" 1 -sudo $alternatives --install /usr/bin/javac javac "${JAVA_HOME}/bin/javac" 1 -sudo $alternatives --install /usr/lib/jvm/java-openjdk java_sdk_openjdk "${JAVA_HOME}" 1 -sudo $alternatives --set java "${JAVA_HOME}/bin/java" -sudo $alternatives --set javac "${JAVA_HOME}/bin/javac" -sudo $alternatives --set java_sdk_openjdk "${JAVA_HOME}" +# If sudo is not found, the commands below will run anyway +SUDO_CMD=$(which sudo) + +$SUDO_CMD "$alternatives" --install /usr/bin/java java "${JAVA_HOME}/bin/java" 1 +$SUDO_CMD "$alternatives" --install /usr/bin/javac javac "${JAVA_HOME}/bin/javac" 1 +$SUDO_CMD "$alternatives" --install /usr/lib/jvm/java-openjdk java_sdk_openjdk "${JAVA_HOME}" 1 +$SUDO_CMD "$alternatives" --set java "${JAVA_HOME}/bin/java" +$SUDO_CMD "$alternatives" --set javac "${JAVA_HOME}/bin/javac" +$SUDO_CMD "$alternatives" --set java_sdk_openjdk "${JAVA_HOME}" echo JAVA_HOME="$JAVA_HOME" > "$JAVA_ENV_FILE" java -version