Chore: Upgrade Jenkins-job-builder to 6.3.0
[releng/global-jjb.git] / shell / update-java-alternatives.sh
1 #!/bin/sh
2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2018 The Linux Foundation and others.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Eclipse Public License v1.0
8 # which accompanies this distribution, and is available at
9 # http://www.eclipse.org/legal/epl-v10.html
10 ##############################################################################
11 echo "---> update-java-alternatives.sh"
12 # This script takes the java-version variable to set the proper alternative
13 # for java, javac and java_sdk_openjdk for ubuntu or centos/fedora/redhat distros
14
15 JAVA_ENV_FILE="/tmp/java.env"
16
17 JAVA_RELEASE=$(echo "$SET_JDK_VERSION" | sed 's/[a-zA-Z]//g')
18 JAVA_RELEASE_NBR=$(echo "$SET_JDK_VERSION" | sed 's/[a-zA-Z:-]//g')
19 #TODO check whether is it worth keeping there 2 distinct variables
20 update_java_redhat() {
21     if [ "${JAVA_RELEASE}" -ge 9 ]; then
22         # Java 9 or newer: new version format
23         export JAVA_HOME="/usr/lib/jvm/java-${JAVA_RELEASE}-openjdk"
24     else
25         # Java 8 or older: old version format
26         export JAVA_HOME="/usr/lib/jvm/java-1.${JAVA_RELEASE_NBR}.0-openjdk"
27     fi
28 }
29
30 update_java_ubuntu() {
31     HOST_ARCH=$(dpkg --print-architecture)
32     export JAVA_HOME="/usr/lib/jvm/java-${JAVA_RELEASE_NBR}-openjdk-${HOST_ARCH}"
33 }
34
35 echo "---> Updating Java version"
36 OS=$(facter operatingsystem | tr '[:upper:]' '[:lower:]')
37
38 case "${OS}" in
39     fedora|centos|redhat)
40         echo "---> RedHat type system detected"
41         update_java_redhat
42         alternatives="/usr/sbin/alternatives"
43     ;;
44     ubuntu|debian)
45         echo "---> Ubuntu/Debian system detected"
46         update_java_ubuntu
47         alternatives=$(which update-alternatives)
48     ;;
49 esac
50
51 if ! [ -d "$JAVA_HOME" ]; then
52     echo "$JAVA_HOME directory not found - trying to find an approaching one"
53     if ls -d "$JAVA_HOME"*; then
54     # shellcheck disable=SC2012
55         JAVA_HOME=$(ls -d "$JAVA_HOME"* | head -1)
56         export JAVA_HOME
57     else
58         echo "no $JAVA_HOME directory nor candidate found -exiting " >&2
59         exit 17
60     fi
61 fi
62
63 # If sudo is not found, the commands below will run anyway
64 SUDO_CMD=$(which sudo)
65
66 $SUDO_CMD "$alternatives" --install /usr/bin/java java "${JAVA_HOME}/bin/java" 1
67 $SUDO_CMD "$alternatives" --install /usr/bin/javac javac "${JAVA_HOME}/bin/javac" 1
68 $SUDO_CMD "$alternatives" --install /usr/lib/jvm/java-openjdk java_sdk_openjdk "${JAVA_HOME}" 1
69 $SUDO_CMD "$alternatives" --set java "${JAVA_HOME}/bin/java"
70 $SUDO_CMD "$alternatives" --set javac "${JAVA_HOME}/bin/javac"
71 $SUDO_CMD "$alternatives" --set java_sdk_openjdk "${JAVA_HOME}"
72 echo JAVA_HOME="$JAVA_HOME" > "$JAVA_ENV_FILE"
73
74 java -version
75 echo JAVA_HOME="${JAVA_HOME}"