1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/ihatemoney_ynh.git synced 2024-09-03 19:26:15 +02:00

Upgrade to 2.x upstream

Huge change in package, mainly because we switch from a tarball-based install to a
pip-based install (which is the default install method).

It require quite some refactor to common.sh and a more consistent and clear
migration handing in upgrade script.

Fix #19 #16
This commit is contained in:
Jocelyn Delalande 2018-06-24 01:24:15 +02:00
parent 09458611d1
commit ca79fcdbdd
6 changed files with 99 additions and 99 deletions

View file

@ -1,5 +1,5 @@
location PATHTOCHANGE/static/ { location PATHTOCHANGE/static/ {
alias /opt/yunohost/ihatemoney/src/budget/static/; alias /opt/yunohost/ihatemoney/venv/lib/python3.4/site-packages/ihatemoney/static/;
} }
location PATHTOCHANGE { location PATHTOCHANGE {
# Force https. # Force https.

View file

@ -1,7 +1,6 @@
[program:budget] [program:budget]
command=/opt/yunohost/ihatemoney/venv/bin/gunicorn -c /etc/ihatemoney/gunicorn.conf.py run:app command=/opt/yunohost/ihatemoney/venv/bin/gunicorn -c /etc/ihatemoney/gunicorn.conf.py ihatemoney.wsgi:application
directory=/opt/yunohost/ihatemoney/src/budget
user=ihatemoney user=ihatemoney
autostart=true autostart=true
autorestart=true autorestart=true
redirect_stderr=True redirect_stderr=true

View file

@ -9,38 +9,6 @@ INSTALL_DIR="/opt/yunohost/ihatemoney"
### Functions ### Functions
fetch_and_extract() {
local DESTDIR=$1
local OWNER_USER=${2:-admin}
VERSION=1.0
SHA256=e2ad6e56b161f13911c1c378aad79656bbdfce495189d80f997414803859348e
SOURCE_URL="https://github.com/spiral-project/ihatemoney/archive/${VERSION}.tar.gz"
tarball="/tmp/ihatemoney.tar.gz"
rm -f "$tarball"
wget -q -O "$tarball" "$SOURCE_URL" \
|| ynh_die "Unable to download tarball"
echo "$SHA256 $tarball" | sha256sum -c >/dev/null \
|| ynh_die "Invalid checksum of downloaded tarball"
test -d $DESTDIR || sudo mkdir $DESTDIR
sudo tar xaf "${tarball}" -C "$DESTDIR" --strip-components 1\
|| ynh_die "Unable to extract tarball"
rm -f "$tarball"
}
fix_permissions() {
local SRC_DIR=$1
sudo find $SRC_DIR -type f | while read LINE; do sudo chmod 640 "$LINE" ; done
sudo find $SRC_DIR -type d | while read LINE; do sudo chmod 755 "$LINE" ; done
sudo chown -R ihatemoney:ihatemoney $SRC_DIR
sudo chown -R www-data:www-data ${SRC_DIR}/budget/static
}
install_apt_dependencies() { install_apt_dependencies() {
sudo apt-get install -y -qq python3-virtualenv supervisor sudo apt-get install -y -qq python3-virtualenv supervisor
@ -62,6 +30,23 @@ init_virtualenv () {
sudo virtualenv /opt/yunohost/ihatemoney/venv --python /usr/bin/python3 sudo virtualenv /opt/yunohost/ihatemoney/venv --python /usr/bin/python3
} }
configure_nginx () {
local domain=$1
local path=$2
sed -i "s@PATHTOCHANGE@$path@g" ../conf/nginx.conf
# Fix double-slash for domain-root install
sed -i "s@location //@location /@" ../conf/nginx.conf
sudo install -o root -g root -m644 \
../conf/nginx.conf /etc/nginx/conf.d/$domain.d/ihatemoney.conf
}
configure_supervisor () {
sudo install -o root -g root -m 644 \
../conf/supervisord.conf /etc/supervisor/conf.d/ihatemoney.conf
}
### Backported helpers (from testing) ### Backported helpers (from testing)

View file

@ -60,16 +60,9 @@ install_apt_dependencies
create_unix_user create_unix_user
# Install source
fetch_and_extract /opt/yunohost/ihatemoney/src/ ihatemoney
# Prepare venv # Prepare venv
init_virtualenv init_virtualenv
sudo /opt/yunohost/ihatemoney/venv/bin/pip install -r /opt/yunohost/ihatemoney/src/requirements.txt sudo /opt/yunohost/ihatemoney/venv/bin/pip install 'gunicorn>=19.3.0' PyMySQL 'ihatemoney>=2,<3'
sudo /opt/yunohost/ihatemoney/venv/bin/pip install 'gunicorn>=19.3.0' PyMySQL
# Fix permissions
fix_permissions /opt/yunohost/ihatemoney/src
create_system_dirs create_system_dirs
@ -78,8 +71,7 @@ sudo install -o ihatemoney -g ihatemoney -m 644 \
../conf/gunicorn.conf.py /etc/ihatemoney/gunicorn.conf.py ../conf/gunicorn.conf.py /etc/ihatemoney/gunicorn.conf.py
# Configure supervisor # Configure supervisor
sudo install -o root -g root -m 644 \ configure_supervisor
../conf/supervisord.conf /etc/supervisor/conf.d/ihatemoney.conf
sudo yunohost service add supervisor sudo yunohost service add supervisor
# Configure ihatemoney # Configure ihatemoney
@ -98,13 +90,8 @@ then
ynh_app_setting_set $app unprotected_uris "/" ynh_app_setting_set $app unprotected_uris "/"
fi fi
# Configure Nginx and reload # Configure Nginx
sed -i "s@PATHTOCHANGE@$path@g" ../conf/nginx.conf configure_nginx "$domain" "$path"
# Fix double-slash for domain-root install
sed -i "s@location //@location /@" ../conf/nginx.conf
sudo install -o root -g root -m644 \
../conf/nginx.conf /etc/nginx/conf.d/$domain.d/ihatemoney.conf
# Start backend # Start backend
sudo service supervisor restart sudo service supervisor restart

View file

@ -52,7 +52,6 @@ create_unix_user
# Restore the app files # Restore the app files
sudo cp -a ./install_dir/. "$INSTALL_DIR" sudo cp -a ./install_dir/. "$INSTALL_DIR"
fix_permissions ${INSTALL_DIR}/src
# Create and restore the database # Create and restore the database
ynh_mysql_create_db "$dbname" "$dbuser" "$dbpass" ynh_mysql_create_db "$dbname" "$dbuser" "$dbpass"

View file

@ -11,28 +11,17 @@ app=$YNH_APP_INSTANCE_NAME
# Installation paths # Installation paths
INSTALL_DIR=/opt/yunohost/ihatemoney INSTALL_DIR=/opt/yunohost/ihatemoney
PIP=${INSTALL_DIR}/venv/bin/pip PIP=${INSTALL_DIR}/venv/bin/pip
NEW_REQUIREMENTS=${INSTALL_DIR}/src-new/requirements.txt
# Source YunoHost helpers # Source YunoHost helpers
. /usr/share/yunohost/helpers . /usr/share/yunohost/helpers
domain=$(ynh_app_setting_get "$app" domain) domain=$(ynh_app_setting_get $app domain)
path=$(ynh_app_setting_get $app path)
# Source local utils
source _common.sh
# Optionaly upgrade arg to typed boolean form
is_public=$(ynh_app_setting_get "$app" is_public) is_public=$(ynh_app_setting_get "$app" is_public)
if [ $is_public = "No" ]; VENV_PY_VERSION=$(echo ${INSTALL_DIR}/venv/bin/python*.*|sed 's/.*python//')
then # Source local utils
is_public=0 source _common.sh
else
is_public=1
fi
ynh_app_setting_set "$app" is_public "$is_public"
function exit_properly () { function exit_properly () {
set +e set +e
@ -41,47 +30,33 @@ function exit_properly () {
then then
sudo mv /opt/yunohost/ihatemoney/venv{-old,} sudo mv /opt/yunohost/ihatemoney/venv{-old,}
fi fi
# Remove downloaded code
sudo rm -rf ${INSTALL_DIR}/src-new/
ynh_die "Upgrade script failed, aborted and rolled back the installation" ynh_die "Upgrade script failed, aborted and rolled back the installation"
} }
trap exit_properly ERR trap exit_properly ERR
# Upgrade code
fetch_and_extract ${INSTALL_DIR}/src-new/ ihatemoney
fix_permissions ${INSTALL_DIR}/src-new/ #----------------------------PRE-UPGRADE MIGRATIONS-----------------------
# Upgrade dependencies
sudo ${PIP} install -r ${NEW_REQUIREMENTS}
sudo ${PIP} install 'gunicorn>=19.3.0' 'PyMySQL'
# Everything went ok ? Let's keep this code.
sudo rm -rf ${INSTALL_DIR}/src
sudo rm -rf ${INSTALL_DIR}/venv-old
sudo mv ${INSTALL_DIR}/src-new ${INSTALL_DIR}/src
## Migration from old versions of the package
# MIGRATION: upgrade arg to typed boolean form
if [ -e /etc/ihatemoney/settings.py ]; then if (($is_public != 0)) && (($is_public != 1))
# Strip out the no longer used part of the settings then
sudo python2 -c"d = open('/etc/ihatemoney/settings.py').read().replace('try:\n from settings import *\nexcept ImportError:\n pass\n', ''); open('/etc/ihatemoney/settings.py', 'w').write(d)" if [ $is_public = "No" ];
# Rename then
sudo mv /etc/ihatemoney/settings.py ${ihatemoney_conf_path} is_public=0
else
is_public=1
fi
ynh_app_setting_set "$app" is_public "$is_public"
fi fi
# Remove no longer used symlink
# (ihatemoney now read its conf by default from /etc/ihatemoney/ihatemoney.cfg)
sudo rm -f ${INSTALL_DIR}/src/budget/settings.py
# MIGRATION: Switch to a python3 venv
# MIGRATION: Optionaly switch to a python3 venv if [[ "$VENV_PY_VERSION" == 2.7 ]]
if [ ${INSTALL_DIR}/venv/bin/python -ef ${INSTALL_DIR}/venv/bin/python2 ]
then then
install_apt_dependencies install_apt_dependencies
# Trash py2 venv # Trash py2 venv
@ -91,17 +66,72 @@ then
# Clears all cookie-sessions, because py2 & py3 sessions are incompatible # Clears all cookie-sessions, because py2 & py3 sessions are incompatible
# Relates https://github.com/lepture/flask-wtf/issues/279 (fix unreleased) # Relates https://github.com/lepture/flask-wtf/issues/279 (fix unreleased)
new_secret_key=`openssl rand -base64 32` new_secret_key=`openssl rand -base64 32`
sudo sed -i "s@SECRET_KEY = \".*\"@SECRET_KEY = \"${new_secret_key}\"@g" ${ihatemoney_conf_path} sudo sed -i "s/SECRET_KEY = \".*\"/SECRET_KEY = \"${new_secret_key}\"/g" /etc/ihatemoney/ihatemoney.cfg
fi fi
# MIGRATION: Switch Python-MySQL -> PyMySQL #-------------------------------UPGRADE-------------------------
# Upgrade code and dependencies
sudo ${PIP} install --upgrade 'gunicorn>=19.3.0' PyMySQL 'ihatemoney>=2,<3'
#-----------------------POST-UPGRADE MIGRATIONS-----------------
# Python-MySQL is no longer maintained and does not support Py3 # Python-MySQL is no longer maintained and does not support Py3
sudo sed -i "s@'mysql://@'mysql+pymysql://@g" ${ihatemoney_conf_path} sudo sed -i "s@'mysql://@'mysql+pymysql://@g" ${ihatemoney_conf_path}
# MIGRATION: Remove old code (from pre-2.x versions, not using pip)
sudo rm -rf ${INSTALL_DIR}/src
# MIGRATION: change the static path (from pre-2.x versions, not using pip)
if grep -q /opt/yunohost/ihatemoney/src/ /etc/nginx/conf.d/${domain}.d/ihatemoney.conf
then
# the static path changed
configure_nginx "$domain" "$path"
# Supervisor no longer change its directory to src/ dir
configure_supervisor
supervisorctl update
fi
# MIGRATION: new-style settings
if [ -e /etc/ihatemoney/settings.py ]; then
# Strip out the no longer used part of the settings
sudo python2 -c"d = open('/etc/ihatemoney/settings.py').read().replace('try:\n from settings import *\nexcept ImportError:\n pass\n', ''); open('/etc/ihatemoney/settings.py', 'w').write(d)"
# Rename
sudo mv /etc/ihatemoney/settings.py ${ihatemoney_conf_path}
fi
# MIGRATION: Remove no longer used symlink
# (ihatemoney now read its conf by default from /etc/ihatemoney/ihatemoney.cfg)
sudo rm -f ${INSTALL_DIR}/src/budget/settings.py
#----------------------------FINALIZATION-----------------------
# Everything went ok ? Let's keep this new venv.
sudo rm -rf ${INSTALL_DIR}/venv-old
# Restart backend # Restart backend
sudo supervisorctl restart budget sudo supervisorctl restart budget
# Reload nginx conf
sudo systemctl reload nginx