mirror of
https://github.com/YunoHost-Apps/couchpotato_ynh.git
synced 2024-09-03 18:16:22 +02:00
Upgrade ynh_install_python
This commit is contained in:
parent
34095093d8
commit
aaecc0ed1c
2 changed files with 230 additions and 102 deletions
|
@ -63,9 +63,9 @@ ynh_secure_remove --file="$final_path"
|
||||||
#=================================================
|
#=================================================
|
||||||
# REMOVE NGINX CONFIGURATION
|
# REMOVE NGINX CONFIGURATION
|
||||||
#=================================================
|
#=================================================
|
||||||
ynh_script_progression --message="Removing nginx web server configuration..."
|
ynh_script_progression --message="Removing NGINX web server configuration..."
|
||||||
|
|
||||||
# Remove the dedicated nginx config
|
# Remove the dedicated NGINX config
|
||||||
ynh_remove_nginx_config
|
ynh_remove_nginx_config
|
||||||
|
|
||||||
#=================================================
|
#=================================================
|
||||||
|
|
|
@ -1,52 +1,91 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
# Need also the helper https://github.com/YunoHost-Apps/Experimental_helpers/blob/master/ynh_handle_getopts_args/ynh_handle_getopts_args
|
ynh_python_try_bash_extension() {
|
||||||
pyenv_version=1.2.19
|
if [ -x src/configure ]; then
|
||||||
pyenv_virtualenv_version=1.1.5
|
src/configure && make -C src || {
|
||||||
pyenv_install_dir="/opt/pyenv"
|
ynh_print_info --message="Optional bash extension failed to build, but things will still work normally."
|
||||||
|
}
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
pyenv_install_dir="/opt/pyenv"
|
||||||
|
python_version_path="$pyenv_install_dir/versions"
|
||||||
# PYENV_ROOT is the directory of pyenv, it needs to be loaded as a environment variable.
|
# PYENV_ROOT is the directory of pyenv, it needs to be loaded as a environment variable.
|
||||||
export PYENV_ROOT="$pyenv_install_dir"
|
export PYENV_ROOT="$pyenv_install_dir"
|
||||||
|
|
||||||
# Required dependencies
|
# Required dependencies
|
||||||
pyenv_dependencies="build-essential libssl1.0-dev|libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python-openssl git"
|
pyenv_dependencies="build-essential libssl1.0-dev|libssl-dev zlib1g-dev libbz2-dev libreadline-dev|libedit-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python-openssl git"
|
||||||
|
|
||||||
# Install python version management
|
# Load the version of Python for an app, and set variables.
|
||||||
#
|
#
|
||||||
# [internal]
|
# ynh_use_python has to be used in any app scripts before using Python for the first time.
|
||||||
|
# This helper will provide alias and variables to use in your scripts.
|
||||||
#
|
#
|
||||||
# usage: ynh_install_pyenv
|
# To use pip or Python, use the alias `ynh_pip` and `ynh_python`
|
||||||
ynh_install_pyenv () {
|
# Those alias will use the correct version installed for the app
|
||||||
echo "Installation of pyenv - python version management" >&2
|
# For example: use `ynh_pip install` instead of `pip install`
|
||||||
# Build an app.src for pyenv
|
#
|
||||||
mkdir -p "../conf"
|
# With `sudo` or `ynh_exec_as`, use instead the fallback variables `$ynh_pip` and `$ynh_python`
|
||||||
echo "SOURCE_URL=https://github.com/pyenv/pyenv/archive/v${pyenv_version}.tar.gz
|
# And propagate $PATH to sudo with $ynh_python_load_path
|
||||||
SOURCE_SUM=e93466735ac9c34d68b7d5d71f32c16a2b4b1a6a1adffb85acc4126a3398b9d0" > "$YNH_APP_BASEDIR/conf/pyenv.src"
|
# Exemple: `ynh_exec_as $app $ynh_python_load_path $ynh_pip install`
|
||||||
# Download and extract pyenv
|
#
|
||||||
ynh_setup_source "$pyenv_install_dir" pyenv
|
# $PATH contains the path of the requested version of Python.
|
||||||
|
# However, $PATH is duplicated into $python_path to outlast any manipulation of $PATH
|
||||||
|
# You can use the variable `$ynh_python_load_path` to quickly load your Python version
|
||||||
|
# in $PATH for an usage into a separate script.
|
||||||
|
# Exemple: $ynh_python_load_path $final_path/script_that_use_pip.sh`
|
||||||
|
#
|
||||||
|
#
|
||||||
|
# Finally, to start a Python service with the correct version, 2 solutions
|
||||||
|
# Either the app is dependent of python or pip, but does not called it directly.
|
||||||
|
# In such situation, you need to load PATH
|
||||||
|
# `Environment="__YNH_PYTHON_LOAD_ENV_PATH__"`
|
||||||
|
# `ExecStart=__FINALPATH__/my_app`
|
||||||
|
# You will replace __YNH_PYTHON_LOAD_ENV_PATH__ with $ynh_python_load_path
|
||||||
|
#
|
||||||
|
# Or Python start the app directly, then you don't need to load the PATH variable
|
||||||
|
# `ExecStart=__YNH_PYTHON__ my_app run`
|
||||||
|
# You will replace __YNH_PYTHON__ with $ynh_python
|
||||||
|
#
|
||||||
|
#
|
||||||
|
# one other variable is also available
|
||||||
|
# - $python_path: The absolute path to Python binaries for the chosen version.
|
||||||
|
#
|
||||||
|
# usage: ynh_use_python
|
||||||
|
#
|
||||||
|
# Requires YunoHost version 2.7.12 or higher.
|
||||||
|
ynh_use_python () {
|
||||||
|
python_version=$(ynh_app_setting_get --app=$app --key=python_version)
|
||||||
|
|
||||||
# Build an app.src for pyenv-virtualenv
|
# Get the absolute path of this version of Python
|
||||||
mkdir -p "../conf"
|
python_path="$python_version_path/$YNH_APP_INSTANCE_NAME/bin"
|
||||||
echo "SOURCE_URL=https://github.com/pyenv/pyenv-virtualenv/archive/v${pyenv_virtualenv_version}.tar.gz
|
|
||||||
SOURCE_SUM=27ae3de027a6f6dccdca4085225512e559c6b94b31625bd2b357a18890a1e618" > "$YNH_APP_BASEDIR/conf/pyenv-virtualenv.src"
|
|
||||||
# Download and extract pyenv-virtualenv
|
|
||||||
ynh_setup_source "$pyenv_install_dir/plugins/pyenv-virtualenv" pyenv-virtualenv
|
|
||||||
|
|
||||||
(cd $pyenv_install_dir
|
# Allow alias to be used into bash script
|
||||||
./src/configure && make -C src)
|
shopt -s expand_aliases
|
||||||
|
|
||||||
# Create shims directory if needed
|
# Create an alias for the specific version of Python and a variable as fallback
|
||||||
if [ ! -d $pyenv_install_dir/shims ] ; then
|
ynh_python="$python_path/python"
|
||||||
mkdir $pyenv_install_dir/shims
|
alias ynh_python="$ynh_python"
|
||||||
fi
|
# And pip
|
||||||
|
ynh_pip="$python_path/pip"
|
||||||
|
alias ynh_pip="$ynh_pip"
|
||||||
|
|
||||||
|
# Load the path of this version of Python in $PATH
|
||||||
|
if [[ :$PATH: != *":$python_path"* ]]; then
|
||||||
|
PATH="$python_path:$PATH"
|
||||||
|
fi
|
||||||
|
# Create an alias to easily load the PATH
|
||||||
|
ynh_python_load_path="PATH=$PATH"
|
||||||
|
|
||||||
|
# Sets the local application-specific Python version
|
||||||
|
pushd $final_path
|
||||||
|
$pyenv_install_dir/bin/pyenv local $python_version
|
||||||
|
popd
|
||||||
}
|
}
|
||||||
|
|
||||||
# Install a specific version of python
|
# Install a specific version of Python
|
||||||
#
|
#
|
||||||
# ynh_install_python will install the version of python provided as argument by using pyenv.
|
# ynh_install_python will install the version of Python provided as argument by using pyenv.
|
||||||
#
|
|
||||||
# pyenv (python version management) stores the target python version in a .python_version file created in the target folder (using pyenv local <version>)
|
|
||||||
# It then uses that information for every python user that uses pyenv provided python command
|
|
||||||
#
|
#
|
||||||
# This helper creates a /etc/profile.d/pyenv.sh that configures PATH environment for pyenv
|
# This helper creates a /etc/profile.d/pyenv.sh that configures PATH environment for pyenv
|
||||||
# for every LOGIN user, hence your user must have a defined shell (as opposed to /usr/sbin/nologin)
|
# for every LOGIN user, hence your user must have a defined shell (as opposed to /usr/sbin/nologin)
|
||||||
|
@ -56,99 +95,188 @@ fi
|
||||||
# When not possible (e.g. in systemd service definition), please use direct path
|
# When not possible (e.g. in systemd service definition), please use direct path
|
||||||
# to pyenv shims (e.g. $PYENV_ROOT/shims/bundle)
|
# to pyenv shims (e.g. $PYENV_ROOT/shims/bundle)
|
||||||
#
|
#
|
||||||
# usage: ynh_install_python python_version user
|
# usage: ynh_install_python --python_version=python_version
|
||||||
# | arg: -v, --python_version= - Version of python to install.
|
# | arg: -v, --python_version= - Version of Python to install.
|
||||||
# If possible, prefer to use major version number (e.g. 3 instead of 3.7.3).
|
#
|
||||||
# The crontab will handle the update of minor versions when needed.
|
# Requires YunoHost version 2.7.12 or higher.
|
||||||
ynh_install_python () {
|
ynh_install_python () {
|
||||||
# Declare an array to define the options of this helper.
|
# Declare an array to define the options of this helper.
|
||||||
declare -Ar args_array=( [v]=python_version= )
|
local legacy_args=v
|
||||||
# Use pyenv, https://github.com/pyenv/pyenv to manage the python versions
|
local -A args_array=( [v]=python_version= )
|
||||||
local python_version
|
local python_version
|
||||||
# Manage arguments with getopts
|
# Manage arguments with getopts
|
||||||
ynh_handle_getopts_args "$@"
|
ynh_handle_getopts_args "$@"
|
||||||
|
|
||||||
# Create $pyenv_install_dir
|
# Install required dependencies
|
||||||
mkdir -p "$pyenv_install_dir/plugins/pyenv-virtualenv"
|
ynh_add_app_dependencies --package="$pyenv_dependencies"
|
||||||
|
|
||||||
# Load pyenv path in PATH
|
# Load pyenv path in PATH
|
||||||
CLEAR_PATH="$pyenv_install_dir/bin:$PATH"
|
local CLEAR_PATH="$pyenv_install_dir/bin:$PATH"
|
||||||
|
|
||||||
# Remove /usr/local/bin in PATH in case of python prior installation
|
# Remove /usr/local/bin in PATH in case of Python prior installation
|
||||||
PATH=$(echo $CLEAR_PATH | sed 's@/usr/local/bin:@@')
|
PATH=$(echo $CLEAR_PATH | sed 's@/usr/local/bin:@@')
|
||||||
|
|
||||||
# Move an existing python binary, to avoid to block pyenv
|
# Move an existing Python binary, to avoid to block pyenv
|
||||||
#test -x /usr/bin/python && mv /usr/bin/python /usr/bin/python_pyenv
|
#test -x /usr/bin/python && mv /usr/bin/python /usr/bin/python_pyenv
|
||||||
|
|
||||||
# If pyenv is not previously setup, install it
|
# Install or update pyenv
|
||||||
if ! type pyenv > /dev/null 2>&1
|
pyenv="$(command -v pyenv $pyenv_install_dir/bin/pyenv | head -1)"
|
||||||
then
|
if [ -n "$pyenv" ]; then
|
||||||
ynh_install_pyenv
|
ynh_print_info --message="pyenv already seems installed in \`$pyenv'."
|
||||||
elif dpkg --compare-versions "$($pyenv_install_dir/bin/pyenv --version | cut -d" " -f2)" lt $pyenv_version
|
pushd "${pyenv%/*/*}"
|
||||||
then
|
if git remote -v 2>/dev/null | grep "https://github.com/pyenv/pyenv.git"; then
|
||||||
ynh_install_pyenv
|
echo "Trying to update with git..."
|
||||||
elif dpkg --compare-versions "$($pyenv_install_dir/plugins/pyenv-virtualenv/bin/pyenv-virtualenv --version | cut -d" " -f2)" lt $pyenv_virtualenv_version
|
git pull -q --tags origin master
|
||||||
then
|
cd ..
|
||||||
ynh_install_pyenv
|
ynh_python_try_bash_extension
|
||||||
fi
|
fi
|
||||||
|
popd
|
||||||
|
else
|
||||||
|
ynh_print_info --message="Installing pyenv with git..."
|
||||||
|
mkdir -p $pyenv_install_dir
|
||||||
|
pushd $pyenv_install_dir
|
||||||
|
git init -q
|
||||||
|
git remote add -f -t master origin https://github.com/pyenv/pyenv.git > /dev/null 2>&1
|
||||||
|
git checkout -q -b master origin/master
|
||||||
|
ynh_python_try_bash_extension
|
||||||
|
pyenv=$pyenv_install_dir/bin/pyenv
|
||||||
|
popd
|
||||||
|
fi
|
||||||
|
|
||||||
# Restore /usr/local/bin in PATH (if needed)
|
pyenv_virtualenv="$(command -v "$pyenv_install_dir"/plugins/*/bin/pyenv-virtualenv pyenv-virtualenv | head -1)"
|
||||||
PATH=$CLEAR_PATH
|
if [ -n "$pyenv_virtualenv" ]; then
|
||||||
|
ynh_print_info --message="\`pyenv virtualenv' command already available in \`$pyenv_virtualenv'."
|
||||||
|
pushd "${pyenv_virtualenv%/*/*}"
|
||||||
|
if git remote -v 2>/dev/null | grep "https://github.com/pyenv/pyenv-virtualenv.git"; then
|
||||||
|
ynh_print_info --message="Trying to update pyenv-virtualenv with git..."
|
||||||
|
git pull -q origin master
|
||||||
|
fi
|
||||||
|
popd
|
||||||
|
else
|
||||||
|
ynh_print_info --message="Installing pyenv-virtualenv with git..."
|
||||||
|
mkdir -p "${pyenv_install_dir}/plugins"
|
||||||
|
git clone -q https://github.com/pyenv/pyenv-virtualenv.git "${pyenv_install_dir}/plugins/pyenv-virtualenv"
|
||||||
|
fi
|
||||||
|
|
||||||
# And replace the old python binary
|
pyenv_latest="$(command -v "$pyenv_install_dir"/plugins/*/bin/pyenv-latest pyenv-latest | head -1)"
|
||||||
# test -x /usr/bin/python_pyenv && mv /usr/bin/python_pyenv /usr/bin/python
|
if [ -n "$pyenv_latest" ]; then
|
||||||
|
ynh_print_info --message="\`pyenv latest' command already available in \`$pyenv_latest'."
|
||||||
|
pushd "${pyenv_latest%/*/*}"
|
||||||
|
if git remote -v 2>/dev/null | grep "https://github.com/momo-lab/xxenv-latest.git"; then
|
||||||
|
ynh_print_info --message="Trying to update xxenv-latest with git..."
|
||||||
|
git pull -q origin master
|
||||||
|
fi
|
||||||
|
popd
|
||||||
|
else
|
||||||
|
ynh_print_info --message="Installing xxenv-latest with git..."
|
||||||
|
mkdir -p "${pyenv_install_dir}/plugins"
|
||||||
|
git clone -q https://github.com/momo-lab/xxenv-latest.git "${pyenv_install_dir}/plugins/xxenv-latest"
|
||||||
|
fi
|
||||||
|
|
||||||
# Install required dependencies
|
# Enable caching
|
||||||
ynh_add_app_dependencies --package="$pyenv_dependencies"
|
mkdir -p "${pyenv_install_dir}/cache"
|
||||||
|
|
||||||
# Install the requested version of python
|
# Create shims directory if needed
|
||||||
pyenv install --skip-existing $python_version
|
mkdir -p "${pyenv_install_dir}/shims"
|
||||||
|
|
||||||
# Store the ID of this app and the version of python requested for it
|
# Restore /usr/local/bin in PATH
|
||||||
echo "$YNH_APP_ID:$python_version" | tee --append "$pyenv_install_dir/ynh_app_version"
|
PATH=$CLEAR_PATH
|
||||||
|
|
||||||
# Store python_version into the config of this app
|
# And replace the old Python binary
|
||||||
ynh_app_setting_set --app=$app --key=python_version --value=$python_version
|
# test -x /usr/bin/python_pyenv && mv /usr/bin/python_pyenv /usr/bin/python
|
||||||
|
|
||||||
# Set environment for python users
|
# Install the requested version of Python
|
||||||
echo "#pyenv
|
local final_python_version=$(pyenv latest --print $python_version)
|
||||||
|
ynh_print_info --message="Installation of Python-$python_version"
|
||||||
|
pyenv install --skip-existing $final_python_version > /dev/null 2>&1
|
||||||
|
|
||||||
|
# Store python_version into the config of this app
|
||||||
|
ynh_app_setting_set --app=$YNH_APP_INSTANCE_NAME --key=python_version --value=$python_version
|
||||||
|
|
||||||
|
# Remove app virtualenv
|
||||||
|
if `pyenv virtualenvs | grep --quiet "$YNH_APP_INSTANCE_NAME " 1>/dev/null 2>&1`
|
||||||
|
then
|
||||||
|
pyenv virtualenv-delete --force $YNH_APP_INSTANCE_NAME
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Create app virtualenv
|
||||||
|
pyenv virtualenv --force $python_version $YNH_APP_INSTANCE_NAME
|
||||||
|
|
||||||
|
# Cleanup Python versions
|
||||||
|
ynh_cleanup_python
|
||||||
|
|
||||||
|
# Set environment for Python users
|
||||||
|
echo "#pyenv
|
||||||
export PYENV_ROOT=$pyenv_install_dir
|
export PYENV_ROOT=$pyenv_install_dir
|
||||||
export PATH=\"$pyenv_install_dir/bin:$PATH\"
|
export PATH=\"$pyenv_install_dir/bin:$PATH\"
|
||||||
eval \"\$(pyenv init -)\"
|
eval \"\$(pyenv init -)\"
|
||||||
#pyenv" > /etc/profile.d/pyenv.sh
|
#pyenv" > /etc/profile.d/pyenv.sh
|
||||||
|
|
||||||
# Load the right environment for the Installation
|
# Load the environment
|
||||||
eval "$(pyenv init -)"
|
eval "$(pyenv init -)"
|
||||||
|
|
||||||
(cd $final_path
|
|
||||||
pyenv local $python_version)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Remove the version of python used by the app.
|
# Remove the version of Python used by the app.
|
||||||
#
|
#
|
||||||
# This helper will check if another app uses the same version of python,
|
# This helper will also cleanup Python versions
|
||||||
# if not, this version of python will be removed.
|
|
||||||
# If no other app uses python, pyenv will be also removed.
|
|
||||||
#
|
#
|
||||||
# usage: ynh_remove_python
|
# usage: ynh_remove_python
|
||||||
ynh_remove_python () {
|
ynh_remove_python () {
|
||||||
python_version=$(ynh_app_setting_get --app=$app --key=python_version)
|
local python_version=$(ynh_app_setting_get --app=$YNH_APP_INSTANCE_NAME --key=python_version)
|
||||||
|
|
||||||
# Remove the line for this app
|
# Load pyenv path in PATH
|
||||||
sed --in-place "/$YNH_APP_ID:$python_version/d" "$pyenv_install_dir/ynh_app_version"
|
local CLEAR_PATH="$pyenv_install_dir/bin:$PATH"
|
||||||
|
|
||||||
# If no other app uses this version of python, remove it.
|
# Remove /usr/local/bin in PATH in case of Python prior installation
|
||||||
if ! grep --quiet "$python_version" "$pyenv_install_dir/ynh_app_version"
|
PATH=$(echo $CLEAR_PATH | sed 's@/usr/local/bin:@@')
|
||||||
then
|
|
||||||
$pyenv_install_dir/bin/pyenv uninstall --force $python_version
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Remove pyenv environment configuration
|
pyenv virtualenv-delete --force $YNH_APP_INSTANCE_NAME
|
||||||
rm /etc/profile.d/pyenv.sh
|
|
||||||
|
|
||||||
# If no other app uses pyenv, remove pyenv and dedicated group
|
# Remove the line for this app
|
||||||
if [ ! -s "$pyenv_install_dir/ynh_app_version" ]
|
ynh_app_setting_delete --app=$YNH_APP_INSTANCE_NAME --key=python_version
|
||||||
then
|
|
||||||
ynh_secure_remove --file="$pyenv_install_dir"
|
# Cleanup Python versions
|
||||||
fi
|
ynh_cleanup_python
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Remove no more needed versions of Python used by the app.
|
||||||
|
#
|
||||||
|
# This helper will check what Python version are no more required,
|
||||||
|
# and uninstall them
|
||||||
|
# If no app uses Python, pyenv will be also removed.
|
||||||
|
#
|
||||||
|
# usage: ynh_cleanup_python
|
||||||
|
ynh_cleanup_python () {
|
||||||
|
|
||||||
|
# List required Python version
|
||||||
|
local installed_apps=$(yunohost app list | grep -oP 'id: \K.*$')
|
||||||
|
local required_python_versions=""
|
||||||
|
for installed_app in $installed_apps
|
||||||
|
do
|
||||||
|
local installed_app_python_version=$(ynh_app_setting_get --app=$installed_app --key="python_version")
|
||||||
|
if [[ $installed_app_python_version ]]
|
||||||
|
then
|
||||||
|
required_python_versions="${installed_app_python_version}\n${required_python_versions}"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
# Remove no more needed Python version
|
||||||
|
local installed_python_versions=$(pyenv versions --bare --skip-aliases | grep -Ev '/')
|
||||||
|
for installed_python_version in $installed_python_versions
|
||||||
|
do
|
||||||
|
if ! `echo ${required_python_versions} | grep "${installed_python_version}" 1>/dev/null 2>&1`
|
||||||
|
then
|
||||||
|
ynh_print_info --message="Removing of Python-$installed_python_version"
|
||||||
|
$pyenv_install_dir/bin/pyenv uninstall --force $installed_python_version
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
# If none Python version is required
|
||||||
|
if [[ ! $required_python_versions ]]
|
||||||
|
then
|
||||||
|
# Remove pyenv environment configuration
|
||||||
|
ynh_print_info --message="Removing of pyenv-$pyenv_version"
|
||||||
|
ynh_secure_remove --file="$pyenv_install_dir"
|
||||||
|
ynh_secure_remove --file="/etc/profile.d/pyenv.sh"
|
||||||
|
fi
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue