Permalink
Fetching contributors…
Cannot retrieve contributors at this time
executable file 279 lines (239 sloc) 9.45 KB
#!/usr/bin/env bash
LATEST_JDK_VERSION="1.8"
DEFAULT_JDK_VERSION="1.8"
DEFAULT_JDK_BASE_URL="https://lang-jvm.s3.amazonaws.com/jdk/${STACK:-"heroku-16"}"
JDK_BASE_URL=${JDK_BASE_URL:-$DEFAULT_JDK_BASE_URL}
JDK_URL_1_10=${JDK_URL_1_10:-"$JDK_BASE_URL/openjdk10.0.1.tar.gz"}
JDK_URL_1_9=${JDK_URL_1_9:-"$JDK_BASE_URL/openjdk9.0.4.tar.gz"}
JDK_URL_1_8=${JDK_URL_1_8:-"$JDK_BASE_URL/openjdk1.8.0_171.tar.gz"}
JDK_URL_1_7=${JDK_URL_1_7:-"$JDK_BASE_URL/openjdk1.7.0_181.tar.gz"}
JDK_URL_1_6=${JDK_URL_1_6:-"https://lang-jvm.s3.amazonaws.com/jdk/openjdk1.6.0_27.tar.gz"}
install_java_with_overlay() {
local buildDir=${1}
local jdkVersion=$(detect_java_version ${buildDir})
local jdkUrl=$(_get_jdk_download_url "${jdkVersion}")
_jvm_mcount "version.${jdkVersion}"
if [[ "$jdkVersion" == *openjdk* ]]; then
status_pending "Installing OpenJDK $(_get_openjdk_version ${jdkVersion})"
_jvm_mcount "vendor.openjdk"
elif [[ "$jdkVersion" == *zulu* ]]; then
status_pending "Installing Azul Zulu JDK $(_get_zulu_version ${jdkVersion})"
_jvm_mcount "vendor.zulu"
else
status_pending "Installing JDK ${jdkVersion}"
_jvm_mcount "vendor.default"
fi
install_java ${buildDir} ${jdkVersion} ${jdkUrl}
jdk_overlay ${buildDir}
status_done
}
install_java() {
local baseDir=$1
if [ ! -d "${baseDir}" ]; then
error_return "Invalid directory to install java."
return 1
fi
local jdkVersion=${2:-$(get_default_java_version)}
local jdkUrl=${3:-$(_get_jdk_download_url "${jdkVersion}")}
local jdkDir="${baseDir}"/.jdk
local jdkTarball="${jdkDir}"/jdk.tar.gz
local javaExe="${jdkDir}/bin/java"
mkdir -p "${jdkDir}"
if [ ! -f "${jdkTarball}" ] && [ ! -f "${javaExe}" ] || is_java_version_change "${jdkDir}" "${jdkVersion}"; then
rm -rf "${jdkDir}"
mkdir -p "${jdkDir}"
validate_jdk_url ${jdkUrl} ${jdkVersion}
curl --retry 3 --silent --show-error --location ${jdkUrl} --output ${jdkTarball}
tar pxzf ${jdkTarball} -C "${jdkDir}"
rm ${jdkTarball}
install_cacerts ${jdkDir}
echo "${jdkVersion}" > "${jdkDir}/version"
if [ ! -f "${javaExe}" ]; then
error_return "Unable to retrieve the JDK."
return 1
fi
fi
export JAVA_HOME=${jdkDir}
export PATH="${jdkDir}/bin:${PATH}"
_install_tools ${baseDir}
_create_profile_script ${baseDir}
_create_export_script ${JAVA_HOME} "$(pwd)"
_install_pgconfig ${JAVA_HOME}
_install_default_proc_warn ${baseDir}
_install_metrics_agent ${baseDir}
}
install_cacerts() {
local jdkDir=$1
if [ -f ${jdkDir}/jre/lib/security/cacerts ] && [ -f /etc/ssl/certs/java/cacerts ]; then
mv ${jdkDir}/jre/lib/security/cacerts ${jdkDir}/jre/lib/security/cacerts.old
ln -s /etc/ssl/certs/java/cacerts ${jdkDir}/jre/lib/security/cacerts
elif [ -f ${jdkDir}/lib/security/cacerts ] && [ -f /etc/ssl/certs/java/cacerts ]; then
mv ${jdkDir}/lib/security/cacerts ${jdkDir}/lib/security/cacerts.old
ln -s /etc/ssl/certs/java/cacerts ${jdkDir}/lib/security/cacerts
fi
}
validate_jdk_url() {
local jdkUrl=${1}
local jdkVersion=${2}
if [ "$(_get_url_status ${jdkUrl})" != "200" ]; then
echo ""
error_return "Unsupported Java version: $jdkVersion
Please check your system.properties file to ensure the java.runtime.version
is among the list of supported version on the Dev Center:
https://devcenter.heroku.com/articles/java-support#supported-java-versions
You can also remove the system.properties from your repo to install
the default ${DEFAULT_JDK_VERSION} version.
If you continue to have trouble, you can open a support ticket here:
https://help.heroku.com
Thanks,
Heroku"
return 1
fi
}
jdk_overlay() {
baseDir=$1
shopt -s dotglob
cacertPath="lib/security/cacerts"
if [ -d ${baseDir}/.jdk ] && [ -d ${baseDir}/.jdk-overlay ]; then
# delete the symlink because a cp will error
if [ -f ${baseDir}/.jdk-overlay/jre/${cacertPath} ] && [ -f ${baseDir}/.jdk/jre/${cacertPath} ]; then
rm ${baseDir}/.jdk/jre/${cacertPath}
elif [ -f ${baseDir}/.jdk-overlay/${cacertPath} ] && [ -f ${baseDir}/.jdk/${cacertPath} ]; then
rm ${baseDir}/.jdk/${cacertPath}
fi
cp -r ${baseDir}/.jdk-overlay/* ${baseDir}/.jdk/
fi
}
is_java_version_change() {
jdkDir=$1
jdkVersion=${2:-${DEFAULT_JDK_VERSION}}
if [ ! -d "${jdkDir}" ]; then echo "Invalid JDK directory."; return 1; fi
test -f "${jdkDir}/version" && [ "$(cat ${jdkDir}/version)" != "${jdkVersion}" ]
}
_get_jdk_download_url() {
local jdkVersion=${1:-${DEFAULT_JDK_VERSION}}
if [ "${jdkVersion}" = "10" ]; then
local jdkUrl="${JDK_URL_1_10}"
elif [ "$(expr "${jdkVersion}" : '^10')" != 0 ]; then
local jdkUrl="${JDK_BASE_URL}/openjdk${jdkVersion}.tar.gz"
elif [ "$(expr "${jdkVersion}" : '^1.[6-9]$')" != 0 ]; then
local minorJdkVersion=$(expr "${jdkVersion}" : '1.\([6-9]\)')
local jdkUrl=$(eval echo \$JDK_URL_1_${minorJdkVersion})
elif [ "$(expr "${jdkVersion}" : '^[6-9]$')" != 0 ]; then
local jdkUrl=$(eval echo \$JDK_URL_1_${jdkVersion})
elif [ "$(expr "${jdkVersion}" : '^1.[6-9]')" != 0 ]; then
local jdkUrl="${JDK_BASE_URL}/openjdk${jdkVersion}.tar.gz"
elif [ "${jdkVersion}" = "9+181" ] || [ "${jdkVersion}" = "9.0.0" ]; then
local jdkUrl="${JDK_BASE_URL}/openjdk9-181.tar.gz"
elif [ "$(expr "${jdkVersion}" : '^9')" != 0 ]; then
local jdkUrl="${JDK_BASE_URL}/openjdk${jdkVersion}.tar.gz"
elif [ "$(expr "${jdkVersion}" : '^zulu-')" != 0 ]; then
local jdkUrl="${JDK_BASE_URL}/${jdkVersion}.tar.gz"
elif [ "$(expr "${jdkVersion}" : '^openjdk-')" != 0 ]; then
local jdkUrl="${JDK_BASE_URL}/$(echo "$jdkVersion" | sed -e 's/k-/k/g').tar.gz"
fi
echo "${jdkUrl}"
}
get_default_java_version() {
echo "$DEFAULT_JDK_VERSION"
}
detect_java_version() {
baseDir=${1:-"No Dir"}
if [ "${baseDir}" = "No Dir" ] || [ ! -d "${baseDir}" ] ; then
error_return "Invalid directory specified for pom file."
return 1
fi
if [ -f ${baseDir}/system.properties ]; then
echo "$(get_app_system_value ${baseDir}/system.properties "java.runtime.version")"
else
echo "$(get_default_java_version)"
fi
}
get_app_system_value() {
local file=${1?"No file specified"}
local key=${2?"No key specified"}
# escape for regex
local escaped_key=$(echo $key | sed "s/\./\\\./g")
[ -f $file ] && \
grep -E ^$escaped_key[[:space:]=]+ $file | \
sed -E -e "s/$escaped_key([\ \t]*=[\ \t]*|[\ \t]+)([_A-Za-z0-9\.-]*).*/\2/g"
}
_create_profile_script() {
local profileScript="${1}/.profile.d/jvmcommon.sh"
local jdbcProfileScript="${1}/.profile.d/jdbc.sh"
local curDir=${JVM_COMMON_DIR:-$(cd $(dirname ${BASH_SOURCE[0]}) && cd .. && pwd )}
mkdir -p $(dirname $profileScript)
cp $curDir/opt/jvmcommon.sh $profileScript
cp $curDir/opt/jdbc.sh $jdbcProfileScript
}
_install_tools() {
local ctxDir=${1:-BUILD_DIR}
local curDir=${JVM_COMMON_DIR:-$(cd $(dirname ${BASH_SOURCE[0]}) && cd .. && pwd )}
mkdir -p $ctxDir/.heroku/bin
mkdir -p $ctxDir/.heroku/with_jmap/bin
cp $curDir/opt/with_jmap $ctxDir/.heroku/bin/with_jmap
cp $curDir/opt/with_jmap_java $ctxDir/.heroku/with_jmap/bin/java
chmod +x $ctxDir/.heroku/bin/with_jmap
chmod +x $ctxDir/.heroku/with_jmap/bin/java
mkdir -p $ctxDir/.heroku/with_jstack/bin
cp $curDir/opt/with_jstack $ctxDir/.heroku/bin/with_jstack
cp $curDir/opt/with_jstack_java $ctxDir/.heroku/with_jstack/bin/java
chmod +x $ctxDir/.heroku/bin/with_jstack
chmod +x $ctxDir/.heroku/with_jstack/bin/java
mkdir -p $ctxDir/.heroku/with_jmap_and_jstack/bin
cp $curDir/opt/with_jmap_and_jstack $ctxDir/.heroku/bin/with_jmap_and_jstack
cp $curDir/opt/with_jmap_and_jstack_java $ctxDir/.heroku/with_jmap_and_jstack/bin/java
chmod +x $ctxDir/.heroku/bin/with_jmap_and_jstack
chmod +x $ctxDir/.heroku/with_jmap_and_jstack/bin/java
}
_install_default_proc_warn() {
local ctxDir=${1:-BUILD_DIR}
mkdir -p ${ctxDir}/.profile.d
cat << EOF > ${ctxDir}/.profile.d/default-proc-warning.sh
if [ ! -f /app/Procfile ] && [ "\$DYNO" = "web.1" ]; then
echo "Create a Procfile to customize the command used to run this process: https://devcenter.heroku.com/articles/procfile"
fi
EOF
}
_create_export_script() {
local javaHome=${1:-JAVA_HOME}
local ctxDir=${2:-CWD}
if [ -w ${ctxDir} ]; then
cat << EOF > ${ctxDir}/export
export JAVA_HOME=$javaHome
export PATH=\$JAVA_HOME/bin:\$PATH
export LD_LIBRARY_PATH="\$JAVA_HOME/jre/lib/amd64/server:\$LD_LIBRARY_PATH"
EOF
fi
}
_install_pgconfig() {
local javaHome=${1:-JAVA_HOME}
local extDir="${javaHome}/jre/lib/ext"
if [ -d "${extDir}" ] && [ -z "${SKIP_PGCONFIG_INSTALL:-}" ]; then
curl --retry 3 -s -L "https://lang-jvm.s3.amazonaws.com/pgconfig.jar" -o ${extDir}/pgconfig.jar
fi
}
_get_zulu_version() {
echo "$(echo ${1} | sed -e 's/zulu-//g')"
}
_get_openjdk_version() {
echo "$(echo ${1} | sed -e 's/openjdk-//g')"
}
_get_url_status() {
curl --retry 3 --silent --head -w %{http_code} -L "${1}" -o /dev/null
}
_jvm_mcount() {
if type -t mcount > /dev/null; then mcount "jvm.${1}"; fi
}
_install_metrics_agent() {
local ctxDir=${1:-BUILD_DIR}
local curDir=${JVM_COMMON_DIR:-$(cd $(dirname ${BASH_SOURCE[0]}) && cd .. && pwd )}
local binDir="${ctxDir}/.heroku/bin/"
local agent_jar="${binDir}/heroku-metrics-agent.jar"
mkdir -p ${binDir}
curl --retry 3 -s -o ${agent_jar} \
-L ${HEROKU_METRICS_JAR_URL:-"https://repo1.maven.org/maven2/com/heroku/agent/heroku-java-metrics-agent/3.7/heroku-java-metrics-agent-3.7.jar"}
[ ! -f ${agent_jar} ] && warning_inline "failed to install metrics agent!"
mkdir -p ${ctxDir}/.profile.d
cp $curDir/opt/heroku-jvm-metrics.sh $ctxDir/.profile.d/
}