Add script to update Java alternatives 93/8493/15
authorJessica Wagantall <jwagantall@linuxfoundation.org>
Wed, 24 Jan 2018 00:31:47 +0000 (16:31 -0800)
committerThanh Ha <thanh.ha@linuxfoundation.org>
Tue, 30 Jan 2018 01:51:28 +0000 (20:51 -0500)
Use ${java-version} to set the correct java, javac
and java_sdk_openjdk alternative for lf-maven-jobs.

Issue-ID: RELENG-752
Change-Id: Iff0c2e7d92c4cc802925ae67cdda1f4247db52be
Signed-off-by: Jessica Wagantall <jwagantall@linuxfoundation.org>
jjb/lf-maven-jobs.yaml
shell/update-java-alternatives.sh [new file with mode: 0644]

index 988eebd..cc711a6 100644 (file)
           - ../shell/common-variables.sh
           - ../shell/maven-deploy.sh
 
+- builder:
+    name: lf-update-java-alternatives
+    builders:
+      - inject:
+          properties-content: 'SET_JDK_VERSION={java-version}'
+      - shell: !include-raw-escape: ../shell/update-java-alternatives.sh
+      - inject:
+          properties-file: '/tmp/java.env'
+
 ####################
 # COMMON FUNCTIONS #
 ####################
     builders:
       - lf-maven-install:
           mvn-version: '{mvn-version}'
+      - lf-update-java-alternatives:
+          java-version: '{java-version}'
       - lf-provide-maven-settings:
           global-settings-file: '{mvn-global-settings}'
           settings-file: '{mvn-settings}'
     builders:
       - lf-maven-install:
           mvn-version: '{mvn-version}'
+      - lf-update-java-alternatives:
+          java-version: '{java-version}'
       - lf-provide-maven-settings:
           global-settings-file: '{mvn-global-settings}'
           settings-file: '{mvn-settings}'
     builders:
       - lf-maven-install:
           mvn-version: '{mvn-version}'
+      - lf-update-java-alternatives:
+          java-version: '{java-version}'
       - lf-provide-maven-settings:
           global-settings-file: '{mvn-global-settings}'
           settings-file: '{mvn-settings}'
       - lf-jacoco-nojava-workaround
       - lf-maven-install:
           mvn-version: '{mvn-version}'
+      - lf-update-java-alternatives:
+          java-version: '{java-version}'
       - lf-provide-maven-settings:
           global-settings-file: '{mvn-global-settings}'
           settings-file: '{mvn-settings}'
       - lf-jacoco-nojava-workaround
       - lf-maven-install:
           mvn-version: '{mvn-version}'
+      - lf-update-java-alternatives:
+          java-version: '{java-version}'
       - lf-provide-maven-settings:
           global-settings-file: '{mvn-global-settings}'
           settings-file: '{mvn-settings}'
 
     builders:
       - lf-infra-maven-sonar:
+          java-version: '{java-version}'
           mvn-settings: '{mvn-settings}'
           mvn-version: '{mvn-version}'
 
     builders:
       - lf-maven-install:
           mvn-version: '{mvn-version}'
+      - lf-update-java-alternatives:
+          java-version: '{java-version}'
       - inject:
           # Switch this to the sonar wrapper when JJB 2.0 is available
           properties-content: SONAR_HOST_URL=$SONAR_URL
       - lf-jacoco-nojava-workaround
       - lf-maven-install:
           mvn-version: '{mvn-version}'
+      - lf-update-java-alternatives:
+          java-version: '{java-version}'
       - lf-provide-maven-settings:
           global-settings-file: '{mvn-global-settings}'
           settings-file: '{mvn-settings}'
       - lf-jacoco-nojava-workaround
       - lf-maven-install:
           mvn-version: '{mvn-version}'
+      - lf-update-java-alternatives:
+          java-version: '{java-version}'
       - lf-provide-maven-settings:
           global-settings-file: '{mvn-global-settings}'
           settings-file: '{mvn-settings}'
diff --git a/shell/update-java-alternatives.sh b/shell/update-java-alternatives.sh
new file mode 100644 (file)
index 0000000..29f6d38
--- /dev/null
@@ -0,0 +1,53 @@
+#!/bin/bash
+# SPDX-License-Identifier: EPL-1.0
+##############################################################################
+# Copyright (c) 2018 The Linux Foundation and others.
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Eclipse Public License v1.0
+# which accompanies this distribution, and is available at
+# http://www.eclipse.org/legal/epl-v10.html
+##############################################################################
+
+# This script takes the java-version variable to set the proper alternative
+# for java, javac and java_sdk_openjdk for ubuntu or centos/fedora/redhat distros
+
+JAVA_ENV_FILE="/tmp/java.env"
+
+update-java-redhat() {
+    export JAVA_HOME="/usr/lib/jvm/java-1.${SET_JDK_VERSION: -1}.0-openjdk"
+    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() {
+    export JAVA_HOME="/usr/lib/jvm/java-${SET_JDK_VERSION: -1}-openjdk-amd64"
+    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"
+}
+
+echo "---> Updating Java version"
+OS=$(facter operatingsystem | tr '[:upper:]' '[:lower:]')
+
+case "${OS}" in
+    fedora|centos|redhat)
+        echo "---> RedHat type system detected"
+        update-java-redhat
+    ;;
+    ubuntu)
+        echo "---> Ubuntu system detected"
+        update-java-ubuntu
+    ;;
+esac
+java -version
+echo JAVA_HOME="${JAVA_HOME}"