diff --git a/conf/nginx.conf b/conf/nginx.conf index 097808f..0d9b249 100644 --- a/conf/nginx.conf +++ b/conf/nginx.conf @@ -1,5 +1,5 @@ location PATHTOCHANGE/static/ { - alias /opt/yunohost/ihatemoney/src/budget/static/; + alias /opt/yunohost/ihatemoney/venv/lib/python3.4/site-packages/ihatemoney/static/; } location PATHTOCHANGE { # Force https. diff --git a/conf/supervisord.conf b/conf/supervisord.conf index ebb5742..86e36b3 100644 --- a/conf/supervisord.conf +++ b/conf/supervisord.conf @@ -1,7 +1,6 @@ [program:budget] -command=/opt/yunohost/ihatemoney/venv/bin/gunicorn -c /etc/ihatemoney/gunicorn.conf.py run:app -directory=/opt/yunohost/ihatemoney/src/budget +command=/opt/yunohost/ihatemoney/venv/bin/gunicorn -c /etc/ihatemoney/gunicorn.conf.py ihatemoney.wsgi:application user=ihatemoney autostart=true autorestart=true -redirect_stderr=True +redirect_stderr=true diff --git a/scripts/_common.sh b/scripts/_common.sh index b6b1bb4..d3bbadb 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -9,38 +9,6 @@ INSTALL_DIR="/opt/yunohost/ihatemoney" ### 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() { 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 } +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) diff --git a/scripts/install b/scripts/install index aa96d73..3965b35 100755 --- a/scripts/install +++ b/scripts/install @@ -60,16 +60,9 @@ install_apt_dependencies create_unix_user -# Install source -fetch_and_extract /opt/yunohost/ihatemoney/src/ ihatemoney - # Prepare venv 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 - -# Fix permissions -fix_permissions /opt/yunohost/ihatemoney/src +sudo /opt/yunohost/ihatemoney/venv/bin/pip install 'gunicorn>=19.3.0' PyMySQL 'ihatemoney>=2,<3' create_system_dirs @@ -78,8 +71,7 @@ sudo install -o ihatemoney -g ihatemoney -m 644 \ ../conf/gunicorn.conf.py /etc/ihatemoney/gunicorn.conf.py # Configure supervisor -sudo install -o root -g root -m 644 \ - ../conf/supervisord.conf /etc/supervisor/conf.d/ihatemoney.conf +configure_supervisor sudo yunohost service add supervisor # Configure ihatemoney @@ -98,13 +90,8 @@ then ynh_app_setting_set $app unprotected_uris "/" fi -# Configure Nginx and reload -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 Nginx +configure_nginx "$domain" "$path" # Start backend sudo service supervisor restart diff --git a/scripts/restore b/scripts/restore index b66f5c3..a2f6a7c 100644 --- a/scripts/restore +++ b/scripts/restore @@ -52,7 +52,6 @@ create_unix_user # Restore the app files sudo cp -a ./install_dir/. "$INSTALL_DIR" -fix_permissions ${INSTALL_DIR}/src # Create and restore the database ynh_mysql_create_db "$dbname" "$dbuser" "$dbpass" diff --git a/scripts/upgrade b/scripts/upgrade index aed909f..000036b 100755 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -11,28 +11,17 @@ app=$YNH_APP_INSTANCE_NAME # Installation paths INSTALL_DIR=/opt/yunohost/ihatemoney PIP=${INSTALL_DIR}/venv/bin/pip -NEW_REQUIREMENTS=${INSTALL_DIR}/src-new/requirements.txt # Source YunoHost helpers . /usr/share/yunohost/helpers -domain=$(ynh_app_setting_get "$app" domain) - -# Source local utils -source _common.sh - -# Optionaly upgrade arg to typed boolean form - +domain=$(ynh_app_setting_get $app domain) +path=$(ynh_app_setting_get $app path) is_public=$(ynh_app_setting_get "$app" is_public) -if [ $is_public = "No" ]; -then - is_public=0 -else - is_public=1 -fi - -ynh_app_setting_set "$app" is_public "$is_public" +VENV_PY_VERSION=$(echo ${INSTALL_DIR}/venv/bin/python*.*|sed 's/.*python//') +# Source local utils +source _common.sh function exit_properly () { set +e @@ -41,47 +30,33 @@ function exit_properly () { then sudo mv /opt/yunohost/ihatemoney/venv{-old,} fi - # Remove downloaded code - sudo rm -rf ${INSTALL_DIR}/src-new/ ynh_die "Upgrade script failed, aborted and rolled back the installation" } trap exit_properly ERR -# Upgrade code -fetch_and_extract ${INSTALL_DIR}/src-new/ ihatemoney -fix_permissions ${INSTALL_DIR}/src-new/ - -# 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 +#----------------------------PRE-UPGRADE MIGRATIONS----------------------- -## Migration from old versions of the package +# MIGRATION: upgrade arg to typed boolean form -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} +if (($is_public != 0)) && (($is_public != 1)) +then + if [ $is_public = "No" ]; + then + is_public=0 + else + is_public=1 + fi + ynh_app_setting_set "$app" is_public "$is_public" 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: Optionaly switch to a python3 venv - -if [ ${INSTALL_DIR}/venv/bin/python -ef ${INSTALL_DIR}/venv/bin/python2 ] +# MIGRATION: Switch to a python3 venv +if [[ "$VENV_PY_VERSION" == 2.7 ]] then install_apt_dependencies # Trash py2 venv @@ -91,17 +66,72 @@ then # Clears all cookie-sessions, because py2 & py3 sessions are incompatible # Relates https://github.com/lepture/flask-wtf/issues/279 (fix unreleased) 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 -# 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 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 sudo supervisorctl restart budget + +# Reload nginx conf +sudo systemctl reload nginx