From 4d3cec57b141965c889808f85768ea93b598a91d Mon Sep 17 00:00:00 2001 From: Kay0u Date: Wed, 3 Mar 2021 01:06:52 +0100 Subject: [PATCH 01/27] fix upgrade script --- scripts/upgrade | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/upgrade b/scripts/upgrade index 6ddfcb0..46dfb32 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -18,6 +18,7 @@ app=$YNH_APP_INSTANCE_NAME domain=$(ynh_app_setting_get --app=$app --key=domain) path_url=$(ynh_app_setting_get --app=$app --key=path) +final_path=$(ynh_app_setting_get --app=$app --key=final_path) #================================================= # CHECK VERSION From 8494485fce404f822c0696324b63f54d3c652fd9 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Fri, 5 Mar 2021 05:00:42 +0100 Subject: [PATCH 02/27] Implement ynh_install_python --- README.md | 3 - scripts/_common.sh | 2 + scripts/backup | 3 - scripts/change_url | 1 + scripts/install | 29 ++-- scripts/remove | 5 +- scripts/restore | 30 ++++- scripts/upgrade | 44 ++++++- scripts/ynh_install_python | 264 +++++++++++++++++++++++++++++++++++++ 9 files changed, 354 insertions(+), 27 deletions(-) create mode 100644 scripts/ynh_install_python diff --git a/README.md b/README.md index 5cab32b..140c5a2 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,6 @@ If you don't have YunoHost, please consult [the guide](https://yunohost.org/#/install) to learn how to install it.* ## Overview - Pepettes is a donation form based on Stripe. **Shipped version:** 1.0 @@ -54,7 +53,6 @@ Can the app be used by multiple users? yes ## Links * Report a bug: https://github.com/YunoHost-Apps/pepettes_ynh/issues - * App website: - * Upstream app repository: https://github.com/YunoHost/pepettes/ * YunoHost website: https://yunohost.org/ @@ -62,7 +60,6 @@ Can the app be used by multiple users? yes ## Developer info -**Only if you want to use a testing branch for coding, instead of merging directly into master.** Please send your pull request to the [testing branch](https://github.com/YunoHost-Apps/pepettes_ynh/tree/testing). To try the testing branch, please proceed like that. diff --git a/scripts/_common.sh b/scripts/_common.sh index bc48e71..ea69553 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -7,6 +7,8 @@ # dependencies used by the app pkg_dependencies="python3-venv python3-dev python3-pip" +python_version=3.9.2 + #================================================= # PERSONAL HELPERS #================================================= diff --git a/scripts/backup b/scripts/backup index 1400566..3beb939 100755 --- a/scripts/backup +++ b/scripts/backup @@ -15,7 +15,6 @@ source /usr/share/yunohost/helpers #================================================= ynh_clean_setup () { - ### Remove this function if there's nothing to clean before calling the remove script. true } # Exit if an error occurs during the execution of the script @@ -51,8 +50,6 @@ ynh_backup --src_path="/etc/nginx/conf.d/$domain.d/$app.conf" #================================================= # SPECIFIC BACKUP -#================================================= - #================================================= # BACKUP SYSTEMD #================================================= diff --git a/scripts/change_url b/scripts/change_url index 17802c4..04784e9 100644 --- a/scripts/change_url +++ b/scripts/change_url @@ -118,6 +118,7 @@ fi #================================================= ynh_script_progression --message="Starting a systemd service..." --weight=1 +# Start a systemd service ynh_systemd_action --service_name=$app --action="start" --log_path="/var/log/$app/$app.log" #================================================= diff --git a/scripts/install b/scripts/install index 1e50915..facc68b 100755 --- a/scripts/install +++ b/scripts/install @@ -7,6 +7,7 @@ #================================================= source _common.sh +source ynh_install_python source /usr/share/yunohost/helpers #================================================= @@ -14,7 +15,6 @@ source /usr/share/yunohost/helpers #================================================= ynh_clean_setup () { - ### Remove this function if there's nothing to clean before calling the remove script. true } # Exit if an error occurs during the execution of the script @@ -84,6 +84,7 @@ ynh_app_setting_set --app=$app --key=port --value=$port ynh_script_progression --message="Installing dependencies..." --weight=1 ynh_install_app_dependencies $pkg_dependencies +ynh_install_python --python_version=$python_version #================================================= # DOWNLOAD, CHECK AND UNPACK SOURCE @@ -113,20 +114,23 @@ ynh_system_user_create --username=$app #================================================= # SPECIFIC SETUP #================================================= +# INSTALL PYTHON DEPENDENCIES +#================================================= +ynh_script_progression --message="Installing Python dependencies..." + pushd $final_path -python3 -m venv venv -venv/bin/pip install -r requirements.txt -venv/bin/pip install gunicorn -mkdir -p /var/log/$app -chown -R $app:www-data /var/log/$app -cat <> wsgi.py + ynh_use_python + $ynh_pip install -r requirements.txt + $ynh_pip install gunicorn + mkdir -p /var/log/$app + chown -R $app:www-data /var/log/$app + cat <> wsgi.py from server import app if __name__ == "__main__": app.run() EOF popd -#================================================= #================================================= # SETUP SYSTEMD @@ -139,17 +143,11 @@ ynh_add_systemd_config #================================================= # MODIFY A CONFIG FILE #================================================= +ynh_script_progression --message="Modifying a config file..." ynh_add_config --template="../conf/gunicorn.py" --destination="$final_path/gunicorn.py" ynh_add_config --template="../conf/settings.py" --destination="$final_path/settings.py" -#================================================= -# STORE THE CONFIG FILE CHECKSUM -#================================================= - -# Calculate and store the config file checksum into the app settings -ynh_store_file_checksum --file="$final_path/gunicorn.py" -ynh_store_file_checksum --file="$final_path/settings.py" for price in $(echo $prices | sed "s/,/ /"); do frequency=$(echo $price | cut -d/ -f1) currency=$(echo $price | cut -d/ -f2) @@ -162,6 +160,7 @@ done #================================================= # SECURE FILES AND DIRECTORIES #================================================= +ynh_script_progression --message="Securing files and directories..." # Set permissions to app files chown -R root: $final_path diff --git a/scripts/remove b/scripts/remove index a2cf263..feedb2a 100755 --- a/scripts/remove +++ b/scripts/remove @@ -7,6 +7,7 @@ #================================================= source _common.sh +source ynh_install_python source /usr/share/yunohost/helpers #================================================= @@ -47,6 +48,7 @@ ynh_remove_systemd_config ynh_script_progression --message="Removing dependencies..." --weight=1 # Remove metapackage and its dependencies +ynh_remove_python ynh_remove_app_dependencies #================================================= @@ -70,9 +72,10 @@ ynh_remove_nginx_config #================================================= # REMOVE VARIOUS FILES #================================================= +ynh_script_progression --message="Removing various files..." # Remove the log files -#ynh_secure_remove --file="/var/log/$app/" +ynh_secure_remove --file="/var/log/$app" #================================================= # GENERIC FINALIZATION diff --git a/scripts/restore b/scripts/restore index a8fc3ba..2fe5759 100755 --- a/scripts/restore +++ b/scripts/restore @@ -8,6 +8,7 @@ # Keep this path for calling _common.sh inside the execution's context of backup and restore scripts source ../settings/scripts/_common.sh +source ../settings/scripts/ynh_install_python source /usr/share/yunohost/helpers #================================================= @@ -15,7 +16,6 @@ source /usr/share/yunohost/helpers #================================================= ynh_clean_setup () { - #### Remove this function if there's nothing to clean before calling the remove script. true } # Exit if an error occurs during the execution of the script @@ -24,7 +24,7 @@ ynh_abort_if_errors #================================================= # LOAD SETTINGS #================================================= -ynh_script_progression --message="Loading installation settings..." --weight=1 +ynh_script_progression --message="Loading settings..." --weight=1 app=$YNH_APP_INSTANCE_NAME @@ -47,6 +47,7 @@ test ! -d $final_path \ #================================================= # RESTORE THE NGINX CONFIGURATION #================================================= +ynh_script_progression --message="Restoring the NGINX web server configuration..." ynh_restore_file --origin_path="/etc/nginx/conf.d/$domain.d/$app.conf" @@ -68,6 +69,7 @@ ynh_system_user_create --username=$app #================================================= # RESTORE USER RIGHTS #================================================= +ynh_script_progression --message="Restoring user rights..." # Restore permissions on app files chown -R root: $final_path @@ -81,6 +83,26 @@ ynh_script_progression --message="Reinstalling dependencies..." --weight=1 # Define and install dependencies ynh_install_app_dependencies $pkg_dependencies +ynh_install_python --python_version=$python_version + +#================================================= +# INSTALL PYTHON DEPENDENCIES +#================================================= +ynh_script_progression --message="Installing Python dependencies..." + +pushd $final_path + ynh_use_python + $ynh_pip install -r requirements.txt + $ynh_pip install gunicorn + mkdir -p /var/log/$app + chown -R $app:www-data /var/log/$app + cat <> wsgi.py +from server import app + +if __name__ == "__main__": + app.run() +EOF +popd #================================================= # RESTORE SYSTEMD @@ -107,9 +129,9 @@ ynh_systemd_action --service_name=$app --action="start" --log_path="/var/log/$ap #================================================= # GENERIC FINALIZATION #================================================= -# RELOAD NGINX AND PHP-FPM +# RELOAD NGINX #================================================= -ynh_script_progression --message="Reloading NGINX web server and PHP-FPM..." --weight=1 +ynh_script_progression --message="Reloading NGINX web server..." --weight=1 ynh_systemd_action --service_name=nginx --action=reload diff --git a/scripts/upgrade b/scripts/upgrade index 46dfb32..9025f28 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -7,6 +7,7 @@ #================================================= source _common.sh +source ynh_install_python source /usr/share/yunohost/helpers #================================================= @@ -20,9 +21,20 @@ domain=$(ynh_app_setting_get --app=$app --key=domain) path_url=$(ynh_app_setting_get --app=$app --key=path) final_path=$(ynh_app_setting_get --app=$app --key=final_path) +project_name=$(ynh_app_setting_get --app=$app --key=project_name) +contact_url=$(ynh_app_setting_get --app=$app --key=contact_url) +logo=$(ynh_app_setting_get --app=$app --key=logo) +favicon=$(ynh_app_setting_get --app=$app --key=favicon) +publishable_key=$(ynh_app_setting_get --app=$app --key=publishable_key) +secret_key=$(ynh_app_setting_get --app=$app --key=secret_key) +prices=$(ynh_app_setting_get --app=$app --key=prices) +secret=$(ynh_app_setting_get --app=$app --key=secret) +csrf_key=$(ynh_app_setting_get --app=$app --key=csrf_key) + #================================================= # CHECK VERSION #================================================= +ynh_script_progression --message="Checking version..." upgrade_type=$(ynh_check_app_version_changed) @@ -80,6 +92,7 @@ ynh_add_nginx_config ynh_script_progression --message="Upgrading dependencies..." --weight=1 ynh_install_app_dependencies $pkg_dependencies +ynh_install_python --python_version=$python_version #================================================= # CREATE DEDICATED USER @@ -92,8 +105,23 @@ ynh_system_user_create --username=$app #================================================= # SPECIFIC UPGRADE #================================================= -# ... +# UPGRADE PYTHON DEPENDENCIES #================================================= +ynh_script_progression --message="Installing Python dependencies..." + +pushd $final_path + ynh_use_python + $ynh_pip install -r requirements.txt + $ynh_pip install gunicorn + mkdir -p /var/log/$app + chown -R $app:www-data /var/log/$app + cat <> wsgi.py +from server import app + +if __name__ == "__main__": + app.run() +EOF +popd #================================================= # SETUP SYSTEMD @@ -106,16 +134,30 @@ ynh_add_systemd_config #================================================= # MODIFY A CONFIG FILE #================================================= +ynh_script_progression --message="Modifying a config file..." +ynh_add_config --template="../conf/gunicorn.py" --destination="$final_path/gunicorn.py" +ynh_add_config --template="../conf/settings.py" --destination="$final_path/settings.py" + +for price in $(echo $prices | sed "s/,/ /"); do + frequency=$(echo $price | cut -d/ -f1) + currency=$(echo $price | cut -d/ -f2) + price_id=$(echo $price | cut -d/ -f3) + echo "DONATION['$frequency']['$currency'] = '$price_id'" >> "$final_path/settings.py" +done #================================================= # GENERIC FINALIZATION #================================================= # SECURE FILES AND DIRECTORIES #================================================= +ynh_script_progression --message="Securing files and directories..." # Set permissions on app files chown -R root: $final_path +chown pepettes:root $final_path +chown pepettes:root $final_path/settings.py +chmod o=--- $final_path/settings.py #================================================= # INTEGRATE SERVICE IN YUNOHOST diff --git a/scripts/ynh_install_python b/scripts/ynh_install_python new file mode 100644 index 0000000..910aaa2 --- /dev/null +++ b/scripts/ynh_install_python @@ -0,0 +1,264 @@ +#!/bin/bash + +pyenv_version=1.2.23 +pyenv_virtualenv_version=1.1.5 +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. +export PYENV_ROOT="$pyenv_install_dir" + +# 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" + +# Install Python Version Management +# +# [internal] +# +# usage: ynh_install_pyenv +# +# Requires YunoHost version 2.7.12 or higher. +ynh_install_pyenv () { + ynh_print_info --message="Installation of pyenv - Python Version Management - pyenv-$pyenv_version/pyenv-virtualenv-$pyenv_virtualenv_version" + + # Build an app.src for pyenv + mkdir -p "../conf" + echo "SOURCE_URL=https://github.com/pyenv/pyenv/archive/v${pyenv_version}.tar.gz +SOURCE_SUM=805058aa5ce257157fb4769543e6a43bac45a88c723ff3c4fcf5b4f759056bf5" > "../conf/pyenv.src" + # Download and extract pyenv + ynh_setup_source --dest_dir="$pyenv_install_dir" --source_id=pyenv + + # Build an app.src for pyenv-virtualenv + mkdir -p "../conf" + echo "SOURCE_URL=https://github.com/pyenv/pyenv-virtualenv/archive/v${pyenv_virtualenv_version}.tar.gz +SOURCE_SUM=27ae3de027a6f6dccdca4085225512e559c6b94b31625bd2b357a18890a1e618" > "../conf/pyenv-virtualenv.src" + # Download and extract pyenv-virtualenv + ynh_setup_source --dest_dir="$pyenv_install_dir/plugins/pyenv-virtualenv" --source_id=pyenv-virtualenv + + (cd $pyenv_install_dir + ./src/configure && make -C src) + +# Create shims directory if needed +if [ ! -d $pyenv_install_dir/shims ] ; then + mkdir $pyenv_install_dir/shims +fi +} + +# Load the version of python for an app, and set variables. +# +# 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. +# +# To use pip or python, use the alias `ynh_pip` and `ynh_python` +# Those alias will use the correct version installed for the app +# For example: use `ynh_pip install` instead of `pip install` +# +# With `sudo` or `ynh_exec_as`, use instead the fallback variables `$ynh_pip` and `$ynh_python` +# And propagate $PATH to sudo with $ynh_python_load_path +# Exemple: `ynh_exec_as $app $ynh_python_load_path $ynh_pip install` +# +# $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 () { + # Get the absolute path of this version of python + python_path="$python_version_path/$YNH_APP_INSTANCE_NAME/bin" + + # Allow alias to be used into bash script + shopt -s expand_aliases + + # Create an alias for the specific version of python and a variable as fallback + ynh_python="$python_path/python" + alias ynh_python="$ynh_python" + # 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 + python_path="$PATH" + # Create an alias to easily load the PATH + ynh_python_load_path="PATH=$python_path" + + # Sets the local application-specific Python version + (cd $final_path + pyenv local $python_version) +} + +# Install a specific version of Python +# +# 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 ) +# 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 +# for every LOGIN user, hence your user must have a defined shell (as opposed to /usr/sbin/nologin) +# +# Don't forget to execute python-dependent command in a login environment +# (e.g. sudo --login option) +# When not possible (e.g. in systemd service definition), please use direct path +# to pyenv shims (e.g. $PYENV_ROOT/shims/bundle) +# +# usage: ynh_install_python --python_version=python_version +# | arg: -v, --python_version= - Version of Python to install. +# +# Requires YunoHost version 2.7.12 or higher. +ynh_install_python () { + # Declare an array to define the options of this helper. + local legacy_args=v + local -A args_array=( [v]=python_version= ) + local python_version + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + # Store python_version into the config of this app + ynh_app_setting_set --app=$YNH_APP_INSTANCE_NAME --key=python_version --value=$python_version + + # Create $pyenv_install_dir if doesn't exist already + mkdir -p "$pyenv_install_dir/plugins/pyenv-virtualenv" + + # Install required dependencies + ynh_add_app_dependencies --package="$pyenv_dependencies" + + # Load pyenv path in PATH + local CLEAR_PATH="$pyenv_install_dir/bin:$PATH" + + # Remove /usr/local/bin in PATH in case of Python prior installation + PATH=$(echo $CLEAR_PATH | sed 's@/usr/local/bin:@@') + + # Move an existing Python binary, to avoid to block pyenv + #test -x /usr/bin/python && mv /usr/bin/python /usr/bin/python_pyenv + + # If pyenv is not previously setup, install it + if ! type pyenv > /dev/null 2>&1 + then + ynh_install_pyenv + elif dpkg --compare-versions "$($pyenv_install_dir/bin/pyenv --version | cut -d" " -f2)" lt $pyenv_version + then + ynh_install_pyenv + elif dpkg --compare-versions "$($pyenv_install_dir/bin/pyenv virtualenv --version | cut -d" " -f2)" lt $pyenv_virtualenv_version + then + ynh_install_pyenv + fi + + # Restore /usr/local/bin in PATH + PATH=$CLEAR_PATH + + # And replace the old Python binary + # test -x /usr/bin/python_pyenv && mv /usr/bin/python_pyenv /usr/bin/python + + # Install the requested version of Python + ynh_print_info --message="Installation of Python-"$python_version + pyenv install --skip-existing $python_version &>/dev/null + + # Remove app virtualenv + if $(pyenv virtualenvs | grep $YNH_APP_INSTANCE_NAME) + 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 PATH=\"$pyenv_install_dir/bin:$PATH\" +eval \"\$(pyenv init -)\" +#pyenv" > /etc/profile.d/pyenv.sh + + # Load the environment + eval "$(pyenv init -)" +} + +# Remove the version of Python used by the app. +# +# This helper will also cleanup Python versions +# +# usage: ynh_remove_python +ynh_remove_python () { + local python_version=$(ynh_app_setting_get --app=$YNH_APP_INSTANCE_NAME --key=python_version) + + # Load pyenv path in PATH + local CLEAR_PATH="$pyenv_install_dir/bin:$PATH" + + # Remove /usr/local/bin in PATH in case of python prior installation + PATH=$(echo $CLEAR_PATH | sed 's@/usr/local/bin:@@') + + pyenv virtualenv-delete --force $YNH_APP_INSTANCE_NAME + + # Remove the line for this app + ynh_app_setting_delete --app=$YNH_APP_INSTANCE_NAME --key=python_version + + # Cleanup Python versions + 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=$(yunohost app setting $installed_app 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" + rm /etc/profile.d/pyenv.sh + fi +} \ No newline at end of file From 796149b39bab6f8b8352378fda8c3fb9ad961335 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Fri, 5 Mar 2021 05:09:28 +0100 Subject: [PATCH 03/27] fixes --- scripts/_common.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/_common.sh b/scripts/_common.sh index ea69553..b339122 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -5,9 +5,9 @@ #================================================= # dependencies used by the app -pkg_dependencies="python3-venv python3-dev python3-pip" +pkg_dependencies="curl" -python_version=3.9.2 +python_version=3.7.3 #================================================= # PERSONAL HELPERS From f9084fa91c60546b61fa74d35434bcecff677f35 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Fri, 5 Mar 2021 10:41:39 +0100 Subject: [PATCH 04/27] Fixes --- conf/gunicorn.py | 2 +- conf/systemd.service | 4 ++-- manifest.json | 10 ---------- scripts/install | 7 ++++--- scripts/restore | 1 + scripts/upgrade | 3 ++- scripts/ynh_install_python | 3 +-- 7 files changed, 11 insertions(+), 19 deletions(-) diff --git a/conf/gunicorn.py b/conf/gunicorn.py index 4c88dda..8af01f8 100644 --- a/conf/gunicorn.py +++ b/conf/gunicorn.py @@ -1,4 +1,4 @@ -command = '__FINALPATH__/venv/bin/gunicorn' +command = '__PYTHON_PATH__/gunicorn' pythonpath = '__FINALPATH__' workers = 4 user = '__NAME__' diff --git a/conf/systemd.service b/conf/systemd.service index 746d3db..29f9e3b 100644 --- a/conf/systemd.service +++ b/conf/systemd.service @@ -1,5 +1,5 @@ [Unit] -Description=Small description of the service +Description=__APP__ Daemon After=network.target [Service] @@ -7,7 +7,7 @@ PIDFile=/run/gunicorn/__APP__-pid User=__APP__ Group=__APP__ WorkingDirectory=__FINALPATH__ -ExecStart=__FINALPATH__/venv/bin/gunicorn -c __FINALPATH__/gunicorn.py wsgi:app +ExecStart=__PYTHON_PATH__/gunicorn -c __FINALPATH__/gunicorn.py wsgi:app ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s TERM $MAINPID PrivateTmp=true diff --git a/manifest.json b/manifest.json index 3077a93..6d5d8ac 100644 --- a/manifest.json +++ b/manifest.json @@ -32,16 +32,6 @@ }, "example": "example.com" }, - { - "name": "path", - "type": "path", - "ask": { - "en": "Choose a path for pepettes", - "fr": "Choisissez un chemin pour pepettes" - }, - "example": "/donation", - "default": "/donation" - }, { "name": "is_public", "type": "boolean", diff --git a/scripts/install b/scripts/install index facc68b..75c0d19 100755 --- a/scripts/install +++ b/scripts/install @@ -25,7 +25,7 @@ ynh_abort_if_errors #================================================= domain=$YNH_APP_ARG_DOMAIN -path_url=$YNH_APP_ARG_PATH +path_url="/" is_public=$YNH_APP_ARG_IS_PUBLIC project_name=$YNH_APP_ARG_PROJECT_NAME contact_url=$YNH_APP_ARG_CONTACT_URL @@ -120,6 +120,7 @@ ynh_script_progression --message="Installing Python dependencies..." pushd $final_path ynh_use_python + $ynh_pip install --upgrade pip $ynh_pip install -r requirements.txt $ynh_pip install gunicorn mkdir -p /var/log/$app @@ -138,7 +139,7 @@ popd ynh_script_progression --message="Configuring a systemd service..." --weight=1 # Create a dedicated systemd config -ynh_add_systemd_config +ynh_add_systemd_config --others_var="python_path" #================================================= # MODIFY A CONFIG FILE @@ -181,7 +182,7 @@ yunohost service add $app --description="A simple donation form" --log="/var/log ynh_script_progression --message="Starting a systemd service..." --weight=1 # Start a systemd service -ynh_systemd_action --service_name=$app --action="start" --log_path="/var/log/$app/$app.log" +ynh_systemd_action --service_name=$app --action="start" --log_path="systemd" #================================================= # SETUP SSOWAT diff --git a/scripts/restore b/scripts/restore index 2fe5759..4d0c4c5 100755 --- a/scripts/restore +++ b/scripts/restore @@ -92,6 +92,7 @@ ynh_script_progression --message="Installing Python dependencies..." pushd $final_path ynh_use_python + $ynh_pip install --upgrade pip $ynh_pip install -r requirements.txt $ynh_pip install gunicorn mkdir -p /var/log/$app diff --git a/scripts/upgrade b/scripts/upgrade index 9025f28..a122c90 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -111,6 +111,7 @@ ynh_script_progression --message="Installing Python dependencies..." pushd $final_path ynh_use_python + $ynh_pip install --upgrade pip $ynh_pip install -r requirements.txt $ynh_pip install gunicorn mkdir -p /var/log/$app @@ -129,7 +130,7 @@ popd ynh_script_progression --message="Upgrading systemd configuration..." --weight=1 # Create a dedicated systemd config -ynh_add_systemd_config +ynh_add_systemd_config --others_var="python_path" #================================================= # MODIFY A CONFIG FILE diff --git a/scripts/ynh_install_python b/scripts/ynh_install_python index 910aaa2..aa5d286 100644 --- a/scripts/ynh_install_python +++ b/scripts/ynh_install_python @@ -99,9 +99,8 @@ ynh_use_python () { if [[ :$PATH: != *":$python_path"* ]]; then PATH="$python_path:$PATH" fi - python_path="$PATH" # Create an alias to easily load the PATH - ynh_python_load_path="PATH=$python_path" + ynh_python_load_path="PATH=$PATH" # Sets the local application-specific Python version (cd $final_path From a4ae7956cd4a415a1d3d13190d688cb678c4b862 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Fri, 5 Mar 2021 10:43:16 +0100 Subject: [PATCH 05/27] Update check_process --- check_process | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/check_process b/check_process index af20c83..3c806e3 100644 --- a/check_process +++ b/check_process @@ -12,7 +12,7 @@ prices="one_time/EUR/price_1IKuPV,recuring/EUR/price_1IKuPV" ; Checks pkg_linter=1 - setup_sub_dir=1 + setup_sub_dir=0 setup_root=1 setup_nourl=0 setup_private=1 From eed68449709b1c1305844bd12ebb044232cd1654 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Fri, 5 Mar 2021 13:27:07 +0100 Subject: [PATCH 06/27] Fix services --- scripts/change_url | 4 ++-- scripts/install | 4 ++-- scripts/restore | 4 ++-- scripts/upgrade | 6 +++--- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/scripts/change_url b/scripts/change_url index 04784e9..cf801f6 100644 --- a/scripts/change_url +++ b/scripts/change_url @@ -74,7 +74,7 @@ fi #================================================= ynh_script_progression --message="Stopping a systemd service..." --weight=1 -ynh_systemd_action --service_name=$app --action="stop" --log_path="/var/log/$app/$app.log" +ynh_systemd_action --service_name=$app --action="stop" --log_path="systemd" --line_match="Stopped" #================================================= # MODIFY URL IN NGINX CONF @@ -119,7 +119,7 @@ fi ynh_script_progression --message="Starting a systemd service..." --weight=1 # Start a systemd service -ynh_systemd_action --service_name=$app --action="start" --log_path="/var/log/$app/$app.log" +ynh_systemd_action --service_name=$app --action="start" --log_path="systemd" --line_match="Started" #================================================= # RELOAD NGINX diff --git a/scripts/install b/scripts/install index 75c0d19..8430a0f 100755 --- a/scripts/install +++ b/scripts/install @@ -174,7 +174,7 @@ chmod o=--- $final_path/settings.py #================================================= ynh_script_progression --message="Integrating service in YunoHost..." --weight=1 -yunohost service add $app --description="A simple donation form" --log="/var/log/$app/$app.log" +yunohost service add $app --description="A simple donation form" --log_type="systemd" #================================================= # START SYSTEMD SERVICE @@ -182,7 +182,7 @@ yunohost service add $app --description="A simple donation form" --log="/var/log ynh_script_progression --message="Starting a systemd service..." --weight=1 # Start a systemd service -ynh_systemd_action --service_name=$app --action="start" --log_path="systemd" +ynh_systemd_action --service_name=$app --action="start" --log_path="systemd" --line_match="Started" #================================================= # SETUP SSOWAT diff --git a/scripts/restore b/scripts/restore index 4d0c4c5..dfc084c 100755 --- a/scripts/restore +++ b/scripts/restore @@ -118,14 +118,14 @@ systemctl enable $app.service --quiet #================================================= ynh_script_progression --message="Integrating service in YunoHost..." --weight=1 -yunohost service add $app --description="A simple donation form" --log="/var/log/$app/$app.log" +yunohost service add $app --description="A simple donation form" --log_type="systemd" #================================================= # START SYSTEMD SERVICE #================================================= ynh_script_progression --message="Starting a systemd service..." --weight=1 -ynh_systemd_action --service_name=$app --action="start" --log_path="/var/log/$app/$app.log" +ynh_systemd_action --service_name=$app --action="start" --log_path="systemd" --line_match="Started" #================================================= # GENERIC FINALIZATION diff --git a/scripts/upgrade b/scripts/upgrade index a122c90..719c018 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -64,7 +64,7 @@ ynh_abort_if_errors #================================================= ynh_script_progression --message="Stopping a systemd service..." --weight=1 -ynh_systemd_action --service_name=$app --action="stop" --log_path="/var/log/$app/$app.log" +ynh_systemd_action --service_name=$app --action="stop" --log_path="systemd" --line_match="Stopped" #================================================= # DOWNLOAD, CHECK AND UNPACK SOURCE @@ -165,14 +165,14 @@ chmod o=--- $final_path/settings.py #================================================= ynh_script_progression --message="Integrating service in YunoHost..." --weight=1 -yunohost service add $app --description="A simple donation form" --log="/var/log/$app/$app.log" +yunohost service add $app --description="A simple donation form" --log_type="systemd" #================================================= # START SYSTEMD SERVICE #================================================= ynh_script_progression --message="Starting a systemd service..." --weight=1 -ynh_systemd_action --service_name=$app --action="start" --log_path="/var/log/$app/$app.log" +ynh_systemd_action --service_name=$app --action="start" --log_path="systemd" --line_match="Started" #================================================= # RELOAD NGINX From 44f221ef7f214570f6f15b2271d3107a85f85f3c Mon Sep 17 00:00:00 2001 From: yalh76 Date: Fri, 5 Mar 2021 13:36:42 +0100 Subject: [PATCH 07/27] Fixing upgrade missing $port --- scripts/upgrade | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/upgrade b/scripts/upgrade index 719c018..0bbe6be 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -20,6 +20,7 @@ app=$YNH_APP_INSTANCE_NAME domain=$(ynh_app_setting_get --app=$app --key=domain) path_url=$(ynh_app_setting_get --app=$app --key=path) final_path=$(ynh_app_setting_get --app=$app --key=final_path) +port=$(ynh_app_setting_get --app=$app --key=port) project_name=$(ynh_app_setting_get --app=$app --key=project_name) contact_url=$(ynh_app_setting_get --app=$app --key=contact_url) From 7102285b60823587573beea7d7d6ea1b37ed0ca6 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Fri, 5 Mar 2021 13:38:32 +0100 Subject: [PATCH 08/27] Update gunicorn.py --- conf/gunicorn.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/conf/gunicorn.py b/conf/gunicorn.py index 8af01f8..982a7d9 100644 --- a/conf/gunicorn.py +++ b/conf/gunicorn.py @@ -1,11 +1,11 @@ command = '__PYTHON_PATH__/gunicorn' pythonpath = '__FINALPATH__' workers = 4 -user = '__NAME__' +user = '__APP__' bind = 'unix:__FINALPATH__/sock' -pid = '/run/gunicorn/__NAME__-pid' -errorlog = '/var/log/__NAME__/error.log' -accesslog = '/var/log/__NAME__/access.log' +pid = '/run/gunicorn/__APP__-pid' +errorlog = '/var/log/__APP__/error.log' +accesslog = '/var/log/__APP__/access.log' access_log_format = '%({X-Real-IP}i)s %({X-Forwarded-For}i)s %(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s"' loglevel = 'warning' capture_output = True From 0234bd48494c49ef6e994c2460b59e990915af0f Mon Sep 17 00:00:00 2001 From: yalh76 Date: Fri, 5 Mar 2021 13:53:27 +0100 Subject: [PATCH 09/27] Fix multi --- scripts/install | 4 ++-- scripts/upgrade | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/scripts/install b/scripts/install index 8430a0f..a806f28 100755 --- a/scripts/install +++ b/scripts/install @@ -165,8 +165,8 @@ ynh_script_progression --message="Securing files and directories..." # Set permissions to app files chown -R root: $final_path -chown pepettes:root $final_path -chown pepettes:root $final_path/settings.py +chown $app:root $final_path +chown $app:root $final_path/settings.py chmod o=--- $final_path/settings.py #================================================= diff --git a/scripts/upgrade b/scripts/upgrade index 0bbe6be..3e02584 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -157,8 +157,8 @@ ynh_script_progression --message="Securing files and directories..." # Set permissions on app files chown -R root: $final_path -chown pepettes:root $final_path -chown pepettes:root $final_path/settings.py +chown $app:root $final_path +chown $app:root $final_path/settings.py chmod o=--- $final_path/settings.py #================================================= From a748f8552d148e11f5a7291e7a7fe2a99be07f14 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Fri, 5 Mar 2021 14:44:04 +0100 Subject: [PATCH 10/27] Fix multi --- scripts/install | 7 ++----- scripts/restore | 10 ++-------- scripts/upgrade | 7 ++----- 3 files changed, 6 insertions(+), 18 deletions(-) diff --git a/scripts/install b/scripts/install index a806f28..1414e0b 100755 --- a/scripts/install +++ b/scripts/install @@ -109,7 +109,7 @@ ynh_add_nginx_config ynh_script_progression --message="Configuring system user..." --weight=1 # Create a system user -ynh_system_user_create --username=$app +ynh_system_user_create --username=$app --home_dir="$final_path" #================================================= # SPECIFIC SETUP @@ -164,10 +164,7 @@ done ynh_script_progression --message="Securing files and directories..." # Set permissions to app files -chown -R root: $final_path -chown $app:root $final_path -chown $app:root $final_path/settings.py -chmod o=--- $final_path/settings.py +chown -R $app:root $final_path #================================================= # INTEGRATE SERVICE IN YUNOHOST diff --git a/scripts/restore b/scripts/restore index dfc084c..bce539d 100755 --- a/scripts/restore +++ b/scripts/restore @@ -64,7 +64,7 @@ ynh_restore_file --origin_path="$final_path" ynh_script_progression --message="Recreating the dedicated system user..." --weight=1 # Create the dedicated user (if not existing) -ynh_system_user_create --username=$app +ynh_system_user_create --username=$app --home_dir="$final_path" #================================================= # RESTORE USER RIGHTS @@ -72,7 +72,7 @@ ynh_system_user_create --username=$app ynh_script_progression --message="Restoring user rights..." # Restore permissions on app files -chown -R root: $final_path +chown -R $app:root $final_path #================================================= # SPECIFIC RESTORATION @@ -97,12 +97,6 @@ pushd $final_path $ynh_pip install gunicorn mkdir -p /var/log/$app chown -R $app:www-data /var/log/$app - cat <> wsgi.py -from server import app - -if __name__ == "__main__": - app.run() -EOF popd #================================================= diff --git a/scripts/upgrade b/scripts/upgrade index 3e02584..044a15c 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -101,7 +101,7 @@ ynh_install_python --python_version=$python_version ynh_script_progression --message="Making sure dedicated system user exists..." --weight=1 # Create a dedicated user (if not existing) -ynh_system_user_create --username=$app +ynh_system_user_create --username=$app --home_dir="$final_path" #================================================= # SPECIFIC UPGRADE @@ -156,10 +156,7 @@ done ynh_script_progression --message="Securing files and directories..." # Set permissions on app files -chown -R root: $final_path -chown $app:root $final_path -chown $app:root $final_path/settings.py -chmod o=--- $final_path/settings.py +chown -R $app:root $final_path #================================================= # INTEGRATE SERVICE IN YUNOHOST From 269739dbe1e8311bb033377daeed6bb7c3486a5a Mon Sep 17 00:00:00 2001 From: yalh76 Date: Fri, 5 Mar 2021 22:26:12 +0100 Subject: [PATCH 11/27] Enhanced ynh_intall_pyhton --- scripts/ynh_install_python | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/scripts/ynh_install_python b/scripts/ynh_install_python index aa5d286..052a30c 100644 --- a/scripts/ynh_install_python +++ b/scripts/ynh_install_python @@ -99,8 +99,9 @@ ynh_use_python () { if [[ :$PATH: != *":$python_path"* ]]; then PATH="$python_path:$PATH" fi + python_path="$PATH" # Create an alias to easily load the PATH - ynh_python_load_path="PATH=$PATH" + ynh_python_load_path="PATH=$python_path" # Sets the local application-specific Python version (cd $final_path @@ -175,7 +176,7 @@ ynh_install_python () { pyenv install --skip-existing $python_version &>/dev/null # Remove app virtualenv - if $(pyenv virtualenvs | grep $YNH_APP_INSTANCE_NAME) + if `pyenv virtualenvs | grep "$YNH_APP_INSTANCE_NAME " 1>/dev/null 2>&1` then pyenv virtualenv-delete --force $YNH_APP_INSTANCE_NAME fi From 1f0009d2a74e28fb06bc4054fa1829e71fe9ba19 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Sat, 6 Mar 2021 01:36:58 +0100 Subject: [PATCH 12/27] Update ynh_install_python --- scripts/ynh_install_python | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/scripts/ynh_install_python b/scripts/ynh_install_python index 052a30c..4e0bbda 100644 --- a/scripts/ynh_install_python +++ b/scripts/ynh_install_python @@ -37,18 +37,18 @@ SOURCE_SUM=27ae3de027a6f6dccdca4085225512e559c6b94b31625bd2b357a18890a1e618" > " (cd $pyenv_install_dir ./src/configure && make -C src) -# Create shims directory if needed -if [ ! -d $pyenv_install_dir/shims ] ; then - mkdir $pyenv_install_dir/shims -fi + # Create shims directory if needed + if [ ! -d $pyenv_install_dir/shims ] ; then + mkdir $pyenv_install_dir/shims + fi } -# Load the version of python for an app, and set variables. +# Load the version of Python for an app, and set variables. # -# ynh_use_python has to be used in any app scripts before using python for the first time. +# 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. # -# To use pip or python, use the alias `ynh_pip` and `ynh_python` +# To use pip or Python, use the alias `ynh_pip` and `ynh_python` # Those alias will use the correct version installed for the app # For example: use `ynh_pip install` instead of `pip install` # @@ -56,46 +56,47 @@ fi # And propagate $PATH to sudo with $ynh_python_load_path # Exemple: `ynh_exec_as $app $ynh_python_load_path $ynh_pip install` # -# $PATH contains the path of the requested version of python. +# $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 +# 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. +# 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 +# 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. +# - $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 () { - # Get the absolute path of this version of python + python_version=$(ynh_app_setting_get --app=$app --key=python_version) + # Get the absolute path of this version of Python python_path="$python_version_path/$YNH_APP_INSTANCE_NAME/bin" # Allow alias to be used into bash script shopt -s expand_aliases - # Create an alias for the specific version of python and a variable as fallback + # Create an alias for the specific version of Python and a variable as fallback ynh_python="$python_path/python" alias ynh_python="$ynh_python" # And pip ynh_pip="$python_path/pip" alias ynh_pip="$ynh_pip" - # Load the path of this version of python in $PATH + # Load the path of this version of Python in $PATH if [[ :$PATH: != *":$python_path"* ]]; then PATH="$python_path:$PATH" fi @@ -176,7 +177,7 @@ ynh_install_python () { pyenv install --skip-existing $python_version &>/dev/null # Remove app virtualenv - if `pyenv virtualenvs | grep "$YNH_APP_INSTANCE_NAME " 1>/dev/null 2>&1` + if `pyenv virtualenvs | grep --quiet "$YNH_APP_INSTANCE_NAME " 1>/dev/null 2>&1` then pyenv virtualenv-delete --force $YNH_APP_INSTANCE_NAME fi From ebcd563647c12996949f21b3ecf4f75e4178de86 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Sat, 6 Mar 2021 01:52:09 +0100 Subject: [PATCH 13/27] Update ynh_install_python --- scripts/ynh_install_python | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/scripts/ynh_install_python b/scripts/ynh_install_python index 4e0bbda..c8d0c16 100644 --- a/scripts/ynh_install_python +++ b/scripts/ynh_install_python @@ -100,9 +100,8 @@ ynh_use_python () { if [[ :$PATH: != *":$python_path"* ]]; then PATH="$python_path:$PATH" fi - python_path="$PATH" # Create an alias to easily load the PATH - ynh_python_load_path="PATH=$python_path" + ynh_python_load_path="PATH=$PATH" # Sets the local application-specific Python version (cd $final_path From 5cc3bad26ede7acd04ec2150990f4a81598668f1 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 8 Mar 2021 21:48:30 +0100 Subject: [PATCH 14/27] Update ynh_install_python --- scripts/ynh_install_python | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/scripts/ynh_install_python b/scripts/ynh_install_python index c8d0c16..5a4987a 100644 --- a/scripts/ynh_install_python +++ b/scripts/ynh_install_python @@ -64,7 +64,7 @@ SOURCE_SUM=27ae3de027a6f6dccdca4085225512e559c6b94b31625bd2b357a18890a1e618" > " # # # 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. +# 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` @@ -83,6 +83,7 @@ SOURCE_SUM=27ae3de027a6f6dccdca4085225512e559c6b94b31625bd2b357a18890a1e618" > " # Requires YunoHost version 2.7.12 or higher. ynh_use_python () { python_version=$(ynh_app_setting_get --app=$app --key=python_version) + # Get the absolute path of this version of Python python_path="$python_version_path/$YNH_APP_INSTANCE_NAME/bin" @@ -112,9 +113,6 @@ ynh_use_python () { # # 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 ) -# 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 # for every LOGIN user, hence your user must have a defined shell (as opposed to /usr/sbin/nologin) # @@ -209,7 +207,7 @@ ynh_remove_python () { # Load pyenv path in 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:@@') pyenv virtualenv-delete --force $YNH_APP_INSTANCE_NAME @@ -261,4 +259,4 @@ ynh_cleanup_python () { ynh_secure_remove --file="$pyenv_install_dir" rm /etc/profile.d/pyenv.sh fi -} \ No newline at end of file +} From c35da86ffac5d0cdf8f9fd61de54868055c005b4 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Tue, 9 Mar 2021 21:19:31 +0100 Subject: [PATCH 15/27] Trying chmod o=--- --- scripts/install | 1 + scripts/restore | 1 + scripts/upgrade | 1 + 3 files changed, 3 insertions(+) diff --git a/scripts/install b/scripts/install index 1414e0b..c3b50ab 100755 --- a/scripts/install +++ b/scripts/install @@ -165,6 +165,7 @@ ynh_script_progression --message="Securing files and directories..." # Set permissions to app files chown -R $app:root $final_path +chmod o=--- $final_path #================================================= # INTEGRATE SERVICE IN YUNOHOST diff --git a/scripts/restore b/scripts/restore index bce539d..ba83397 100755 --- a/scripts/restore +++ b/scripts/restore @@ -73,6 +73,7 @@ ynh_script_progression --message="Restoring user rights..." # Restore permissions on app files chown -R $app:root $final_path +chmod o=--- $final_path #================================================= # SPECIFIC RESTORATION diff --git a/scripts/upgrade b/scripts/upgrade index 044a15c..06b9341 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -157,6 +157,7 @@ ynh_script_progression --message="Securing files and directories..." # Set permissions on app files chown -R $app:root $final_path +chmod o=--- $final_path #================================================= # INTEGRATE SERVICE IN YUNOHOST From 19e5ca74ef0a663710e14a075d37df15d921d1b3 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Wed, 10 Mar 2021 22:07:12 +0100 Subject: [PATCH 16/27] Fixing PID --- conf/gunicorn.py | 2 +- conf/systemd.service | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/conf/gunicorn.py b/conf/gunicorn.py index 982a7d9..dc73fdb 100644 --- a/conf/gunicorn.py +++ b/conf/gunicorn.py @@ -3,7 +3,7 @@ pythonpath = '__FINALPATH__' workers = 4 user = '__APP__' bind = 'unix:__FINALPATH__/sock' -pid = '/run/gunicorn/__APP__-pid' +pid = '__FINALPATH__/__APP__-pid' errorlog = '/var/log/__APP__/error.log' accesslog = '/var/log/__APP__/access.log' access_log_format = '%({X-Real-IP}i)s %({X-Forwarded-For}i)s %(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s"' diff --git a/conf/systemd.service b/conf/systemd.service index 29f9e3b..2ecd49c 100644 --- a/conf/systemd.service +++ b/conf/systemd.service @@ -3,11 +3,11 @@ Description=__APP__ Daemon After=network.target [Service] -PIDFile=/run/gunicorn/__APP__-pid +PIDFile=__FINALPATH__/__APP__-pid User=__APP__ Group=__APP__ WorkingDirectory=__FINALPATH__ -ExecStart=__PYTHON_PATH__/gunicorn -c __FINALPATH__/gunicorn.py wsgi:app +ExecStart=__PYTHON_PATH__/gunicorn -c __FINALPATH__/gunicorn.py wsgi:app --pid __FINALPATH__/__APP__-pid ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s TERM $MAINPID PrivateTmp=true From 1a0f985e5175040d63b38862a9e2889742e4aaa6 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Wed, 10 Mar 2021 22:07:22 +0100 Subject: [PATCH 17/27] Fix rights --- scripts/install | 2 +- scripts/restore | 2 +- scripts/upgrade | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/install b/scripts/install index c3b50ab..f4cb130 100755 --- a/scripts/install +++ b/scripts/install @@ -164,7 +164,7 @@ done ynh_script_progression --message="Securing files and directories..." # Set permissions to app files -chown -R $app:root $final_path +chown -R $app:www-data $final_path chmod o=--- $final_path #================================================= diff --git a/scripts/restore b/scripts/restore index ba83397..d209b9e 100755 --- a/scripts/restore +++ b/scripts/restore @@ -72,7 +72,7 @@ ynh_system_user_create --username=$app --home_dir="$final_path" ynh_script_progression --message="Restoring user rights..." # Restore permissions on app files -chown -R $app:root $final_path +chown -R $app:www-data $final_path chmod o=--- $final_path #================================================= diff --git a/scripts/upgrade b/scripts/upgrade index 06b9341..135b4bd 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -156,7 +156,7 @@ done ynh_script_progression --message="Securing files and directories..." # Set permissions on app files -chown -R $app:root $final_path +chown -R $app:www-data $final_path chmod o=--- $final_path #================================================= From 682a86b9ad585ad5a6c3acaa6790695c6b627f13 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Thu, 11 Mar 2021 20:28:54 +0100 Subject: [PATCH 18/27] Reorder steps to fix restore --- scripts/restore | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/scripts/restore b/scripts/restore index d209b9e..cdc7ca5 100755 --- a/scripts/restore +++ b/scripts/restore @@ -66,15 +66,6 @@ ynh_script_progression --message="Recreating the dedicated system user..." --wei # Create the dedicated user (if not existing) ynh_system_user_create --username=$app --home_dir="$final_path" -#================================================= -# RESTORE USER RIGHTS -#================================================= -ynh_script_progression --message="Restoring user rights..." - -# Restore permissions on app files -chown -R $app:www-data $final_path -chmod o=--- $final_path - #================================================= # SPECIFIC RESTORATION #================================================= @@ -100,6 +91,15 @@ pushd $final_path chown -R $app:www-data /var/log/$app popd +#================================================= +# RESTORE USER RIGHTS +#================================================= +ynh_script_progression --message="Restoring user rights..." + +# Restore permissions on app files +chown -R $app:www-data $final_path +chmod o=--- $final_path + #================================================= # RESTORE SYSTEMD #================================================= From b20a6a6f5356baa68666379d0c6d9a39cf12ee90 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Sat, 13 Mar 2021 01:09:24 +0100 Subject: [PATCH 19/27] Switch to git --- scripts/ynh_install_python | 138 +++++++++++++++++++++---------------- 1 file changed, 79 insertions(+), 59 deletions(-) diff --git a/scripts/ynh_install_python b/scripts/ynh_install_python index 5a4987a..45239cc 100644 --- a/scripts/ynh_install_python +++ b/scripts/ynh_install_python @@ -1,47 +1,20 @@ #!/bin/bash -pyenv_version=1.2.23 -pyenv_virtualenv_version=1.1.5 +ynh_python_try_bash_extension() { + if [ -x src/configure ]; then + src/configure && make -C src || { + echo "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. export PYENV_ROOT="$pyenv_install_dir" # 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" - -# Install Python Version Management -# -# [internal] -# -# usage: ynh_install_pyenv -# -# Requires YunoHost version 2.7.12 or higher. -ynh_install_pyenv () { - ynh_print_info --message="Installation of pyenv - Python Version Management - pyenv-$pyenv_version/pyenv-virtualenv-$pyenv_virtualenv_version" - - # Build an app.src for pyenv - mkdir -p "../conf" - echo "SOURCE_URL=https://github.com/pyenv/pyenv/archive/v${pyenv_version}.tar.gz -SOURCE_SUM=805058aa5ce257157fb4769543e6a43bac45a88c723ff3c4fcf5b4f759056bf5" > "../conf/pyenv.src" - # Download and extract pyenv - ynh_setup_source --dest_dir="$pyenv_install_dir" --source_id=pyenv - - # Build an app.src for pyenv-virtualenv - mkdir -p "../conf" - echo "SOURCE_URL=https://github.com/pyenv/pyenv-virtualenv/archive/v${pyenv_virtualenv_version}.tar.gz -SOURCE_SUM=27ae3de027a6f6dccdca4085225512e559c6b94b31625bd2b357a18890a1e618" > "../conf/pyenv-virtualenv.src" - # Download and extract pyenv-virtualenv - ynh_setup_source --dest_dir="$pyenv_install_dir/plugins/pyenv-virtualenv" --source_id=pyenv-virtualenv - - (cd $pyenv_install_dir - ./src/configure && make -C src) - - # Create shims directory if needed - if [ ! -d $pyenv_install_dir/shims ] ; then - mkdir $pyenv_install_dir/shims - fi -} +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" # Load the version of Python for an app, and set variables. # @@ -105,8 +78,9 @@ ynh_use_python () { ynh_python_load_path="PATH=$PATH" # Sets the local application-specific Python version - (cd $final_path - pyenv local $python_version) + pushd $final_path + $pyenv_install_dir/bin/pyenv local $python_version + popd } # Install a specific version of Python @@ -133,12 +107,6 @@ ynh_install_python () { # Manage arguments with getopts ynh_handle_getopts_args "$@" - # Store python_version into the config of this app - ynh_app_setting_set --app=$YNH_APP_INSTANCE_NAME --key=python_version --value=$python_version - - # Create $pyenv_install_dir if doesn't exist already - mkdir -p "$pyenv_install_dir/plugins/pyenv-virtualenv" - # Install required dependencies ynh_add_app_dependencies --package="$pyenv_dependencies" @@ -151,18 +119,66 @@ ynh_install_python () { # Move an existing Python binary, to avoid to block pyenv #test -x /usr/bin/python && mv /usr/bin/python /usr/bin/python_pyenv - # If pyenv is not previously setup, install it - if ! type pyenv > /dev/null 2>&1 - then - ynh_install_pyenv - elif dpkg --compare-versions "$($pyenv_install_dir/bin/pyenv --version | cut -d" " -f2)" lt $pyenv_version - then - ynh_install_pyenv - elif dpkg --compare-versions "$($pyenv_install_dir/bin/pyenv virtualenv --version | cut -d" " -f2)" lt $pyenv_virtualenv_version - then - ynh_install_pyenv + # Install or update pyenv + pyenv="$(command -v pyenv $pyenv_install_dir/bin/pyenv | head -1)" + if [ -n "$pyenv" ]; then + ynh_print_info --message="pyenv already seems installed in \`$pyenv'." + pushd "${pyenv%/*/*}" + if git remote -v 2>/dev/null | grep -q pyenv; then + echo "Trying to update with git..." + git pull -q --tags origin master + cd .. + ynh_python_try_bash_extension + 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 + pyenv_virtualenv="$(command -v "$pyenv_install_dir"/plugins/*/bin/pyenv-virtualenv pyenv-virtualenv | head -1)" + 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 -q pyenv-virtualenv; 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 + + pyenv_latest="$(command -v "$pyenv_install_dir"/plugins/*/bin/pyenv-latest pyenv-latest | head -1)" + 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 -q xxenv-latest; 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 + + # Enable caching + mkdir -p "${pyenv_install_dir}/cache" + + # Create shims directory if needed + mkdir -p "${pyenv_install_dir}/shims" + # Restore /usr/local/bin in PATH PATH=$CLEAR_PATH @@ -170,8 +186,12 @@ ynh_install_python () { # test -x /usr/bin/python_pyenv && mv /usr/bin/python_pyenv /usr/bin/python # Install the requested version of Python - ynh_print_info --message="Installation of Python-"$python_version - pyenv install --skip-existing $python_version &>/dev/null + 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` @@ -246,7 +266,7 @@ ynh_cleanup_python () { 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 + ynh_print_info --message="Removing of Python-$installed_python_version" $pyenv_install_dir/bin/pyenv uninstall --force $installed_python_version fi done @@ -255,8 +275,8 @@ ynh_cleanup_python () { if [[ ! $required_python_versions ]] then # Remove pyenv environment configuration - ynh_print_info --message="Removing of pyenv-"$pyenv_version + ynh_print_info --message="Removing of pyenv-$pyenv_version" ynh_secure_remove --file="$pyenv_install_dir" - rm /etc/profile.d/pyenv.sh + ynh_secure_remove --file="/etc/profile.d/pyenv.sh" fi } From db62524365452725a57dc14720d6be6e82d4e96d Mon Sep 17 00:00:00 2001 From: yalh76 Date: Sat, 13 Mar 2021 13:06:57 +0100 Subject: [PATCH 20/27] Typo --- scripts/ynh_install_python | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/ynh_install_python b/scripts/ynh_install_python index 45239cc..4e9c468 100644 --- a/scripts/ynh_install_python +++ b/scripts/ynh_install_python @@ -3,7 +3,7 @@ ynh_python_try_bash_extension() { if [ -x src/configure ]; then src/configure && make -C src || { - echo "Optional bash extension failed to build, but things will still work normally." + ynh_print_info --message="Optional bash extension failed to build, but things will still work normally." } fi } From cf0812e40842b79b7f08b106291ae5ea576fdac4 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Sat, 13 Mar 2021 14:13:02 +0100 Subject: [PATCH 21/27] Swith to PYTHON_VERSION --- scripts/_common.sh | 2 +- scripts/install | 2 +- scripts/restore | 2 +- scripts/upgrade | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/scripts/_common.sh b/scripts/_common.sh index b339122..41eb8bd 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -7,7 +7,7 @@ # dependencies used by the app pkg_dependencies="curl" -python_version=3.7.3 +PYTHON_VERSION=3.7.3 #================================================= # PERSONAL HELPERS diff --git a/scripts/install b/scripts/install index f4cb130..e208ff9 100755 --- a/scripts/install +++ b/scripts/install @@ -84,7 +84,7 @@ ynh_app_setting_set --app=$app --key=port --value=$port ynh_script_progression --message="Installing dependencies..." --weight=1 ynh_install_app_dependencies $pkg_dependencies -ynh_install_python --python_version=$python_version +ynh_install_python --python_version=$PYTHON_VERSION #================================================= # DOWNLOAD, CHECK AND UNPACK SOURCE diff --git a/scripts/restore b/scripts/restore index cdc7ca5..d2502d2 100755 --- a/scripts/restore +++ b/scripts/restore @@ -75,7 +75,7 @@ ynh_script_progression --message="Reinstalling dependencies..." --weight=1 # Define and install dependencies ynh_install_app_dependencies $pkg_dependencies -ynh_install_python --python_version=$python_version +ynh_install_python --python_version=$PYTHON_VERSION #================================================= # INSTALL PYTHON DEPENDENCIES diff --git a/scripts/upgrade b/scripts/upgrade index 135b4bd..c0d3565 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -93,7 +93,7 @@ ynh_add_nginx_config ynh_script_progression --message="Upgrading dependencies..." --weight=1 ynh_install_app_dependencies $pkg_dependencies -ynh_install_python --python_version=$python_version +ynh_install_python --python_version=$PYTHON_VERSION #================================================= # CREATE DEDICATED USER From c8ceae60171302cdacc65071e2e4aa138b536f86 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Sun, 14 Mar 2021 00:48:39 +0100 Subject: [PATCH 22/27] Do not use 'yunohost app setting' --- scripts/ynh_install_python | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/ynh_install_python b/scripts/ynh_install_python index 4e9c468..7f620ed 100644 --- a/scripts/ynh_install_python +++ b/scripts/ynh_install_python @@ -253,7 +253,7 @@ ynh_cleanup_python () { local required_python_versions="" for installed_app in $installed_apps do - local installed_app_python_version=$(yunohost app setting $installed_app python_version) + 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}" From 1c5bf636b3103d934e5767b052861134f6364dd6 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Sun, 14 Mar 2021 06:00:40 +0100 Subject: [PATCH 23/27] Fix repository detection --- scripts/ynh_install_python | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/ynh_install_python b/scripts/ynh_install_python index 7f620ed..9c285c8 100644 --- a/scripts/ynh_install_python +++ b/scripts/ynh_install_python @@ -124,7 +124,7 @@ ynh_install_python () { if [ -n "$pyenv" ]; then ynh_print_info --message="pyenv already seems installed in \`$pyenv'." pushd "${pyenv%/*/*}" - if git remote -v 2>/dev/null | grep -q pyenv; then + if git remote -v 2>/dev/null | grep "https://github.com/pyenv/pyenv.git"; then echo "Trying to update with git..." git pull -q --tags origin master cd .. @@ -147,7 +147,7 @@ ynh_install_python () { 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 -q pyenv-virtualenv; then + 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 @@ -162,7 +162,7 @@ ynh_install_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 -q xxenv-latest; then + 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 From 195a6a2cc34f7eb1972c5a6306dc0cd66f2787ec Mon Sep 17 00:00:00 2001 From: yalh76 Date: Wed, 17 Mar 2021 00:48:20 +0100 Subject: [PATCH 24/27] Remove ynh_install python --- conf/systemd.service | 2 +- scripts/_common.sh | 4 +- scripts/install | 12 +- scripts/remove | 1 - scripts/restore | 10 +- scripts/upgrade | 12 +- scripts/ynh_install_python | 282 ------------------------------------- 7 files changed, 16 insertions(+), 307 deletions(-) delete mode 100644 scripts/ynh_install_python diff --git a/conf/systemd.service b/conf/systemd.service index 2ecd49c..ccd671c 100644 --- a/conf/systemd.service +++ b/conf/systemd.service @@ -7,7 +7,7 @@ PIDFile=__FINALPATH__/__APP__-pid User=__APP__ Group=__APP__ WorkingDirectory=__FINALPATH__ -ExecStart=__PYTHON_PATH__/gunicorn -c __FINALPATH__/gunicorn.py wsgi:app --pid __FINALPATH__/__APP__-pid +ExecStart=__PYTHON_PATH__/gunicorn -c __FINALPATH__/venv/bin/gunicorn.py wsgi:app --pid __FINALPATH__/__APP__-pid ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s TERM $MAINPID PrivateTmp=true diff --git a/scripts/_common.sh b/scripts/_common.sh index 41eb8bd..bc48e71 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -5,9 +5,7 @@ #================================================= # dependencies used by the app -pkg_dependencies="curl" - -PYTHON_VERSION=3.7.3 +pkg_dependencies="python3-venv python3-dev python3-pip" #================================================= # PERSONAL HELPERS diff --git a/scripts/install b/scripts/install index e208ff9..64f5dc5 100755 --- a/scripts/install +++ b/scripts/install @@ -7,7 +7,6 @@ #================================================= source _common.sh -source ynh_install_python source /usr/share/yunohost/helpers #================================================= @@ -84,7 +83,6 @@ ynh_app_setting_set --app=$app --key=port --value=$port ynh_script_progression --message="Installing dependencies..." --weight=1 ynh_install_app_dependencies $pkg_dependencies -ynh_install_python --python_version=$PYTHON_VERSION #================================================= # DOWNLOAD, CHECK AND UNPACK SOURCE @@ -119,10 +117,10 @@ ynh_system_user_create --username=$app --home_dir="$final_path" ynh_script_progression --message="Installing Python dependencies..." pushd $final_path - ynh_use_python - $ynh_pip install --upgrade pip - $ynh_pip install -r requirements.txt - $ynh_pip install gunicorn + python3 -m venv venv + venv/bin/pip install --upgrade pip + venv/bin/pip install -r requirements.txt + venv/bin/pip install gunicorn mkdir -p /var/log/$app chown -R $app:www-data /var/log/$app cat <> wsgi.py @@ -139,7 +137,7 @@ popd ynh_script_progression --message="Configuring a systemd service..." --weight=1 # Create a dedicated systemd config -ynh_add_systemd_config --others_var="python_path" +ynh_add_systemd_config #================================================= # MODIFY A CONFIG FILE diff --git a/scripts/remove b/scripts/remove index feedb2a..73412c9 100755 --- a/scripts/remove +++ b/scripts/remove @@ -7,7 +7,6 @@ #================================================= source _common.sh -source ynh_install_python source /usr/share/yunohost/helpers #================================================= diff --git a/scripts/restore b/scripts/restore index d2502d2..6f48eb9 100755 --- a/scripts/restore +++ b/scripts/restore @@ -8,7 +8,6 @@ # Keep this path for calling _common.sh inside the execution's context of backup and restore scripts source ../settings/scripts/_common.sh -source ../settings/scripts/ynh_install_python source /usr/share/yunohost/helpers #================================================= @@ -75,7 +74,6 @@ ynh_script_progression --message="Reinstalling dependencies..." --weight=1 # Define and install dependencies ynh_install_app_dependencies $pkg_dependencies -ynh_install_python --python_version=$PYTHON_VERSION #================================================= # INSTALL PYTHON DEPENDENCIES @@ -83,10 +81,10 @@ ynh_install_python --python_version=$PYTHON_VERSION ynh_script_progression --message="Installing Python dependencies..." pushd $final_path - ynh_use_python - $ynh_pip install --upgrade pip - $ynh_pip install -r requirements.txt - $ynh_pip install gunicorn + python3 -m venv venv + venv/bin/pip install --upgrade pip + venv/bin/pip install -r requirements.txt + venv/bin/pip install gunicorn mkdir -p /var/log/$app chown -R $app:www-data /var/log/$app popd diff --git a/scripts/upgrade b/scripts/upgrade index c0d3565..32e3dec 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -7,7 +7,6 @@ #================================================= source _common.sh -source ynh_install_python source /usr/share/yunohost/helpers #================================================= @@ -93,7 +92,6 @@ ynh_add_nginx_config ynh_script_progression --message="Upgrading dependencies..." --weight=1 ynh_install_app_dependencies $pkg_dependencies -ynh_install_python --python_version=$PYTHON_VERSION #================================================= # CREATE DEDICATED USER @@ -111,10 +109,10 @@ ynh_system_user_create --username=$app --home_dir="$final_path" ynh_script_progression --message="Installing Python dependencies..." pushd $final_path - ynh_use_python - $ynh_pip install --upgrade pip - $ynh_pip install -r requirements.txt - $ynh_pip install gunicorn + python3 -m venv venv + venv/bin/pip install --upgrade pip + venv/bin/pip install -r requirements.txt + venv/bin/pip install gunicorn mkdir -p /var/log/$app chown -R $app:www-data /var/log/$app cat <> wsgi.py @@ -131,7 +129,7 @@ popd ynh_script_progression --message="Upgrading systemd configuration..." --weight=1 # Create a dedicated systemd config -ynh_add_systemd_config --others_var="python_path" +ynh_add_systemd_config #================================================= # MODIFY A CONFIG FILE diff --git a/scripts/ynh_install_python b/scripts/ynh_install_python deleted file mode 100644 index 9c285c8..0000000 --- a/scripts/ynh_install_python +++ /dev/null @@ -1,282 +0,0 @@ -#!/bin/bash - -ynh_python_try_bash_extension() { - if [ -x src/configure ]; then - src/configure && make -C src || { - 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. -export PYENV_ROOT="$pyenv_install_dir" - -# Required dependencies -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" - -# Load the version of Python for an app, and set variables. -# -# 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. -# -# To use pip or Python, use the alias `ynh_pip` and `ynh_python` -# Those alias will use the correct version installed for the app -# For example: use `ynh_pip install` instead of `pip install` -# -# With `sudo` or `ynh_exec_as`, use instead the fallback variables `$ynh_pip` and `$ynh_python` -# And propagate $PATH to sudo with $ynh_python_load_path -# Exemple: `ynh_exec_as $app $ynh_python_load_path $ynh_pip install` -# -# $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) - - # Get the absolute path of this version of Python - python_path="$python_version_path/$YNH_APP_INSTANCE_NAME/bin" - - # Allow alias to be used into bash script - shopt -s expand_aliases - - # Create an alias for the specific version of Python and a variable as fallback - ynh_python="$python_path/python" - alias ynh_python="$ynh_python" - # 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 -# -# ynh_install_python will install the version of Python provided as argument by using 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) -# -# Don't forget to execute python-dependent command in a login environment -# (e.g. sudo --login option) -# When not possible (e.g. in systemd service definition), please use direct path -# to pyenv shims (e.g. $PYENV_ROOT/shims/bundle) -# -# usage: ynh_install_python --python_version=python_version -# | arg: -v, --python_version= - Version of Python to install. -# -# Requires YunoHost version 2.7.12 or higher. -ynh_install_python () { - # Declare an array to define the options of this helper. - local legacy_args=v - local -A args_array=( [v]=python_version= ) - local python_version - # Manage arguments with getopts - ynh_handle_getopts_args "$@" - - # Install required dependencies - ynh_add_app_dependencies --package="$pyenv_dependencies" - - # Load pyenv path in PATH - local CLEAR_PATH="$pyenv_install_dir/bin:$PATH" - - # Remove /usr/local/bin in PATH in case of Python prior installation - PATH=$(echo $CLEAR_PATH | sed 's@/usr/local/bin:@@') - - # Move an existing Python binary, to avoid to block pyenv - #test -x /usr/bin/python && mv /usr/bin/python /usr/bin/python_pyenv - - # Install or update pyenv - pyenv="$(command -v pyenv $pyenv_install_dir/bin/pyenv | head -1)" - if [ -n "$pyenv" ]; then - ynh_print_info --message="pyenv already seems installed in \`$pyenv'." - pushd "${pyenv%/*/*}" - if git remote -v 2>/dev/null | grep "https://github.com/pyenv/pyenv.git"; then - echo "Trying to update with git..." - git pull -q --tags origin master - cd .. - ynh_python_try_bash_extension - 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 - - pyenv_virtualenv="$(command -v "$pyenv_install_dir"/plugins/*/bin/pyenv-virtualenv pyenv-virtualenv | head -1)" - 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 - - pyenv_latest="$(command -v "$pyenv_install_dir"/plugins/*/bin/pyenv-latest pyenv-latest | head -1)" - 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 - - # Enable caching - mkdir -p "${pyenv_install_dir}/cache" - - # Create shims directory if needed - mkdir -p "${pyenv_install_dir}/shims" - - # Restore /usr/local/bin in PATH - PATH=$CLEAR_PATH - - # And replace the old Python binary - # test -x /usr/bin/python_pyenv && mv /usr/bin/python_pyenv /usr/bin/python - - # Install the requested version of Python - 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 PATH=\"$pyenv_install_dir/bin:$PATH\" -eval \"\$(pyenv init -)\" -#pyenv" > /etc/profile.d/pyenv.sh - - # Load the environment - eval "$(pyenv init -)" -} - -# Remove the version of Python used by the app. -# -# This helper will also cleanup Python versions -# -# usage: ynh_remove_python -ynh_remove_python () { - local python_version=$(ynh_app_setting_get --app=$YNH_APP_INSTANCE_NAME --key=python_version) - - # Load pyenv path in PATH - local CLEAR_PATH="$pyenv_install_dir/bin:$PATH" - - # Remove /usr/local/bin in PATH in case of Python prior installation - PATH=$(echo $CLEAR_PATH | sed 's@/usr/local/bin:@@') - - pyenv virtualenv-delete --force $YNH_APP_INSTANCE_NAME - - # Remove the line for this app - ynh_app_setting_delete --app=$YNH_APP_INSTANCE_NAME --key=python_version - - # Cleanup Python versions - 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 -} From 8c95179f41951387bfdfc6ab29f42999c1a47e67 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Wed, 17 Mar 2021 00:51:58 +0100 Subject: [PATCH 25/27] More ynh_install_python removal --- conf/gunicorn.py | 2 +- conf/systemd.service | 2 +- scripts/install | 2 +- scripts/upgrade | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/conf/gunicorn.py b/conf/gunicorn.py index dc73fdb..db092a2 100644 --- a/conf/gunicorn.py +++ b/conf/gunicorn.py @@ -1,4 +1,4 @@ -command = '__PYTHON_PATH__/gunicorn' +command = '__FINALPATH__/venv/bin/gunicorn' pythonpath = '__FINALPATH__' workers = 4 user = '__APP__' diff --git a/conf/systemd.service b/conf/systemd.service index ccd671c..c25b84f 100644 --- a/conf/systemd.service +++ b/conf/systemd.service @@ -7,7 +7,7 @@ PIDFile=__FINALPATH__/__APP__-pid User=__APP__ Group=__APP__ WorkingDirectory=__FINALPATH__ -ExecStart=__PYTHON_PATH__/gunicorn -c __FINALPATH__/venv/bin/gunicorn.py wsgi:app --pid __FINALPATH__/__APP__-pid +ExecStart=__FINALPATH__/venv/bin/gunicorn -c __FINALPATH__/gunicorn.py wsgi:app --pid __FINALPATH__/__APP__-pid ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s TERM $MAINPID PrivateTmp=true diff --git a/scripts/install b/scripts/install index 64f5dc5..4c3f7b7 100755 --- a/scripts/install +++ b/scripts/install @@ -137,7 +137,7 @@ popd ynh_script_progression --message="Configuring a systemd service..." --weight=1 # Create a dedicated systemd config -ynh_add_systemd_config +ynh_add_systemd_config #================================================= # MODIFY A CONFIG FILE diff --git a/scripts/upgrade b/scripts/upgrade index 32e3dec..41e1ebb 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -129,7 +129,7 @@ popd ynh_script_progression --message="Upgrading systemd configuration..." --weight=1 # Create a dedicated systemd config -ynh_add_systemd_config +ynh_add_systemd_config #================================================= # MODIFY A CONFIG FILE From 67bf7ed9212b7a107548cf5a02e243b96b80c5fa Mon Sep 17 00:00:00 2001 From: yalh76 Date: Wed, 17 Mar 2021 00:53:41 +0100 Subject: [PATCH 26/27] Update remove --- scripts/remove | 1 - 1 file changed, 1 deletion(-) diff --git a/scripts/remove b/scripts/remove index 73412c9..b492b54 100755 --- a/scripts/remove +++ b/scripts/remove @@ -47,7 +47,6 @@ ynh_remove_systemd_config ynh_script_progression --message="Removing dependencies..." --weight=1 # Remove metapackage and its dependencies -ynh_remove_python ynh_remove_app_dependencies #================================================= From 5143588a311069152a202a80de48d07fc5f5fca8 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Mon, 22 Mar 2021 20:42:32 +0100 Subject: [PATCH 27/27] Adding checksum after Donation --- scripts/install | 2 ++ scripts/upgrade | 2 ++ 2 files changed, 4 insertions(+) diff --git a/scripts/install b/scripts/install index 4c3f7b7..cdcd2d7 100755 --- a/scripts/install +++ b/scripts/install @@ -154,6 +154,8 @@ for price in $(echo $prices | sed "s/,/ /"); do echo "DONATION['$frequency']['$currency'] = '$price_id'" >> "$final_path/settings.py" done +ynh_store_file_checksum --file="$final_path/settings.py" + #================================================= # GENERIC FINALIZATION #================================================= diff --git a/scripts/upgrade b/scripts/upgrade index 41e1ebb..2428f02 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -146,6 +146,8 @@ for price in $(echo $prices | sed "s/,/ /"); do echo "DONATION['$frequency']['$currency'] = '$price_id'" >> "$final_path/settings.py" done +ynh_store_file_checksum --file="$final_path/settings.py" + #================================================= # GENERIC FINALIZATION #=================================================