Fix: Update path/location of update-alternatives 33/71133/8
authorMatthew Watkins <mwatkins@linuxfoundation.org>
Thu, 12 Jan 2023 00:25:35 +0000 (00:25 +0000)
committerBengt Thuree <bthuree@linuxfoundation.org>
Tue, 7 Mar 2023 12:27:50 +0000 (23:27 +1100)
The path is different between CentOS7/8 and was incorrect for Ubuntu

Issue-ID: IT-25261
Signed-off-by: Matthew Watkins <mwatkins@linuxfoundation.org>
Change-Id: I91bfed027dcbe10c0dc39beac2ae71fb4f5f00ad
Signed-off-by: Bengt Thuree <bthuree@linuxfoundation.org>
releasenotes/notes/fix-java-alternative-cmd-path-aec5c0808a28e376.yaml [new file with mode: 0644]
shell/update-java-alternatives.sh

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 (file)
index 0000000..60ac5bf
--- /dev/null
@@ -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.
index fdd87c7..d6c069e 100644 (file)
@@ -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