#!/bin/bash # Enable the version of java for an app, and set variables. # # usage: ynh_use_java # # `ynh_use_java` has to be used in any app scripts before using java for the first time. # This helper will provide alias and variables to use in your scripts. # # To use java, use the alias `ynh_java`. # # Those alias will use the correct version installed for the app. # For example: use `ynh_java -jar your-app.jar` instead of `java -jar your-app.jar` # # With `sudo` or `ynh_exec_as`, use instead the fallback variables `$ynh_java` # And propagate $PATH to sudo with $ynh_java_load_PATH # Example: `ynh_exec_as $app $ynh_java_load_PATH $ynh_java -jar yuor-app.jar` # # $PATH contains the path of the requested version of java. # However, $PATH is duplicated into $java_PATH to outlast any manipulation of `$PATH` # You can use the variable `$ynh_java_load_PATH` to quickly load your java version # in $PATH for an usage into a separate script. # Example: $ynh_java_load_PATH $final_path/script_that_use_java.sh` # # # Finally, to start a java service with the correct version, 2 solutions # Either the app is dependent of java but does not call it directly. # In such situation, you need to load PATH : # ``` # Environment="__JAVA_ENV_PATH__" # ExecStart=__FINALPATH__/my_app # ``` # You will replace __JAVA_ENV_PATH__ with $ynh_java_load_PATH. # # Or java starts the app directly, then you don't need to load the PATH variable # ``` # ExecStart=__YNH_JAVA__ -jar your-app.jar # ``` # You will replace __YNH_JAVA__ with $ynh_java # # # 2 other variables are also available # - $java_path: The absolute path to java binaries for the chosen version. # - $java_version: Just the version number of java for this app. Stored as 'java_version' in settings.yml. # # Requires YunoHost version 11 or higher. ynh_use_java() { java_version=$(ynh_app_setting_get --app=$app --key=java_version) #get the architecture: amd or arm ? ynh_get_architecture # Get the absolute path of this version of java java_path="/usr/lib/jvm/java-$java_version-openjdk-$ynh_architecture/bin" # Allow alias to be used into bash script shopt -s expand_aliases # Create an alias for the specific version of node and a variable as fallback ynh_java="$java_path/java" alias ynh_java="$ynh_java" # Load the path of this version of node in $PATH if [[ :$PATH: != *":$java_path"* ]]; then PATH="$java_path:$PATH" fi java_PATH="$PATH" # Create an alias to easily load the PATH ynh_java_load_PATH="PATH=$java_PATH" # Same var but in lower case to be compatible with ynh_replace_vars... ynh_java_load_path="PATH=$java_PATH" } # Install a specific version of java # # ynh_install_java will install the version of Java provided as arguments. # # usage: ynh_install_java --java_version=java_version --jdk_type=jre --java_engine=headless # | arg: -j, --java_version= - Version of java to install: 17 (default) or 11. Use major version only. # | arg: -t, --jdk_type= - Select the type of jdk to install: jre (default) or jdk. # | arg: -e, --java_engine= - Select the engine to use: headless (default), zero, dcevm or leave empty for full # # Please look at the available Debian Openjdk packages to see the possible combinations of values. You can do so by running `apt-cache search openjdk` # No default ones are set, please use ynh_use_java to setup environment variables to ensure the version of Java is correct # # Requires YunoHost version 11 or higher. ynh_install_java() { # Use debian packages to install the requested Java version # Declare an array to define the options of this helper. local legacy_args=jte local -A args_array=([j]=java_version= [t]=jdk_type= [e]=java_engine) local java_version local jdk_type local java_engine # Manage arguments with getopts ynh_handle_getopts_args "$@" java_version="${java_version:-17}" jdk_type="${jdk_type:-jre}" java_engine="${java_engine:-}" package_name=openjdk-${java_version}-${jdk_type} if [ -n "$java_engine" ]; then package_name=${package_name}-${java_engine} fi ynh_install_app_dependencies ${package_name} # Store java_version into the config of this app ynh_app_setting_set --app=$app --key=java_version --value=$java_version ynh_app_setting_set --app=$app --key=jdk_type --value=$jdk_type ynh_app_setting_set --app=$app --key=java_engine --value=$java_engine ynh_use_java } # Extract the architecture used to run this script # sets ynh_architecture as arm64 or amd64 # ynh_get_architecture() { uname=$(uname --machine) if [[ $uname =~ aarch64 || $uname =~ arm64 ]]; then ynh_architecture="arm64" else ynh_architecture="amd64" fi }