Skip to content

Commit

Permalink
Use Zulu API v1 when upgrading Java
Browse files Browse the repository at this point in the history
Additionally, to clarify what Java version this will install, when
available, the API will prefer downloading a JRE. However, when a JRE is
not available it will use a JDK instead. However, when JDK's are
installed only the essential binaries are linked to update-alternatives
so extra JDK related software is unlinked (i.e. javac).

At the moment, Azul appears to only provide JDK builds for ARM platforms
and so, as such, ARM platforms are most often the ones to not get a JRE
installed and the resulting installation tends to be a bit larger (~100
MB).

One minor inconvenience is that with the current Azul API it does not
allow explicitly choosing builds without the JavaFX additional binaries.
So at times, the Java version installed will have extra JavaFX binaries.
This is usually seen when the architecture being installed is x86 based.

Fixes openhab#714
Fixes openhab#723
See Also openhab#629

Signed-off-by: Ethan Dye <[email protected]>
  • Loading branch information
ecdye committed Apr 30, 2020
1 parent c49196f commit ae1569b
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 81 deletions.
158 changes: 84 additions & 74 deletions functions/java-jre.bash
Original file line number Diff line number Diff line change
@@ -1,36 +1,33 @@
#!/usr/bin/env bash
# shellcheck disable=SC2181

## Install best suitible java version depending on platform.
## Valid argument choose between: 64-bit, 32-bit
## Install appropriate Java version based on current platform.
## Valid arguments: 64-bit, 32-bit
##
## java_install_and_update(String arch)
##
java_install_or_update(){
# Make sure we don't overwrite existing none Java Zulu installations
# Make sure we don't overwrite existing non Java Zulu installations
if ! [ -x "$(command -v java)" ] || [[ ! "$(java -version)" == *"Zulu"* ]]; then
cond_redirect systemctl stop openhab2.service

if [ "$1" == "64-bit" ]; then
if is_x86_64; then
java_zulu_enterprise_8_apt
else
if java_zulu_tar_update_available; then
if java_zulu_tar_update_available 64-bit; then
java_zulu_8_tar 64-bit
fi
fi

else # Default to 32-bit installation
if java_zulu_tar_update_available; then
if java_zulu_tar_update_available 32-bit; then
java_zulu_8_tar 32-bit
fi
fi
cond_redirect systemctl start openhab2.service
fi
}

## Install Java Zulu 8 from direct from fetched .TAR file
## Valid argument choose between: 64-bit, 32-bit
## Install Java Zulu 8 directly from fetched .tar.gz file
## Valid arguments: 64-bit, 32-bit
##
## java_zulu_8_tar(String arch)
##
Expand All @@ -41,53 +38,49 @@ java_zulu_8_tar(){
local jdkBin
local jdkLib
local jdkArch
local jdkSecurity

cond_redirect systemctl stop openhab2.service

if [ "$1" == "32-bit" ]; then
echo -n "$(timestamp) [openHABian] Installing Java Zulu 32-Bit OpenJDK... "
link=$(fetch_zulu_tar_url 32-bit)
if is_arm; then
#link="$(fetch_zulu_tar_url "arm-32-bit-hf")";
link="https://cdn.azul.com/zulu-embedded/bin/zulu8.40.0.178-ca-jdk1.8.0_222-linux_aarch32hf.tar.gz"
jdkArch="aarch32"
else
#link="$(fetch_zulu_tar_url "x86-32-bit")";
link="https://cdn.azul.com/zulu/bin/zulu8.42.0.21-ca-jdk8.0.232-linux_i686.tar.gz"
jdkArch="i386"
fi

if is_aarch64; then
dpkg --add-architecture armhf
cond_redirect apt-get update
cond_redirect apt -y install libc6:armhf libncurses5:armhf libstdc++6:armhf
cond_redirect apt-get --yes install libc6:armhf libncurses5:armhf libstdc++6:armhf
fi

if is_x86_64; then
dpkg --add-architecture i386
cond_redirect apt update
cond_redirect apt -y install libc6:i386 libncurses5:i386 libstdc++6:i386
cond_redirect apt-get update
cond_redirect apt-get --yes install libc6:i386 libncurses5:i386 libstdc++6:i386
fi

elif [ "$1" == "64-bit" ]; then
echo -n "$(timestamp) [openHABian] Installing Java Zulu 64-Bit OpenJDK... "
link=$(fetch_zulu_tar_url 64-bit)
if is_arm; then
#link="$(fetch_zulu_tar_url "arm-64-bit")";
link="https://cdn.azul.com/zulu-embedded/bin/zulu8.40.0.178-ca-jdk1.8.0_222-linux_aarch64.tar.gz"
jdkArch="aarch64"
else
#link="$(fetch_zulu_tar_url "x86-64-bit")";
link="https://cdn.azul.com/zulu/bin/zulu8.42.0.21-ca-jdk8.0.232-linux_x64.tar.gz"
jdkArch="amd64"
fi

else
echo -n "[DEBUG] Unvalid argument to function java_zulu_8_tar()"
echo -n "[DEBUG] Invalid argument to function java_zulu_8_tar()"
exit 1
fi
jdkTempLocation="$(mktemp -d /tmp/openhabian.XXXXX)"
if [ -z "$jdkTempLocation" ]; then echo "FAILED"; exit 1; fi
jdkInstallLocation="/opt/jdk"
mkdir -p $jdkInstallLocation

# Fetch and copy new JAVA runtime
# Fetch and copy new Java Zulu 8 runtime
cond_redirect wget -nv -O "$jdkTempLocation"/zulu8.tar.gz "$link"
tar -xpzf "$jdkTempLocation"/zulu8.tar.gz -C "${jdkTempLocation}"
if [ $? -ne 0 ]; then echo "FAILED"; rm -rf "$jdkTempLocation"/zulu8.tar.gz; exit 1; fi
Expand All @@ -100,95 +93,112 @@ java_zulu_8_tar(){
jdkBin=$(find "${jdkInstallLocation}"/*/bin ... -print -quit)
jdkLib=$(find "${jdkInstallLocation}"/*/lib ... -print -quit)
cond_redirect update-alternatives --remove-all java
## TODO: remove sometime in late 2020
cond_redirect update-alternatives --remove-all javac
cond_redirect update-alternatives --install /usr/bin/java java "$jdkBin"/java 1083000
cond_redirect update-alternatives --install /usr/bin/javac javac "$jdkBin"/javac 1083000
echo "$jdkLib"/"$jdkArch" > /etc/ld.so.conf.d/java.conf
echo "$jdkLib"/"$jdkArch"/jli >> /etc/ld.so.conf.d/java.conf
ldconfig

java_zulu_install_crypto_extension

if [ $? -eq 0 ]; then echo "OK"; else echo "FAILED"; exit 1; fi
cond_redirect systemctl start openhab2.service
}

## Install Azuls Java Zulu Enterprise using APT repository.
## (Updated version only availible on x86-64bit when checked August 2019)
## Install Azul's Java Zulu Enterprise using APT repository.
## (package manager distributions only available on x86-64bit platforms when checked in April 2020)
java_zulu_enterprise_8_apt(){
if ! dpkg -s 'zulu-8' >/dev/null 2>&1; then # Check if already is installed
cond_redirect systemctl stop openhab2.service
echo -n "$(timestamp) [openHABian] Installing Zulu Enterprise 64-Bit OpenJDK... "
cond_redirect apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 219BD9C9
if [ $? -ne 0 ]; then echo "FAILED (keyserver)"; exit 1; fi
echo "deb http://repos.azulsystems.com/debian stable main" > /etc/apt/sources.list.d/zulu-enterprise.list
cond_redirect apt-get update
if cond_redirect apt-get -y install zulu-8 && java_zulu_install_crypto_extension; then echo "OK"; else echo "FAILED"; exit 1; fi
if cond_redirect apt-get --yes install zulu-8 && java_zulu_install_crypto_extension; then echo "OK"; else echo "FAILED"; exit 1; fi
cond_redirect systemctl start openhab2.service
fi
}

## Fetches download link for Java Zulu 8.
## Valid architectures: arm-64-bit, arm-32-bit-sf, arm-32-bit-hf, x86-32-bit, x86-64-bit
## Fetch Java Zulu 8 directly from Azul API v1
## Valid arguments: 64-bit, 32-bit
##
## fetch_zulu_tar_url(String arch)
##
## TODO: Rewrite using download API when it leaves alpha state. https://www.azul.com/downloads/zulu-community/api/
fetch_zulu_tar_url(){
local link
local downloadlink
local filter
if [ ! -x "$(command -v jq)" ]; then
apt-get install -y jq

link="https://api.azul.com/zulu/download/community/v1.0/bundles/latest/binary/?jdk_version=8&ext=tar.gz&os=linux"

if [ "$1" == "32-bit" ]; then
if is_arm; then
downloadlink=$(curl "${link}&arch=arm&hw_bitness=32" -s -L -I -o /dev/null -w '%{url_effective}')
else
downloadlink=$(curl "${link}&arch=x86&hw_bitness=32" -s -L -I -o /dev/null -w '%{url_effective}')
fi

elif [ "$1" == "64-bit" ]; then
if is_arm; then
downloadlink=$(curl "${link}&arch=arm&hw_bitness=64" -s -L -I -o /dev/null -w '%{url_effective}')
else
downloadlink=$(curl "${link}&arch=x86&hw_bitness=64" -s -L -I -o /dev/null -w '%{url_effective}')
fi

else
echo -n "[DEBUG] Invalid argument to function fetch_zulu_tar_url()"
exit 1
fi
filter='.[] | select(.category_slug == "java-8-lts") | select(.latest == 1) | select(.packaging_slug == "jdk") | select(.arch_slug == "'$1'") | .["bundles"] | .[] | select(.extension == "tar.gz") | .["link"]' # Temporary
# filter='.[] | select(.os == "Linux") | select(.category_slug == "java-8-lts") | select(.latest == 1) | select(.packaging_slug == "jdk") | select(.arch_slug == "'$1'") | select((.os_flavor | index("Debian")) or (.os_flavor == "[]")) | .["bundles"] | .[] | select(.extension == "tar.gz") | .["link"]' # $1 = e.g. "arm-64-bit"
# Fetch an JSON array of download candidates from azul and filter them
# shellcheck disable=SC2006
downloadlink=`curl 'https://www.azul.com/wp-admin/admin-ajax.php' -s \
-H 'Accept: */*' \
-H 'X-Requested-With: XMLHttpRequest' \
-H 'Content-Type: multipart/form-data; boundary=---------------------------17447165291708986765346780730' \
-H 'Connection: keep-alive' \
-H 'Cache-Control: max-age=0' -H 'TE: Trailers' \
--data-binary $'-----------------------------17447165291708986765346780730\r\nContent-Disposition: form-data; name="action"\r\n\r\nbundles_filter_query\r\n-----------------------------17447165291708986765346780730\r\nContent-Disposition: form-data; name="action"\r\n\r\nsearch_bundles\r\n-----------------------------17447165291708986765346780730--\r\n' \
| jq -r "$filter"`

if [ -z "$downloadlink" ]; then return 1; fi
# shellcheck disable=SC2046,SC2005,SC2006
echo `echo "$downloadlink" | head -n1`
echo "$downloadlink"
return 0
}

## Check whatever a newer version Zulu version is available.
## Returns 0 / true if new version exist
## TODO: Rewrite using download API when it leaves alpha state. https://www.azul.com/downloads/zulu-community/api/
## Check if a newer version of Java Zulu 8 is available.
## Returns 0 / true if new version exists
## Valid arguments: 64-bit, 32-bit
##
## java_zulu_tar_update_available(String arch)
##
java_zulu_tar_update_available(){
if [ ! -x "$(command -v java)" ]; then return 0; fi

local availableVersion # format: "8u222-b10"
local availableVersion
local javaVersion
local filter
if [ ! -x "$(command -v jq)" ]; then
apt-get install -y jq
local link

filter='[.zulu_version[] | tostring] | join(".")'
link="https://api.azul.com/zulu/download/community/v1.0/bundles/latest/?jdk_version=8&ext=tar.gz&os=linux"
javaVersion=$(java -version 2>&1 | grep -m 1 -o "[0-9]\{0,3\}\.[0-9]\{0,3\}\.[0-9]\{0,3\}\.[0-9]\{0,3\}")

if [ "$1" == "32-bit" ]; then
if is_arm; then
availableVersion=$(curl -s -H "Accept: application/json" "${link}&arch=arm&hw_bitness=32" | jq -r "$filter")
else
availableVersion=$(curl -s -H "Accept: application/json" "${link}&arch=x86&hw_bitness=32" | jq -r "$filter")
fi

elif [ "$1" == "64-bit" ]; then
if is_arm; then
availableVersion=$(curl -s -H "Accept: application/json" "${link}&arch=arm&hw_bitness=64" | jq -r "$filter")
else
availableVersion=$(curl -s -H "Accept: application/json" "${link}&arch=x86&hw_bitness=64" | jq -r "$filter")
fi

else
echo -n "[DEBUG] Invalid argument to function java_zulu_tar_update_available()"
exit 1
fi
filter='[.[] | select(.category_slug == "java-8-lts") | select(.latest == 1) | .["openjdk_version"]] | first' # Temporary
#filter='[.[] | select(.os == "Linux") | select(.category_slug == "java-8-lts") | select(.latest == 1) | .["openjdk_version"]] | first'
# Fetch an JSON array of download candidates from azul and filter them
# shellcheck disable=SC2006
availableVersion=`curl 'https://www.azul.com/wp-admin/admin-ajax.php' -s \
-H 'Accept: */*' \
-H 'X-Requested-With: XMLHttpRequest' \
-H 'Content-Type: multipart/form-data; boundary=---------------------------17447165291708986765346780730' \
-H 'Connection: keep-alive' \
-H 'Cache-Control: max-age=0' -H 'TE: Trailers' \
--data-binary $'-----------------------------17447165291708986765346780730\r\nContent-Disposition: form-data; name="action"\r\n\r\nbundles_filter_query\r\n-----------------------------17447165291708986765346780730\r\nContent-Disposition: form-data; name="action"\r\n\r\nsearch_bundles\r\n-----------------------------17447165291708986765346780730--\r\n' \
| jq -r "$filter"`
availableVersion="${availableVersion:2}" # Strip first two char -> "222-b10"
javaVersion=$(java -version)
if [[ $javaVersion == *"$availableVersion"* ]]; then
# Java updated
return 1

if [[ $javaVersion == "$availableVersion" ]]; then
return 1 # Java is up-to-date
fi
return 0
}

## Install Cryptography Extension Kit to enable cryptos using more then 128 bits
## Install Zulu Cryptography Extension Kit to enable cryptos using more then 128 bits
java_zulu_install_crypto_extension(){
local jdkPath
local jdkSecurity
Expand All @@ -199,7 +209,7 @@ java_zulu_install_crypto_extension(){
policyTempLocation="$(mktemp -d /tmp/openhabian.XXXXX)"
if [ -z "$policyTempLocation" ]; then echo "FAILED"; exit 1; fi

cond_redirect wget -nv -O "$policyTempLocation"/crypto.zip http://cdn.azul.com/zcek/bin/ZuluJCEPolicies.zip
cond_redirect wget -nv -O "$policyTempLocation"/crypto.zip https://cdn.azul.com/zcek/bin/ZuluJCEPolicies.zip
cond_redirect unzip "$policyTempLocation"/crypto.zip -d "$policyTempLocation"
cp "$policyTempLocation"/ZuluJCEPolicies/*.jar "$jdkSecurity"

Expand Down
14 changes: 7 additions & 7 deletions functions/java-jre.bats
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
load java-jre
load helpers

@test "unit-zulu_fetch_tar_url" {
run echo "https://cdn.azul.com/zulu-embedded/bin/zulu8.40.0.178-ca-jdk1.8.0_222-linux_aarch32hf.tar.gz"
echo "# Fetched .TAR download link for \"arm-32-bit-hf\": $output"
curl -s --head "$output" | head -n 1 | grep "HTTP/1.[01] [23].." > /dev/null # Check if link is valid, result it $?
[ $? -eq 0 ]
}

@test "installation-java_exist" {
run java -version
[ "$status" -eq 0 ]
[[ $output == *"Zulu"* ]]
}

@test "unit-zulu_fetch_tar_url" {
run fetch_zulu_tar_url 32-bit
echo "# Fetched .tar.gz download link for Java Zulu 32-bit: $output"
curl -s --head "$output" | head -n 1 | grep "HTTP/1.[01] [23].." > /dev/null # Check if link is valid, result it $?
[ $? -eq 0 ]
}

@test "destructive-update_java-64bit_tar" {
echo -e "# \e[36mZulu 64-bit Java installation is being (test-)installed..." >&3
case "$(uname -m)" in
Expand Down
2 changes: 2 additions & 0 deletions functions/system.bash
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ whiptail_check() {
system_upgrade() {
echo -n "$(timestamp) [openHABian] Updating repositories and upgrading installed packages... "
cond_redirect apt-get --yes upgrade
# shellcheck disable=SC2154
if cond_redirect java_install_or_update "$java_arch"; then echo "OK"; else echo "FAILED"; exit 1; fi
}

basic_packages() {
Expand Down

0 comments on commit ae1569b

Please sign in to comment.