From: guillaume.lambert Date: Thu, 1 Sep 2022 09:58:58 +0000 (+0200) Subject: Fix: JAVA_HOME directory detection X-Git-Tag: v0.81.2~1^2 X-Git-Url: https://gerrit.linuxfoundation.org/infra/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F79%2F70579%2F5;p=releng%2Fglobal-jjb.git Fix: JAVA_HOME directory detection OpenDaylight jenkins maven jobs with jdk17 and CentOS7 currently fails with a confusing message stating that the JAVA_HOME variable is not correctly set. This can happen in various cases, usually when there is a mismatch between the jdk used by maven and the folder pointed by JAVA_HOME. It appears that openjdk17 is not available with CentOS7 and that the folder indeed does not exist. To avoid misinterpretation - add a folder existence check in related script before propagating JAVA_HOME variable to other scripts - if no folder was found, try to find an approaching solution and exit in case of failure with a more relevant error message - adapt and refactor code consequently to be more agnostic to distribution and jdk installation specificities Signed-off-by: guillaume.lambert Change-Id: I585cb34e8126ac5827ae33b5c1ed771fd78b3d10 --- diff --git a/releasenotes/notes/Fix-JAVA_HOME-directory-detection-b164f29820d36206.yaml b/releasenotes/notes/Fix-JAVA_HOME-directory-detection-b164f29820d36206.yaml new file mode 100644 index 00000000..e5540f1b --- /dev/null +++ b/releasenotes/notes/Fix-JAVA_HOME-directory-detection-b164f29820d36206.yaml @@ -0,0 +1,24 @@ +--- +prelude: > + OpenDaylight jenkins maven jobs with jdk17 and CentOS7 currently fails + with a confusing message stating that the JAVA_HOME variable is not + correctly set. + This can happen in various cases, usually when there is a mismatch + between the jdk used by maven and the folder pointed by JAVA_HOME. + It appears that openjdk17 is not available with CentOS7 and that + the folder indeed does not exist +issues: + - | + Current message (JAVA_HOME variable is not set) is confusing and can lead + to erroneous interpretations. +fixes: + - | + Add a folder existence check in related script before propagating + JAVA_HOME variable to other scripts. + If no folder was found, try to find an approaching solution and exit in + case of failure with a more relevant error message. +other: + - | + Adapt and refactor code consequently to be more agnostic to distribution + and jdk installation specificities + diff --git a/shell/update-java-alternatives.sh b/shell/update-java-alternatives.sh index 58a83fc6..fdd87c70 100644 --- a/shell/update-java-alternatives.sh +++ b/shell/update-java-alternatives.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/sh # SPDX-License-Identifier: EPL-1.0 ############################################################################## # Copyright (c) 2018 The Linux Foundation and others. @@ -14,34 +14,22 @@ echo "---> update-java-alternatives.sh" JAVA_ENV_FILE="/tmp/java.env" -update-java-redhat() { - JAVA_RELEASE=${SET_JDK_VERSION//[a-zA-Z]/} - if [[ ${JAVA_RELEASE} -ge 9 ]]; then +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 # Java 9 or newer: new version format export JAVA_HOME="/usr/lib/jvm/java-${JAVA_RELEASE}-openjdk" else # Java 8 or older: old version format - export JAVA_HOME="/usr/lib/jvm/java-1.${SET_JDK_VERSION//[a-zA-Z:-]/}.0-openjdk" + export JAVA_HOME="/usr/lib/jvm/java-1.${JAVA_RELEASE_NBR}.0-openjdk" fi - sudo /usr/sbin/alternatives --install /usr/bin/java java "${JAVA_HOME}/bin/java" 1 - sudo /usr/sbin/alternatives --install /usr/bin/javac javac "${JAVA_HOME}/bin/javac" 1 - sudo /usr/sbin/alternatives --install /usr/lib/jvm/java-openjdk java_sdk_openjdk "${JAVA_HOME}" 1 - sudo /usr/sbin/alternatives --set java "${JAVA_HOME}/bin/java" - sudo /usr/sbin/alternatives --set javac "${JAVA_HOME}/bin/javac" - sudo /usr/sbin/alternatives --set java_sdk_openjdk "${JAVA_HOME}" - echo JAVA_HOME="$JAVA_HOME" > "$JAVA_ENV_FILE" } -update-java-ubuntu() { +update_java_ubuntu() { HOST_ARCH=$(dpkg --print-architecture) - export JAVA_HOME="/usr/lib/jvm/java-${SET_JDK_VERSION//[a-zA-Z:-]/}-openjdk-${HOST_ARCH}" - sudo /usr/bin/update-alternatives --install /usr/bin/java java "${JAVA_HOME}/bin/java" 1 - sudo /usr/bin/update-alternatives --install /usr/bin/javac javac "${JAVA_HOME}/bin/javac" 1 - sudo /usr/bin/update-alternatives --install /usr/lib/jvm/java-openjdk java_sdk_openjdk "${JAVA_HOME}" 1 - sudo /usr/bin/update-alternatives --set java "${JAVA_HOME}/bin/java" - sudo /usr/bin/update-alternatives --set javac "${JAVA_HOME}/bin/javac" - sudo /usr/bin/update-alternatives --set java_sdk_openjdk "${JAVA_HOME}" - echo JAVA_HOME="$JAVA_HOME" > "$JAVA_ENV_FILE" + export JAVA_HOME="/usr/lib/jvm/java-${JAVA_RELEASE_NBR}-openjdk-${HOST_ARCH}" } echo "---> Updating Java version" @@ -50,12 +38,33 @@ OS=$(facter operatingsystem | tr '[:upper:]' '[:lower:]') case "${OS}" in fedora|centos|redhat) echo "---> RedHat type system detected" - update-java-redhat + update_java_redhat + alternatives="/usr/sbin/alternatives" ;; ubuntu|debian) echo "---> Ubuntu/Debian system detected" - update-java-ubuntu + update_java_ubuntu + alternatives="/usr/sbin/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) + 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}" +echo JAVA_HOME="$JAVA_HOME" > "$JAVA_ENV_FILE" + java -version echo JAVA_HOME="${JAVA_HOME}"